fluke/devops-client/lib/devops-client/handler/helpers/resources_selector.rb
2015-04-14 16:29:02 +04:00

101 lines
3.5 KiB
Ruby

require 'devops-client/handler/helpers/resources_fetcher'
require 'devops-client/handler/helpers/input_utils'
module Helpers
class ResourcesSelector
include InputUtils
# fetcher_instance_or_attrs should be:
# instance of ResourcesFetcher
# OR
# hash with these keys:
# :host
# :handler_object_options
# :auth
def initialize(fetcher_instance_or_attrs)
if fetcher_instance_or_attrs.is_a?(ResourcesFetcher)
@fetcher = fetcher_instance_or_attrs
else
@fetcher = ResourcesFetcher.new(fetcher_instance_or_attrs)
end
end
def select_available_provider(options={})
providers, table = @fetcher.fetch_with_table('provider')
# somewhy returns provider name as String.
select_item_from_table(I18n.t("headers.provider"), providers, table)
end
def select_available_project(options={})
projects, table = @fetcher.fetch_with_table('project')
project = select_item_from_table(I18n.t("headers.project"), projects, table)
project['name']
end
def select_available_stack_template(options={})
stack_templates, table = @fetcher.fetch_with_table('stack_template', options[:provider])
stack_template = select_item_from_table(I18n.t("headers.stack_template"), stack_templates, table)
stack_template['id']
end
def select_available_flavor(options={})
flavors, table = @fetcher.fetch_with_table('flavor', options[:provider])
abort(I18n.t("handler.flavor.list.empty")) if flavors.empty?
flavors_descriptions = flavors.map {|f| "#{f["id"]}. #{f["name"]} - #{f["ram"]}, #{f["disk"]}, #{f["v_cpus"]} CPU"}
index = choose_number_from_list(I18n.t("headers.flavor"), flavors_descriptions, table)
flavors[index]['id']
end
def select_available_image(options={})
images, table = @fetcher.fetch_with_table('image', options[:provider])
image = select_item_from_table(I18n.t("headers.image"), images, table)
image['id']
end
def select_available_network(options={})
networks, table = @fetcher.fetch_with_table('network', options[:provider])
table_title = options[:table_title] || I18n.t("headers.network")
selected_networks = select_items_from_table(table_title, networks, table)
selected_networks.map {|network| network['name']}
end
def select_available_users(options={})
title = options[:table_title] || I18n.t("handler.project.create.user")
users, table = @fetcher.fetch_with_table('user')
users = select_items_from_table(title, users, table)
users.map {|user| user['id']}
end
def select_available_groups(options={})
title = options[:table_title] || I18n.t("options.project.create.groups")
groups, table = @fetcher.fetch_with_table('group', options[:provider], options[:vpc_id])
# somewhy groups returned as a hash, not array
groups_array = []
groups.each do |group_id, group_attrs|
group_attrs['id'] = group_id
groups_array << group_attrs
end
default_index = groups_array.index {|t| t['id'] == 'default'}
select_items_from_table(title, groups_array, table, default: 'default', default_index: default_index)
end
private
def select_item_from_table(title, items, table)
items[ choose_number_from_list(title, items, table) ]
end
def select_items_from_table(title, items, table, options={})
indexes = choose_indexes_from_list(title, items, table, options[:default], options[:default_index])
items.values_at(*indexes)
end
end
end