Class: Morpheus::Cli::InstanceTypes

Inherits:
Object
  • Object
show all
Includes:
CliCommand, ProvisioningHelper
Defined in:
lib/morpheus/cli/commands/instance_types.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from ProvisioningHelper

#accounts_interface, #add_perms_options, #api_client, #apps_interface, #cloud_datastores_interface, #clouds_interface, #datastores_interface, #find_app_by_id, #find_app_by_name, #find_app_by_name_or_id, #find_cloud_by_id_for_provisioning, #find_cloud_by_name_for_provisioning, #find_cloud_by_name_or_id_for_provisioning, #find_cloud_resource_pool_by_name_or_id, #find_group_by_id_for_provisioning, #find_group_by_name_for_provisioning, #find_group_by_name_or_id_for_provisioning, #find_host_by_id, #find_host_by_name, #find_host_by_name_or_id, #find_instance_by_id, #find_instance_by_name, #find_instance_by_name_or_id, #find_instance_type_by_code, #find_instance_type_by_id, #find_instance_type_by_name, #find_instance_type_by_name_or_id, #find_instance_type_layout_by_id, #find_server_by_id, #find_server_by_name, #find_server_by_name_or_id, #find_workflow_by_id, #find_workflow_by_name, #find_workflow_by_name_or_id, #format_app_status, #format_blueprint_type, #format_container_connection_string, #format_container_status, #format_instance_connection_string, #format_instance_container_display_name, #format_instance_status, #format_metadata, #format_snapshot_status, #get_available_accounts, #get_available_clouds, #get_available_environments, #get_available_groups, #get_available_plans, #get_provision_type_for_zone_type, #get_static_environments, included, #instance_type_layouts_interface, #instance_types_interface, #instances_interface, #load_balance_protocols_dropdown, #options_interface, #parse_blueprint_type, #parse_host_id_list, #parse_instance_id_list, #parse_metadata, #parse_resource_id_list, #parse_server_id_list, #print_permissions, #prompt_evars, #prompt_exposed_ports, #prompt_instance_load_balancer, #prompt_metadata, #prompt_network_interfaces, #prompt_new_instance, #prompt_permissions, #prompt_permissions_v2, #prompt_resize_volumes, #prompt_security_groups, #prompt_service_plan_options, #prompt_volumes, #provision_types_interface, #reject_service_plan_option_types, #servers_interface

Methods included from CliCommand

#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_many_options, #build_standard_add_options, #build_standard_api_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #execute_api, #execute_api_payload, #execute_api_request, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_each_payload, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_options, #parse_parameter_as_resource_id!, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Constructor Details

#initializeInstanceTypes

Returns a new instance of InstanceTypes.



11
12
13
# File 'lib/morpheus/cli/commands/instance_types.rb', line 11

def initialize()
  # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
end

Instance Method Details

#connect(opts) ⇒ Object



15
16
17
18
# File 'lib/morpheus/cli/commands/instance_types.rb', line 15

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @instance_types_interface = @api_client.instance_types
end

#get(args) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/morpheus/cli/commands/instance_types.rb', line 100

def get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "Get instance type details." + "\n" +
                  "[name] is required. This is the name or id of an instance type."
  end
  optparse.parse!(args)
  if args.count < 1
    puts_error  "#{Morpheus::Terminal.angry_prompt}wrong number of arguments. Expected 1 and received #{args.count} #{args.inspect}\n#{optparse}"
    return 1
  end
  connect(options)
  begin
    instance_type = find_instance_type_by_name_or_id(args[0])
    if instance_type.nil?
      return 1
    end
    @instance_types_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @instance_types_interface.dry.get(instance_type['id'])
      return
    end
    json_response = nil
    if args[0].to_s =~ /\A\d{1,}\Z/
      json_response = {'instanceType' => instance_type}
    else
      json_response = @instance_types_interface.get(instance_type['id'])
      instance_type = json_response['instanceType']
    end

    if options[:json]
      puts as_json(json_response, options, "instanceType")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "instanceType")
      return 0
    elsif options[:csv]
      puts records_as_csv([json_response['instanceType']], options)
      return 0
    end

    print_h1 "Instance Type Details"
    versions = instance_type['versions'].join(', ')
    print cyan, "=  #{instance_type['name']} (#{instance_type['code']}) - #{versions}\n"
    if instance_type['instanceTypeLayouts']
      layout_names = instance_type['instanceTypeLayouts'].collect { |layout| layout['name'] }.uniq.sort
      layout_names.each do |layout_name|
        print green, "     - #{layout_name}\n",reset
      end
    else
      print yellow,"No layouts found for instance type.","\n",reset
    end
    print reset,"\n"
    

  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#handle(args) ⇒ Object



20
21
22
# File 'lib/morpheus/cli/commands/instance_types.rb', line 20

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/morpheus/cli/commands/instance_types.rb', line 25

def list(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:list, :query, :json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.on('--category VALUE', String, "Filter by category") do |val|
      params['category'] = val
    end
    opts.on('--code VALUE', String, "Filter by code") do |val|
      params['code'] = val
    end
    opts.on('--technology VALUE', String, "Filter by technology") do |val|
      params['provisionTypeCode'] = val
    end
    opts.footer = "List instance types."
  end
  optparse.parse!(args)
  if args.count > 0
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "wrong number of arguments, expected 0 and got (#{args.count}) #{args.inspect}\n#{optparse}"
    return 1
  end
  connect(options)
  begin
    # construct payload
    params.merge!(parse_list_options(options))
    @instance_types_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @instance_types_interface.dry.list(params)
      return
    end
    # do it
    json_response = @instance_types_interface.list(params)
    instance_types = json_response['instanceTypes']
    # print result and return output
    if options[:json]
      puts as_json(json_response, options, "instanceTypes")
      return 0
    elsif options[:csv]
      puts records_as_csv(json_response['instanceTypes'], options)
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "instanceTypes")
      return 0
    end
    instance_types = json_response['instanceTypes']
    title = "Morpheus Instance Types"
    subtitles = []
    subtitles += parse_list_subtitles(options)
    print_h1 title, subtitles
    if instance_types.empty?
      print yellow,"No instance types found.",reset,"\n"
    else
      instance_types.each do |instance_type|
        versions = instance_type['versions'].join(', ')
        print cyan, "=  #{instance_type['name']} (#{instance_type['code']}) - #{versions}\n"
        if instance_type['instanceTypeLayouts']
          layout_names = instance_type['instanceTypeLayouts'].collect { |layout| layout['name'] }.uniq.sort
          layout_names.each do |layout_name|
            print green, "     - #{layout_name}\n",reset
          end
        end
      end
      if json_response['meta']
        print_results_pagination(json_response, {:label => "instance type", :n_label => "instance types"})
      end
    end
    print reset,"\n"
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end