morpheus-cli 2.9.4 → 2.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,8 +10,6 @@ class Morpheus::Cli::InstanceTypes
10
10
  @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
11
11
  @access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials()
12
12
  @instance_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).instance_types
13
- @groups_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).groups
14
- @active_groups = ::Morpheus::Cli::Groups.load_group_file
15
13
  end
16
14
 
17
15
 
@@ -41,7 +41,7 @@ class Morpheus::Cli::Instances
41
41
 
42
42
 
43
43
  def handle(args)
44
- usage = "Usage: morpheus instances [list,add,remove,stop,start,restart,backup,run-workflow,stop-service,start-service,restart-service,resize,upgrade,clone,envs,setenv,delenv] [name]"
44
+ usage = "Usage: morpheus instances [list,details,add,update,remove,stop,start,restart,backup,run-workflow,stop-service,start-service,restart-service,resize,upgrade,clone,envs,setenv,delenv] [name]"
45
45
  if args.empty?
46
46
  puts "\n#{usage}\n\n"
47
47
  return
@@ -50,8 +50,12 @@ class Morpheus::Cli::Instances
50
50
  case args[0]
51
51
  when 'list'
52
52
  list(args[1..-1])
53
+ when 'details'
54
+ details(args[1..-1])
53
55
  when 'add'
54
56
  add(args[1..-1])
57
+ when 'update'
58
+ update(args[1..-1])
55
59
  when 'remove'
56
60
  remove(args[1..-1])
57
61
  when 'stop'
@@ -74,8 +78,6 @@ class Morpheus::Cli::Instances
74
78
  stats(args[1..-1])
75
79
  when 'logs'
76
80
  logs(args[1..-1])
77
- when 'details'
78
- details(args[1..-1])
79
81
  when 'envs'
80
82
  envs(args[1..-1])
81
83
  when 'setenv'
@@ -101,146 +103,51 @@ class Morpheus::Cli::Instances
101
103
  def add(args)
102
104
  options = {}
103
105
  optparse = OptionParser.new do|opts|
104
- opts.banner = "Usage: morpheus instances add TYPE NAME"
105
- opts.on( '-g', '--group GROUP', "Group" ) do |val|
106
- options[:group] = val
106
+ opts.banner = "Usage: morpheus instances add [type] [name]"
107
+ opts.on( '-g', '--group GROUP', "Group Name" ) do |val|
108
+ options[:group_name] = val
107
109
  end
108
- opts.on( '-c', '--cloud CLOUD', "Cloud" ) do |val|
109
- options[:cloud] = val
110
+ opts.on( '-G', '--group-id ID', "Group Id" ) do |val|
111
+ options[:group_id] = val
112
+ end
113
+ opts.on( '-c', '--cloud CLOUD', "Cloud Name" ) do |val|
114
+ options[:cloud_name] = val
115
+ end
116
+ # this conflicts with --nocolor option
117
+ # opts.on( '-C', '--cloud CLOUD', "Cloud Id" ) do |val|
118
+ # options[:cloud] = val
119
+ # end
120
+ opts.on( '-t', '--type CODE', "Instance Type" ) do |val|
121
+ options[:instance_type_code] = val
110
122
  end
111
123
  build_common_options(opts, options, [:options, :json, :dry_run, :remote])
112
124
 
113
125
  end
114
126
 
115
- if args.count < 2
116
- puts "\n#{optparse}\n\n"
117
- return
118
- end
119
127
  optparse.parse(args)
120
128
  connect(options)
121
- instance_name = args[1]
122
- instance_type_code = args[0]
123
- instance_type = find_instance_type_by_code(instance_type_code)
124
- if instance_type.nil?
125
- exit 1
126
- end
127
- group_id = nil
128
- if !options[:group].nil?
129
- group_id = find_group_by_name(options[:group])
130
- else
131
- group_id = @active_groups[@appliance_name.to_sym]
132
- end
133
-
134
- if group_id.nil?
135
- puts "Group not found or specified! \n #{optparse}"
136
- exit 1
137
- end
138
-
139
- if options[:cloud].nil?
140
- puts "Cloud not specified! \n #{optparse}"
141
- exit 1
142
- end
143
- cloud_id = find_cloud_by_name(group_id, options[:cloud])
144
- if cloud_id.nil?
145
- puts "Cloud not found! \n #{optparse}"
146
- exit 1
147
- end
148
129
 
