Class: Morpheus::Cli::LibraryContainerTypesCommand
- Inherits:
-
Object
- Object
- Morpheus::Cli::LibraryContainerTypesCommand
show all
- Includes:
- CliCommand, LibraryHelper
- Defined in:
- lib/morpheus/cli/commands/library_container_types_command.rb
Instance Attribute Summary
Attributes included from CliCommand
#no_prompt
Instance Method Summary
collapse
#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
#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
Returns a new instance of LibraryContainerTypesCommand.
11
12
13
|
# File 'lib/morpheus/cli/commands/library_container_types_command.rb', line 11
def initialize()
end
|
Instance Method Details
#_get(id, options) ⇒ Object
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
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
|
# File 'lib/morpheus/cli/commands/library_container_types_command.rb', line 122
def _get(id, options)
layout_id = nil
begin
@library_container_types_interface.setopts(options)
if options[:dry_run]
if id.to_s =~ /\A\d{1,}\Z/
print_dry_run @library_container_types_interface.dry.get(layout_id, id.to_i)
else
print_dry_run @library_container_types_interface.dry.list(layout_id, {name:id})
end
return
end
container_type = find_container_type_by_name_or_id(layout_id, id)
if container_type.nil?
return 1
end
json_response = {'containerType' => container_type}
if options[:json]
puts as_json(json_response, options, "containerType")
return 0
elsif options[:yaml]
puts as_yaml(json_response, options, "containerType")
return 0
elsif options[:csv]
puts records_as_csv([json_response['containerType']], options)
return 0
end
print_h1 "Node Type Details"
print cyan
description_cols = {
"ID" => lambda {|it| it['id'] },
"Name" => lambda {|it| it['name'] },
"Short Name" => lambda {|it| it['shortName'] },
"Version" => lambda {|it| it['containerVersion'] },
"Labels" => lambda {|it| format_list(it['labels'], '', 3) },
"Technology" => lambda {|it| format_container_type_technology(it) },
"Category" => lambda {|it| it['category'] },
"Virtual Image" => lambda {|it|
it['virtualImage'] ? it['virtualImage']['name'] : ''
},
}
print_description_list(description_cols, container_type)
evars = container_type['environmentVariables']
if evars && 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(evars, evar_columns)
else
end
exposed_ports = container_type['containerPorts']
if exposed_ports && exposed_ports.size > 0
print_h2 "Exposed Ports"
columns = [
{"NAME" => lambda {|it| it['name'] } },
{"PORT" => lambda {|it| it['port'] } },
{"LB PROTOCOL" => lambda {|it| it['loadBalanceProtocol'] } },
]
print as_pretty_table(exposed_ports, columns)
else
end
container_scripts = container_type['containerScripts'] || container_type['scripts']
if container_scripts && container_scripts.size > 0
print_h2 "Scripts"
columns = [
{"ID" => lambda {|it| it['id'] } },
{"NAME" => lambda {|it| it['name'] } }
]
print as_pretty_table(container_scripts, columns)
else
end
container_file_templates = container_type['containerTemplates'] || container_type['templates']
if container_file_templates && container_file_templates.size > 0
print_h2 "File Templates"
columns = [
{"ID" => lambda {|it| it['id'] } },
{"NAME" => lambda {|it| it['name'] } }
]
print as_pretty_table(container_file_templates, columns)
else
end
print reset,"\n"
rescue RestClient::Exception => e
print_rest_exception(e, options)
return 1
end
end
|
#add(args) ⇒ Object
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
302
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
|
# File 'lib/morpheus/cli/commands/library_container_types_command.rb', line 243
def add(args)
options = {}
params = {}
layout = nil
layout_id = nil
script_ids = nil
file_template_ids = nil
evars = nil
optparse = Morpheus::Cli::OptionParser.new do|opts|
opts.banner = subcommand_usage()
opts.on('--name VALUE', String, "Name for this node type") do |val|
params['name'] = val
end
opts.on('-l', '--labels [LIST]', String, "Labels") do |val|
params['labels'] = parse_labels(val)
end
opts.on('--shortName VALUE', String, "Short Name") do |val|
params['shortName'] = val
end
opts.on('--version VALUE', String, "Version") do |val|
params['containerVersion'] = val
end
opts.on('--technology CODE', String, "Technology. This is the provision type code.") do |val|
params['provisionTypeCode'] = val
end
opts.on('--evars-json JSON', String, 'Environment variables JSON: {"name":"Foo", "value":"Bar", "masked":true, "export":true}' ) do |val|
begin
evars = JSON.parse(val.to_s)
evars = evars.kind_of?(Array) ? evars : [evars]
rescue JSON::ParserError => e
print_red_alert "Unable to parse evars JSON"
exit 1
end
end
opts.on('-e', '--evars LIST', Array, "Environment variables list. Comma delimited list of name=value pairs") do |val|
evars = val.collect do |nv|
parts = nv.split('=')
{'name' => parts[0].strip, 'value' => (parts.count > 1 ? parts[1].strip : '')}
end
end
opts.on('--ports NAME=PORT,NAME=PORT', String, "List of exposed port definitions in the format NAME=PORT|PROTOCOL, Example: \"WEB=80|HTTP,SECURE=443|HTTPS\"") do |val|
params['containerPorts'] ||= []
parsed_ports = val.split(",").each do |value_pair|
k,v = value_pair.strip.split("=")
value_array = v.split("|")
port_name = k.to_s.strip
port_number = value_array[0].to_s.strip.to_i
exposed_port = {'name' => port_name,'port' => port_number }
if value_array[1]
exposed_port['loadBalanceProtocol'] = value_array[1].strip
end
if exposed_port['name'].to_s.empty? || !exposed_port['port'] || exposed_port['port'].to_i <= 0
raise_command_error "Invalid exposed port definition: '#{value_pair}'. Expected format is 'NAME=PORT'", args, optparse
end
params['containerPorts'] << exposed_port
end
end
opts.on('--scripts x,y,z', Array, "List of Script IDs") do |val|
script_ids = val end
opts.on('--file-templates x,y,z', Array, "List of File Template IDs") do |val|
file_template_ids = val end
build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote])
opts. = "Create a node type." + "\n" +
"[name] is required and can be passed as --name instead."
"Technology --technology is required. Additional options vary by type."
end
optparse.parse!(args)
connect(options)
if args.count > 1
print_error Morpheus::Terminal.angry_prompt
puts_error "wrong number of arguments, expected 0-1 and got (#{args.count}) #{args.inspect}\n#{optparse}"
return 1
end
if args[0]
params['name'] = args[0]
end
begin
if layout_id
layout = find_layout_by_name_or_id(instance_type_id)
return 1 if layout.nil?
layout_id = layout['id']
end
payload = nil
if options[:payload]
payload = options[:payload]
else
payload = {}
payload.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]
prompt_params = params.merge(:no_prompt=>options[:no_prompt]) if !params['name']
v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => 'Name', 'required' => true}], prompt_params)
params['name'] = v_prompt['name']
end
if !params['shortName']
v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'shortName', 'type' => 'text', 'fieldLabel' => 'Short Name', 'required' => true, 'description' => 'The short name is a lowercase name with no spaces used for display in your container list.'}], prompt_params)
params['shortName'] = v_prompt['shortName']
end
if !params['containerVersion']
v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'containerVersion', 'type' => 'text', 'fieldLabel' => 'Version', 'required' => true}], prompt_params)
params['containerVersion'] = v_prompt['containerVersion']
end
provision_types = @provision_types_interface.list({customSupported: true})['provisionTypes']
if provision_types.empty?
print_red_alert "No available provision types found!"
return 1
end
provision_type_options = provision_types.collect {|it| { 'name' => it['name'], 'value' => it['code']} }
provision_type = nil
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.'}], prompt_params)
params['provisionTypeCode'] = v_prompt['provisionTypeCode']
end
provision_type = provision_types.find {|it| it['code'] == params['provisionTypeCode'] }
if provision_type.nil?
print_red_alert "Provision Type not found by code '#{params['provisionTypeCode']}'!"
return 1
end
provision_type_custom_option_types = provision_type['customOptionTypes']
provision_type_v_prompt = nil
if (!provision_type_custom_option_types || provision_type_custom_option_types.empty?)
else
field_group_name = provision_type_custom_option_types.first['fieldGroup'] || "#{provision_type['name']} Options"
field_group_name = "#{provision_type['name']} Options"
puts field_group_name
puts "==============="
provision_type_v_prompt = Morpheus::Cli::OptionTypes.prompt(provision_type_custom_option_types,options[:options],@api_client, {provisionTypeCode: params['provisionTypeCode']})
end
if evars
payload['environmentVariables'] = evars
else
end
if script_ids
params['scripts'] = script_ids.collect {|it| it.to_i }
else
end
if file_template_ids
params['templates'] = file_template_ids.collect {|it| it.to_i }
else
end
payload['containerType'] ||= {}
payload['containerType'].deep_merge!(params)
if provision_type_v_prompt
payload.deep_merge!(provision_type_v_prompt)
end
end
if payload['containerType']
payload['containerType']['config'] ||= {}
end
@library_container_types_interface.setopts(options)
if options[:dry_run]
print_dry_run @library_container_types_interface.dry.create(layout_id, payload)
return
end
json_response = @library_container_types_interface.create(layout_id, payload)
if options[:json]
print JSON.pretty_generate(json_response), "\n"
return
end
container_type = json_response['containerType']
print_green_success "Added Node Type #{container_type['name']}"
get([json_response['containerType']['id']])
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#connect(opts) ⇒ Object
15
16
17
18
19
20
21
22
23
|
# File 'lib/morpheus/cli/commands/library_container_types_command.rb', line 15
def connect(opts)
@api_client = establish_remote_appliance_connection(opts)
@library_container_types_interface = @api_client.library_container_types
@library_layouts_interface = @api_client.library_layouts
@provision_types_interface = @api_client.provision_types
@option_types_interface = @api_client.option_types
end
|
#get(args) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/morpheus/cli/commands/library_container_types_command.rb', line 102
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. = "Display node type details." + "\n" +
"[name] is required. This is the name or id of a node type."
end
optparse.parse!(args)
if args.count < 1
puts optparse
return 1
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
25
26
27
|
# File 'lib/morpheus/cli/commands/library_container_types_command.rb', line 25
def handle(args)
handle_subcommand(args)
end
|
#list(args) ⇒ Object
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
99
100
|
# File 'lib/morpheus/cli/commands/library_container_types_command.rb', line 29
def list(args)
options = {}
params = {}
layout_id = nil
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage()
opts.on('--layout ID', String, "Filter by Layout") do |val|
layout_id = val
end
opts.on('--technology VALUE', String, "Filter by technology") do |val|
params['provisionType'] = val
end
opts.on('--category VALUE', String, "Filter by category") do |val|
params['category'] = 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. = "List node types."
end
optparse.parse!(args)
if args.count > 0
options[:phrase] = args.join(" ")
end
connect(options)
begin
params.merge!(parse_list_options(options))
@library_container_types_interface.setopts(options)
if options[:dry_run]
print_dry_run @library_container_types_interface.dry.list(layout_id, params)
return
end
json_response = @library_container_types_interface.list(layout_id, params)
if options[:json]
puts as_json(json_response, options, "containerTypes")
return 0
elsif options[:csv]
puts records_as_csv(json_response['containerTypes'], options)
return 0
elsif options[:yaml]
puts as_yaml(json_response, options, "containerTypes")
return 0
end
container_types = json_response['containerTypes']
title = "Morpheus Library - Node Types"
subtitles = []
if layout_id
subtitles << "Layout: #{layout_id}".strip
end
subtitles += parse_list_subtitles(options)
print_h1 title, subtitles
if container_types.empty?
print cyan,"No node types found.",reset,"\n"
else
print_container_types_table(container_types, options)
(json_response, {:label => "node type", :n_label => "node types"})
end
print reset,"\n"
rescue RestClient::Exception => e
print_rest_exception(e, options)
return 1
end
end
|
#remove(args) ⇒ Object
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
|
# File 'lib/morpheus/cli/commands/library_container_types_command.rb', line 604
def remove(args)
layout_id = nil
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. = "Delete a node type."
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
container_type = find_container_type_by_name_or_id(layout_id, args[0])
if container_type.nil?
return 1
end
unless Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the node type #{container_type['name']}?", options)
exit
end
@library_container_types_interface.setopts(options)
if options[:dry_run]
print_dry_run @library_container_types_interface.dry.destroy(nil, container_type['id'])
return
end
json_response = @library_container_types_interface.destroy(nil, container_type['id'])
if options[:json]
print JSON.pretty_generate(json_response), "\n"
return
end
print_green_success "Removed Node Type #{container_type['name']}"
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#update(args) ⇒ Object
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
527
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
|
# File 'lib/morpheus/cli/commands/library_container_types_command.rb', line 455
def update(args)
options = {}
params = {}
layout = nil
layout_id = nil
script_ids = nil
file_template_ids = nil
evars = nil
optparse = Morpheus::Cli::OptionParser.new do|opts|
opts.banner = subcommand_usage("[name] [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('--shortName VALUE', String, "Short Name") do |val|
params['shortName'] = val
end
opts.on('--version VALUE', String, "Version") do |val|
params['containerVersion'] = val
end
opts.on('--evars-json JSON', String, 'Environment variables JSON: {"name":"Foo", "value":"Bar", "masked":true, "export":true}' ) do |val|
begin
evars = JSON.parse(val.to_s)
evars = evars.kind_of?(Array) ? evars : [evars]
rescue JSON::ParserError => e
print_red_alert "Unable to parse evars JSON"
exit 1
end
end
opts.on('-e', '--evars LIST', Array, "Environment variables list. Comma delimited list of name=value pairs") do |val|
evars = val.collect do |nv|
parts = nv.split('=')
{'name' => parts[0].strip, 'value' => (parts.count > 1 ? parts[1].strip : '')}
end
end
opts.on('--ports NAME=PORT,NAME=PORT', String, "List of exposed port definitions in the format NAME=PORT|PROTOCOL, Example: \"WEB=80|HTTP,SECURE=443|HTTPS\"") do |val|
params['containerPorts'] ||= []
parsed_ports = val.split(",").each do |value_pair|
k,v = value_pair.strip.split("=")
value_array = v.split("|")
port_name = k.to_s.strip
port_number = value_array[0].to_s.strip.to_i
exposed_port = {'name' => port_name,'port' => port_number }
if value_array[1]
exposed_port['loadBalanceProtocol'] = value_array[1].strip
end
if exposed_port['name'].to_s.empty? || !exposed_port['port'] || exposed_port['port'].to_i <= 0
raise_command_error "Invalid exposed port definition: '#{value_pair}'. Expected format is 'NAME=PORT'", args, optparse
end
params['containerPorts'] << exposed_port
end
end
opts.on('--scripts x,y,z', Array, "List of Script IDs") do |val|
script_ids = val end
opts.on('--file-templates x,y,z', Array, "List of File Template IDs") do |val|
file_template_ids = val end
build_common_options(opts, options, [:options, :json, :dry_run, :remote])
opts. = "Update a node type."
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
container_type = find_container_type_by_name_or_id(layout_id, args[0])
if container_type.nil?
return 1
end
passed_options = options[:options] ? options[:options].reject {|k,v| k.is_a?(Symbol) } : {}
payload = nil
if options[:payload]
payload = options[:payload]
payload.deep_merge!({'containerType' => passed_options}) unless passed_options.empty?
else
payload = {'containerType' => {} }
payload.deep_merge!({'containerType' => passed_options}) unless passed_options.empty?
if evars
payload['environmentVariables'] = evars
else
end
if script_ids
params['scripts'] = script_ids.collect {|it| it.to_i }
else
end
if file_template_ids
params['templates'] = file_template_ids.collect {|it| it.to_i }
else
end
if params.empty? && passed_options.empty? && evars.nil?
raise_command_error "Specify at least one option to update.\n#{optparse}"
end
payload['containerType'] ||= {}
payload['containerType'].deep_merge!(params)
end
@library_container_types_interface.setopts(options)
if options[:dry_run]
print_dry_run @library_container_types_interface.dry.update(layout_id, container_type['id'], payload)
return
end
json_response = @library_container_types_interface.update(layout_id, container_type['id'], payload)
if options[:json]
print JSON.pretty_generate(json_response), "\n"
return
end
container_type = json_response['containerType']
print_green_success "Updated Node Type #{container_type['name']}"
get([container_type['id']])
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|