morpheus-cli 2.10.0 → 2.10.1

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.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/bin/morpheus +27 -32
  3. data/lib/morpheus/api/accounts_interface.rb +36 -47
  4. data/lib/morpheus/api/api_client.rb +141 -110
  5. data/lib/morpheus/api/app_templates_interface.rb +56 -72
  6. data/lib/morpheus/api/apps_interface.rb +111 -132
  7. data/lib/morpheus/api/auth_interface.rb +30 -0
  8. data/lib/morpheus/api/clouds_interface.rb +71 -76
  9. data/lib/morpheus/api/custom_instance_types_interface.rb +21 -46
  10. data/lib/morpheus/api/dashboard_interface.rb +10 -17
  11. data/lib/morpheus/api/deploy_interface.rb +60 -72
  12. data/lib/morpheus/api/deployments_interface.rb +53 -71
  13. data/lib/morpheus/api/groups_interface.rb +55 -45
  14. data/lib/morpheus/api/instance_types_interface.rb +19 -23
  15. data/lib/morpheus/api/instances_interface.rb +179 -177
  16. data/lib/morpheus/api/key_pairs_interface.rb +11 -17
  17. data/lib/morpheus/api/license_interface.rb +18 -23
  18. data/lib/morpheus/api/load_balancers_interface.rb +54 -69
  19. data/lib/morpheus/api/logs_interface.rb +25 -29
  20. data/lib/morpheus/api/options_interface.rb +13 -17
  21. data/lib/morpheus/api/provision_types_interface.rb +19 -22
  22. data/lib/morpheus/api/roles_interface.rb +75 -94
  23. data/lib/morpheus/api/security_group_rules_interface.rb +28 -37
  24. data/lib/morpheus/api/security_groups_interface.rb +39 -51
  25. data/lib/morpheus/api/servers_interface.rb +113 -115
  26. data/lib/morpheus/api/setup_interface.rb +31 -0
  27. data/lib/morpheus/api/task_sets_interface.rb +36 -38
  28. data/lib/morpheus/api/tasks_interface.rb +56 -69
  29. data/lib/morpheus/api/users_interface.rb +67 -76
  30. data/lib/morpheus/api/virtual_images_interface.rb +61 -61
  31. data/lib/morpheus/api/whoami_interface.rb +12 -15
  32. data/lib/morpheus/cli.rb +71 -60
  33. data/lib/morpheus/cli/accounts.rb +254 -315
  34. data/lib/morpheus/cli/alias_command.rb +219 -0
  35. data/lib/morpheus/cli/app_templates.rb +264 -272
  36. data/lib/morpheus/cli/apps.rb +608 -671
  37. data/lib/morpheus/cli/cli_command.rb +259 -21
  38. data/lib/morpheus/cli/cli_registry.rb +99 -14
  39. data/lib/morpheus/cli/clouds.rb +599 -372
  40. data/lib/morpheus/cli/config_file.rb +126 -0
  41. data/lib/morpheus/cli/credentials.rb +141 -117
  42. data/lib/morpheus/cli/dashboard_command.rb +48 -56
  43. data/lib/morpheus/cli/deployments.rb +254 -268
  44. data/lib/morpheus/cli/deploys.rb +150 -142
  45. data/lib/morpheus/cli/error_handler.rb +38 -0
  46. data/lib/morpheus/cli/groups.rb +551 -179
  47. data/lib/morpheus/cli/hosts.rb +862 -617
  48. data/lib/morpheus/cli/instance_types.rb +103 -95
  49. data/lib/morpheus/cli/instances.rb +1335 -1009
  50. data/lib/morpheus/cli/key_pairs.rb +82 -90
  51. data/lib/morpheus/cli/library.rb +498 -499
  52. data/lib/morpheus/cli/license.rb +83 -101
  53. data/lib/morpheus/cli/load_balancers.rb +314 -300
  54. data/lib/morpheus/cli/login.rb +66 -44
  55. data/lib/morpheus/cli/logout.rb +47 -46
  56. data/lib/morpheus/cli/mixins/accounts_helper.rb +69 -31
  57. data/lib/morpheus/cli/mixins/infrastructure_helper.rb +106 -0
  58. data/lib/morpheus/cli/mixins/print_helper.rb +181 -17
  59. data/lib/morpheus/cli/mixins/provisioning_helper.rb +535 -458
  60. data/lib/morpheus/cli/mixins/whoami_helper.rb +2 -2
  61. data/lib/morpheus/cli/option_parser.rb +35 -0
  62. data/lib/morpheus/cli/option_types.rb +232 -192
  63. data/lib/morpheus/cli/recent_activity_command.rb +61 -65
  64. data/lib/morpheus/cli/remote.rb +446 -199
  65. data/lib/morpheus/cli/roles.rb +884 -906
  66. data/lib/morpheus/cli/security_group_rules.rb +213 -203
  67. data/lib/morpheus/cli/security_groups.rb +237 -192
  68. data/lib/morpheus/cli/shell.rb +338 -231
  69. data/lib/morpheus/cli/tasks.rb +326 -308
  70. data/lib/morpheus/cli/users.rb +457 -462
  71. data/lib/morpheus/cli/version.rb +1 -1
  72. data/lib/morpheus/cli/version_command.rb +16 -18
  73. data/lib/morpheus/cli/virtual_images.rb +526 -345
  74. data/lib/morpheus/cli/whoami.rb +125 -111
  75. data/lib/morpheus/cli/workflows.rb +338 -185
  76. data/lib/morpheus/formatters.rb +8 -1
  77. data/lib/morpheus/logging.rb +1 -1
  78. data/lib/morpheus/rest_client.rb +17 -8
  79. metadata +9 -3
  80. data/lib/morpheus/api/custom_instance_types.rb +0 -55