149
- payload = {
150
- :servicePlan => nil,
151
- :zoneId => cloud_id,
152
- :instance => {
153
- :name => instance_name,
154
- :site => {
155
- :id => group_id
156
- },
157
- :instanceType => {
158
- :code => instance_type_code
159
- }
160
- }
161
- }
162
-
163
- version_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'version', 'type' => 'select', 'fieldLabel' => 'Version', 'optionSource' => 'instanceVersions', 'required' => true, 'skipSingleOption' => true, 'description' => 'Select which version of the instance type to be provisioned.'}],options[:options],@api_client,{groupId: group_id, cloudId: cloud_id, instanceTypeId: instance_type['id']})
164
- layout_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'layout', 'type' => 'select', 'fieldLabel' => 'Layout', 'optionSource' => 'layoutsForCloud', 'required' => true, 'description' => 'Select which configuration of the instance type to be provisioned.'}],options[:options],@api_client,{groupId: group_id, cloudId: cloud_id, instanceTypeId: instance_type['id'], version: version_prompt['version']})
165
- layout_id = layout_prompt['layout']
166
- layout = instance_type['instanceTypeLayouts'].find{ |lt| lt['id'] == layout_id.to_i}
167
- payload[:instance][:layout] = {id: layout['id']}
168
-
169
- # prompt for service plan
170
- service_plans_json = @instances_interface.service_plans({zoneId: cloud_id, layoutId: layout_id})
171
- service_plans = service_plans_json["plans"]
172
- service_plans_dropdown = service_plans.collect {|sp| {'name' => sp["name"], 'value' => sp["id"]} } # already sorted
173
- plan_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'servicePlan', 'type' => 'select', 'fieldLabel' => 'Plan', 'selectOptions' => service_plans_dropdown, 'required' => true, 'description' => 'Choose the appropriately sized plan for this instance'}],options[:options])
174
- service_plan = service_plans.find {|sp| sp["id"] == plan_prompt['servicePlan'].to_i }
175
- payload[:servicePlan] = service_plan["id"]
176
-
177
- # prompt for volumes
178
- volumes = prompt_volumes(service_plan, options, @api_client, {})
179
- if !volumes.empty?
180
- payload[:volumes] = volumes
181
- end
182
-
183
- if layout["provisionType"] && layout["provisionType"]["id"] && layout["provisionType"]["hasNetworks"]
184
- # prompt for network interfaces (if supported)
185
- begin
186
- network_interfaces = prompt_network_interfaces(cloud_id, layout["provisionType"]["id"], options, @api_client)
187
- if !network_interfaces.empty?
188
- payload[:networkInterfaces] = network_interfaces
189
- end
190
- rescue RestClient::Exception => e
191
- print_yellow_warning "Unable to load network options. Proceeding..."
192
- print_rest_exception(e, options) if Morpheus::Logging.print_stacktrace?
130
+ # support old format of `instance add TYPE NAME`
131
+ if args[0] && args[0] !~ /\A\-/
132
+ options[:instance_type_code] = args[0]
133
+ if args[1] && args[1] !~ /\A\-/
134
+ options[:instance_name] = args[1]
193
135
  end
194
136
  end
195
137
 
