Class: Morpheus::Cli::LibraryLayoutsCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand, LibraryHelper, ProvisioningHelper
Defined in:
lib/morpheus/cli/commands/library_layouts_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 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

#initializeLibraryLayoutsCommand

Returns a new instance of LibraryLayoutsCommand.



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

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

Instance Method Details

#_get(id, options) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
200
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
245
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/morpheus/cli/commands/library_layouts_command.rb', line 139

def _get(id, options)
  exit_code, err = 0, nil
  instance_type_id = nil
  params = {}
  params.merge!(parse_query_options(options))
  @library_layouts_interface.setopts(options)
  if options[:dry_run]
    if id.to_s =~ /\A\d{1,}\Z/
      print_dry_run @library_layouts_interface.dry.get(instance_type_id, id.to_i, params)
    else
      print_dry_run @library_layouts_interface.dry.list(instance_type_id, params.merge({name:id}))
    end
    return
  end
  layout = find_layout_by_name_or_id(instance_type_id, id)
  if layout.nil?
    return 1
  end
  # skip redundant request
  #json_response = @library_layouts_interface.get(instance_type_id, layout['id'])
  json_response = {'instanceTypeLayout' => layout}
  #layout = json_response['instanceTypeLayout']
  if options[:json]
    puts as_json(json_response, options, "instanceTypeLayout")
    return 0
  elsif options[:yaml]
    puts as_yaml(json_response, options, "instanceTypeLayout")
    return 0
  elsif options[:csv]
    puts records_as_csv([json_response['instanceTypeLayout']], options)
    return 0
  end

  print_h1 "Layout Details"
  print cyan
  description_cols = {
    "ID" => lambda {|it| it['id'] },
    "Name" => lambda {|it| it['name'] },
    "Instance Type" => lambda {|it| it['instanceType']['name'] rescue '' },
    #"Code" => lambda {|it| it['code'] },
    "Version" => lambda {|it| it['instanceVersion'] },
    "Description" => lambda {|it| it['description'] },
    "Technology" => lambda {|it| format_layout_technology(it) },
    "Labels" => lambda {|it| format_list(it['labels'], '') },
    "Min Memory" => lambda {|it| 
      if it['memoryRequirement'].to_i != 0
        (it['memoryRequirement'].to_i / (1024*1024)).to_s + " MB"
      else
        ""
      end
    },
    "Workflow" => lambda {|it| 
      if it['taskSets']
        it['taskSets'][0]['name'] rescue ""
      else
        ""
      end
    },
    "Tfvar Secret" => lambda {|it| it['tfvarSecret'] },
    # "Category" => lambda {|it| it['category'].to_s.capitalize },
    # # "Logo" => lambda {|it| it['logo'].to_s },
    # "Visiblity" => lambda {|it| it['visibility'].to_s.capitalize },
    # "Environment Prefix" => lambda {|it| it['environmentPrefix'] },
    # "Enable Settings" => lambda {|it| format_boolean it['hasSettings'] },
    # "Enable Scaling" => lambda {|it| format_boolean it['hasAutoScale'] },
    # "Supports Deployments" => lambda {|it| format_boolean it['hasDeployment'] },
    # "Featured" => lambda {|it| format_boolean it['featured'] },
    # "Owner" => lambda {|it| it['account'] ? it['account']['name'] : '' },
    # "Active" => lambda {|it| format_boolean it['active'] },
    # "Created" => lambda {|it| format_local_dt(it['dateCreated']) },
    # "Updated" => lambda {|it| format_local_dt(it['lastUpdated']) }
  }
  description_cols.delete("Tfvar Secret") if !(layout['provisionType'] && layout['provisionType']['code'] == 'terraform')
  print_description_list(description_cols, layout)

  price_sets = layout['priceSets']
    if price_sets && price_sets.size > 0
      print_h2 "Price Sets"
      price_set_columns = [
        {"ID" => lambda {|it| it['id'] } },
        {"NAME" => lambda {|it| it['name'] } },
        {"PRICE UNIT" => lambda {|it| it['priceUnit'] } },
      ]
      print as_pretty_table(price_sets, price_set_columns)
    else
      # print cyan,"No price sets found for this instance type.","\n",reset
    end

  layout_evars = layout['environmentVariables']
  if layout_evars && layout_evars.size > 0
    print_h2 "Environment Variables"
    evar_columns = [
      {"NAME" => lambda {|it| it['name'] } },
      {"VALUE" => lambda {|it| it['defaultValue'] } },
      {"TYPE" => lambda {|it| it['valueType'].to_s.capitalize } },
      {"EXPORT" => lambda {|it| format_boolean it['export'] } },
      {"MASKED" => lambda {|it| format_boolean it['masked'] } },
    ]
    print as_pretty_table(layout_evars, evar_columns)
  else
    # print yellow,"No environment variables found for this instance type.","\n",reset
  end

  layout_option_types = layout['optionTypes']
  if layout_option_types && layout_option_types.size > 0
    print_h2 "Option Types"
    columns = [
      {"ID" => lambda {|it| it['id'] } },
      {"NAME" => lambda {|it| it['name'] } },
      {"TYPE" => lambda {|it| it['type'] } },
      {"FIELD NAME" => lambda {|it| it['fieldName'] } },
      {"FIELD LABEL" => lambda {|it| it['fieldLabel'] } },
      {"DEFAULT" => lambda {|it| it['defaultValue'] } },
      {"REQUIRED" => lambda {|it| format_boolean it['required'] } },
    ]
    print as_pretty_table(layout_option_types, columns)
  else
    # print yellow,"No option types found for this layout.","\n",reset
  end

  layout_node_types = layout['containerTypes']
  if layout_node_types && layout_node_types.size > 0
    print_h2 "Node Types"
    # match UI sorting [version desc, name asc]
    # or use something simpler like one of these
    layout_node_types = layout_node_types.sort { |a,b| a['name'] <=> b['name'] }
    # layout_node_types = layout_node_types.sort { |a,b| a['sortOrder'] <=> b['sortOrder'] }
    node_type_columns = [
      {"ID" => lambda {|it| it['id'] } },
      {"NAME" => lambda {|it| it['name'] } },
      {"SHORT NAME" => lambda {|it| it['shortName'] } },
      {"VERSION" => lambda {|it| it['containerVersion'] } },
      {"TECHNOLOGY" => lambda {|it| it['provisionType'] ? it['provisionType']['name'] : '' } },
      {"CATEGORY" => lambda {|it| it['category'] } },
    ]
    print as_pretty_table(layout_node_types, node_type_columns)
  else
    # print yellow,"No node types for this layout.","\n",reset
  end

  layout_spec_templates = layout['specTemplates']
  if layout_spec_templates && layout_spec_templates.size > 0
    print_h2 "Spec Templates"
    layout_spec_templates = layout_spec_templates.sort { |a,b| a['name'] <=> b['name'] }
    spec_template_columns = [
      {"ID" => lambda {|it| it['id'] } },
      {"NAME" => lambda {|it| it['name'] } },
      {"TYPE" => lambda {|it| it['type']['name'] rescue '' } }
    ]
    print as_pretty_table(layout_spec_templates, spec_template_columns)
  else
    # print yellow,"No spec templates for this layout.","\n",reset
  end


  if options[:show_perms] || (layout['permissions'] && !layout['permissions'].empty?)
    print_permissions(layout['permissions'], layout_permission_excludes)
    print reset
  else
    print reset,"\n"
  end
  return exit_code, err