data/lib/morpheus/cli.rb CHANGED
@@ -8,65 +8,76 @@ Dir[File.dirname(__FILE__) + "/ext/*.rb"].each {|file| require file }
8
8
 
9
9
  module Morpheus
10
10
  module Cli
11
- require 'morpheus/api/api_client'
12
- require 'morpheus/api/whoami_interface'
13
- require 'morpheus/api/options_interface'
14
- require 'morpheus/api/groups_interface'
15
- require 'morpheus/api/clouds_interface'
16
- require 'morpheus/api/servers_interface'
17
- require 'morpheus/api/tasks_interface'
18
- require 'morpheus/api/deployments_interface'
19
- require 'morpheus/api/license_interface'
20
- require 'morpheus/api/virtual_images_interface'
21
- require 'morpheus/api/task_sets_interface'
22
- require 'morpheus/api/load_balancers_interface'
23
- require 'morpheus/api/instances_interface'
24
- require 'morpheus/api/instance_types_interface'
25
- require 'morpheus/api/provision_types_interface'
26
- require 'morpheus/api/apps_interface'
27
- require 'morpheus/api/app_templates_interface'
28
- require 'morpheus/api/deploy_interface'
29
- require 'morpheus/api/security_groups_interface'
30
- require 'morpheus/api/security_group_rules_interface'
31
- require 'morpheus/api/accounts_interface'
32
- require 'morpheus/api/users_interface'
33
- require 'morpheus/api/logs_interface'
34
- require 'morpheus/api/roles_interface'
35
- require 'morpheus/api/key_pairs_interface'
36
- require 'morpheus/api/custom_instance_types_interface'
37
- require 'morpheus/api/dashboard_interface'
38
-
39
- require 'morpheus/cli/cli_command'
40
- require 'morpheus/cli/remote'
41
- require 'morpheus/cli/login'
42
- require 'morpheus/cli/logout'
43
- require 'morpheus/cli/whoami'
44
- require 'morpheus/cli/dashboard_command'
45
- require 'morpheus/cli/recent_activity_command'
46
- require 'morpheus/cli/credentials'
47
- require 'morpheus/cli/groups'
48
- require 'morpheus/cli/clouds'
49
- require 'morpheus/cli/hosts'
50
- require 'morpheus/cli/load_balancers'
51
- require 'morpheus/cli/shell'
52
- require 'morpheus/cli/tasks'
53
- require 'morpheus/cli/workflows'
54
- require 'morpheus/cli/deployments'
55
- require 'morpheus/cli/instances'
56
- require 'morpheus/cli/apps'
57
- require 'morpheus/cli/app_templates'
58
- require 'morpheus/cli/deploys'
59
- require 'morpheus/cli/license'
60
- require 'morpheus/cli/instance_types'
61
- require 'morpheus/cli/security_groups'
62
- require 'morpheus/cli/security_group_rules'
63
- require 'morpheus/cli/accounts'
64
- require 'morpheus/cli/users'
65
- require 'morpheus/cli/roles'
66
- require 'morpheus/cli/key_pairs'
67
- require 'morpheus/cli/virtual_images'
68
- require 'morpheus/cli/library'
69
- require 'morpheus/cli/version_command'
70
- # Your code goes here...
11
+
12
+ # the home directory, where morpheus-cli stores things
13
+ def self.home_directory
14
+ if ENV['MORPHEUS_CLI_HOME']
15
+ ENV['MORPHEUS_CLI_HOME']
16
+ else
17
+ File.join(Dir.home, ".morpheus")
18
+ end
19
+ end
20
+
21
+ # the location of your config file
22
+ # this is not configurable right now.
23
+ def self.config_filename
24
+ File.join(self.home_directory, ".morpheusrc")
25
+ end
26
+
27
+ # load all the well known commands and utilties they need
28
+ def self.load!()
29
+ # load interfaces
30
+ require 'morpheus/api/api_client.rb'
31
+ Dir[File.dirname(__FILE__) + "/api/*.rb"].each {|file| load file }
32
+
33
+ # load mixins
34
+ Dir[File.dirname(__FILE__) + "/cli/mixins/*.rb"].each {|file| load file }
35
+
36
+ # load commands
37
+ # Dir[File.dirname(__FILE__) + "/cli/*.rb"].each {|file| load file }
38
+
39
+ # utilites
40
+ load 'morpheus/cli/credentials.rb'
41
+ load 'morpheus/cli/cli_command.rb'
42
+ load 'morpheus/cli/option_types.rb'
43
+
44
+ # all the known commands
45
+ load 'morpheus/cli/remote.rb'
46
+ load 'morpheus/cli/login.rb'
47
+ load 'morpheus/cli/logout.rb'
48
+ load 'morpheus/cli/whoami.rb'
49
+ load 'morpheus/cli/dashboard_command.rb'
50
+ load 'morpheus/cli/recent_activity_command.rb'
51
+ load 'morpheus/cli/groups.rb'
52
+ load 'morpheus/cli/clouds.rb'
53
+ load 'morpheus/cli/hosts.rb'
54
+ load 'morpheus/cli/load_balancers.rb'
55
+ load 'morpheus/cli/shell.rb'
56
+ load 'morpheus/cli/tasks.rb'
57
+ load 'morpheus/cli/workflows.rb'
58
+ load 'morpheus/cli/deployments.rb'
59
+ load 'morpheus/cli/instances.rb'
60
+ load 'morpheus/cli/apps.rb'
61
+ load 'morpheus/cli/app_templates.rb'
62
+ load 'morpheus/cli/deploys.rb'
63
+ load 'morpheus/cli/license.rb'
64
+ load 'morpheus/cli/instance_types.rb'
65
+ load 'morpheus/cli/security_groups.rb'
66
+ load 'morpheus/cli/security_group_rules.rb'
67
+ load 'morpheus/cli/accounts.rb'
68
+ load 'morpheus/cli/users.rb'
69
+ load 'morpheus/cli/roles.rb'
70
+ load 'morpheus/cli/key_pairs.rb'
71
+ load 'morpheus/cli/virtual_images.rb'
72
+ load 'morpheus/cli/library.rb'
73
+ load 'morpheus/cli/version_command.rb'
74
+ load 'morpheus/cli/alias_command.rb'
75
+ # Your new commands goes here...
76
+
77
+ end
78
+
79
+ load!
80
+
71
81
  end
