morpheus-cli 0.9.7 → 0.9.8

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.
@@ -162,21 +162,23 @@ class Morpheus::Cli::Hosts
162
162
  end
163
163
 
164
164
  def add(args)
165
- if args.count < 2
166
- puts "\nUsage: morpheus hosts add CLOUD [name]\n\n"
167
- return
168
- end
165
+
169
166
  options = {zone: args[0], params:{}}
170
167
  name = args[1]
171
168
 
172
169
  optparse = OptionParser.new do|opts|
173
- opts.banner = "Usage: morpheus server add CLOUD NAME -t HOST_TYPE [options]"
170
+ opts.banner = "Usage: morpheus hosts add CLOUD NAME -t HOST_TYPE [options]"
174
171
  opts.on( '-t', '--type TYPE', "Host Type" ) do |server_type|
175
172
  options[:server_type] = server_type
176
173
  end
177
174
  Morpheus::Cli::CliCommand.genericOptions(opts,options)
178
175
  end
176
+ if args.count < 2
177
+ puts "\n#{optparse}\n\n"
178
+ exit 1
179
+ end
179
180
  optparse.parse(args)
181
+
180
182
  connect(options)
181
183
 
182
184
  params = {}
@@ -196,6 +198,7 @@ class Morpheus::Cli::Hosts
196
198
  server_type = zone_type['serverTypes'].find{|b| b['creatable'] == true && (b['code'] == options[:server_type] || b['name'] == options[:server_type])}
197
199
  params = Morpheus::Cli::OptionTypes.prompt(server_type['optionTypes'],options[:options],@api_client, options[:params])
198
200
  begin
201
+ params['server'] = params['server'] || {}
199
202
  server_payload = {server: {name: name, zone: {id: zone['id']}, computeServerType: [id: server_type['id']]}.merge(params['server']), config: params['config'], network: params['network']}
200
203
  response = @servers_interface.create(server_payload)
201
204
  rescue RestClient::Exception => e
@@ -215,7 +218,7 @@ class Morpheus::Cli::Hosts
215
218
  return
216
219
  end
217
220
  options = {}
218
- query_params = {name: args[0], removeResources: 'on', force: 'off'}
221
+ query_params = {removeResources: 'on', force: 'off'}
219
222
  optparse = OptionParser.new do|opts|
220
223
  opts.on( '-c', '--cloud CLOUD', "Cloud" ) do |cloud|
221
224
  options[:zone] = cloud
@@ -242,7 +245,7 @@ class Morpheus::Cli::Hosts
242
245
  query_params[:zoneId] = zone['id']
243
246
  end
244
247
  server = nil
245
- server_results = @servers_interface.get(query_params)
248
+ server_results = @servers_interface.get({name: args[0]})
246
249
  if server_results['servers'].nil? || server_results['servers'].empty?
247
250
  server_results = @servers_interface.get(args[0].to_i)
248
251
  server = server_results['server']
@@ -265,7 +268,7 @@ class Morpheus::Cli::Hosts
265
268
  exit 1
266
269
  end
267
270
 
268
- json_response = @servers_interface.destroy(server['id'])
271
+ json_response = @servers_interface.destroy(server['id'], query_params)
269
272
  if options[:json]
270
273
  print JSON.pretty_generate(json_response)
271
274
  print "\n"
@@ -421,6 +421,9 @@ class Morpheus::Cli::Instances
421
421
  connect(options)
422
422
  begin
423
423
  instance = find_instance_by_name(args[0])
424
+ if !::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to stop this instance?", options)
425
+ exit 1
426
+ end
424
427
  json_response = @instances_interface.stop(instance['id'])
425
428
  if options[:json]
426
429
  print JSON.pretty_generate(json_response)
@@ -473,6 +476,9 @@ class Morpheus::Cli::Instances
473
476
  connect(options)
474
477
  begin
475
478
  instance = find_instance_by_name(args[0])
479
+ if !::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to restart this instance?", options)
480
+ exit 1
481
+ end
476
482
  json_response = @instances_interface.restart(instance['id'])
