fluke/devops-client/lib/devops-client/handler/image.rb

141 lines
3.6 KiB
Ruby

require "devops-client/handler/handler"
require "devops-client/options/image_options"
require "devops-client/output/image"
require "devops-client/handler/bootstrap_templates"
class Image < Handler
output_with Output::Image
def initialize(host, def_options={})
# QUESTION:
# why we set @options variable here while we always reset it in #handle method?
@host, @options = host, def_options
@options_parser = ImageOptions.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 :update
update_handler
end
end
def get_templates
bt = BootstrapTemplates.new(@host, self.options)
bt.auth = self.auth
list = bt.list_handler
return list, nil if list.empty?
[list, bt.outputter.table]
end
def create_handler
q = {}
q[:provider] = options[:provider] || resources_selector.select_available_provider
provider_images(q[:provider])
# QUESTION:
# why we select image in create image handler?
image = nil
if options[:image_id].nil?
image = choose_image_cmd(@list, self.outputter.table)
else
image = @list.detect{|i| i["id"] == options[:image_id]}
abort("Invalid image id '#{options[:image_id]}'") if image.nil?
end
q["name"] = image["name"]
q["id"] = image["id"]
q["remote_user"] = options[:ssh_username] || enter_parameter(I18n.t("handler.image.create.ssh_user") + ": ")
q["bootstrap_template"] = if options[:bootstrap_template].nil? and options[:no_bootstrap_template] == false
bt, bt_t = get_templates
if bt.empty?
puts I18n.t("handler.image.create.template_empty")
nil
else
# QUESTION:
# what does this '-1' mean?
i = choose_number_from_list(I18n.t("handler.image.create.template"), bt, bt_t, -1)
if i == -1
nil
else
bt[i]
end
end
else
nil
end
json = JSON.pretty_generate(q)
post_body "/image", json if question(I18n.t("handler.image.question.create")){puts json}
end
def list_handler(provider=nil)
provider ||= @options[:given_provider]
@list = if provider
provider_images(provider)
else
get("/images")
end
end
def provider_images provider
if provider == 'static'
@options_parser.invalid_list_command
abort()
end
if @options[:provider_account]
@list = get("/images/provider/#{provider}/#{@options[:provider_account]}")
else
@list = get("/images/provider/#{provider}")
end
end
def show_handler
id = @args[2]
r = inspect_parameters @options_parser.show_params, id
unless r.nil?
@options_parser.invalid_show_command
abort(r)
end
@show = get "/image/#{id}"
end
def delete_handler
id = @args[2]
r = inspect_parameters @options_parser.delete_params, id
unless r.nil?
@options_parser.invalid_delete_command
abort(r)
end
if question(I18n.t("handler.image.question.delete", :name => id))
delete "/image/#{id}"
end
end
# QUESTION:
# what does inspect_parameters do and what do args[2] and args[3] actually mean?
def update_handler
r = inspect_parameters @options_parser.update_params, @args[2], @args[3]
unless r.nil?
@options_parser.invalid_update_command
abort(r)
end
update_object_from_file "image", @args[2], @args[3]
end
end