Class: Morpheus::Cli::LibraryOptionTypesCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand, LibraryHelper
Defined in:
lib/morpheus/cli/library_option_types_command.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from LibraryHelper

#api_client, #find_container_type_by_id, #find_container_type_by_name, #find_container_type_by_name_or_id, #find_instance_type_by_id, #find_instance_type_by_name, #find_instance_type_by_name_or_id, #find_option_type_by_id, #find_option_type_by_name, #find_option_type_by_name_or_id, #find_option_type_list_by_id, #find_option_type_list_by_name, #find_option_type_list_by_name_or_id, #find_spec_template_by_id, #find_spec_template_by_name, #find_spec_template_by_name_or_id, #find_spec_template_type_by_id, #find_spec_template_type_by_name_or_code, #find_spec_template_type_by_name_or_code_id, #format_container_type_technology, #format_instance_type_technology, #get_all_spec_template_types, included, #load_balance_protocols_dropdown, #print_container_types_table, #print_instance_types_table, #print_resource_specs_table, #prompt_exposed_ports, #prompt_for_container_types, #prompt_for_option_types, #prompt_for_spec_templates

Methods included from CliCommand

#apply_options, #build_common_options, #build_option_type_options, #build_standard_add_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, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #get_subcommand_description, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_bytes_param, #parse_id_list, #parse_list_options, #parse_list_subtitles, #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

#initializeLibraryOptionTypesCommand

Returns a new instance of LibraryOptionTypesCommand.



14
15
16
# File 'lib/morpheus/cli/library_option_types_command.rb', line 14

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

Instance Method Details

#_get(id, options) ⇒ Object



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
# File 'lib/morpheus/cli/library_option_types_command.rb', line 112

def _get(id, options)
  begin
    @option_types_interface.setopts(options)
    if options[:dry_run]
      if id.to_s =~ /\A\d{1,}\Z/
        print_dry_run @option_types_interface.dry.get(id.to_i)
      else
        print_dry_run @option_types_interface.dry.list({name: id})
      end
      return
    end
    option_type = find_option_type_by_name_or_id(id)
    return 1 if option_type.nil?
    json_response = {'optionType' => option_type}

    render_result = render_with_format(json_response, options, 'optionType')
    return 0 if render_result

    print_h1 "Option Type Details"
    print cyan
    columns = {
      "ID" => 'id',
      "Name" => 'name',
      "Description" => 'description',
      "Field Label" => 'fieldLabel',
      # "Field Context" => 'fieldContext',
      # "Field Name" => 'fieldName',
      "Full Field Name" => lambda {|it| [it['fieldContext'], it['fieldName']].select {|it| !it.to_s.empty? }.join('.') },
      "Type" => lambda {|it| it['type'].to_s.capitalize },
      "Option List" => lambda {|it| it['optionList'] ? it['optionList']['name'] : nil },
      "Placeholder" => 'placeHolder',
      "Help Block" => 'helpBlock',
      "Default Value" => 'defaultValue',
      "Required" => lambda {|it| format_boolean(it['required']) },
      "Export As Tag" => lambda {|it| it['exportMeta'].nil? ? '' : format_boolean(it['exportMeta']) },
    }
    columns.delete("Option List") if option_type['optionList'].nil?
    print as_description_list(option_type, columns, options)
    print reset,"\n"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#add(args) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/morpheus/cli/library_option_types_command.rb', line 158

def add(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[options]")
    build_option_type_options(opts, options, new_option_type_option_types)
    build_standard_add_options(opts, options)
    opts.footer = "Create a new option type."
  end
  optparse.parse!(args)
  connect(options)
  begin
    payload = nil
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!({'optionType' => parse_passed_options(options)})
    else
      payload = {}
      payload.deep_merge!({'optionType' => parse_passed_options(options)})
      option_type_payload = Morpheus::Cli::OptionTypes.prompt(new_option_type_option_types, options[:options], @api_client)
      # tweak payload for API
      option_type_payload['optionList'] = {'id' => option_type_payload['optionList'].to_i} if option_type_payload['optionList'].is_a?(String) || option_type_payload['optionList'].is_a?(Numeric)
      option_type_payload['required'] = ['on','true'].include?(option_type_payload['required'].to_s) if option_type_payload.key?('required')
      option_type_payload['exportMeta'] = ['on','true'].include?(option_type_payload['exportMeta'].to_s) if option_type_payload.key?('exportMeta')
      payload.deep_merge!({'optionType' => option_type_payload})
    end
    @option_types_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @option_types_interface.dry.create(payload)
      return
    end
    json_response = @option_types_interface.create(payload)
    render_result = render_with_format(json_response, options)
    return 0 if render_result
    option_type = json_response['optionType']
    print_green_success "Added Option Type #{option_type['name']}"
    #list([])
    get([option_type['id']] + (options[:remote] ? ["-r",options[:remote]] : []))
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#connect(opts) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/morpheus/cli/library_option_types_command.rb', line 18

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @library_instance_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).library_instance_types
  @provision_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).provision_types
  @option_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).option_types
  @option_type_lists_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).option_type_lists
