Merge branch 'cid-396-getting_images_for_account' into features

This commit is contained in:
Anton Chuchkalov 2016-02-09 13:02:51 +07:00
commit 62024f78b0
10 changed files with 99 additions and 19 deletions

View File

@ -92,12 +92,16 @@ class Image < Handler
end
def provider_images provider
if Providers.has_functionality?(provider, :images)
@list = get("/images/provider/#{provider}")
else
if provider == 'static'
@options_parser.invalid_list_command
abort()
end
if @options[:provider_account]
@list = get("/images/provider/#{provider}/#{@options[:provider_account]}")
else
@list = get("/images/provider/#{provider}")
end
end
def show_handler

View File

@ -12,22 +12,29 @@ class Provider < Handler
end
def handle
current_command = ARGV[1].to_sym
@options, @args = @options_parser.parse_options_for!(current_command)
case current_command
when :list
list_handler
output
output(output_type: :list)
when :accounts
@provider = @args[2]
accounts_handler
output(output_type: :accounts, provider: @provider)
end
end
def list_handler
r = inspect_parameters @options_parser.list_params
unless r.nil?
@options_parser.invalid_list_command
abort(r)
end
@list = get("/providers").sort!{|x,y| x["id"] <=> y["id"]}
end
def accounts_handler
r = inspect_parameters @options_parser.accounts_params, @provider
unless r.nil?
@options_parser.invalid_accounts_command
abort(r)
end
@list = get("/provider/#{@provider}/accounts")
end
end

View File

@ -27,6 +27,14 @@ class ImageOptions < CommonOptions
end
end
def list_options
self.options do |parser, options|
parser.banner << self.list_banner
parser.recognize_option_value(:provider_account, resource_name: :image)
end
end
extend_options_method :list_options do |options|
if args[2] == "provider" and args[3]
options[:given_provider] = args[3]

View File

@ -3,13 +3,14 @@ require "devops-client/options/common_options"
class ProviderOptions < CommonOptions
commands :list
commands :list, :accounts
def initialize args, def_options
super(args, def_options)
self.header = I18n.t("headers.provider")
self.banner_header = "provider"
self.list_params = []
self.accounts_params = %w(PROVIDER)
end
end

View File

@ -5,6 +5,8 @@ require "json"
module Output
class Base
MAX_CELL_WIDTH = 80
attr_reader :options
def initialize(data_to_output, options={})
@ -43,6 +45,7 @@ module Output
def create_table headers, rows, title=nil, with_num=true, separator=false
return nil if headers.nil? or rows.nil?
shrink_cells_if_width_exceeded(rows)
if with_num
headers.unshift(I18n.t("output.table_header.number"))
rows.each_with_index {|row, i| row.unshift(i + 1)}
@ -82,5 +85,19 @@ module Output
[headers, rows]
end
private
def shrink_cells_if_width_exceeded(rows)
rows.each do |row|
row.each_with_index do |cell, i|
row[i] = split_to_parts_of_size(cell, MAX_CELL_WIDTH)
end
end
end
def split_to_parts_of_size(string, size)
(string || '').chars.each_slice(size).map(&:join).join("\n")
end
end
end

View File

@ -4,18 +4,30 @@ module Output
class Provider < Base
def table
headers, rows = create
create_table(headers, rows, I18n.t("output.title.provider.list"))
case options[:output_type]
when :list
headers, rows = create_list
title = I18n.t("output.title.provider.list")
when :accounts
headers, rows = create_accounts
title = I18n.t("output.title.provider.accounts", provider: options[:provider])
end
create_table(headers, rows, title)
end
def csv
headers, rows = create
create_csv(headers, rows)
case options[:output_type]
when :list
headers, rows = create_list
when :accounts
headers, rows = create_accounts
end
create_csv(headers, rows, with_num?)
end
private
def create
def create_list
abort(I18n.t("output.not_found.provider.list")) if @data.empty?
headers = [ I18n.t("output.table_header.provider") ]
rows = []
@ -25,5 +37,21 @@ module Output
return headers, rows
end
def create_accounts
headers = [
I18n.t("output.table_header.id"),
I18n.t("output.table_header.description"),
I18n.t("output.table_header.ssh_key")
]
rows = @data.map do |account|
[
account['account_name'],
account['description'],
account['ssh_key']
]
end
[headers, rows]
end
end
end

View File

@ -194,6 +194,7 @@ en:
stack_template: "Stack Template"
cloud_stack_id: "Cloud Stack id"
stack_status: Stack status
ssh_key: SSH key
title:
flavor:
list: "Flavors"
@ -218,6 +219,7 @@ en:
envs: "Project '%{name}' deploy envs"
provider:
list: "Providers"
accounts: "Provider %{provider} accounts"
script:
list: "Scripts"
server:
@ -316,6 +318,7 @@ en:
ssh_username: SSH user name
bootstrap_template: Bootstrap template
no_bootstrap_template: Do not specify bootstrap template
provider_account: Provider account id
project:
groups: "Security groups (comma separated list)"
deploy_env: "Deploy environment identifier"

View File

@ -20,6 +20,10 @@ module Devops
Image.get_available_provider_images(Devops::Db.connector, provider)
end
def provider_account_images provider, account
Image.get_available_provider_images(Devops::Db.connector, provider, account)
end
def image id
Devops::Db.connector.image(id)
end

View File

@ -59,6 +59,12 @@ module Devops
json Devops::API2_0::Handler::Image.new(request).provider_images(provider)
end
app.get_with_headers "/images/provider/:provider/:account", :headers => [:accept] do |provider, account|
check_privileges("image", "r")
check_provider(provider)
json Devops::API2_0::Handler::Image.new(request).provider_account_images(provider, account)
end
# Create devops image
#
# * *Request*

View File

@ -2,10 +2,12 @@ require "providers/provider_factory"
module ImageCommands
def get_available_provider_images mongo, provider
def get_available_provider_images mongo, provider, account=nil
filters = mongo.available_images(provider)
if filters.empty?
[]
return [] if filters.empty?
if account
::Provider::ProviderFactory.get(provider, account).images(filters)
else
::Provider::ProviderFactory.get(provider).images(filters)
end