end

#add(args) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/morpheus/cli/commands/library_layouts_command.rb', line 303

def add(args)
  options = {}
  params = {}
  instance_type_id = nil
  evars = nil
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[instance-type]")
    opts.on('--instance-type ID', String, "Instance Type") do |val|
      instance_type_id = val
    end
    opts.on('--name VALUE', String, "Name for this layout") do |val|
      params['name'] = val
    end
    opts.on('-l', '--labels [LIST]', String, "Labels") do |val|
      params['labels'] = parse_labels(val)
    end
    opts.on('--version VALUE', String, "Version") do |val|
      params['instanceVersion'] = val
    end
    opts.on('--description VALUE', String, "Description") do |val|
      params['description'] = val
    end
    opts.on("--creatable [on|off]", ['on','off'], "Creatable") do |val|
      params['creatable'] = (val.to_s != 'false' && val.to_s != 'off')
    end
    opts.on('--technology CODE', String, "Technology") do |val|
      params['provisionTypeCode'] = val
    end
    opts.on('--min-memory VALUE', String, "Minimum Memory (MB)") do |val|
      params['memoryRequirement'] = val
    end
    opts.on("--auto-scale [on|off]", ['on','off'], "Enable Scaling (Horizontal)") do |val|
      params['hasAutoScale'] = (val.to_s != 'false' && val.to_s != 'off')
    end
    opts.on("--convert-to-managed [on|off]", ['on','off'], "Supports Convert To Managed") do |val|
      params['supportsConvertToManaged'] = (val.to_s != 'false' && val.to_s != 'off')
    end
    opts.on('--workflow ID', String, "Workflow") do |val|
      params['taskSetId'] = val.to_i
    end
    opts.on('--option-types [x,y,z]', Array, "List of Option Type IDs") do |list|
      if list.nil?
        params['optionTypes'] = []
      else
        params['optionTypes'] = list.collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq
      end
    end
    opts.on('--node-types [x,y,z]', Array, "List of Node Type IDs") do |list|
      if list.nil?
        params['containerTypes'] = []
      else
        params['containerTypes'] = list.collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq
      end
    end
    opts.on('--spec-templates [x,y,z]', Array, "List of Spec Templates to include in this layout, comma separated list of names or IDs.") do |list|
      if list.nil?
        params['specTemplates'] = []
      else
        params['specTemplates'] = list.collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq
      end
    end
    opts.on('--tfvar-secret VALUE', String, "Tfvar Secret name, eg. 'tfvars/dev-key'") do |val|
      params['tfvarSecret'] = val == 'null' ? nil : val
    end
    opts.on('--price-sets [LIST]', Array, 'Price set(s), comma separated list of price set IDs') do |list|
      params['priceSets'] = list.collect {|it| it.to_s.strip.empty? || !it.to_i ? nil : it.to_s.strip}.compact.uniq.collect {|it| {'id' => it.to_i}}
    end
    add_perms_options(opts, options, layout_permission_excludes)
    build_standard_add_options(opts, options)
    opts.footer = <<-EOT
