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