82
+
72
83
  end
@@ -10,320 +10,259 @@ require 'json'
10
10
  class Morpheus::Cli::Accounts
11
11
  include Morpheus::Cli::CliCommand
12
12
  include Morpheus::Cli::AccountsHelper
13
-
14
- def initialize()
15
- @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
16
- end
17
-
18
- def connect(opts)
19
- @access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials()
20
- if @access_token.empty?
21
- print_red_alert "Invalid Credentials. Unable to acquire access token. Please verify your credentials and try again."
22
- exit 1
23
- end
24
- @api_client = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url)
25
- @users_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).users
26
- @accounts_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).accounts
27
- @roles_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).roles
28
- end
29
-
30
- def handle(args)
31
- usage = "Usage: morpheus accounts [list,details,add,update,remove] [name]"
32
- if args.empty?
33
- puts "\n#{usage}\n\n"
34
- exit 1
35
- end
36
-
37
- case args[0]
38
- when 'list'
39
- list(args[1..-1])
40
- when 'details'
41
- details(args[1..-1])
42
- when 'add'
43
- add(args[1..-1])
44
- when 'update'
45
- update(args[1..-1])
46
- when 'remove'
47
- remove(args[1..-1])
48
- else
49
- puts "\n#{usage}\n\n"
50
- exit 127
51
- end
52
- end
53
-
54
- def list(args)
55
- usage = "Usage: morpheus accounts list"
56
- options = {}
57
- optparse = OptionParser.new do|opts|
58
- opts.banner = usage
59
- build_common_options(opts, options, [:list, :json])
60
- end
61
- optparse.parse(args)
62
- connect(options)
63
- begin
64
- params = {}
65
- [:phrase, :offset, :max, :sort, :direction].each do |k|
66
- params[k] = options[k] unless options[k].nil?
67
- end
68
-
69
- json_response = @accounts_interface.list(params)
70
- accounts = json_response['accounts']
71
- if options[:json]
72
- print JSON.pretty_generate(json_response)
73
- print "\n"
74
- else
75
- print "\n" ,cyan, bold, "Morpheus Accounts\n","==================", reset, "\n\n"
76
- if accounts.empty?
77
- puts yellow,"No accounts found.",reset
78
- else
79
- print_accounts_table(accounts)
80
- end
81
- print reset,"\n\n"
82
- end
83
- rescue RestClient::Exception => e
84
- print_rest_exception(e, options)
85
- exit 1
86
- end
87
- end
88
-
89
- def details(args)
90
- usage = "Usage: morpheus accounts details [name] [options]"
91
- options = {}
92
- optparse = OptionParser.new do|opts|
93
- opts.banner = usage
94
- build_common_options(opts, options, [:json])
95
- end
96
- optparse.parse(args)
97
-
98
- if args.count < 1
99
- puts "\n#{usage}\n\n"
100
- exit 1
101
- end
102
- name = args[0]
103
-
104
- connect(options)
105
- begin
106
-
107
- # todo: accounts_response = @accounts_interface.list({name: name})
108
- # there may be response data outside of account that needs to be displayed
109
-
110
- account = nil
111
- if name.to_s =~ /\Aid:/
112
- id = name.sub('id:', '')
113
- account = find_account_by_id(id)
114
- else
115
- account = find_account_by_name(name)
116
- end
117
- exit 1 if account.nil?
118
-
119
- if options[:json]
120
- print JSON.pretty_generate({account: account})
121
- print "\n"
122
- else
123
- print "\n" ,cyan, bold, "Account Details\n","==================", reset, "\n\n"
124
- print cyan
125
- puts "ID: #{account['id']}"
126
- puts "Name: #{account['name']}"
127
- puts "Description: #{account['description']}"
128
- puts "Currency: #{account['currency']}"
129
- # puts "# Users: #{account['usersCount']}"
130
- # puts "# Instances: #{account['instancesCount']}"
131
- puts "Date Created: #{format_local_dt(account['dateCreated'])}"
132
- puts "Last Updated: #{format_local_dt(account['lastUpdated'])}"
133
- status_state = nil
134
- if account['active']
135
- status_state = "#{green}ACTIVE#{cyan}"
136
- else
137
- status_state = "#{red}INACTIVE#{cyan}"
138
- end
139
- puts "Status: #{status_state}"
140
- print "\n" ,cyan, bold, "Account Instance Limits\n","==================", reset, "\n\n"
141
- print cyan
142
- puts "Max Storage (bytes): #{account['instanceLimits'] ? account['instanceLimits']['maxStorage'] : 0}"
143
- puts "Max Memory (bytes): #{account['instanceLimits'] ? account['instanceLimits']['maxMemory'] : 0}"
144
- puts "CPU Count: #{account['instanceLimits'] ? account['instanceLimits']['maxCpu'] : 0}"
145
- print cyan
146
- print reset,"\n\n"
147
- end
148
- rescue RestClient::Exception => e
149
- print_rest_exception(e, options)
150
- exit 1
151
- end
152
- end
153
-
154
- def add(args)
155
- usage = "Usage: morpheus accounts add [options]"
156
- options = {}
157
- optparse = OptionParser.new do|opts|
158
- opts.banner = usage
159
- build_common_options(opts, options, [:options, :json])
160
- end
161
- optparse.parse(args)
162
-
163
- connect(options)
164
-
165
- begin
166
-
167
- params = Morpheus::Cli::OptionTypes.prompt(add_account_option_types, options[:options], @api_client, options[:params])
168
-
169
- #puts "parsed params is : #{params.inspect}"
170
- account_keys = ['name', 'description', 'currency']
171
- account_payload = params.select {|k,v| account_keys.include?(k) }
172
- account_payload['currency'] = account_payload['currency'].to_s.empty? ? "USD" : account_payload['currency'].upcase
173
- if !account_payload['instanceLimits']
174
- account_payload['instanceLimits'] = {}
175
- account_payload['instanceLimits']['maxStorage'] = params['instanceLimits.maxStorage'].to_i if params['instanceLimits.maxStorage'].to_s.strip != ''
176
- account_payload['instanceLimits']['maxMemory'] = params['instanceLimits.maxMemory'].to_i if params['instanceLimits.maxMemory'].to_s.strip != ''
177
- account_payload['instanceLimits']['maxCpu'] = params['instanceLimits.maxCpu'].to_i if params['instanceLimits.maxCpu'].to_s.strip != ''
178
- end
179
- if params['role'].to_s != ''
180
- role = find_role_by_name(nil, params['role'])
181
- exit 1 if role.nil?
182
- account_payload['role'] = {id: role['id']}
183
- end
184
- request_payload = {account: account_payload}
185
- json_response = @accounts_interface.create(request_payload)
186
-
187
- if options[:json]
188
- print JSON.pretty_generate(json_response)
189
- print "\n"
190
- else
191
- print_green_success "Account #{account_payload['name']} added"
192
- details([account_payload["name"]])
193
- end
194
-
195
- rescue RestClient::Exception => e
196
- print_rest_exception(e, options)
197
- exit 1
198
- end
199
- end
200
-
201
- def update(args)
202
- usage = "Usage: morpheus accounts update [name] [options]"
203
- options = {}
204
- optparse = OptionParser.new do|opts|
205
- opts.banner = usage
206
- build_common_options(opts, options, [:options, :json])
207
- end
208
- optparse.parse(args)
209
-
210
- if args.count < 1
211
- puts "\n#{usage}\n\n"
212
- exit 1
213
- end
214
- name = args[0].strip
215
-
216
- connect(options)
217
-
218
- begin
219
-
220
- account = nil
221
- if name.to_s =~ /\Aid:/
222
- id = name.sub('id:', '').strip
223
- account = find_account_by_id(id)
224
- else
225
- account = find_account_by_name(name)
226
- end
227
- exit 1 if account.nil?
228
-
229
- #params = Morpheus::Cli::OptionTypes.prompt(update_account_option_types, options[:options], @api_client, options[:params])
230
- params = options[:options] || {}
231
-
232
- if params.empty?
233
- puts "\n#{usage}\n\n"
234
- option_lines = update_account_option_types.collect {|it| "\t-O #{it['fieldName']}=\"value\"" }.join("\n")
235
- puts "\nAvailable Options:\n#{option_lines}\n\n"
236
- exit 1
237
- end
238
-
239
- #puts "parsed params is : #{params.inspect}"
240
- account_keys = ['name', 'description', 'currency', 'instanceLimits']
241
- account_payload = params.select {|k,v| account_keys.include?(k) }
242
- account_payload['currency'] = account_payload['currency'].upcase unless account_payload['currency'].to_s.empty?
243
- if !account_payload['instanceLimits']
244
- account_payload['instanceLimits'] = {}
245
- account_payload['instanceLimits']['maxStorage'] = params['instanceLimits.maxStorage'].to_i if params['instanceLimits.maxStorage'].to_s.strip != ''
246
- account_payload['instanceLimits']['maxMemory'] = params['instanceLimits.maxMemory'].to_i if params['instanceLimits.maxMemory'].to_s.strip != ''
247
- account_payload['instanceLimits']['maxCpu'] = params['instanceLimits.maxCpu'].to_i if params['instanceLimits.maxCpu'].to_s.strip != ''
248
- end
249
- if params['role'].to_s != ''
250
- role = find_role_by_name(nil, params['role'])
251
- exit 1 if role.nil?
252
- account_payload['role'] = {id: role['id']}
253
- end
254
- request_payload = {account: account_payload}
255
- json_response = @accounts_interface.update(account['id'], request_payload)
256
-
257
- if options[:json]
258
- print JSON.pretty_generate(json_response)
259
- print "\n"
260
- else
261
- account_name = account_payload['name'] || account['name']
262
- print_green_success "Account #{account_name} updated"
263
- details([account_name])
264
- end
265
-
266
- rescue RestClient::Exception => e
267
- print_rest_exception(e, options)
268
- exit 1
269
- end
270
- end
271
-
272
- def remove(args)
273
- usage = "Usage: morpheus accounts remove [name]"
274
- options = {}
275
- optparse = OptionParser.new do|opts|
276
- opts.banner = usage
277
- build_common_options(opts, options, [:auto_confirm, :json])
278
- end
279
- optparse.parse(args)
280
-
281
- if args.count < 1
282
- puts "\n#{usage}\n\n"
283
- exit 1
284
- end
285
- name = args[0]
286
-
287
- connect(options)
288
- begin
289
- # allow finding by ID since name is not unique!
290
- account = ((name.to_s =~ /\A\d{1,}\Z/) ? find_account_by_id(name) : find_account_by_name(name) )
291
- exit 1 if account.nil?
292
- unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the account #{account['name']}?")
293
- exit
294
- end
295
- json_response = @accounts_interface.destroy(account['id'])
296
-
297
- if options[:json]
298
- print JSON.pretty_generate(json_response)
299
- print "\n"
300
- else
301
- print_green_success "Account #{account['name']} removed"
302
- end
303
-
304
- rescue RestClient::Exception => e
305
- print_rest_exception(e, options)
306
- exit 1
307
- end
308
- end
309
-
310
- private
311
-
312
-
313
- def add_account_option_types
314
- [
315
- {'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'displayOrder' => 1},
316
- {'fieldName' => 'description', 'fieldLabel' => 'Description', 'type' => 'text', 'displayOrder' => 2},
317
- {'fieldName' => 'role', 'fieldLabel' => 'Base Role', 'type' => 'text', 'displayOrder' => 3},
318
- {'fieldName' => 'currency', 'fieldLabel' => 'Currency', 'type' => 'text', 'displayOrder' => 4},
319
- {'fieldName' => 'instanceLimits.maxStorage', 'fieldLabel' => 'Max Storage (bytes)', 'type' => 'text', 'displayOrder' => 5},
320
- {'fieldName' => 'instanceLimits.maxMemory', 'fieldLabel' => 'Max Memory (bytes)', 'type' => 'text', 'displayOrder' => 6},
321
- {'fieldName' => 'instanceLimits.maxCpu', 'fieldLabel' => 'CPU Count', 'type' => 'text', 'displayOrder' => 7},
322
- ]
323
- end
324
-
325
- def update_account_option_types
326
- add_account_option_types
327
- end
13
+ register_subcommands :list, :get, :add, :update, :remove
14
+ alias_subcommand :details, :get
15
+ set_default_subcommand :list
16
+
17
+ def initialize()
18
+ @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
19
+ end
20
+
21
+ def connect(opts)
22
+ @api_client = establish_remote_appliance_connection(opts)
23
+ @users_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).users
24
+ @accounts_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).accounts
25
+ @roles_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).roles
26
+ end
27
+
28
+ def handle(args)
29
+ handle_subcommand(args)
30
+ end
31
+
32
+ def list(args)
33
+ options = {}
34
+ optparse = OptionParser.new do|opts|
35
+ opts.banner = subcommand_usage()
36
+ build_common_options(opts, options, [:list, :json])
37
+ end
38
+ optparse.parse!(args)
39
+ connect(options)
40
+ begin
41
+ params = {}
42
+ [:phrase, :offset, :max, :sort, :direction].each do |k|
43
+ params[k] = options[k] unless options[k].nil?
44
+ end
45
+
46
+ json_response = @accounts_interface.list(params)
47
+ accounts = json_response['accounts']
48
+ if options[:json]
49
+ print JSON.pretty_generate(json_response)
50
+ print "\n"
51
+ else
52
+ print "\n" ,cyan, bold, "Morpheus Accounts\n","==================", reset, "\n\n"
53
+ if accounts.empty?
54
+ puts yellow,"No accounts found.",reset
55
+ else
56
+ print_accounts_table(accounts)
57
+ print_results_pagination(json_response)
58
+ end
59
+ print reset,"\n"
60
+ end
61
+ rescue RestClient::Exception => e
62
+ print_rest_exception(e, options)
63
+ exit 1
64
+ end
65
+ end
66
+
67
+ def get(args)
68
+ options = {}
69
+ optparse = OptionParser.new do|opts|
70
+ opts.banner = subcommand_usage("[name]")
71
+ build_common_options(opts, options, [:json])
72
+ end
73
+ optparse.parse!(args)
74
+ if args.count < 1
75
+ puts optparse
76
+ exit 1
77
+ end
78
+ connect(options)
79
+ begin
80
+
81
+ account = find_account_by_name_or_id(args[0])
82
+ exit 1 if account.nil?
83
+
84
+ if options[:json]
85
+ print JSON.pretty_generate({account: account})
86
+ print "\n"
87
+ else
88
+ print "\n" ,cyan, bold, "Account Details\n","==================", reset, "\n\n"
89
+ print cyan
90
+ puts "ID: #{account['id']}"
91
+ puts "Name: #{account['name']}"
92
+ puts "Description: #{account['description']}"
93
+ puts "Currency: #{account['currency']}"
94
+ # puts "# Users: #{account['usersCount']}"
95
+ # puts "# Instances: #{account['instancesCount']}"
96
+ puts "Date Created: #{format_local_dt(account['dateCreated'])}"
97
+ puts "Last Updated: #{format_local_dt(account['lastUpdated'])}"
98
+ status_state = nil
99
+ if account['active']
100
+ status_state = "#{green}ACTIVE#{cyan}"
101
+ else
102
+ status_state = "#{red}INACTIVE#{cyan}"
103
+ end
104
+ puts "Status: #{status_state}"
105
+ print "\n" ,cyan, bold, "Account Instance Limits\n","==================", reset, "\n\n"
106
+ print cyan
107
+ puts "Max Storage (bytes): #{account['instanceLimits'] ? account['instanceLimits']['maxStorage'] : 0}"
108
+ puts "Max Memory (bytes): #{account['instanceLimits'] ? account['instanceLimits']['maxMemory'] : 0}"
109
+ puts "CPU Count: #{account['instanceLimits'] ? account['instanceLimits']['maxCpu'] : 0}"
110
+ print reset,"\n"
111
+ end
112
+ rescue RestClient::Exception => e
113
+ print_rest_exception(e, options)
114
+ exit 1
115
+ end
116
+ end
117
+
118
+ def add(args)
119
+ options = {}
120
+ optparse = OptionParser.new do|opts|
121
+ opts.banner = subcommand_usage("[options]")
122
+ build_common_options(opts, options, [:options, :json])
123
+ end
124
+ optparse.parse!(args)
125
+ connect(options)
126
+ begin
127
+ params = Morpheus::Cli::OptionTypes.prompt(add_account_option_types, options[:options], @api_client, options[:params])
128
+ #puts "parsed params is : #{params.inspect}"
129
+ account_keys = ['name', 'description', 'currency']
130
+ account_payload = params.select {|k,v| account_keys.include?(k) }
131
+ account_payload['currency'] = account_payload['currency'].to_s.empty? ? "USD" : account_payload['currency'].upcase
132
+ if !account_payload['instanceLimits']
133
+ account_payload['instanceLimits'] = {}
134
+ account_payload['instanceLimits']['maxStorage'] = params['instanceLimits.maxStorage'].to_i if params['instanceLimits.maxStorage'].to_s.strip != ''
135
+ account_payload['instanceLimits']['maxMemory'] = params['instanceLimits.maxMemory'].to_i if params['instanceLimits.maxMemory'].to_s.strip != ''
136
+ account_payload['instanceLimits']['maxCpu'] = params['instanceLimits.maxCpu'].to_i if params['instanceLimits.maxCpu'].to_s.strip != ''
137
+ end
138
+ if params['role'].to_s != ''
139
+ role = find_role_by_name(nil, params['role'])
140
+ exit 1 if role.nil?
141
+ account_payload['role'] = {id: role['id']}
142
+ end
143
+ request_payload = {account: account_payload}
144
+ json_response = @accounts_interface.create(request_payload)
145
+ if options[:json]
146
+ print JSON.pretty_generate(json_response)
147
+ print "\n"
148
+ else
149
+ print_green_success "Account #{account_payload['name']} added"
150
+ get([account_payload["name"]])
151
+ end
152
+
153
+ rescue RestClient::Exception => e
154
+ print_rest_exception(e, options)
155
+ exit 1
156
+ end
157
+ end
158
+
159
+ def update(args)
160
+ options = {}
161
+ optparse = OptionParser.new do|opts|
162
+ opts.banner = subcommand_usage("[name] [options]")
163
+ build_common_options(opts, options, [:options, :json])
164
+ end
165
+ optparse.parse!(args)
166
+ if args.count < 1
167
+ puts optparse
168
+ exit 1
169
+ end
170
+ connect(options)
171
+ begin
172
+ account = find_account_by_name_or_id(args[0])
173
+ exit 1 if account.nil?
174
+
175
+ #params = Morpheus::Cli::OptionTypes.prompt(update_account_option_types, options[:options], @api_client, options[:params])
176
+ params = options[:options] || {}
177
+
178
+ if params.empty?
179
+ puts optparse
180
+ option_lines = update_account_option_types.collect {|it| "\t-O #{it['fieldName']}=\"value\"" }.join("\n")
181
+ puts "\nAvailable Options:\n#{option_lines}\n\n"
182
+ exit 1
183
+ end
184
+
185
+ #puts "parsed params is : #{params.inspect}"
186
+ account_keys = ['name', 'description', 'currency', 'instanceLimits']
187
+ account_payload = params.select {|k,v| account_keys.include?(k) }
188
+ account_payload['currency'] = account_payload['currency'].upcase unless account_payload['currency'].to_s.empty?
189
+ if !account_payload['instanceLimits']
190
+ account_payload['instanceLimits'] = {}
191
+ account_payload['instanceLimits']['maxStorage'] = params['instanceLimits.maxStorage'].to_i if params['instanceLimits.maxStorage'].to_s.strip != ''
192
+ account_payload['instanceLimits']['maxMemory'] = params['instanceLimits.maxMemory'].to_i if params['instanceLimits.maxMemory'].to_s.strip != ''
193
+ account_payload['instanceLimits']['maxCpu'] = params['instanceLimits.maxCpu'].to_i if params['instanceLimits.maxCpu'].to_s.strip != ''
194
+ end
195
+ if params['role'].to_s != ''
196
+ role = find_role_by_name(nil, params['role'])
197
+ exit 1 if role.nil?
198
+ account_payload['role'] = {id: role['id']}
199
+ end
200
+ request_payload = {account: account_payload}
201
+ json_response = @accounts_interface.update(account['id'], request_payload)
202
+
203
+ if options[:json]
204
+ print JSON.pretty_generate(json_response)
205
+ print "\n"
206
+ else
207
+ account_name = account_payload['name'] || account['name']
208
+ print_green_success "Account #{account_name} updated"
209
+ get([account_name])
210
+ end
211
+ rescue RestClient::Exception => e
212
+ print_rest_exception(e, options)
213
+ exit 1
214
+ end
215
+ end
216
+
217
+ def remove(args)
218
+ options = {}
219
+ optparse = OptionParser.new do|opts|
220
+ opts.banner = subcommand_usage("[name]")
221
+ build_common_options(opts, options, [:auto_confirm, :json])
222
+ end
223
+ optparse.parse!(args)
224
+ if args.count < 1
225
+ puts optparse
226
+ exit 1
227
+ end
228
+ connect(options)
229
+ begin
230
+ # allow finding by ID since name is not unique!
231
+ account = find_account_by_name_or_id(args[0])
232
+ exit 1 if account.nil?
233
+ unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the account #{account['name']}?")
234
+ exit
235
+ end
236
+ json_response = @accounts_interface.destroy(account['id'])
237
+ if options[:json]
238
+ print JSON.pretty_generate(json_response)
239
+ print "\n"
240
+ else
241
+ print_green_success "Account #{account['name']} removed"
242
+ end
243
+
244
+ rescue RestClient::Exception => e
245
+ print_rest_exception(e, options)
246
+ exit 1
247
+ end
248
+ end
249
+
250
+ private
251
+
252
+ def add_account_option_types
253
+ [
254
+ {'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'displayOrder' => 1},
255
+ {'fieldName' => 'description', 'fieldLabel' => 'Description', 'type' => 'text', 'displayOrder' => 2},
256
+ {'fieldName' => 'role', 'fieldLabel' => 'Base Role', 'type' => 'text', 'displayOrder' => 3},
257
+ {'fieldName' => 'currency', 'fieldLabel' => 'Currency', 'type' => 'text', 'displayOrder' => 4},
258
+ {'fieldName' => 'instanceLimits.maxStorage', 'fieldLabel' => 'Max Storage (bytes)', 'type' => 'text', 'displayOrder' => 5},
259
+ {'fieldName' => 'instanceLimits.maxMemory', 'fieldLabel' => 'Max Memory (bytes)', 'type' => 'text', 'displayOrder' => 6},
260
+ {'fieldName' => 'instanceLimits.maxCpu', 'fieldLabel' => 'CPU Count', 'type' => 'text', 'displayOrder' => 7},
261
+ ]
262
+ end
263
+
264
+ def update_account_option_types
265
+ add_account_option_types
266
+ end
328
267
 
329
268
  end