Create a new layout.
[instance-type] is required and can be passed as --instance-type instead.
EOT
  end
  optparse.parse!(args)
  if args.count > 1
    raise_command_error "wrong number of arguments, expected 0-1 and got (#{args.count}) #{args}\n#{optparse}"
  end
  if args[0]
    #params["name"] = args[0]
    instance_type_id = args[0]
  end
  if instance_type_id.nil?
    instance_type_id = args[0]
  end
  if !instance_type_id
    puts optparse
    return 1
  end
  connect(options)
  begin
    instance_type = find_instance_type_by_name_or_id(instance_type_id)
    exit 1 if instance_type.nil?
    instance_type_id = instance_type['id']

    # construct payload
    payload = nil
    if options[:payload]
      payload = options[:payload]
    else
      params.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]
      
      if !params['name']
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => 'Name', 'required' => true}], options[:options])
        params['name'] = v_prompt['name']
      end
      if !params['instanceVersion']
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'instanceVersion', 'type' => 'text', 'fieldLabel' => 'Version', 'required' => true}], options[:options])
        params['instanceVersion'] = v_prompt['instanceVersion']
      end
      if !params['description']
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'description', 'type' => 'text', 'fieldLabel' => 'Description', 'required' => false}], options[:options])
        params['description'] = v_prompt['description'] if v_prompt['description']
      end
      if params['creatable'].nil?
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'creatable', 'type' => 'checkbox', 'fieldLabel' => 'Creatable', 'defaultValue' => 'on'}], options[:options])
        params['creatable'] = ['true','on'].include?(v_prompt['creatable'].to_s) if v_prompt['creatable'] != nil
      end

      provision_types = @provision_types_interface.list({customSupported: true})['provisionTypes']
      if provision_types.empty?
        print_red_alert "No available provision types found!"
        exit 1
      end
      provision_type_options = provision_types.collect {|it| { 'name' => it['name'], 'value' => it['code']} }

      if !params['provisionTypeCode']
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'provisionTypeCode', 'type' => 'select', 'selectOptions' => provision_type_options, 'fieldLabel' => 'Technology', 'required' => true, 'description' => 'The type of container technology.'}], options[:options])
        params['provisionTypeCode'] = v_prompt['provisionTypeCode']
      else

      end
      provision_type = provision_types.find {|it| it['code'] == params['provisionTypeCode'] }

      if !params['memoryRequirement']
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'memoryRequirement', 'type' => 'text', 'fieldLabel' => 'Min Memory (MB)', 'required' => false, 'description' => 'This will override any memory requirement set on the virtual image'}], options[:options])
        params['memoryRequirement'] = v_prompt['memoryRequirement'] if v_prompt['memoryRequirement']
      end
      if params['hasAutoScale'].nil?
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'hasAutoScale', 'type' => 'checkbox', 'fieldLabel' => 'Enable Scaling (Horizontal)'}], options[:options])
        params['hasAutoScale'] = ['true','on'].include?(v_prompt['hasAutoScale'].to_s) if v_prompt['hasAutoScale'] != nil
      end
      if params['supportsConvertToManaged'].nil?
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'supportsConvertToManaged', 'type' => 'checkbox', 'fieldLabel' => 'Supports Convert To Managed'}], options[:options])
        params['supportsConvertToManaged'] = ['true','on'].include?(v_prompt['supportsConvertToManaged'].to_s) if v_prompt['supportsConvertToManaged'] != nil
      end        
      if !params['taskSetId']
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'taskSetId', 'type' => 'text', 'fieldLabel' => 'Workflow ID', 'required' => false, 'description' => 'Worflow ID'}], options[:options])
        params['taskSetId'] = v_prompt['taskSetId'].to_i if v_prompt['taskSetId']
      end
      
      # ENVIRONMENT VARIABLES
      if evars

      else
        # prompt
      end

      # OPTION TYPES
      prompt_results = prompt_for_option_types(params, options, @api_client)
      if prompt_results[:success]
        params['optionTypes'] = prompt_results[:data] unless prompt_results[:data].nil?
      else
        return 1
      end

      # NODE TYPES
      prompt_results = prompt_for_container_types(params, options, @api_client)
      if prompt_results[:success]
        params['containerTypes'] = prompt_results[:data] unless prompt_results[:data].nil?
      else
        return 1
      end

      # SPEC TEMPLATES
      prompt_results = prompt_for_spec_templates(params, options, @api_client)
      if prompt_results[:success]
        params['specTemplates'] = prompt_results[:data] unless prompt_results[:data].nil?
      else
        return 1
      end
      
      # TFVAR SECRET
      if !params['tfvarSecret']
        if provision_type && provision_type['code'] == 'terraform'
          v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'tfvarSecret', 'type' => 'select', 'optionSource' => 'tfvarSecrets', 'fieldLabel' => 'Tfvar Secret', 'required' => false}], options[:options], @api_client)
          params['tfvarSecret'] = v_prompt['tfvarSecret'] if v_prompt['tfvarSecret']
        end
      end

      payload = {'instanceTypeLayout' => params}
      
      # Resource Permissions (Groups only for layouts)
      perms = prompt_permissions(options.merge({}), layout_permission_excludes)
      perms_payload = {}
      perms_payload['resourcePermissions'] = perms['resourcePermissions'] if !perms['resourcePermissions'].empty?
      #perms_payload['tenantPermissions'] = perms['tenantPermissions'] if !perms['tenantPermissions'].nil?

      payload['instanceTypeLayout']['permissions'] = perms_payload
      payload['instanceTypeLayout']['visibility'] = perms['resourcePool']['visibility'] if !perms['resourcePool'].nil? && !perms['resourcePool']['visibility'].nil?

    end
    @library_layouts_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @library_layouts_interface.dry.create(instance_type_id, payload)
      return
    end

    json_response = @library_layouts_interface.create(instance_type_id, payload)

    if options[:json]
      print JSON.pretty_generate(json_response), "\n"
      return
    end

    print_green_success "Added layout #{params['name']}"

    #get([json_response['instanceTypeLayout']['id']])

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