196
-
197
- type_payload = {}
198
- if !layout['optionTypes'].nil? && !layout['optionTypes'].empty?
199
- type_payload = Morpheus::Cli::OptionTypes.prompt(layout['optionTypes'],options[:options],@api_client,{groupId: group_id, cloudId: cloud_id, zoneId: cloud_id, instanceTypeId: instance_type['id'], version: version_prompt['version']})
200
- elsif !instance_type['optionTypes'].nil? && !instance_type['optionTypes'].empty?
201
- type_payload = Morpheus::Cli::OptionTypes.prompt(instance_type['optionTypes'],options[:options],@api_client,{groupId: group_id, cloudId: cloud_id, zoneId: cloud_id, instanceTypeId: instance_type['id'], version: version_prompt['version']})
202
- end
203
- if !type_payload['config'].nil?
204
- payload.merge!(type_payload['config'])
205
- end
206
-
207
- provision_payload = {}
208
- if !layout['provisionType'].nil? && !layout['provisionType']['optionTypes'].nil? && !layout['provisionType']['optionTypes'].empty?
209
- instance_type_option_types = layout['provisionType']['optionTypes']
210
- # remove volume options if volumes were configured
211
- if !payload[:volumes].empty?
212
- instance_type_option_types = reject_volume_option_types(instance_type_option_types)
213
- end
214
- # remove networkId option if networks were configured above
215
- if !payload[:networkInterfaces].empty?
216
- instance_type_option_types = reject_networking_option_types(instance_type_option_types)
217
- end
218
- provision_payload = Morpheus::Cli::OptionTypes.prompt(instance_type_option_types,options[:options],@api_client,{groupId: group_id, cloudId: cloud_id, zoneId: cloud_id, instanceTypeId: instance_type['id'], version: version_prompt['version']})
138
+ # use active group by default
139
+ if !options[:group_name] && !options[:group_id]
140
+ options[:group_id] = @active_groups[@appliance_name.to_sym]
219
141
  end
220
142
 
221
- if !provision_payload.nil? && !provision_payload['config'].nil?
222
- payload.merge!(provision_payload['config'])
223
- end
224
- if !provision_payload.nil? && !provision_payload['server'].nil?
225
- payload[:server] = provision_payload['server']
226
- end
143
+ options[:name_required] = true
144
+
145
+ begin
227
146
 
228
- # avoid 500 error
229
- payload[:servicePlanOptions] = {}
147
+ payload = prompt_new_instance(options)
230
148
 
231
- begin
232
149
  if options[:dry_run]
233
- print "\n" ,cyan, bold, "DRY RUN\n","==================", "\n\n", reset
234
- print cyan
235
- print "Request: ", "\n"
236
- print reset
237
- print "POST #{@appliance_url}/api/instances", "\n\n"
238
- print cyan
239
- print "JSON: ", "\n"
240
- print reset
241
- print JSON.pretty_generate(payload)
242
- print "\n"
243
- print reset
150
+ print_dry_run("POST #{@appliance_url}/api/instances", payload)
244
151
  return
245
152
  end
246
153
  json_response = @instances_interface.create(payload)
@@ -248,6 +155,7 @@ class Morpheus::Cli::Instances
248
155
  print JSON.pretty_generate(json_response)
249
156
  print "\n"
250
157
  else
158
+ instance_name = json_response["instance"]["name"]
251
159
  print_green_success "Provisioning instance #{instance_name}"
252
160
  list([])
253
161
  end
@@ -257,6 +165,74 @@ class Morpheus::Cli::Instances
257
165
  end
258
166
  end
259
167
 