477
483
  if options[:json]
478
484
  print JSON.pretty_generate(json_response)
@@ -499,6 +505,9 @@ class Morpheus::Cli::Instances
499
505
  connect(options)
500
506
  begin
501
507
  instance = find_instance_by_name(args[0])
508
+ if !::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to stop this instance?", options)
509
+ exit 1
510
+ end
502
511
  json_response = @instances_interface.stop(instance['id'],false)
503
512
  if options[:json]
504
513
  print JSON.pretty_generate(json_response)
@@ -555,6 +564,9 @@ class Morpheus::Cli::Instances
555
564
  connect(options)
556
565
  begin
557
566
  instance = find_instance_by_name(args[0])
567
+ if !::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to restart this instance?", options)
568
+ exit 1
569
+ end
558
570
  json_response = @instances_interface.restart(instance['id'],false)
559
571
  if options[:json]
560
572
  print JSON.pretty_generate(json_response)
@@ -649,9 +661,9 @@ class Morpheus::Cli::Instances
649
661
  if !instance['connectionInfo'].nil? && instance['connectionInfo'].empty? == false
650
662
  connection_string = "#{instance['connectionInfo'][0]['ip']}:#{instance['connectionInfo'][0]['port']}"
651
663
  end
652
- {id: instance['id'], name: instance['name'], connection: connection_string, environment: instance['instanceContext'], nodes: instance['containers'].count, status: status_string, type: instance['instanceType']['name']}
664
+ {id: instance['id'], name: instance['name'], connection: connection_string, environment: instance['instanceContext'], nodes: instance['containers'].count, status: status_string, type: instance['instanceType']['name'], group: !instance['group'].nil? ? instance['group']['name'] : nil}
653
665
  end
654
- tp instance_table, :id, :name, :type, :environment, :nodes, :connection, :status
666
+ tp instance_table, :id, :name,:group, :type, :environment, :nodes, :connection, :status
655
667
  end
656
668
  print reset,"\n\n"
657
669
  end
@@ -664,19 +676,30 @@ class Morpheus::Cli::Instances
664
676
 
665
677
  def remove(args)
666
678
  options = {}
679
+ query_params = {keepBackups: 'off', force: 'off'}
667
680
  optparse = OptionParser.new do|opts|
668
- opts.banner = "Usage: morpheus instances remove [name]"
681
+ opts.banner = "Usage: morpheus instances remove [name] [-fB]"
682
+ opts.on( '-f', '--force', "Force Remove" ) do
683
+ query_params[:force] = 'on'
684
+ end
685
+ opts.on( '-B', '--keep-backups', "Preserve copy of backups" ) do
686
+ query_params[:keepBackups] = 'on'
687
+ end
669
688
  Morpheus::Cli::CliCommand.genericOptions(opts,options)
689
+
670
690
  end
671
691
  if args.count < 1
672
- puts "\n#{optparse.banner}\n\n"
692
+ puts "\n#{optparse}\n\n"
673
693
  exit 1
674
694
  end
675
695
  optparse.parse(args)
676
696
  connect(options)
677
697
  begin
678
698
  instance = find_instance_by_name(args[0])
679
- @instances_interface.destroy(instance['id'])
699
+ if !::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to remove this instance?", options)
700
+ exit 1
701
+ end
702
+ @instances_interface.destroy(instance['id'],query_params)
680
703
  list([])
681
704
  rescue RestClient::Exception => e
682
705
  ::Morpheus::Cli::ErrorHandler.new.print_rest_exception(e)
@@ -307,7 +307,7 @@ class Morpheus::Cli::LoadBalancers
307
307
  optparse.parse(args)
308
308
  connect(options)
309
309
  begin
310
- lb = find_lb_by_name_or_code_or_id(lb_name)
310
+ lb = find_lb_by_name(lb_name)
311
311
  exit 1 if lb.nil?
312
312
  exit unless Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the load balancer #{lb['name']}?")
313
313
  json_response = @load_balancers_interface.destroy(lb['id'])
@@ -35,8 +35,8 @@ module Morpheus
35
35
  if option_type['fieldContext']
