morpheus-cli 3.3.1.4 → 3.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/lib/morpheus/api/api_client.rb +28 -0
  3. data/lib/morpheus/api/instance_types_interface.rb +12 -10
  4. data/lib/morpheus/api/instances_interface.rb +4 -0
  5. data/lib/morpheus/api/library_container_scripts_interface.rb +49 -0
  6. data/lib/morpheus/api/library_container_templates_interface.rb +49 -0
  7. data/lib/morpheus/api/library_container_types_interface.rb +65 -0
  8. data/lib/morpheus/api/library_container_upgrades_interface.rb +66 -0
  9. data/lib/morpheus/api/library_instance_types_interface.rb +59 -0
  10. data/lib/morpheus/api/library_layouts_interface.rb +65 -0
  11. data/lib/morpheus/api/servers_interface.rb +4 -0
  12. data/lib/morpheus/api/user_sources_interface.rb +120 -0
  13. data/lib/morpheus/api/virtual_images_interface.rb +7 -0
  14. data/lib/morpheus/cli.rb +12 -1
  15. data/lib/morpheus/cli/accounts.rb +35 -9
  16. data/lib/morpheus/cli/cli_command.rb +82 -2
  17. data/lib/morpheus/cli/curl_command.rb +1 -1
  18. data/lib/morpheus/cli/echo_command.rb +1 -1
  19. data/lib/morpheus/cli/hosts.rb +40 -14
  20. data/lib/morpheus/cli/instance_types.rb +106 -64
  21. data/lib/morpheus/cli/instances.rb +39 -15
  22. data/lib/morpheus/cli/library.rb +1 -1184
  23. data/lib/morpheus/cli/library_container_scripts_command.rb +437 -0
  24. data/lib/morpheus/cli/library_container_templates_command.rb +397 -0
  25. data/lib/morpheus/cli/library_container_types_command.rb +653 -0
  26. data/lib/morpheus/cli/library_instance_types_command.rb +491 -0
  27. data/lib/morpheus/cli/library_layouts_command.rb +650 -0
  28. data/lib/morpheus/cli/library_option_lists_command.rb +476 -0
  29. data/lib/morpheus/cli/library_option_types_command.rb +549 -0
  30. data/lib/morpheus/cli/library_upgrades_command.rb +604 -0
  31. data/lib/morpheus/cli/mixins/library_helper.rb +123 -0
  32. data/lib/morpheus/cli/mixins/print_helper.rb +21 -22
  33. data/lib/morpheus/cli/mixins/provisioning_helper.rb +56 -11
  34. data/lib/morpheus/cli/network_services_command.rb +1 -1
  35. data/lib/morpheus/cli/option_types.rb +12 -2
  36. data/lib/morpheus/cli/power_scheduling_command.rb +1 -1
  37. data/lib/morpheus/cli/shell.rb +120 -22
  38. data/lib/morpheus/cli/sleep_command.rb +45 -0
  39. data/lib/morpheus/cli/user_sources_command.rb +963 -0
  40. data/lib/morpheus/cli/users.rb +33 -2
  41. data/lib/morpheus/cli/version.rb +1 -1
  42. data/lib/morpheus/cli/version_command.rb +1 -1
  43. data/lib/morpheus/cli/virtual_images.rb +93 -39
  44. data/lib/morpheus/formatters.rb +37 -27
  45. data/lib/morpheus/terminal.rb +1 -1
  46. metadata +20 -2