168
+ def update(args)
169
+ usage = "Usage: morpheus instances update [name] [options]"
170
+ options = {}
171
+ optparse = OptionParser.new do|opts|
172
+ opts.banner = usage
173
+ build_common_options(opts, options, [:options, :json, :dry_run])
174
+ end
175
+ optparse.parse(args)
176
+ if args.count < 1
177
+ puts "\n#{optparse.banner}\n\n"
178
+ exit 1
179
+ end
180
+ connect(options)
181
+
182
+ begin
183
+
184
+ instance = find_instance_by_name_or_id(args[0])
185
+
186
+ # group = find_group_from_options(options)
187
+
188
+ payload = {
189
+ 'instance' => {id: instance["id"]}
190
+ }
191
+
192
+ update_instance_option_types = [
193
+ {'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'description' => 'Enter a name for this instance'},
194
+ {'fieldName' => 'description', 'fieldLabel' => 'Description', 'type' => 'text', 'required' => false},
195
+ {'fieldName' => 'instanceContext', 'fieldLabel' => 'Environment', 'type' => 'select', 'required' => false, 'selectOptions' => instance_context_options()},
196
+ {'fieldName' => 'tags', 'fieldLabel' => 'Tags', 'type' => 'text', 'required' => false}
197
+ ]
198
+
199
+ params = options[:options] || {}
200
+
201
+ if params.empty?
202
+ puts "\n#{usage}\n"
203
+ option_lines = update_instance_option_types.collect {|it| "\t-O #{it['fieldName']}=\"value\"" }.join("\n")
204
+ puts "\nAvailable Options:\n#{option_lines}\n\n"
205
+ exit 1
206
+ end
207
+
208
+ instance_keys = ['name', 'description', 'instanceContext', 'tags']
209
+ params = params.select {|k,v| instance_keys.include?(k) }
210
+ params['tags'] = params['tags'].split(',').collect {|it| it.to_s.strip }.compact.uniq if params['tags']
211
+ payload['instance'].merge!(params)
212
+
213
+ json_response = @instances_interface.update(instance["id"], payload)
214
+
215
+ if options[:dry_run]
216
+ print_dry_run("PUT #{@appliance_url}/api/instances", payload)
217
+ return
218
+ end
219
+
220
+ if options[:json]
221
+ print JSON.pretty_generate(json_response)
222
+ print "\n"
223
+ else
224
+ print_green_success "Updated instance #{instance['name']}"
225
+ list([])
226
+ # details_options = [payload['app']['name']]
227
+ # details(details_options)
228
+ end
229
+
230
+ rescue RestClient::Exception => e
231
+ print_rest_exception(e, options)
232
+ exit 1
233
+ end
234
+ end
235
+
260
236
  def stats(args)
261
237
  options = {}
262
238
  optparse = OptionParser.new do|opts|
@@ -270,14 +246,14 @@ class Morpheus::Cli::Instances
270
246
  optparse.parse(args)
271
247
  connect(options)
272
248
  begin
273
- instance_results = @instances_interface.get({name: args[0]})
274
- if instance_results['instances'].empty?
275
- puts "Instance not found by name #{args[0]}"
249
+ instance = find_instance_by_name_or_id(args[0])
250
+ json_response = @instances_interface.get(instance['id'])
251
+ if options[:json]
252
+ print JSON.pretty_generate(json_response)
276
253
  return
277
254
  end
278
- instance = instance_results['instances'][0]
279
- instance_id = instance['id']
280
- stats = instance_results['stats'][instance_id.to_s]
255
+ instance = json_response['instance']
256
+ stats = json_response['stats'] || {}
281
257
  if options[:json]
282
258
  print JSON.pretty_generate(stats)
283
259
  print "\n"
@@ -312,7 +288,7 @@ class Morpheus::Cli::Instances
312
288
  optparse.parse(args)
313
289
  connect(options)
314
290
  begin
315
- instance = find_instance_by_name(args[0])
291
+ instance = find_instance_by_name_or_id(args[0])
316
292
  container_ids = instance['containers']
317
293
  if options[:node_id] && container_ids.include?(options[:node_id])
318
294
  container_ids = [options[:node_id]]
@@ -353,23 +329,53 @@ class Morpheus::Cli::Instances
353
329
  end
354
330
  if args.count < 1
355
331
  puts "\n#{optparse.banner}\n\n"
356
- return
332
+ exit 1
357
333
  end
358
334
  optparse.parse(args)
359
335
  connect(options)
360
336
  begin
361
- instance_results = @instances_interface.get({name: args[0]})
362
- if instance_results['instances'].empty?
363
- puts "Instance not found by name #{args[0]}"
337
+ instance = find_instance_by_name_or_id(args[0])
338
+ json_response = @instances_interface.get(instance['id'])
339
+ if options[:json]
340
+ print JSON.pretty_generate(json_response)
364
341
  return
365
342
  end
366
- instance = instance_results['instances'][0]
367
- instance_id = instance['id']
368
- stats = instance_results['stats'][instance_id.to_s]
369
- print "\n" ,cyan, bold, "#{instance['name']} (#{instance['instanceType']['name']})\n","==================", reset, "\n\n"
343
+ instance = json_response['instance']
344
+ stats = json_response['stats'] || {}
345
+ # load_balancers = stats = json_response['loadBalancers'] || {}
346
+
347
+ status_string = instance['status']
348
+ if status_string == 'running'
349
+ status_string = "#{green}#{status_string.upcase}#{cyan}"
350
+ elsif status_string == 'stopped' or status_string == 'failed'
351
+ status_string = "#{red}#{status_string.upcase}#{cyan}"
352
+ elsif status_string == 'unknown'
353
+ status_string = "#{white}#{status_string.upcase}#{cyan}"
354
+ else
355
+ status_string = "#{yellow}#{status_string.upcase}#{cyan}"
356
+ end
357
+ connection_string = ''
358
+ if !instance['connectionInfo'].nil? && instance['connectionInfo'].empty? == false
359
+ connection_string = "#{instance['connectionInfo'][0]['ip']}:#{instance['connectionInfo'][0]['port']}"
360
+ end
361
+
362
+ print "\n" ,cyan, bold, "Instance Details\n","==================", reset, "\n\n"
363
+ print cyan
364
+ puts "ID: #{instance['id']}"
365
+ puts "Name: #{instance['name']}"
366
+ puts "Description: #{instance['description']}"
367
+ puts "Group: #{instance['group'] ? instance['group']['name'] : ''}"
368
+ puts "Cloud: #{instance['cloud'] ? instance['cloud']['name'] : ''}"
369
+ puts "Type: #{instance['instanceType']['name']}"
370
+ puts "Environment: #{instance['instanceContext']}"
371
+ puts "Nodes: #{instance['containers'] ? instance['containers'].count : 0}"
372
+ puts "Connection: #{connection_string}"
373
+ #puts "Account: #{instance['account'] ? instance['account']['name'] : ''}"
374
+ puts "Status: #{status_string}"
370
375
  print cyan, "Memory: \t#{Filesize.from("#{stats['usedMemory']} B").pretty} / #{Filesize.from("#{stats['maxMemory']} B").pretty}\n"
371
376
  print cyan, "Storage: \t#{Filesize.from("#{stats['usedStorage']} B").pretty} / #{Filesize.from("#{stats['maxStorage']} B").pretty}\n\n",reset
372
- puts instance
377
+
378
+ #puts instance
373
379
  rescue RestClient::Exception => e
374
380
  print_rest_exception(e, options)
375
381
  exit 1
@@ -389,7 +395,7 @@ class Morpheus::Cli::Instances
389
395
  optparse.parse(args)
390
396
  connect(options)
391
397
  begin
392
- instance = find_instance_by_name(args[0])
398
+ instance = find_instance_by_name_or_id(args[0])
393
399
  env_results = @instances_interface.get_envs(instance['id'])
394
400
  print "\n" ,cyan, bold, "#{instance['name']} (#{instance['instanceType']['name']})\n","==================", "\n\n", reset, cyan
395
401
  envs = env_results['envs'] || {}
@@ -428,7 +434,7 @@ class Morpheus::Cli::Instances
428
434
  optparse.parse(args)
429
435
  connect(options)
430
436
  begin
431
- instance = find_instance_by_name(args[0])
437
+ instance = find_instance_by_name_or_id(args[0])
432
438
  evar = {name: args[1], value: args[2], export: options[:export], masked: options[:masked]}
433
439
  params = {}
434
440
  @instances_interface.create_env(instance['id'], [evar])
@@ -452,7 +458,7 @@ class Morpheus::Cli::Instances
452
458
  optparse.parse(args)
453
459
  connect(options)
454
460
  begin
455
- instance = find_instance_by_name(args[0])
461
+ instance = find_instance_by_name_or_id(args[0])
456
462
  @instances_interface.del_env(instance['id'], args[1])
457
463
  envs([args[0]])
458
464
  rescue RestClient::Exception => e
@@ -474,7 +480,7 @@ class Morpheus::Cli::Instances
474
480
  optparse.parse(args)
475
481
  connect(options)
476
482
  begin
477
- instance = find_instance_by_name(args[0])
483
+ instance = find_instance_by_name_or_id(args[0])
478
484
  unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to stop this instance?", options)
479
485
  exit 1
480
486
  end
@@ -503,7 +509,7 @@ class Morpheus::Cli::Instances
503
509
  optparse.parse(args)
504
510
  connect(options)
505
511
  begin
506
- instance = find_instance_by_name(args[0])
512
+ instance = find_instance_by_name_or_id(args[0])
507
513
  json_response = @instances_interface.start(instance['id'])
508
514
  if options[:json]
509
515
  print JSON.pretty_generate(json_response)
@@ -529,7 +535,7 @@ class Morpheus::Cli::Instances
529
535
  optparse.parse(args)
530
536
  connect(options)
531
537
  begin
532
- instance = find_instance_by_name(args[0])
538
+ instance = find_instance_by_name_or_id(args[0])
533
539
  unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to restart this instance?", options)
534
540
  exit 1
535
541
  end
@@ -558,7 +564,7 @@ class Morpheus::Cli::Instances
558
564
  optparse.parse(args)
559
565
  connect(options)
560
566
  begin
561
- instance = find_instance_by_name(args[0])
567
+ instance = find_instance_by_name_or_id(args[0])
562
568
  unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to stop this instance?", options)
563
569
  exit 1
564
570
  end
@@ -589,7 +595,7 @@ class Morpheus::Cli::Instances
589
595
  optparse.parse(args)
590
596
  connect(options)
591
597
  begin
592
- instance = find_instance_by_name(args[0])
598
+ instance = find_instance_by_name_or_id(args[0])
593
599
  json_response = @instances_interface.start(instance['id'],false)
594
600
  if options[:json]
595
601
  print JSON.pretty_generate(json_response)
@@ -617,7 +623,7 @@ class Morpheus::Cli::Instances
617
623
  optparse.parse(args)
618
624
  connect(options)
619
625
  begin
620
- instance = find_instance_by_name(args[0])
626
+ instance = find_instance_by_name_or_id(args[0])
621
627
  unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to restart this instance?", options)
622
628
  exit 1
623
629
  end
@@ -639,7 +645,7 @@ class Morpheus::Cli::Instances
639
645
  options = {}
640
646
  optparse = OptionParser.new do|opts|
641
647
  opts.banner = "Usage: morpheus instances resize [name]"
642
- build_common_options(opts, options, [:options, :json, :remote])
648
+ build_common_options(opts, options, [:options, :json, :dry_run, :remote])
643
649
  end
644
650
  if args.count < 1
645
651
  puts "\n#{optparse.banner}\n\n"
@@ -648,17 +654,19 @@ class Morpheus::Cli::Instances
648
654
  optparse.parse(args)
649
655
  connect(options)
650
656
  begin
651
- instance = find_instance_by_name(args[0])
657
+ instance = find_instance_by_name_or_id(args[0])
652
658
 
653
659
  group_id = instance['group']['id']
654
660
  cloud_id = instance['cloud']['id']
655
661
  layout_id = instance['layout']['id']
656
662
 
657
663
  plan_id = instance['plan']['id']
658
- payload = {}
664
+ payload = {
665
+ :instance => {:id => instance["id"]}
666
+ }
659
667
 
660
668
  # avoid 500 error
661
- payload[:servicePlanOptions] = {}
669
+ # payload[:servicePlanOptions] = {}
662
670
 
663
671
  puts "\nDue to limitations by most Guest Operating Systems, Disk sizes can only be expanded and not reduced.\nIf a smaller plan is selected, memory and CPU (if relevant) will be reduced but storage will not.\n\n"
664
672
 
@@ -676,6 +684,7 @@ class Morpheus::Cli::Instances
676
684
  new_plan_id = service_plan["id"]
677
685
  #payload[:servicePlan] = new_plan_id # ew, this api uses servicePlanId instead
678
686
  payload[:servicePlanId] = new_plan_id
687
+ payload[:instance][:plan] = {id: service_plan["id"]}
679
688
 
680
689
  volumes_response = @instances_interface.volumes(instance['id'])
681
690
  current_volumes = volumes_response['volumes'].sort {|x,y| x['displayOrder'] <=> y['displayOrder'] }
@@ -690,6 +699,10 @@ class Morpheus::Cli::Instances
690
699
  # for now, always do this
691
700
  payload[:deleteOriginalVolumes] = true
692
701
 
702
+ if options[:dry_run]
703
+ print_dry_run("PUT #{@appliance_url}/api/instances/#{instance['id']}/resize", payload)
704
+ return
705
+ end
693
706
  json_response = @instances_interface.resize(instance['id'], payload)
694
707
  if options[:json]
695
708
  print JSON.pretty_generate(json_response)
@@ -718,7 +731,7 @@ class Morpheus::Cli::Instances
718
731
  optparse.parse(args)
719
732
  connect(options)
720
733
  begin
721
- instance = find_instance_by_name(args[0])
734
+ instance = find_instance_by_name_or_id(args[0])
722
735
  json_response = @instances_interface.backup(instance['id'])
723
736
  if options[:json]
724
737
  print JSON.pretty_generate(json_response)
@@ -736,8 +749,8 @@ class Morpheus::Cli::Instances
736
749
  def list(args)
737
750
  options = {}
738
751
  optparse = OptionParser.new do|opts|
739
- opts.on( '-g', '--group GROUP', "Group Name" ) do |group|
740
- options[:group] = group
752
+ opts.on( '-g', '--group GROUP', "Group Name" ) do |val|
753
+ options[:group_name] = val
741
754
  end
742
755
  build_common_options(opts, options, [:list, :json, :remote])
743
756
  end
@@ -745,11 +758,9 @@ class Morpheus::Cli::Instances
745
758
  connect(options)
746
759
  begin
747
760
  params = {}
748
- if !options[:group].nil?
749
- group = find_group_by_name(options[:group])
750
- if !group.nil?
751
- params['site'] = group
752
- end
761
+ group = find_group_from_options(options)
762
+ if group
763
+ params['site'] = group['id']
753
764
  end
754
765
  [:phrase, :offset, :max, :sort, :direction].each do |k|
755
766
  params[k] = options[k] unless options[k].nil?
@@ -816,8 +827,8 @@ class Morpheus::Cli::Instances
816
827
  optparse.parse(args)
817
828
  connect(options)
818
829
  begin
819
- instance = find_instance_by_name(args[0])
820
- unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to remove this instance?", options)
830
+ instance = find_instance_by_name_or_id(args[0])
831
+ unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to remove the instance '#{instance['name']}'?", options)
821
832
  exit 1
822
833
  end
823
834
  @instances_interface.destroy(instance['id'],query_params)
@@ -841,7 +852,7 @@ class Morpheus::Cli::Instances
841
852
  optparse.parse(args)
842
853
  connect(options)
843
854
  begin
844
- instance = find_instance_by_name(args[0])
855
+ instance = find_instance_by_name_or_id(args[0])
845
856
  @instances_interface.firewall_disable(instance['id'])
846
857
  security_groups([args[0]])
847
858
  rescue RestClient::Exception => e
@@ -889,7 +900,7 @@ class Morpheus::Cli::Instances
889
900
  optparse.parse(args)
890
901
  connect(options)
891
902
  begin
892
- instance = find_instance_by_name(args[0])
903
+ instance = find_instance_by_name_or_id(args[0])
893
904
  instance_id = instance['id']
894
905
  json_response = @instances_interface.security_groups(instance_id)
895
906
 
@@ -939,7 +950,7 @@ class Morpheus::Cli::Instances
939
950
  end
940
951
 
941
952
  begin
942
- instance = find_instance_by_name(args[0])
953
+ instance = find_instance_by_name_or_id(args[0])
943
954
  @instances_interface.apply_security_groups(instance['id'], options)
944
955
  security_groups([args[0]])
945
956
  rescue RestClient::Exception => e
@@ -963,7 +974,7 @@ class Morpheus::Cli::Instances
963
974
 
964
975
  optparse.parse(args)
965
976
  connect(options)
966
- instance = find_instance_by_name(args[0])
977
+ instance = find_instance_by_name_or_id(args[0])
967
978
  workflow = find_workflow_by_name(args[1])
968
979
  task_types = @tasks_interface.task_types()
969
980
  editable_options = []
@@ -1005,14 +1016,37 @@ class Morpheus::Cli::Instances
1005
1016
 
1006
1017
  private
1007
1018
 
1019
+ def find_instance_by_id(id)
1020
+ instance_results = @instances_interface.get(id.to_i)
1021
+ if instance_results['instance'].empty?
1022
+ print_red_alert "Instance not found by id #{id}"
1023
+ exit 1
1024
+ end
1025
+ instance = instance_results['instance']
1026
+ # instance['stats'] = instance_results['stats'] || {}
1027
+ # instance['loadBalancers'] = instance_results['loadBalancers'][0]['lbs'] || {}
1028
+ return instance
1029
+ end
1030
+
1008
1031
  def find_instance_by_name(name)
1009
1032
  instance_results = @instances_interface.get({name: name})
1010
1033
  if instance_results['instances'].empty?
1011
1034
  print_red_alert "Instance not found by name #{name}"
1012
1035
  exit 1
1013
1036
  end
1014
- return instance_results['instances'][0]
1037
+ instance = instance_results['instances'][0]
1038
+ # instance['stats'] = instance_results['stats'][instance['id'].to_s] || {}
1039
+ return instance
1040
+ end
1041
+
1042
+ def find_instance_by_name_or_id(val)
1043
+ if val.to_s =~ /\A\d{1,}\Z/
1044
+ return find_instance_by_id(val)
1045
+ else
1046
+ return find_instance_by_name(val)
1047
+ end
1015
1048
  end
1049
+
1016
1050
  def find_workflow_by_name(name)
1017
1051
  task_set_results = @task_sets_interface.get(name)
1018
1052
  if !task_set_results['taskSets'].nil? && !task_set_results['taskSets'].empty?
@@ -1022,32 +1056,5 @@ private
1022
1056
  exit 1
1023
1057
  end
1024
1058
  end
1025
- def find_group_by_name(name)
1026
- option_results = @options_interface.options_for_source('groups',{})
1027
- match = option_results['data'].find { |grp| grp['value'].to_s == name.to_s || grp['name'].downcase == name.downcase}
1028
- if match.nil?
1029
- return nil
1030
- else
1031
- return match['value']
1032
- end
1033
- end
1034
1059
 
1035
- def find_cloud_by_name(group_id, name)
1036
- option_results = @options_interface.options_for_source('clouds',{groupId: group_id})
1037
- match = option_results['data'].find { |grp| grp['value'].to_s == name.to_s || grp['name'].downcase == name.downcase}
1038
- if match.nil?
1039
- return nil
1040
- else
1041
- return match['value']
1042
- end
1043
- end
1044
-
1045
- def find_instance_type_by_code(code)
1046
- instance_type_results = @instance_types_interface.get({code: code})
1047
- if instance_type_results['instanceTypes'].empty?
1048
- puts "Instance Type not found by code #{code}"
1049
- return nil
1050
- end
1051
- return instance_type_results['instanceTypes'][0]
1052
- end
1053
1060
  end