36
36
  results[option_type['fieldContext']] ||= {}
37
37
  context_map = results[option_type['fieldContext']]
38
- if options[option_type['fieldContext']] and options[option_type['fieldContext']].key?(option_type['fieldLabel'])
39
- value = options[option_type['fieldContext']][option_type['fieldLabel']]
38
+ if options[option_type['fieldContext']] and options[option_type['fieldContext']].key?(option_type['fieldName'])
39
+ value = options[option_type['fieldContext']][option_type['fieldName']]
40
40
  value_found = true
41
41
  end
42
42
  end
@@ -140,7 +140,7 @@ module Morpheus
140
140
  while !value_found do
141
141
  Readline.completion_append_character = ""
142
142
  Readline.basic_word_break_characters = ''
143
- Readline.completion_proc = proc {|s| source_options.collect{|opt| opt['name']}.grep(/^#{Regexp.escape(s)}/)}
143
+ Readline.completion_proc = proc {|s| source_options.clone.collect{|opt| opt['name']}.grep(/^#{Regexp.escape(s)}/)}
144
144
  input = Readline.readline("#{option_type['fieldLabel']}#{option_type['fieldAddOn'] ? ('(' + option_type['fieldAddOn'] + ') ') : '' }#{!option_type['required'] ? ' (optional)' : ''} ['?' for options]: ", false).to_s
145
145
  input = input.chomp.strip
146
146
  if option_type['optionSource']
@@ -101,7 +101,7 @@ class Morpheus::Cli::Shell
101
101
  puts "Argument Syntax Error..."
102
102
  rescue SystemExit, Interrupt
103
103
  # nothing to do
104
- puts "Interrupted..."
104
+ print "\n"
105
105
  rescue => e
106
106
  print red, "\n", e.message, "\n", reset
107
107
  print e.backtrace.join("\n"), "\n"
@@ -183,7 +183,6 @@ class Morpheus::Cli::Tasks
183
183
  if changes_payload
184
184
  task_payload.merge!(changes_payload)
185
185
  end
186
- puts params
187
186
  if params['taskOptions']
188
187
  task_payload['taskOptions'].merge!(params['taskOptions'])
189
188
  end
@@ -272,23 +271,28 @@ class Morpheus::Cli::Tasks
272
271
  puts "Task Type must be specified...\n#{optparse.banner}"
273
272
  exit 1
274
273
  end
275
-
276
- task_type = find_task_type_by_name(task_type_name)
277
- if task_type.nil?
278
- puts "Task Type not found!"
279
- exit 1
280
- end
281
- input_options = Morpheus::Cli::OptionTypes.prompt(task_type['optionTypes'],options[:options],@api_client, options[:params])
282
- payload = {task: {name: task_name, taskOptions: input_options['taskOptions'], taskType: {code: task_type['code'], id: task_type['id']}}}
283
- json_response = @tasks_interface.create(payload)
284
- if options[:json]
285
- print JSON.pretty_generate(json_response)
286
- else
287
- if json_response['success']
288
- print "\n", cyan, "Task #{json_response['task']['name']} created successfully", reset, "\n\n"
274
+ begin
275
+ task_type = find_task_type_by_name(task_type_name)
276
+ if task_type.nil?
277
+ puts "Task Type not found!"
278
+ exit 1
279
+ end
280
+ input_options = Morpheus::Cli::OptionTypes.prompt(task_type['optionTypes'],options[:options],@api_client, options[:params])
281
+ payload = {task: {name: task_name, taskOptions: input_options['taskOptions'], taskType: {code: task_type['code'], id: task_type['id']}}}
282
+ json_response = @tasks_interface.create(payload)
283
+ if options[:json]
284
+ print JSON.pretty_generate(json_response)
289
285
  else
290
- print "\n", red, "Error Creating Task #{json_response['errors']}", reset, "\n\n"
286
+ print "\n", cyan, "Task #{json_response['task']['name']} created successfully", reset, "\n\n"
291
287
  end
288
+ rescue RestClient::Exception => e
289
+ if e.response.code == 400
290
+ error = JSON.parse(e.response.to_s)
291
+ ::Morpheus::Cli::ErrorHandler.new.print_errors(error,options)
292
+ else
293
+ puts "Error Communicating with the Appliance. Please try again later. #{e}"
294
+ end
295
+ exit 1
292
296
  end
293
297
  end
294
298
 
@@ -1,5 +1,5 @@
1
1
  module Morpheus
2
2
  module Cli
3
- VERSION = "0.9.7"
3
+ VERSION = "0.9.8"
4
4
  end
5
5
  end
@@ -0,0 +1,385 @@
1
+ # require 'yaml'
2
+ require 'io/console'
3
+ require 'rest_client'
4
+ require 'term/ansicolor'
5
+ require 'optparse'
6
+ require 'table_print'
7
+ require 'morpheus/cli/cli_command'
8
+
9
+ class Morpheus::Cli::VirtualImages
10
+ include Morpheus::Cli::CliCommand
11
+ include Term::ANSIColor
12
+ def initialize()
13
+ @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
14
+ end
15
+
16
+ def connect(opts)
17
+ if opts[:remote]
18
+ @appliance_url = opts[:remote]
19
+ @appliance_name = opts[:remote]
20
+ @access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials(opts)
21
+ else
22
+ @access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials(opts)
23
+ end
24
+ @api_client = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url)
25
+ @virtual_images_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).virtual_images
26
+
27
+ if @access_token.empty?
28
+ print red,bold, "\nInvalid Credentials. Unable to acquire access token. Please verify your credentials and try again.\n\n",reset
29
+ exit 1
30
+ end
31
+ end
32
+
33
+
34
+ def handle(args)
35
+ if args.empty?
36
+ puts "\nUsage: morpheus virtual-images [list,add, update,remove, details, lb-types]\n\n"
37
+ return
38
+ end
39
+
40
+ case args[0]
41
+ when 'list'
42
+ list(args[1..-1])
43
+ when 'add'
44
+ add(args[1..-1])
45
+ when 'update'
46
+ # update(args[1..-1])
47
+ when 'details'
48
+ details(args[1..-1])
49
+ when 'remove'
50
+ remove(args[1..-1])
51
+ when 'virtual-image-types'
52
+ virtual_image_types(args[1..-1])
53
+ else
54
+ puts "\nUsage: morpheus virtual-images [list,add, update,remove, details, lb-types]\n\n"
55
+ exit 127
56
+ end
57
+ end
58
+
59
+ def list(args)
60
+ options = {}
61
+ optparse = OptionParser.new do|opts|
62
+ opts.banner = "Usage: morpheus virtual-images list [-s] [-o] [-m] [-t]"
63
+ opts.on( '-t', '--type IMAGE_TYPE', "Image Type" ) do |val|
64
+ options[:imageType] = val.downcase
65
+ end
66
+
67
+ opts.on( '', '--all', "All Images" ) do |val|
68
+ options[:filterType] = 'All'
69
+ end
70
+ opts.on( '', '--user', "User Images" ) do |val|
71
+ options[:filterType] = 'User'
72
+ end
73
+ opts.on( '', '--system', "System Images" ) do |val|
74
+ options[:filterType] = 'System'
75
+ end
76
+ Morpheus::Cli::CliCommand.genericOptions(opts,options)
77
+ end
78
+ optparse.parse(args)
79
+ connect(options)
80
+ begin
81
+ params = {}
82
+ if options[:offset]
83
+ params[:offset] = options[:offset]
84
+ end
85
+ if options[:max]
86
+ params[:max] = options[:max]
87
+ end
88
+ if options[:phrase]
89
+ params[:phrase] = options[:phrase]
90
+ end
91
+ if options[:imageType]
92
+ params[:imageType] = options[:imageType]
93
+ end
94
+ if options[:filterType]
95
+ params[:filterType] = options[:filterType]
96
+ end
97
+ json_response = @virtual_images_interface.get(params)
98
+ if options[:json]
99
+ print JSON.pretty_generate(json_response)
100
+ else
101
+ images = json_response['virtualImages']
102
+ print "\n" ,cyan, bold, "Morpheus Virtual Images\n","=======================", reset, "\n\n"
103
+ if images.empty?
104
+ puts yellow,"No virtual images currently exist.",reset
105
+ else
106
+ print cyan
107
+ image_table_data = images.collect do |image|
108
+ {name: image['name'], id: image['id'], type: image['imageType'].upcase, source: image['userUploaded'] ? "#{green}UPLOADED#{cyan}" : (image['systemImage'] ? 'SYSTEM' : "#{white}SYNCED#{cyan}"), storage: !image['storageProvider'].nil? ? image['storageProvider']['name'] : 'Default', size: image['rawSize'].nil? ? 'Unknown' : "#{Filesize.from("#{image['rawSize']} B").pretty}"}
109
+ end
110
+ tp image_table_data, :id, :name, :type, :storage, :size, :source
111
+ end
112
+ print reset,"\n\n"
113
+ end
114
+
115
+
116
+ rescue RestClient::Exception => e
117
+ if e.response.code == 400
118
+ error = JSON.parse(e.response.to_s)
119
+ ::Morpheus::Cli::ErrorHandler.new.print_errors(error,options)
120
+ else
121
+ puts "Error Communicating with the Appliance. Please try again later. #{e}"
122
+ end
123
+ exit 1
124
+ end
125
+ end
126
+
127
+ def details(args)
128
+ image_name = args[0]
129
+ options = {}
130
+ optparse = OptionParser.new do|opts|
131
+ opts.banner = "Usage: morpheus virtual-images details [name]"
132
+ Morpheus::Cli::CliCommand.genericOptions(opts,options)
133
+ end
134
+ if args.count < 1
135
+ puts "\n#{optparse.banner}\n\n"
136
+ exit 1
137
+ end
138
+ optparse.parse(args)
139
+ connect(options)
140
+ begin
141
+ lb = find_lb_by_name(image_name)
142
+
143
+ exit 1 if lb.nil?
144
+ lb_type = find_lb_type_by_name(lb['type']['name'])
145
+ if options[:json]
146
+ puts JSON.pretty_generate({task:task})
147
+ else
148
+ print "\n", cyan, "Lb #{lb['name']} - #{lb['type']['name']}\n\n"
149
+ lb_type['optionTypes'].sort { |x,y| x['displayOrder'].to_i <=> y['displayOrder'].to_i }.each do |optionType|
150
+ puts " #{optionType['fieldLabel']} : " + (optionType['type'] == 'password' ? "#{task['taskOptions'][optionType['fieldName']] ? '************' : ''}" : "#{task['taskOptions'][optionType['fieldName']] || optionType['defaultValue']}")
151
+ end
152
+ print reset,"\n\n"
153
+ end
154
+ rescue RestClient::Exception => e
155
+ if e.response.code == 400
156
+ error = JSON.parse(e.response.to_s)
157
+ ::Morpheus::Cli::ErrorHandler.new.print_errors(error,options)
158
+ else
159
+ puts "Error Communicating with the Appliance. Please try again later. #{e}"
160
+ end
161
+ exit 1
162
+ end
163
+ end
164
+
165
+ def update(args)
166
+ image_name = args[0]
167
+ options = {}
168
+ account_name = nil
169
+ optparse = OptionParser.new do|opts|
170
+ opts.banner = "Usage: morpheus tasks update [task] [options]"
171
+ Morpheus::Cli::CliCommand.genericOptions(opts,options)
172
+ end
173
+ if args.count < 1
174
+ puts "\n#{optparse.banner}\n\n"
175
+ exit 1
176
+ end
177
+ optparse.parse(args)
178
+
179
+ connect(options)
180
+
181
+ begin
182
+
183
+
184
+ task = find_task_by_name_or_code_or_id(image_name)
185
+ exit 1 if task.nil?
186
+ lb_type = find_lb_type_by_name(task['type']['name'])
187
+
188
+ #params = Morpheus::Cli::OptionTypes.prompt(add_user_option_types, options[:options], @api_client, options[:params]) # options[:params] is mysterious
189
+ params = options[:options] || {}
190
+
191
+ if params.empty?
192
+ puts "\n#{optparse.banner}\n\n"
193
+ option_lines = update_task_option_types(lb_type).collect {|it| "\t-O #{it['fieldContext'] ? (it['fieldContext'] + '.') : ''}#{it['fieldName']}=\"value\"" }.join("\n")
194
+ puts "\nAvailable Options:\n#{option_lines}\n\n"
195
+ exit 1
196
+ end
197
+
198
+ #puts "parsed params is : #{params.inspect}"
199
+ task_keys = ['name']
200
+ changes_payload = (params.select {|k,v| task_keys.include?(k) })
201
+ task_payload = task
202
+ if changes_payload
203
+ task_payload.merge!(changes_payload)
204
+ end
205
+ puts params
206
+ if params['taskOptions']
207
+ task_payload['taskOptions'].merge!(params['taskOptions'])
208
+ end
209
+
210
+ request_payload = {task: task_payload}
211
+ response = @virtual_images_interface.update(task['id'], request_payload)
212
+ if options[:json]
213
+ print JSON.pretty_generate(json_response)
214
+ if !response['success']
215
+ exit 1
216
+ end
217
+ else
218
+ print "\n", cyan, "Task #{response['task']['name']} updated", reset, "\n\n"
219
+ end
220
+ rescue RestClient::Exception => e
221
+ if e.response.code == 400
222
+ error = JSON.parse(e.response.to_s)
223
+ ::Morpheus::Cli::ErrorHandler.new.print_errors(error)
224
+ else
225
+ puts "Error Communicating with the Appliance. Please try again later. #{e}"
226
+ end
227
+ exit 1
228
+ end
229
+ end
230
+
231
+
232
+ def virtual_image_types(args)
233
+ options = {}
234
+ optparse = OptionParser.new do|opts|
235
+ opts.banner = "Usage: morpheus virtual-images lb-types"
236
+ Morpheus::Cli::CliCommand.genericOptions(opts,options)
237
+ end
238
+ optparse.parse(args)
239
+ connect(options)
240
+ begin
241
+ json_response = @virtual_images_interface.load_balancer_types()
242
+ if options[:json]
243
+ print JSON.pretty_generate(json_response)
244
+ else
245
+ lb_types = json_response['virtualImageTypes']
246
+ print "\n" ,cyan, bold, "Morpheus Virtual Image Types\n","============================", reset, "\n\n"
247
+ if lb_types.nil? || lb_types.empty?
248
+ puts yellow,"No image types currently exist on this appliance. This could be a seed issue.",reset
249
+ else
250
+ print cyan
251
+ lb_table_data = lb_types.collect do |lb_type|
252
+ {name: lb_type['name'], id: lb_type['id'], code: lb_type['code']}
253
+ end
254
+ tp lb_table_data, :id, :name, :code
255
+ end
256
+
257
+ print reset,"\n\n"
258
+ end
259
+
260
+
261
+ rescue => e
262
+ if e.response.code == 400
263
+ error = JSON.parse(e.response.to_s)
264
+ ::Morpheus::Cli::ErrorHandler.new.print_errors(error,options)
265
+ else
266
+ puts "Error Communicating with the Appliance. Please try again later. #{e}"
267
+ end
268
+ exit 1
269
+ end
270
+ end
271
+
272
+ def add(args)
273
+ image_name = args[0]
274
+ lb_type_name = nil
275
+ options = {}
276
+ optparse = OptionParser.new do|opts|
277
+ opts.banner = "Usage: morpheus virtual-images add [lb] -t LB_TYPE"
278
+ opts.on( '-t', '--type LB_TYPE', "Lb Type" ) do |val|
279
+ lb_type_name = val
280
+ end
281
+ Morpheus::Cli::CliCommand.genericOptions(opts,options)
282
+ end
283
+ if args.count < 1
284
+ puts "\n#{optparse.banner}\n\n"
285
+ exit 1
286
+ end
287
+ optparse.parse(args)
288
+ connect(options)
289
+
290
+ if lb_type_name.nil?
291
+ puts "LB Type must be specified...\n#{optparse.banner}"
292
+ exit 1
293
+ end
294
+
295
+ lb_type = find_lb_type_by_name(lb_type_name)
296
+ if lb_type.nil?
297
+ puts "LB Type not found!"
298
+ exit 1
299
+ end
300
+ input_options = Morpheus::Cli::OptionTypes.prompt(lb_type['optionTypes'],options[:options],@api_client, options[:params])
301
+ payload = {task: {name: image_name, taskOptions: input_options['taskOptions'], type: {code: lb_type['code'], id: lb_type['id']}}}
302
+ begin
303
+ json_response = @virtual_images_interface.create(payload)
304
+ if options[:json]
305
+ print JSON.pretty_generate(json_response)
306
+ else
307
+ print "\n", cyan, "LB #{json_response['virtualImage']['name']} created successfully", reset, "\n\n"
308
+ end
309
+ rescue RestClient::Exception => e
310
+ ::Morpheus::Cli::ErrorHandler.new.print_rest_exception(e)
311
+ exit 1
312
+ end
313
+ end
314
+
315
+ def remove(args)
316
+ image_name = args[0]
317
+ options = {}
318
+ optparse = OptionParser.new do|opts|
319
+ opts.banner = "Usage: morpheus virtual-images remove [name]"
320
+ Morpheus::Cli::CliCommand.genericOptions(opts,options)
321
+ end
322
+ if args.count < 1
323
+ puts "\n#{optparse.banner}\n\n"
324
+ exit 1
325
+ end
326
+ optparse.parse(args)
327
+ connect(options)
328
+ begin
329
+ image = find_image_by_name(image_name)
330
+ exit 1 if image.nil?
331
+ exit unless Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the virtual image #{image['name']}?")
332
+ json_response = @virtual_images_interface.destroy(image['id'])
333
+ if options[:json]
334
+ print JSON.pretty_generate(json_response)
335
+ else
336
+ print "\n", cyan, "Virtual Image #{image['name']} removed", reset, "\n\n"
337
+ end
338
+ rescue RestClient::Exception => e
339
+ if e.response.code == 400
340
+ error = JSON.parse(e.response.to_s)
341
+ ::Morpheus::Cli::ErrorHandler.new.print_errors(error,options)
342
+ else
343
+ puts "Error Communicating with the Appliance. Please try again later. #{e}"
344
+ end
345
+ exit 1
346
+ end
347
+ end
348
+
349
+
350
+ private
351
+ def find_image_by_name(val)
352
+ raise "find_image_by_name passed a bad name: #{val.inspect}" if val.to_s == ''
353
+ results = @virtual_images_interface.get(val)
354
+ result = nil
355
+ if !results['virtualImages'].nil? && !results['virtualImages'].empty?
356
+ result = results['virtualImages'][0]
357
+ elsif val.to_i.to_s == val
358
+ results = @virtual_images_interface.get(val.to_i)
359
+ result = results['virtualImage']
360
+ end
361
+ if result.nil?
362
+ print red,bold, "\nVirtual Image not found by '#{val}'\n\n",reset
363
+ return nil
364
+ end
365
+ return result
366
+ end
367
+
368
+ def find_image_type_by_name(val)
369
+ raise "find_,age_type_by_name passed a bad name: #{val.inspect}" if val.to_s == ''
370
+ results = @virtual_images_interface.virtual_image_types(val)
371
+ result = nil
372
+ if !results['virtualImageTypes'].nil? && !results['virtualImageTypes'].empty?
373
+ result = results['virtualImageTypes'][0]
374
+ elsif val.to_i.to_s == val
375
+ results = @virtual_images_interface.virtual_image_types(val.to_i)
376
+ result = results['virtualImageType']
377
+ end
378
+ if result.nil?
379
+ print red,bold, "\nImage Type not found by '#{val}'\n\n",reset
380
+ return nil
381
+ end
382
+ return result
383
+ end
384
+
385
+ end