@@ -0,0 +1,604 @@
1
+ require 'io/console'
2
+ require 'optparse'
3
+ require 'filesize'
4
+ require 'morpheus/cli/cli_command'
5
+ require 'morpheus/cli/mixins/library_helper'
6
+
7
+ class Morpheus::Cli::LibraryUpgradesCommand
8
+ include Morpheus::Cli::CliCommand
9
+ include Morpheus::Cli::LibraryHelper
10
+
11
+ set_command_name :'library-upgrades'
12
+
13
+ register_subcommands :list, :get, :add, :update, :remove
14
+
15
+ def initialize()
16
+ # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
17
+ end
18
+
19
+ def connect(opts)
20
+ @api_client = establish_remote_appliance_connection(opts)
21
+ @library_container_upgrades_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).library_container_upgrades
22
+ @library_layouts_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).library_layouts
23
+ @library_instance_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).library_instance_types
24
+ @provision_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).provision_types
25
+ @option_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).option_types
26
+ @option_type_lists_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).option_type_lists
27
+ end
28
+
29
+ def handle(args)
30
+ handle_subcommand(args)
31
+ end
32
+
33
+ def list(args)
34
+ options = {}
35
+ params = {}
36
+ instance_type = nil
37
+ instance_type_id = nil
38
+ optparse = Morpheus::Cli::OptionParser.new do |opts|
39
+ opts.banner = subcommand_usage("[instance-type]")
40
+ # opts.on('--instance-type ID', String, "Filter by Instance Type") do |val|
41
+ # instance_type_id = val
42
+ # end
43
+ opts.on('--code VALUE', String, "Filter by code") do |val|
44
+ params['code'] = val
45
+ end
46
+ # opts.on('--technology VALUE', String, "Filter by technology") do |val|
47
+ # params['provisionType'] = val
48
+ # end
49
+ build_common_options(opts, options, [:list, :query, :json, :yaml, :csv, :fields, :dry_run, :remote])
50
+ opts.footer = "List upgrades." + "\n" +
51
+ "[instance-type] is required."
52
+ end
53
+ optparse.parse!(args)
54
+ connect(options)
55
+ # instance is required right now.
56
+ instance_type_id = args[0] if !instance_type_id
57
+ if args.count != 1
58
+ print_error Morpheus::Terminal.angry_prompt
59
+ puts_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args.inspect}\n#{optparse}"
60
+ return 1
61
+ end
62
+ begin
63
+ # construct payload
64
+ if instance_type_id
65
+ instance_type = find_instance_type_by_name_or_id(instance_type_id)
66
+ return 1 if instance_type.nil?
67
+ instance_type_id = instance_type['id']
68
+ end
69
+
70
+ params.merge!(parse_list_options(options))
71
+
72
+ if options[:dry_run]
73
+ print_dry_run @library_container_upgrades_interface.dry.list(instance_type_id, params)
74
+ return
75
+ end
76
+
77
+ json_response = @library_container_upgrades_interface.list(instance_type_id, params)
78
+ if options[:include_fields]
79
+ json_response = {"upgrades" => filter_data(json_response["upgrades"], options[:include_fields]) }
80
+ end
81
+ if options[:json]
82
+ puts as_json(json_response, options)
83
+ return 0
84
+ elsif options[:csv]
85
+ puts records_as_csv(json_response['upgrades'], options)
86
+ return 0
87
+ elsif options[:yaml]
88
+ puts as_yaml(json_response, options)
89
+ return 0
90
+ end
91
+ upgrades = json_response['upgrades']
92
+ title = "Morpheus Library - Upgrades"
93
+ subtitles = []
94
+ if instance_type
95
+ subtitles << "Instance Type: #{instance_type['name']}".strip
96
+ end
97
+ subtitles += parse_list_subtitles(options)
98
+ print_h1 title, subtitles
99
+ if upgrades.empty?
100
+ print cyan,"No upgrades found for instance type.",reset,"\n"
101
+ else
102
+ print_upgrades_table(upgrades, options)
103
+ print_results_pagination(json_response, {:label => "upgrade", :n_label => "upgrades"})
104
+ # print_results_pagination(json_response)
105
+ end
106
+ print reset,"\n"
107
+ rescue RestClient::Exception => e
108
+ print_rest_exception(e, options)
109
+ return 1
110
+ end
111
+ end
112
+
113
+
114
+
115
+ def get(args)
116
+ options = {}
117
+ params = {}
118
+ optparse = Morpheus::Cli::OptionParser.new do |opts|
119
+ opts.banner = subcommand_usage("[name]")
120
+ build_common_options(opts, options, [:json, :yaml, :csv, :fields, :dry_run, :remote])
121
+ opts.footer = "Get details about an upgrade." + "\n" +
122
+ "[instance-type] is required." + "\n" +
123
+ "[name] is required. This is the name or id of an upgrade."
124
+ end
125
+ optparse.parse!(args)
126
+ connect(options)
127
+ if args.count < 2
128
+ print_error Morpheus::Terminal.angry_prompt
129
+ puts_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args.inspect}\n#{optparse}"
130
+ return 1
131
+ end
132
+ instance_type_id = args[0]
133
+ upgrade_id = args[1]
134
+ instance_type = find_instance_type_by_name_or_id(instance_type_id)
135
+ exit 1 if instance_type.nil?
136
+ instance_type_id = instance_type['id']
137
+ begin
138
+ if options[:dry_run]
139
+ if id.to_s =~ /\A\d{1,}\Z/
140
+ print_dry_run @library_container_upgrades_interface.dry.get(instance_type_id, upgrade_id.to_i)
141
+ else
142
+ print_dry_run @library_container_upgrades_interface.dry.list(instance_type_id, {name:upgrade_id})
143
+ end
144
+ return
145
+ end
146
+ upgrade = find_upgrade_by_name_or_id(instance_type_id, upgrade_id)
147
+ if upgrade.nil?
148
+ return 1
149
+ end
150
+ # skip redundant request
151
+ #json_response = @library_container_upgrades_interface.get(instance_type_id, upgrade['id'])
152
+ json_response = {'upgrade' => upgrade}
153
+ #upgrade = json_response['upgrade']
154
+ if options[:include_fields]
155
+ json_response = {"upgrade" => filter_data(json_response["upgrade"], options[:include_fields]) }
156
+ end
157
+ if options[:json]
158
+ puts as_json(json_response, options)
159
+ return 0
160
+ elsif options[:yaml]
161
+ puts as_yaml(json_response, options)
162
+ return 0
163
+ elsif options[:csv]
164
+ puts records_as_csv([json_response['upgrade']], options)
165
+ return 0
166
+ end
167
+
168
+ print_h1 "Morpheus Upgrade Details"
169
+ print cyan
170
+ description_cols = {
171
+ "ID" => lambda {|it| it['id'] },
172
+ "Name" => lambda {|it| it['name'] },
173
+ "Code" => lambda {|it| it['code'] },
174
+ "Description" => lambda {|it| it['description'] },
175
+ "From Version" => lambda {|it| it['srcVersion'] },
176
+ "To Version" => lambda {|it| it['tgtVersion'] },
177
+ }
178
+ print_description_list(description_cols, upgrade)
179
+
180
+
181
+ from_layout = upgrade['instanceTypeLayout']
182
+ to_layout = upgrade['targetInstanceTypeLayout']
183
+ layout_columns = [
184
+ {"NAME" => lambda {|it| it['name'] } },
185
+ {"VERSION" => lambda {|it| it['instanceVersion'] } },
186
+ ]
187
+ if from_layout
188
+ print_h2 "Source Layout"
189
+ print as_pretty_table(from_layout, layout_columns)
190
+ end
191
+
192
+ if to_layout
193
+ print_h2 "Target Layout"
194
+ print as_pretty_table(to_layout, layout_columns)
195
+ end
196
+
197
+ if upgrade['upgradeCommand']
198
+ print_h2 "Upgrade Command"
199
+ puts upgrade['upgradeCommand']
200
+ end
201
+
202
+ print reset,"\n"
203
+
204
+ rescue RestClient::Exception => e
205
+ print_rest_exception(e, options)
206
+ return 1
207
+ end
208
+ end
209
+
210
+ def add(args)
211
+ options = {}
212
+ params = {}
213
+ instance_type_id = nil
214
+ option_type_ids = nil
215
+ node_type_ids = nil
216
+ evars = nil
217
+ optparse = Morpheus::Cli::OptionParser.new do|opts|
218
+ opts.banner = subcommand_usage("[instance-type] [name]")
219
+ opts.on('--instance-type ID', String, "Instance Type this upgrade belongs to") do |val|
220
+ instance_type_id = val
221
+ end
222
+ opts.on('--name VALUE', String, "Name for this upgrade") do |val|
223
+ params['name'] = val
224
+ end
225
+ opts.on('--code CODE', String, "Code") do |val|
226
+ params['code'] = val
227
+ end
228
+ opts.on('--description VALUE', String, "Description") do |val|
229
+ params['description'] = val
230
+ end
231
+ opts.on('--source-layout ID', String, "Source Layout ID to upgrade from") do |val|
232
+ params['instanceTypeLayout'] = {'id' => val}
233
+ end
234
+ opts.on('--target-layout ID', String, "Target Layout ID to upgrade to") do |val|
235
+ params['targetInstanceTypeLayout'] = {'id' => val}
236
+ end
237
+ opts.on('--upgradeCommand TEXT', String, "Upgrade Command") do |val|
238
+ params['upgradeCommand'] = val
239
+ end
240
+
241
+ #build_option_type_options(opts, options, add_upgrade_option_types())
242
+ build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote])
243
+ opts.footer = "Create a new upgrade." + "\n" +
244
+ "[instance-type] is required."
245
+ end
246
+ optparse.parse!(args)
247
+ connect(options)
248
+ if args.count > 2
249
+ print_error Morpheus::Terminal.angry_prompt
250
+ puts_error "wrong number of arguments, expected 0-2 and got (#{args.count}) #{args.inspect}\n#{optparse}"
251
+ return 1
252
+ end
253
+ if args[0]
254
+ instance_type_id = args[0]
255
+ end
256
+ if args[1]
257
+ params['name'] = args[1]
258
+ end
259
+ begin
260
+ # find the instance type first, or just prompt for that too please.
261
+ if !instance_type_id
262
+ print_error Morpheus::Terminal.angry_prompt
263
+ puts_error "missing required argument [instance-type]\n#{optparse}"
264
+ return 1
265
+ end
266
+ instance_type = find_instance_type_by_name_or_id(instance_type_id)
267
+ return 1 if instance_type.nil?
268
+ instance_type_id = instance_type['id']
269
+
270
+ # construct payload
271
+ payload = nil
272
+ if options[:payload]
273
+ payload = options[:payload]
274
+ else
275
+ payload = {'upgrade' => {}}
276
+
277
+ # support old -O options
278
+ params.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]
279
+
280
+ # Name
281
+ if params['name']
282
+ payload['upgrade']['name'] = params['name']
283
+ else
284
+ v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => 'Name', 'required' => true}], options[:options])
285
+ payload['upgrade']['name'] = v_prompt['name'] if v_prompt['name']
286
+ end
287
+
288
+ # Source Layout
289
+ if !params['instanceTypeLayout']
290
+ v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'instanceTypeLayout', 'type' => 'text', 'fieldLabel' => 'Source Layout', 'required' => true}], options[:options])
291
+ source_layout_id = v_prompt['instanceTypeLayout']
292
+ params['name'] = v_prompt['instanceTypeLayout']
293
+ end
294
+
295
+
296
+ # provision_types = @provision_types_interface.get({customSupported: true})['provisionTypes']
297
+ # if provision_types.empty?
298
+ # print_red_alert "No available provision types found!"
299
+ # exit 1
300
+ # end
301
+ # provision_type_options = provision_types.collect {|it| { 'name' => it['name'], 'value' => it['code']} }
302
+
303
+ # if !params['provisionTypeCode']
304
+ # 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])
305
+ # params['provisionTypeCode'] = v_prompt['provisionTypeCode']
306
+ # else
307
+
308
+ # end
309
+ # provision_type = provision_types.find {|it| it['code'] == params['provisionTypeCode'] }
310
+
311
+ # get available layouts
312
+ #available_layouts = instance_type['instanceTypeLayouts']
313
+ available_source_layouts = @library_layout_interface.list(instance_type_id)['instanceTypeLayouts']
314
+
315
+ # Source Layout
316
+ if params['instanceTypeLayout']
317
+ payload['upgrade']['instanceTypeLayout'] = params['instanceTypeLayout']
318
+ else
319
+ if available_source_layouts.empty?
320
+ print_red_alert "No available source layouts found!"
321
+ return 1
322
+ end
323
+ source_layout_dropdown_options = available_source_layouts.collect {|it| { 'name' => it['name'], 'value' => it['code']} }
324
+ v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'instanceTypeLayout', 'type' => 'select', 'selectOptions' => source_layout_dropdown_options, 'fieldLabel' => 'Source Layout', 'required' => true, 'description' => 'The source layout to upgrade from.'}], options[:options])
325
+ payload['upgrade']['instanceTypeLayout'] = {'id' => v_prompt['instanceTypeLayout'] }
326
+ end
327
+
328
+ # Target Layout
329
+ if params['targetInstanceTypeLayout']
330
+ payload['upgrade']['targetInstanceTypeLayout'] = params['targetInstanceTypeLayout']
331
+ else
332
+ available_target_layouts = available_source_layouts.select {|it| it['id'] != params['instanceTypeLayout']['id'] }
333
+ # available_target_layouts = @library_layout_interface.list(instance_type_id)['instanceTypeLayouts']
334
+ if available_target_layouts.empty?
335
+ print_red_alert "No available target layouts found!"
336
+ return 1
337
+ end
338
+ target_layout_dropdown_options = available_target_layouts.collect {|it| { 'name' => it['name'], 'value' => it['code']} }
339
+ v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'targetInstanceTypeLayout', 'type' => 'select', 'selectOptions' => target_layout_dropdown_options, 'fieldLabel' => 'Target Layout', 'required' => true, 'description' => 'The target layout to upgrade to.'}], options[:options])
340
+ payload['upgrade']['targetInstanceTypeLayout'] = {'id' => v_prompt['targetInstanceTypeLayout'] }
341
+ end
342
+
343
+ # Upgrade Command
344
+ if params['upgradeCommand']
345
+ payload['upgrade']['upgradeCommand'] = params['upgradeCommand']
346
+ else
347
+ if ::Morpheus::Cli::OptionTypes::confirm("Enter Upgrade Command?", options.merge({default: false}))
348
+ v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'description', 'type' => 'code', 'fieldLabel' => 'Upgrade Command', 'required' => false}], options[:options])
349
+ payload['upgrade']['upgradeCommand'] = v_prompt['upgradeCommand'] if v_prompt['upgradeCommand']
350
+ end
351
+ end
352
+
353
+ # any other options or custom option types?
354
+
355
+ end
356
+ # dry run?
357
+ if options[:dry_run]
358
+ print_dry_run @library_container_upgrades_interface.dry.create(instance_type_id, payload)
359
+ return
360
+ end
361
+ # do it
362
+ json_response = @library_container_upgrades_interface.create(instance_type_id, payload)
363
+ # print and return result
364
+ if options[:json]
365
+ puts as_json(json_response, options)
366
+ return 0
367
+ end
368
+ upgrade = json_response['upgrade']
369
+ print_green_success "Added upgrade #{upgrade['name']}"
370
+ get(instance_type_id, [upgrade['id']])
371
+ return 0
372
+ rescue RestClient::Exception => e
373
+ print_rest_exception(e, options)
374
+ exit 1
375
+ end
376
+ end
377
+
378
+ def update(args)
379
+ options = {}
380
+ params = {}
381
+ instance_type_id = nil
382
+ option_type_ids = nil
383
+ node_type_ids = nil
384
+ evars = nil
385
+ optparse = Morpheus::Cli::OptionParser.new do|opts|
386
+ opts.banner = subcommand_usage("[name] [options]")
387
+ opts.on('--name VALUE', String, "Name for this upgrade") do |val|
388
+ params['name'] = val
389
+ end
390
+ opts.on('--version VALUE', String, "Version") do |val|
391
+ params['instanceVersion'] = val
392
+ end
393
+ opts.on('--description VALUE', String, "Description") do |val|
394
+ params['description'] = val
395
+ end
396
+ # opts.on('--technology CODE', String, "Technology") do |val|
397
+ # params['provisionTypeCode'] = val
398
+ # end
399
+ opts.on('--min-memory VALUE', String, "Minimum Memory (MB)") do |val|
400
+ params['memoryRequirement'] = val
401
+ end
402
+ opts.on('--workflow ID', String, "Workflow") do |val|
403
+ params['taskSetId'] = val.to_i
404
+ end
405
+ opts.on('--option-types x,y,z', Array, "List of Option Type IDs") do |val|
406
+ option_type_ids = val #.collect {|it| it.to_i }
407
+ end
408
+ opts.on('--node-types x,y,z', Array, "List of Node Type IDs") do |val|
409
+ node_type_ids = val #.collect {|it| it.to_i }
410
+ end
411
+ #build_option_type_options(opts, options, update_upgrade_option_types())
412
+ build_common_options(opts, options, [:options, :json, :dry_run, :remote])
413
+ opts.footer = "Update a upgrade."
414
+ end
415
+ optparse.parse!(args)
416
+ if args.count < 1
417
+ puts optparse
418
+ exit 1
419
+ end
420
+ connect(options)
421
+ begin
422
+ upgrade = find_upgrade_by_name_or_id(nil, args[0])
423
+ exit 1 if upgrade.nil?
424
+ payload = nil
425
+ if options[:payload]
426
+ payload = options[:payload]
427
+ else
428
+ # option_types = update_upgrade_option_types(instance_type)
429
+ # params = Morpheus::Cli::OptionTypes.prompt(option_types, options[:options], @api_client, options[:params])
430
+ params.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]
431
+
432
+ # ENVIRONMENT VARIABLES
433
+ if evars
434
+
435
+ else
436
+ # prompt
437
+ end
438
+
439
+ # OPTION TYPES
440
+ if option_type_ids
441
+ params['optionTypes'] = option_type_ids.collect {|it| it.to_i }.select { |it| it != 0 }
442
+ else
443
+ # prompt
444
+ end
445
+
446
+ # NODE TYPES
447
+ if node_type_ids
448
+ params['containerTypes'] = node_type_ids.collect {|it| it.to_i }.select { |it| it != 0 }
449
+ else
450
+ # prompt
451
+ end
452
+
453
+ if params.empty?
454
+ puts optparse
455
+ exit 1
456
+ end
457
+
458
+ payload = {'upgrade' => params}
459
+
460
+ end
461
+
462
+ if options[:dry_run]
463
+ print_dry_run @library_container_upgrades_interface.dry.update(nil, upgrade['id'], payload)
464
+ return
465
+ end
466
+
467
+ json_response = @library_container_upgrades_interface.update(nil, upgrade['id'], payload)
468
+
469
+ if options[:json]
470
+ print JSON.pretty_generate(json_response), "\n"
471
+ return
472
+ end
473
+
474
+ print_green_success "Updated upgrade #{params['name'] || upgrade['name']}"
475
+ #list([])
476
+ rescue RestClient::Exception => e
477
+ print_rest_exception(e, options)
478
+ exit 1
479
+ end
480
+ end
481
+
482
+ def remove(args)
483
+ options = {}
484
+ optparse = Morpheus::Cli::OptionParser.new do|opts|
485
+ opts.banner = subcommand_usage("[name]")
486
+ build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
487
+ opts.footer = "Delete a upgrade."
488
+ end
489
+ optparse.parse!(args)
490
+ if args.count < 1
491
+ puts optparse
492
+ exit 1
493
+ end
494
+ connect(options)
495
+
496
+ begin
497
+ upgrade = find_upgrade_by_name_or_id(nil, args[0])
498
+ exit 1 if upgrade.nil?
499
+
500
+ unless Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the upgrade #{upgrade['name']}?", options)
501
+ exit
502
+ end
503
+ if options[:dry_run]
504
+ print_dry_run @library_container_upgrades_interface.dry.destroy(nil, upgrade['id'])
505
+ return
506
+ end
507
+ json_response = @library_container_upgrades_interface.destroy(nil, upgrade['id'])
508
+
509
+ if options[:json]
510
+ print JSON.pretty_generate(json_response), "\n"
511
+ return
512
+ end
513
+
514
+ print_green_success "Removed upgrade #{upgrade['name']}"
515
+ #list([])
516
+ rescue RestClient::Exception => e
517
+ print_rest_exception(e, options)
518
+ exit 1
519
+ end
520
+ end
521
+
522
+ private
523
+
524
+ def find_upgrade_by_name_or_id(instance_type_id, val)
525
+ if val.to_s =~ /\A\d{1,}\Z/
526
+ return find_upgrade_by_id(instance_type_id, val)
527
+ else
528
+ return find_upgrade_by_name(instance_type_id, val)
529
+ end
530
+ end
531
+
532
+ def find_upgrade_by_id(instance_type_id, id)
533
+ begin
534
+ json_response = @library_container_upgrades_interface.get(instance_type_id, id.to_i)
535
+ return json_response['upgrade']
536
+ rescue RestClient::Exception => e
537
+ if e.response && e.response.code == 404
538
+ print_red_alert "Upgrade not found by id #{id}"
539
+ else
540
+ raise e
541
+ end
542
+ end
543
+ end
544
+
545
+ def find_upgrade_by_name(instance_type_id, name)
546
+ upgrades = @library_container_upgrades_interface.list(instance_type_id, {name: name.to_s})['upgrades']
547
+ if upgrades.empty?
548
+ print_red_alert "Upgrade not found by name #{name}"
549
+ return nil
550
+ elsif upgrades.size > 1
551
+ print_red_alert "#{upgrades.size} upgrades found by name #{name}"
552
+ print_upgrades_table(upgrades, {color: red})
553
+ print_red_alert "Try using ID instead"
554
+ print reset,"\n"
555
+ return nil
556
+ else
557
+ return upgrades[0]
558
+ end
559
+ end
560
+
561
+ def print_upgrades_table(upgrades, opts={})
562
+ columns = [
563
+ {"ID" => lambda {|upgrade| upgrade['id'] } },
564
+ # {"INSTANCE TYPE" => lambda {|upgrade| upgrade['instanceType'] ? upgrade['instanceType']['name'] : '' } },
565
+ {"NAME" => lambda {|upgrade| upgrade['name'] } },
566
+ {"FROM VERSION" => lambda {|upgrade| upgrade['srcVersion'] } },
567
+ {"TO VERSION" => lambda {|upgrade| upgrade['tgtVersion'] } },
568
+ # {"FROM" => lambda {|upgrade| upgrade['instanceTypeLayout'] ? ['instanceTypeLayout']['instanceVersion'] : '' } },
569
+ # {"TO" => lambda {|upgrade| upgrade['targetInstanceTypeLayout'] ? ['targetInstanceTypeLayout']['instanceVersion'] : '' } },
570
+ ]
571
+ if opts[:include_fields]
572
+ columns = opts[:include_fields]
573
+ end
574
+ print as_pretty_table(upgrades, columns, opts)
575
+ end
576
+
577
+ def add_upgrade_option_types
578
+ [
579
+ # {'fieldName' => 'instanceTypeId', 'fieldLabel' => 'Instance Type ID', 'type' => 'text', 'required' => true, 'displayOrder' => 2, 'description' => 'The instance type this upgrade belongs to'},
580
+ {'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'displayOrder' => 1},
581
+ {'fieldName' => 'code', 'fieldLabel' => 'Code', 'type' => 'text', 'required' => true, 'displayOrder' => 2, 'description' => 'Useful shortcode for provisioning naming schemes and export reference.'},
582
+ {'fieldName' => 'description', 'fieldLabel' => 'Description', 'type' => 'text', 'displayOrder' => 3},
583
+ {'fieldName' => 'category', 'fieldLabel' => 'Category', 'type' => 'select', 'optionSource' => 'categories', 'required' => true, 'displayOrder' => 4},
584
+ {'fieldName' => 'logo', 'fieldLabel' => 'Icon File', 'type' => 'text', 'displayOrder' => 5},
585
+ {'fieldName' => 'visibility', 'fieldLabel' => 'Visibility', 'type' => 'select', 'selectOptions' => [{'name' => 'Private', 'value' => 'private'}, {'name' => 'Public', 'value' => 'public'}], 'defaultValue' => 'private', 'displayOrder' => 6},
586
+ {'fieldName' => 'environmentPrefix', 'fieldLabel' => 'Environment Prefix', 'type' => 'text', 'displayOrder' => 7, 'description' => 'Used for exportable environment variables when tying instance types together in app contexts. If not specified a name will be generated.'},
587
+ {'fieldName' => 'hasSettings', 'fieldLabel' => 'Enable Settings', 'type' => 'checkbox', 'displayOrder' => 8},
588
+ {'fieldName' => 'hasAutoScale', 'fieldLabel' => 'Enable Scaling (Horizontal)', 'type' => 'checkbox', 'displayOrder' => 9},
589
+ {'fieldName' => 'hasDeployment', 'fieldLabel' => 'Supports Deployments', 'type' => 'checkbox', 'displayOrder' => 10, 'description' => 'Requires a data volume be configured on each version. Files will be copied into this location.'}
590
+ ]
591
+ end
592
+
593
+ def update_upgrade_option_types(instance_type=nil)
594
+ if instance_type
595
+ opts = add_upgrade_option_types
596
+ opts.find {|opt| opt['fieldName'] == 'name'}['defaultValue'] = instance_type['name']
597
+ opts
598
+ else
599
+ add_upgrade_option_types
600
+ end
601
+ end
602
+
603
+
604
+ end