client: add "provider accounts" and "image list --provider_account"

This commit is contained in:
Anton Chuchkalov 2016-02-08 23:56:05 +07:00
parent 53ce4e266b
commit b7688a4821
10 changed files with 99 additions and 19 deletions

View File

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

View File

@ -12,22 +12,29 @@ class Provider < Handler
end end
def handle def handle
current_command = ARGV[1].to_sym
@options, @args = @options_parser.parse_options_for!(current_command) @options, @args = @options_parser.parse_options_for!(current_command)
case current_command case current_command
when :list when :list
list_handler list_handler
output output(output_type: :list)
when :accounts
@provider = @args[2]
accounts_handler
output(output_type: :accounts, provider: @provider)
end end
end end
def list_handler 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"]} @list = get("/providers").sort!{|x,y| x["id"] <=> y["id"]}
end 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 end

View File

@ -27,6 +27,14 @@ class ImageOptions < CommonOptions
end end
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| extend_options_method :list_options do |options|
if args[2] == "provider" and args[3] if args[2] == "provider" and args[3]
options[:given_provider] = args[3] options[:given_provider] = args[3]

View File

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

View File

@ -5,6 +5,8 @@ require "json"
module Output module Output
class Base class Base
MAX_CELL_WIDTH = 80
attr_reader :options attr_reader :options
def initialize(data_to_output, 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 def create_table headers, rows, title=nil, with_num=true, separator=false
return nil if headers.nil? or rows.nil? return nil if headers.nil? or rows.nil?
shrink_cells_if_width_exceeded(rows)
if with_num if with_num
headers.unshift(I18n.t("output.table_header.number")) headers.unshift(I18n.t("output.table_header.number"))
rows.each_with_index {|row, i| row.unshift(i + 1)} rows.each_with_index {|row, i| row.unshift(i + 1)}
@ -82,5 +85,19 @@ module Output
[headers, rows] [headers, rows]
end 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
end end

View File

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

View File

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

View File

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

View File

@ -59,6 +59,12 @@ module Devops
json Devops::API2_0::Handler::Image.new(request).provider_images(provider) json Devops::API2_0::Handler::Image.new(request).provider_images(provider)
end 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 # Create devops image
# #
# * *Request* # * *Request*

View File

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