Class: Morpheus::Cli::ClusterTypes

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

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

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

Instance Method Details

#connect(opts) ⇒ Object



14
15
16
17
# File 'lib/morpheus/cli/commands/cluster_types.rb', line 14

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @clusters_interface = @api_client.clusters
end

#get(args) ⇒ Object



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
99
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
# File 'lib/morpheus/cli/commands/cluster_types.rb', line 68

def get(args)
  params = {}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = "Usage: #{prog_name} clusters get-type [type] "
    # opts.banner = subcommand_usage("[type]")
    # opts.on('--optionTypes [true|false]', String, "Include optionTypes in the response. Default is true.") do |val|
    #   params['optionTypes'] = (val.to_s == '' || val.to_s == 'on' || val.to_s == 'true')
    # end
    build_standard_get_options(opts, options)
    opts.footer = "Get details about a specific cluster type.\n[type] is required. This is the name or id of a cluster type.\n"
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:1)
  id = args.join(" ")
  connect(options)
  params.merge!(parse_query_options(options))
  cluster_type = nil
  
  # /api/cluster-types/$id does not exist, this loads them all.
  # todo: fix api to support /api/cluster-types/$id
  cluster_type = find_cluster_type_by_name_or_id(id)
  if cluster_type.nil?
    raise_command_error("cluster type not found for name or id '#{id}'") if cluster_type.nil?
  end
  id = cluster_type['id']
  # /api/cluster-types does not return optionTypes by default, use ?optionTypes=true
  @clusters_interface.setopts(options)
  if options[:dry_run]
    # print_dry_run @clusters_interface.dry.get_type(id, params)
    print_dry_run @clusters_interface.dry.cluster_types(params)
    return
  end
  # json_response = @clusters_interface.get(id, params)
  # cluster_type = json_response[cluster_type_object_key]
  json_response = cluster_type
  render_response(json_response, options) do
    print_h1 "Cluster Type Details", [], options
    print cyan
    show_columns = list_columns = {
        "ID" => 'id',
        "Name" => 'name',
        "Code" => 'code',
        "Description" => 'description',
      }
    print_description_list(show_columns, cluster_type)

    if cluster_type['optionTypes'] && cluster_type['optionTypes'].size > 0
      print_h2 "Configuration Options"
      opt_columns = [
        # {"ID" => lambda {|it| it['id'] } },
        {"FIELD NAME" => lambda {|it| (it['fieldContext'] && it['fieldContext'] != 'domain') ? [it['fieldContext'], it['fieldName']].join('.') : it['fieldName']  } },
        {"FIELD LABEL" => lambda {|it| it['fieldLabel'] } },
        {"TYPE" => lambda {|it| it['type'] } },
        {"DEFAULT" => lambda {|it| it['defaultValue'] } },
        {"REQUIRED" => lambda {|it| format_boolean it['required'] } },
        # {"DESCRIPTION" => lambda {|it| it['description'] }, # do it!
      ]
      print as_pretty_table(cluster_type['optionTypes'], opt_columns)
    else
      # print cyan,"No option types found for this cluster type.","\n",reset
    end

    controller_types = cluster_type['controllerTypes'] || []
    if controller_types && controller_types.size > 0
      print_h2 "Controller Types"
      print as_pretty_table(controller_types, [:name, :code], options)
    else
      # print cyan,"No worker types found for this cluster type.","\n",reset
    end

    worker_types = cluster_type['workerTypes'] || []
    if worker_types && worker_types.size > 0
      print_h2 "Worker Types"
      print as_pretty_table(worker_types, [:name, :code], options)
    else
      # print cyan,"No worker types found for this cluster type.","\n",reset
    end

    print reset,"\n"
  end
  return 0, nil
end

#handle(args) ⇒ Object



19
20
21
# File 'lib/morpheus/cli/commands/cluster_types.rb', line 19

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



23
24
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
# File 'lib/morpheus/cli/commands/cluster_types.rb', line 23

def list(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = "Usage: #{prog_name} clusters list-types [search]"
    #opts.banner = subcommand_usage("[search]")
    # opts.on('--optionTypes [true|false]', String, "Include optionTypes in the response. Default is false.") do |val|
    #   params['optionTypes'] = (val.to_s == '' || val.to_s == 'on' || val.to_s == 'true')
    # end
    build_standard_list_options(opts, options)
    opts.footer = "List cluster types."
  end
  optparse.parse!(args)
  connect(options)
  # verify_args!(args:args, optparse:optparse, count:0)
  if args.count > 0
    options[:phrase] = args.join(" ")
  end
  params.merge!(parse_list_options(options))
  @clusters_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @clusters_interface.dry.cluster_types(params)
    return
  end
  json_response = @clusters_interface.cluster_types(params)
  render_response(json_response, options, "clusterTypes") do
    cluster_types = json_response["clusterTypes"]
    print_h1 "Morpheus Cluster Types", parse_list_subtitles(options), options
    if cluster_types.empty?
      print cyan,"No cluster types found.",reset,"\n"
    else
      list_columns = {
        "ID" => 'id',
        "NAME" => 'name',
        "CODE" => 'code',
        "DESCRIPTION" => lambda {|it| truncate_string(it['description'], options[:wrap] ? nil : 100) },
      }
      print as_pretty_table(cluster_types, list_columns, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
  end
  return 0, nil
end