#connect(opts) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/morpheus/cli/commands/library_layouts_command.rb', line 18

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @library_layouts_interface = @api_client.library_layouts
  @library_instance_types_interface = @api_client.library_instance_types
  @library_container_types_interface = @api_client.library_container_types
  @spec_templates_interface = @api_client.library_spec_templates
  @spec_template_types_interface = @api_client.library_spec_template_types
  @provision_types_interface = @api_client.provision_types
  @option_types_interface = @api_client.option_types
  @option_type_lists_interface = @api_client.option_type_lists
end

#get(args) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/morpheus/cli/commands/library_layouts_command.rb', line 117

def get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[layout]")
    # opts.on( nil, '--permissions', "Display permissions" ) do
    #   options[:show_perms] = true
    # end
    build_standard_get_options(opts, options)
    opts.footer = <<-EOT
Get details about a layout.
[layout] is required. This is the name or id of a layout.
EOT
  end
  verify_args!(args:args, optparse:optparse, min:1)
  optparse.parse!(args)
  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



30
31
32
# File 'lib/morpheus/cli/commands/library_layouts_command.rb', line 30

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/morpheus/cli/commands/library_layouts_command.rb', line 35

def list(args)
  options = {}
  params = {}
  instance_type = nil
  instance_type_id = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    opts.on('--instance-type ID', String, "Filter by Instance Type") do |val|
      instance_type_id = val
    end
    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['provisionType'] = val
    end
    opts.on('-l', '--labels LABEL', String, "Filter by labels, can match any of the values") do |val|
      add_query_parameter(params, 'labels', parse_labels(val))
    end
    opts.on('--all-labels LABEL', String, "Filter by labels, must match all of the values") do |val|
      add_query_parameter(params, 'allLabels', parse_labels(val))
    end
    build_common_options(opts, options, [:list, :query, :json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "List layouts."
  end
  optparse.parse!(args)
  # verify_args!(args:args, optparse:optparse, count:0)
  if args.count > 0
    options[:phrase] = args.join(" ")
  end
  connect(options)
  begin
    # construct payload
    if instance_type_id
      instance_type = find_instance_type_by_name_or_id(instance_type_id)
      return 1 if instance_type.nil?
      instance_type_id = instance_type['id']
    end
    
    params.merge!(parse_list_options(options))
    @library_layouts_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @library_layouts_interface.dry.list(instance_type_id, params)
      return
    end

    json_response = @library_layouts_interface.list(instance_type_id, params)
    if options[:json]
      puts as_json(json_response, options, "instanceTypeLayouts")
      return 0
    elsif options[:csv]
      puts records_as_csv(json_response['instanceTypeLayouts'], options)
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "instanceTypeLayouts")
      return 0
    end
    layouts = json_response['instanceTypeLayouts']
    title = "Morpheus Library - Layouts"
    subtitles = []
    if instance_type
      subtitles << "Instance Type: #{instance_type['name']}".strip
    end
    subtitles += parse_list_subtitles(options)
    print_h1 title, subtitles
    if layouts.empty?
      print cyan,"No layouts found.",reset,"\n"
    else
      print_layouts_table(layouts, options)
      print_results_pagination(json_response, {:label => "layout", :n_label => "layouts"})
      # print_results_pagination(json_response)
    end
    print reset,"\n"
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#remove(args) ⇒ Object



736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# File 'lib/morpheus/cli/commands/library_layouts_command.rb', line 736

def remove(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
    opts.footer = "Delete a layout."
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  connect(options)

  begin
    layout = find_layout_by_name_or_id(nil, args[0])
    exit 1 if layout.nil?

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

    if options[:json]
      print JSON.pretty_generate(json_response), "\n"
      return
    end

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

#update(args) ⇒ Object



528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
# File 'lib/morpheus/cli/commands/library_layouts_command.rb', line 528

def update(args)
  options = {}
  params = {}
  instance_type_id = nil
  evars = nil
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[layout] [options]")
    opts.on('--name VALUE', String, "Name for this layout") do |val|
      params['name'] = val
    end
    opts.on('-l', '--labels [LIST]', String, "Labels") do |val|
      params['labels'] = parse_labels(val)
    end
    opts.on('--version VALUE', String, "Version") do |val|
      params['instanceVersion'] = val
    end
    opts.on('--description VALUE', String, "Description") do |val|
      params['description'] = val
    end
    opts.on("--creatable [on|off]", ['on','off'], "Creatable") do |val|
      params['creatable'] = (val.to_s != 'false' && val.to_s != 'off')
    end
    opts.on('--technology CODE', String, "Technology") do |val|
      params['provisionTypeCode'] = val
    end
    opts.on('--min-memory VALUE', String, "Minimum Memory (MB)") do |val|
      params['memoryRequirement'] = val
    end
    opts.on("--auto-scale [on|off]", ['on','off'], "Enable Scaling (Horizontal)") do |val|
      params['hasAutoScale'] = (val.to_s != 'false' && val.to_s != 'off')
    end
    opts.on("--convert-to-managed [on|off]", ['on','off'], "Supports Convert To Managed") do |val|
      params['supportsConvertToManaged'] = (val.to_s != 'false' && val.to_s != 'off')
    end
    opts.on('--workflow ID', String, "Workflow") do |val|
      params['taskSetId'] = val.to_i
    end
    opts.on('--option-types [x,y,z]', Array, "List of Option Type IDs") do |list|
      if list.nil?
        params['optionTypes'] = []
      else
        params['optionTypes'] = list.collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq
      end
    end
    opts.on('--node-types [x,y,z]', Array, "List of Node Type IDs") do |list|
      if list.nil?
        params['containerTypes'] = []
      else
        params['containerTypes'] = list.collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq
      end
    end
    opts.on('--spec-templates [x,y,z]', Array, "List of Spec Templates to include in this layout, comma separated list of names or IDs.") do |list|
      if list.nil?
        params['specTemplates'] = []
      else
        params['specTemplates'] = list.collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq
      end
    end
    opts.on('--tfvar-secret VALUE', String, "Tfvar Secret name, eg. 'tfvars/dev-key'") do |val|
      params['tfvarSecret'] = val == 'null' ? nil : val
    end
    opts.on('--price-sets [LIST]', Array, 'Price set(s), comma separated list of price set IDs') do |list|
      params['priceSets'] = list.collect {|it| it.to_s.strip.empty? || !it.to_i ? nil : it.to_s.strip}.compact.uniq.collect {|it| {'id' => it.to_i}}
    end
    add_perms_options(opts, options, layout_permission_excludes)
    build_standard_update_options(opts, options)
          opts.footer = <<-EOT
Update a layout.
[layout] is required. This is the name or id of a layout.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  exit_code, err = 0, nil
  
    layout = find_layout_by_name_or_id(nil, args[0])
    exit 1 if layout.nil?
    payload = nil
    if options[:payload]
      payload = options[:payload]
    else
      params.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]
      
      # ENVIRONMENT VARIABLES
      if evars

      else
        # prompt
      end

      # OPTION TYPES
      if params['optionTypes']
        prompt_results = prompt_for_option_types(params, options, @api_client)
        if prompt_results[:success]
          params['optionTypes'] = prompt_results[:data] unless prompt_results[:data].nil?
        else
          return 1
        end
      end

      # NODE TYPES
      if params['containerTypes']
        prompt_results = prompt_for_container_types(params, options, @api_client)
        if prompt_results[:success]
          params['containerTypes'] = prompt_results[:data] unless prompt_results[:data].nil?
        else
          return 1
        end
      end

      # SPEC TEMPLATES
      if params['specTemplates']
        prompt_results = prompt_for_spec_templates(params, options, @api_client)
        if prompt_results[:success]
          params['specTemplates'] = prompt_results[:data] unless prompt_results[:data].nil?
        else
          return 1
        end
      end

      # perms
      if options[:groupAccessAll] != nil || options[:groupAccessList]
        perms = prompt_permissions(options.merge({no_prompt:true}), layout_permission_excludes)
        perms_payload = {}
        perms_payload['resourcePermissions'] = perms['resourcePermissions'] if !perms['resourcePermissions'].empty?
        params.deep_merge!({'permissions' => perms_payload}) if !perms_payload.empty?
      end
      
      params['visibility'] = options[:visibility] if !options[:visibility].nil?

      if params.empty?
        raise_command_error "Specify at least one option to update.\n#{optparse}"
      end

      payload = {'instanceTypeLayout' => params}

    end
    @library_layouts_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @library_layouts_interface.dry.update(nil, layout['id'], payload)
      return
    end
    
    json_response = @library_layouts_interface.update(nil, layout['id'], payload)
    
    if options[:json]
      print JSON.pretty_generate(json_response), "\n"
      return
    end

    print_green_success "Updated layout #{params['name'] || layout['name']}"
    #list([])
  return exit_code, err
end

#update_permissions(args) ⇒ Object



684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'lib/morpheus/cli/commands/library_layouts_command.rb', line 684

def update_permissions(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage( "[layout]")
    add_perms_options(opts, options, layout_permission_excludes)
    build_standard_update_options(opts, options)
    opts.footer = <<-EOT
Update layout permissions.
[layout] is required. This is the name or id of a layout.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  exit_code, err = 0, nil
  # if !is_master_account
  #   print_red_alert "Permissions only available for master tenant"
  #   return 1
  # end
  layout = find_layout_by_name_or_id(nil, args[0])
  return 1, "layout not found for #{args[0]}" if layout.nil?
  payload = {}
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!(parse_passed_options(options))
  else
    payload.deep_merge!(parse_passed_options(options))
    
    perms = prompt_permissions(options.merge({}), layout_permission_excludes)
    perms_payload = {}
    perms_payload['resourcePermissions'] = perms['resourcePermissions'] if !perms['resourcePermissions'].empty?
    payload.deep_merge!({'permissions' => perms_payload}) if !perms_payload.empty?
  end

  @library_layouts_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @library_layouts_interface.dry.update_permissions(layout['id'], payload)
    return
  end
  json_response = @library_layouts_interface.update_permissions(layout['id'], payload)
  render_response(json_response, options, 'instanceTypeLayout') do
    # note: this api does not return 400 when it fails?
    if json_response['success']
      print_green_success "Updated layout permissions"
    else
      print_rest_errors(json_response, options)
      exit_code, err = 3, (json_response['msg'] || "api did not return success:true")
    end
  end
  return exit_code, err
end