102 lines
2.5 KiB
Ruby
102 lines
2.5 KiB
Ruby
require 'devops-client/handler/handler'
|
|
|
|
require 'devops-client/options/stack_options'
|
|
require 'devops-client/output/stack'
|
|
|
|
class Stack < Handler
|
|
|
|
output_with Output::Stack
|
|
|
|
def initialize(host, def_options={})
|
|
@host = host
|
|
@options_parser = StackOptions.new(ARGV, def_options)
|
|
end
|
|
|
|
def handle
|
|
@options, @args = @options_parser.parse_options_for!(current_command)
|
|
case current_command
|
|
when :list
|
|
list_handler
|
|
output
|
|
when :show
|
|
show_handler
|
|
output
|
|
when :create
|
|
create_handler
|
|
when :delete
|
|
delete_handler
|
|
when :sync_details
|
|
sync_handler
|
|
output
|
|
end
|
|
end
|
|
|
|
def create_handler
|
|
q = {}
|
|
|
|
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[:deploy_env] = options[:deploy_env] || enter_parameter(I18n.t('handler.stack.create.deploy_env'))
|
|
q[:stack_template] = options[:stack_template] || resources_selector.select_available_stack_template(provider: q[:provider])
|
|
q[:parameters] = options[:parameters] || enter_hash(I18n.t('handler.stack.create.parameters'))
|
|
|
|
json = JSON.pretty_generate(q)
|
|
if question(I18n.t("handler.stack.question.create")) {puts json}
|
|
post_body "/stack", json
|
|
end
|
|
end
|
|
|
|
def list_handler
|
|
@list = if @options[:given_provider]
|
|
provider_stacks(@options[:given_provider])
|
|
else
|
|
get("/stacks")
|
|
end
|
|
end
|
|
|
|
def show_handler
|
|
stack_id = @args[2]
|
|
r = inspect_parameters(@options_parser.show_params, stack_id)
|
|
unless r.nil?
|
|
@options_parser.invalid_show_command
|
|
abort(r)
|
|
end
|
|
@show = get "/stack/#{stack_id}"
|
|
end
|
|
|
|
def sync_handler
|
|
stack_id = @args[2]
|
|
r = inspect_parameters(@options_parser.sync_details_params, stack_id)
|
|
unless r.nil?
|
|
@options_parser.invalid_sync_command
|
|
abort(r)
|
|
end
|
|
@show = post "/stack/#{stack_id}/sync_details"
|
|
end
|
|
|
|
def delete_handler
|
|
stack_id = @args[2]
|
|
r = inspect_parameters(@options_parser.delete_params, stack_id)
|
|
unless r.nil?
|
|
@options_parser.invalid_delete_command
|
|
abort(r)
|
|
end
|
|
if question(I18n.t("handler.stack.question.delete", name: stack_id))
|
|
delete "/stack/#{stack_id}"
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def provider_stacks(provider)
|
|
if Providers.has_functionality?(provider, :stacks)
|
|
get("/stacks/provider/#{provider}")
|
|
else
|
|
@options_parser.invalid_list_command
|
|
abort()
|
|
end
|
|
end
|
|
|
|
end
|