select env instead of inputting its name on stack creating
This commit is contained in:
parent
ac0a81831c
commit
9bc8f75ba3
@ -87,7 +87,7 @@ class DeployEnv
|
||||
end
|
||||
|
||||
def fetcher
|
||||
@fetcher ||= Helpers::ResourcesFetcher.new(host: @host, handler_object_options: @options, auth: @auth)
|
||||
@fetcher ||= Helpers::ResourcesFetcher.new(host: @host, handler_options: @options, auth: @auth)
|
||||
end
|
||||
|
||||
|
||||
|
||||
@ -26,8 +26,12 @@ class Handler
|
||||
attr_accessor :auth
|
||||
|
||||
def host
|
||||
if @host.start_with?('http')
|
||||
@host
|
||||
else
|
||||
"http://#{@host}"
|
||||
end
|
||||
end
|
||||
|
||||
#TODO: only basic auth now
|
||||
def username
|
||||
@ -50,30 +54,9 @@ protected
|
||||
end
|
||||
|
||||
def fetcher
|
||||
@fetcher ||= Helpers::ResourcesFetcher.new(host: @host, handler_object_options: @options, auth: @auth)
|
||||
@fetcher ||= Helpers::ResourcesFetcher.new(host: @host, handler_options: @options, auth: @auth)
|
||||
end
|
||||
|
||||
def params_filter params
|
||||
r = []
|
||||
return params if params.kind_of?(String)
|
||||
params.each do |k,v|
|
||||
key = k.to_s
|
||||
if v.kind_of?(Array)
|
||||
v.each do |val|
|
||||
r.push "#{key}[]=#{val}"
|
||||
end
|
||||
elsif v.kind_of?(Hash)
|
||||
buf = {}
|
||||
v.each do |k1,v1|
|
||||
buf["#{key}[#{k1}]"] = v1
|
||||
end
|
||||
r = r + params_filter(buf)
|
||||
else
|
||||
r.push "#{key}=#{v}"
|
||||
end
|
||||
end
|
||||
r
|
||||
end
|
||||
|
||||
def inspect_parameters names, *args
|
||||
names.each_with_index do |name, i|
|
||||
|
||||
@ -143,4 +143,26 @@ module HttpUtils
|
||||
params_filter(params.select{|k,v| k != :cmd and !v.nil?}).join("&")
|
||||
end
|
||||
|
||||
def params_filter params
|
||||
r = []
|
||||
return params if params.kind_of?(String)
|
||||
params.each do |k,v|
|
||||
key = k.to_s
|
||||
if v.kind_of?(Array)
|
||||
v.each do |val|
|
||||
r.push "#{key}[]=#{val}"
|
||||
end
|
||||
elsif v.kind_of?(Hash)
|
||||
buf = {}
|
||||
v.each do |k1,v1|
|
||||
buf["#{key}[#{k1}]"] = v1
|
||||
end
|
||||
r = r + params_filter(buf)
|
||||
else
|
||||
r.push "#{key}=#{v}"
|
||||
end
|
||||
end
|
||||
r
|
||||
end
|
||||
|
||||
end
|
||||
@ -1,13 +1,22 @@
|
||||
require "devops-client/handler/helpers/http_utils"
|
||||
require 'devops-client/helpers/string_helper'
|
||||
|
||||
# fetches resources list along with table
|
||||
# Rewrite this to avoid dependency on handlers
|
||||
module Helpers
|
||||
class ResourcesFetcher
|
||||
include HttpUtils
|
||||
|
||||
# have the same meaning as in handlers
|
||||
attr_reader :username, :password, :options, :host
|
||||
|
||||
def initialize(options)
|
||||
@host = options.fetch(:host)
|
||||
@handler_object_options = options.fetch(:handler_object_options)
|
||||
@host = "http://#{options.fetch(:host)}"
|
||||
@options = options.fetch(:handler_options)
|
||||
@auth = options.fetch(:auth)
|
||||
|
||||
# username, password and options are used to perform http queries with module HttpUtils
|
||||
@username, @password = @auth[:username], @auth[:password]
|
||||
end
|
||||
|
||||
def fetch(collection_name, *args)
|
||||
@ -19,12 +28,21 @@ module Helpers
|
||||
[handler.list_handler(*args), handler.outputter.table]
|
||||
end
|
||||
|
||||
def fetch_project(project_id)
|
||||
@fetched_projects = {}
|
||||
if cached = @fetched_projects[project_id]
|
||||
cached
|
||||
else
|
||||
@fetched_projects[project_id] = get("/project/#{project_id}")
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_handler(collection_name)
|
||||
require_handler_file(collection_name)
|
||||
|
||||
handler = resource_handler_klass(collection_name).new(@host, @handler_object_options)
|
||||
handler = resource_handler_klass(collection_name).new(host, options)
|
||||
handler.auth = @auth
|
||||
handler
|
||||
end
|
||||
|
||||
@ -1,38 +1,36 @@
|
||||
require 'devops-client/handler/helpers/resources_fetcher'
|
||||
require 'devops-client/handler/helpers/input_utils'
|
||||
require 'devops-client/output/project'
|
||||
|
||||
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
|
||||
def initialize(fetcher)
|
||||
raise "fetcher should be instance of ResourcesFetcher" unless fetcher.is_a?(ResourcesFetcher)
|
||||
@fetcher = fetcher
|
||||
end
|
||||
|
||||
def select_available_provider(options={})
|
||||
def select_available_provider
|
||||
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={})
|
||||
def select_available_project
|
||||
projects, table = @fetcher.fetch_with_table('project')
|
||||
project = select_item_from_table(I18n.t("headers.project"), projects, table)
|
||||
project['name']
|
||||
end
|
||||
|
||||
def select_available_env(project_id)
|
||||
project = @fetcher.fetch_project(project_id)
|
||||
outputter = Output::Project.new(project, {output_type: :show, with_num: true})
|
||||
env = select_item_from_table("Select deploy env", project['deploy_envs'], outputter.table)
|
||||
env['identifier']
|
||||
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)
|
||||
|
||||
@ -51,12 +51,11 @@ class Stack < Handler
|
||||
q = {}
|
||||
|
||||
q[:without_bootstrap] = options[:without_bootstrap]
|
||||
q[:provider] = options[:provider] || resources_selector.select_available_provider
|
||||
# q[:id] = options[:id] || enter_parameter(I18n.t('handler.stack.create.id'))
|
||||
# q[:provider] = options[:provider] || resources_selector.select_available_provider
|
||||
q[:project] = options[:project] || resources_selector.select_available_project
|
||||
q[:deploy_env] = options[:deploy_env] || enter_parameter(I18n.t('handler.stack.create.deploy_env'))
|
||||
# q[:run_list] = options[:run_list] || enter_parameter_or_empty(I18n.t('handler.stack.create.run_list'))
|
||||
# q[:run_list] = q[:run_list].split(',')
|
||||
q[:deploy_env] = options[:deploy_env] || resources_selector.select_available_env(q[:project])
|
||||
env = fetcher.fetch_project(q[:project])['deploy_envs'].detect {|env| env['identifier'] == q[:deploy_env]}
|
||||
q[:provider] = env['provider']
|
||||
|
||||
filepath = options[:parameters_file] || enter_parameter(I18n.t('handler.stack.create.parameters_file'))
|
||||
q[:parameters] = JSON.parse(File.read(filepath))
|
||||
|
||||
@ -38,7 +38,7 @@ module Output
|
||||
end
|
||||
|
||||
def with_num?
|
||||
outputting_list?
|
||||
@options[:with_num] || outputting_list?
|
||||
end
|
||||
|
||||
def create_table headers, rows, title=nil, with_num=true, separator=false
|
||||
|
||||
@ -60,7 +60,7 @@ module Output
|
||||
|
||||
def create_show show
|
||||
rows = []
|
||||
headers = if show["type"] == "multi"
|
||||
if show["type"] == "multi"
|
||||
show["deploy_envs"].each do |de|
|
||||
subprojects = []
|
||||
nodes = []
|
||||
@ -72,14 +72,14 @@ module Output
|
||||
end
|
||||
rows.push [ de["identifier"], subprojects.join("\n"), nodes.join("\n"), de["users"].join("\n") ]
|
||||
end
|
||||
[
|
||||
headers = [
|
||||
I18n.t("output.table_header.deploy_env"),
|
||||
I18n.t("output.table_header.subproject") + " - " + I18n.t("output.table_header.deploy_env"),
|
||||
I18n.t("output.table_header.node_number"),
|
||||
I18n.t("output.table_header.users")
|
||||
]
|
||||
else
|
||||
show["deploy_envs"].each do |de|
|
||||
show["deploy_envs"].each_with_index do |de, i|
|
||||
rows.push [
|
||||
show["name"],
|
||||
de["identifier"],
|
||||
@ -91,7 +91,7 @@ module Output
|
||||
(de["users"] || []).join("\n")
|
||||
]
|
||||
end
|
||||
[
|
||||
headers = [
|
||||
I18n.t("output.table_header.id"),
|
||||
I18n.t("output.table_header.deploy_env"),
|
||||
I18n.t("output.table_header.image_id"),
|
||||
|
||||
@ -43,6 +43,7 @@ en:
|
||||
user: "User"
|
||||
stack: "Stack"
|
||||
stack_template: "Stack template"
|
||||
env: "Deploy environment"
|
||||
handler:
|
||||
flavor:
|
||||
list:
|
||||
@ -214,6 +215,7 @@ en:
|
||||
show: "Project '%{name}' information"
|
||||
servers: "Project '%{title}' servers"
|
||||
test: "Project test: %{project} - %{env}"
|
||||
envs: "Project '%{name}' deploy envs"
|
||||
provider:
|
||||
list: "Providers"
|
||||
script:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user