end

#get(args) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/morpheus/cli/library_option_types_command.rb', line 93

def get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    build_standard_get_options(opts, options)
    opts.footer = "Get details about an option type.\n" + 
                  "[name] is required. This is the name or id of an option type. Supports 1-N [name] arguments."
  end
  optparse.parse!(args)
  if args.count < 1
    raise_command_error "wrong number of arguments, expected 1-N and got (#{args.count}) #{args.join(', ')}\n#{optparse}"
  end
  connect(options)
  id_list = parse_id_list(args)
  return run_command_for_each_arg(id_list) do |arg|
    _get(arg, options)
  end
end

#handle(args) ⇒ Object



26
27
28
# File 'lib/morpheus/cli/library_option_types_command.rb', line 26

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



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
# File 'lib/morpheus/cli/library_option_types_command.rb', line 30

def list(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_standard_list_options(opts, options)
    opts.footer = "List option types."
  end
  optparse.parse!(args)
  connect(options)
  begin
    params = {}
    params.merge!(parse_list_options(options))
    @option_types_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @option_types_interface.dry.list(params)
      return
    end

    json_response = @option_types_interface.list(params)

    render_result = render_with_format(json_response, options, 'optionTypes')
    return 0 if render_result

    option_types = json_response['optionTypes']
    subtitles = []
    subtitles += parse_list_subtitles(options)
    print_h1 "Morpheus Option Types", subtitles
    if option_types.empty?
      print cyan,"No option types found.",reset,"\n"
    else
      rows = option_types.collect do |option_type|
        {
          id: option_type['id'],
          name: option_type['name'],
          type: option_type['type'],
          fieldLabel: option_type['fieldLabel'],
          fieldName: option_type['fieldName'],
          default: option_type['defaultValue'],
          required: (option_type['required'].nil? ? '' : format_boolean(option_type['required'])),
          export: (option_type['exportMeta'].nil? ? '' : format_boolean(option_type['exportMeta']))
        }
      end
      print cyan
      print as_pretty_table(rows, [
        :id,
        :name,
        :type,
        {:fieldLabel => {:display_name => "Field Label"} },
        {:fieldName => {:display_name => "Field Name"} },
        :default,
        :required,
        :export,
      ], options)
      print reset
      print_results_pagination(json_response)
    end
    print reset,"\n"
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#remove(args) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/morpheus/cli/library_option_types_command.rb', line 246

def remove(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    build_standard_remove_options(opts, options)
    opts.footer = "Delete an option type.\n" +
                  "[name] is required. This is the name or id of an option type."
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  connect(options)

  begin
    option_type = find_option_type_by_name_or_id(args[0])
    exit 1 if option_type.nil?

    unless Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the option type #{option_type['name']}?", options)
      exit
    end
    @option_types_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @option_types_interface.dry.destroy(option_type['id'])
      return
    end
    json_response = @option_types_interface.destroy(option_type['id'])

    render_result = render_with_format(json_response, options)
    return 0 if render_result

    print_green_success "Removed Option Type #{option_type['name']}"
    #list([])
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update(args) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/morpheus/cli/library_option_types_command.rb', line 201

def update(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] [options]")
    build_option_type_options(opts, options, update_option_type_option_types)
    build_standard_update_options(opts, options)
    opts.footer = "Update an option type.\n" +
                  "[name] is required. This is the name or id of an option type."
  end
  optparse.parse!(args)
  connect(options)
  begin
    option_type = find_option_type_by_name_or_id(args[0])
    exit 1 if option_type.nil?
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!({'optionType' => parse_passed_options(options)})
    else
      payload = {}
      payload.deep_merge!({'optionType' => parse_passed_options(options)})
      option_type_payload = Morpheus::Cli::OptionTypes.no_prompt(update_option_type_option_types, options[:options], @api_client)
      # tweak payload for API
      option_type_payload['optionList'] = {'id' => option_type_payload['optionList'].to_i} if option_type_payload['optionList'].is_a?(String) || option_type_payload['optionList'].is_a?(Numeric)
      option_type_payload['required'] = ['on','true'].include?(option_type_payload['required'].to_s) if option_type_payload.key?('required')
      option_type_payload['exportMeta'] = ['on','true'].include?(option_type_payload['exportMeta'].to_s) if option_type_payload.key?('exportMeta')
      payload.deep_merge!({'optionType' => option_type_payload})
      raise_command_error "Specify at least one option to update.\n#{optparse}" if payload['optionType'].empty?
    end
    @option_types_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @option_types_interface.dry.update(option_type['id'], payload)
      return
    end
    json_response = @option_types_interface.update(option_type['id'], payload)
    render_result = render_with_format(json_response, options)
    return 0 if render_result
    print_green_success "Updated Option Type #{option_type_payload['name']}"
    #list([])
    get([option_type['id']] + (options[:remote] ? ["-r",options[:remote]] : []))
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end