morpheus-cli 4.1.12 → 4.1.13

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.
@@ -135,7 +135,7 @@ module Morpheus::Cli::AccountsHelper
135
135
 
136
136
  def find_user_by_id(account_id, id)
137
137
  begin
138
- json_response = users_interface.get(account_id, id.to_i)
138
+ json_response = users_interface.get(account_id, id.to_i, {includePermissions:true})
139
139
  return json_response['user']
140
140
  rescue RestClient::Exception => e
141
141
  if e.response && e.response.code == 404
@@ -669,6 +669,9 @@ class Morpheus::Cli::Tasks
669
669
  end
670
670
  opts.add_hidden_option('--server')
671
671
  opts.add_hidden_option('--servers')
672
+ opts.on('-a', '--appliance', "Execute on the appliance, the target is the appliance itself.") do
673
+ target_type = 'appliance'
674
+ end
672
675
  opts.on('--config [TEXT]', String, "Custom config") do |val|
673
676
  params['customConfig'] = val.to_s
674
677
  end
@@ -706,6 +709,8 @@ class Morpheus::Cli::Tasks
706
709
  servers << server
707
710
  end
708
711
  params['servers'] = servers.collect {|it| it['id'] }
712
+ elsif target_type == 'appliance'
713
+ # cool, run it locally.
709
714
  else
710
715
  raise_command_error "missing required option: --instance or --host\n#{optparse}"
711
716
  end
@@ -728,24 +733,28 @@ class Morpheus::Cli::Tasks
728
733
  print_dry_run @tasks_interface.dry.run(task['id'], payload)
729
734
  return
730
735
  end
731
- response = @tasks_interface.run(task['id'], payload)
736
+ json_response = @tasks_interface.run(task['id'], payload)
732
737
  if options[:json]
733
- print JSON.pretty_generate(json_response)
734
- if !response['success']
735
- return 1
736
- end
738
+ puts as_json(json_response, options)
739
+ return json_response['success'] ? 0 : 1
737
740
  else
738
741
  target_desc = ""
739
742
  if instances.size() > 0
740
743
  target_desc = (instances.size() == 1) ? "instance #{instances[0]['name']}" : "#{instances.size()} instances"
741
744
  elsif servers.size() > 0
742
745
  target_desc = (servers.size() == 1) ? "host #{servers[0]['name']}" : "#{servers.size()} hosts"
746
+ elsif target_type == 'appliance'
747
+ target_desc = "appliance"
743
748
  end
744
749
  print_green_success "Executing task #{task['name']} on #{target_desc}"
745
- # todo: load job/execution
746
- # get([task['id']])
750
+ # todo: refresh, use get processId and load process record isntead? err
751
+ if json_response["jobExecution"] && json_response["jobExecution"]["id"]
752
+ get_args = [json_response["jobExecution"]["id"], "--details"] + (options[:remote] ? ["-r",options[:remote]] : [])
753
+ Morpheus::Logging::DarkPrinter.puts((['jobs', 'get-execution'] + get_args).join(' ')) if Morpheus::Logging.debug?
754
+ return ::Morpheus::Cli::JobsCommand.new.handle(['get-execution'] + get_args)
755
+ end
756
+ return json_response['success'] ? 0 : 1
747
757
  end
748
- return 0
749
758
  rescue RestClient::Exception => e
750
759
  print_rest_exception(e, options)
751
760
  return 1
@@ -10,7 +10,7 @@ require 'json'
10
10
  class Morpheus::Cli::Users
11
11
  include Morpheus::Cli::CliCommand
12
12
  include Morpheus::Cli::AccountsHelper
13
- register_subcommands :list, :count, :get, :add, :update, :remove
13
+ register_subcommands :list, :count, :get, :add, :update, :remove, :permissions
14
14
  register_subcommands :'passwd' => :change_password
15
15
  alias_subcommand :details, :get
16
16
  set_default_subcommand :list
@@ -127,7 +127,7 @@ class Morpheus::Cli::Users
127
127
  # opts.on(nil,'--instance-type-access', "Display Instance Type Access") do
128
128
  # options[:include_instance_type_access] = true
129
129
  # end
130
- opts.on(nil,'--all-access', "Display All Access Lists") do
130
+ opts.on(nil,'--all', "Display All Access Lists") do
131
131
  options[:include_feature_access] = true
132
132
  options[:include_group_access] = true
133
133
  options[:include_cloud_access] = true
@@ -151,13 +151,13 @@ class Morpheus::Cli::Users
151
151
  @users_interface.setopts(options)
152
152
  if options[:dry_run]
153
153
  if args[0].to_s =~ /\A\d{1,}\Z/
154
- print_dry_run @users_interface.dry.get(account_id, args[0].to_i)
154
+ print_dry_run @users_interface.dry.get(account_id, args[0].to_i, {includePermissions:true})
155
155
  else
156
156
  print_dry_run @users_interface.dry.get(account_id, {username: args[0]})
157
157
  end
158
- if options[:include_feature_access]
159
- print_dry_run @users_interface.dry.feature_permissions(account_id, ":id")
160
- end
158
+ # if options[:include_feature_access]
159
+ # print_dry_run @users_interface.dry.feature_permissions(account_id, ":id")
160
+ # end
161
161
  return
162
162
  end
163
163
  # todo: users_response = @users_interface.list(account_id, {name: name})
@@ -169,11 +169,11 @@ class Morpheus::Cli::Users
169
169
  # json_response['user']['featurePermissions'] = user_feature_permissions if options[:include_feature_access]
170
170
  if options[:json]
171
171
  puts as_json(json_response, options, "user")
172
- puts as_json(@users_interface.feature_permissions(account_id, user['id']), options) if options[:include_feature_access]
172
+ #puts as_json(@users_interface.feature_permissions(account_id, user['id']), options) if options[:include_feature_access]
173
173
  return 0
174
174
  elsif options[:yaml]
175
175
  puts as_yaml(json_response, options, "user")
176
- puts as_yaml(@users_interface.feature_permissions(account_id, user['id']), options) if options[:include_feature_access]
176
+ #puts as_yaml(@users_interface.feature_permissions(account_id, user['id']), options) if options[:include_feature_access]
177
177
  return 0
178
178
  elsif options[:csv]
179
179
  puts records_as_csv([user], options)
@@ -183,7 +183,8 @@ class Morpheus::Cli::Users
183
183
  user_feature_permissions = nil
184
184
  if options[:include_feature_access]
185
185
  user_feature_permissions_json = @users_interface.feature_permissions(account_id, user['id'])
186
- user_feature_permissions = user_feature_permissions_json['featurePermissions']
186
+ # permissions (Array) has replaced featurePermissions (map)
187
+ user_feature_permissions = user_feature_permissions_json['permissions'] || user_feature_permissions_json['featurePermissions']
187
188
  end
188
189
  print_h1 "User Details", options
189
190
  print cyan
@@ -207,10 +208,18 @@ class Morpheus::Cli::Users
207
208
  if user_feature_permissions
208
209
  print_h2 "Feature Permissions", options
209
210
  print cyan
210
- rows = user_feature_permissions.collect do |code, access|
211
- {code: code, access: get_access_string(access) }
211
+ if user_feature_permissions.is_a?(Array)
212
+ rows = user_feature_permissions.collect do |it|
213
+ {name: it['name'], code: it['code'], access: get_access_string(it['access']) }
214
+ end
215
+ print as_pretty_table(rows, [:name, :code, :access], options)
216
+ else
217
+ rows = user_feature_permissions.collect do |code, access|
218
+ {code: code, access: get_access_string(access) }
219
+ end
220
+ print as_pretty_table(rows, [:code, :access], options)
212
221
  end
213
- print as_pretty_table(rows, [:name, :access], options)
222
+
214
223
  else
215
224
  puts yellow,"No permissions found.",reset
216
225
  end
@@ -225,6 +234,75 @@ class Morpheus::Cli::Users
225
234
  end
226
235
  end
227
236
 
237
+ def permissions(args)
238
+ options = {}
239
+ optparse = Morpheus::Cli::OptionParser.new do |opts|
240
+ opts.banner = subcommand_usage("[username]")
241
+ build_common_options(opts, options, [:account, :json, :yaml, :csv, :fields, :dry_run, :remote])
242
+ opts.footer = "Display Permissions for a user." + "\n" +
243
+ "[username] is required. This is the username or id of a user."
244
+ end
245
+ optparse.parse!(args)
246
+
247
+ if args.count < 1
248
+ puts optparse
249
+ return 1
250
+ end
251
+
252
+ connect(options)
253
+ begin
254
+ account = find_account_from_options(options)
255
+ account_id = account ? account['id'] : nil
256
+ user = find_user_by_username_or_id(account_id, args[0])
257
+ return 1 if user.nil?
258
+ @users_interface.setopts(options)
259
+ if options[:dry_run]
260
+ print_dry_run @users_interface.dry.feature_permissions(account_id, user['id'])
261
+ return
262
+ end
263
+
264
+ json_response = @users_interface.feature_permissions(account_id, user['id'])
265
+ # json_response['user']['featurePermissions'] = user_feature_permissions if options[:include_feature_access]
266
+ if options[:json]
267
+ puts as_json(json_response, options, 'permissions')
268
+ return 0
269
+ elsif options[:yaml]
270
+ puts as_yaml(json_response, options, 'permissions')
271
+ return 0
272
+ elsif options[:csv]
273
+ puts records_as_csv(json_response['permissions'], options)
274
+ return 0
275
+ else
276
+ user_feature_permissions = nil
277
+ # permissions (Array) has replaced featurePermissions (map)
278
+ user_feature_permissions = json_response['permissions'] || json_response['featurePermissions']
279
+ print_h1 "User Permissions: #{user['username']}", options
280
+ if user_feature_permissions
281
+ print cyan
282
+ if user_feature_permissions.is_a?(Array)
283
+ rows = user_feature_permissions.collect do |it|
284
+ {name: it['name'], code: it['code'], access: get_access_string(it['access']) }
285
+ end
286
+ print as_pretty_table(rows, [:name, :code, :access], options)
287
+ else
288
+ rows = user_feature_permissions.collect do |code, access|
289
+ {code: code, access: get_access_string(access) }
290
+ end
291
+ print as_pretty_table(rows, [:code, :access], options)
292
+ end
293
+
294
+ else
295
+ print yellow,"No permissions found.",reset,"\n"
296
+ end
297
+ print reset,"\n"
298
+ return 0
299
+ end
300
+ rescue RestClient::Exception => e
301
+ print_rest_exception(e, options)
302
+ return 1
303
+ end
304
+ end
305
+
228
306
  def add(args)
229
307
  options = {}
230
308
  params = {}
@@ -1,6 +1,6 @@
1
1
 
2
2
  module Morpheus
3
3
  module Cli
4
- VERSION = "4.1.12"
4
+ VERSION = "4.1.13"
5
5
  end
6
6
  end
@@ -639,24 +639,28 @@ class Morpheus::Cli::Workflows
639
639
  print_dry_run @task_sets_interface.dry.run(workflow['id'], payload)
640
640
  return 0
641
641
  end
642
- response = @task_sets_interface.run(workflow['id'], payload)
642
+ json_response = @task_sets_interface.run(workflow['id'], payload)
643
643
  if options[:json]
644
- print JSON.pretty_generate(json_response)
645
- if !response['success']
646
- return 1
647
- end
644
+ puts as_json(json_response, options)
645
+ return json_response['success'] ? 0 : 1
648
646
  else
649
647
  target_desc = ""
650
648
  if instances.size() > 0
651
649
  target_desc = (instances.size() == 1) ? "instance #{instances[0]['name']}" : "#{instances.size()} instances"
652
650
  elsif servers.size() > 0
653
651
  target_desc = (servers.size() == 1) ? "host #{servers[0]['name']}" : "#{servers.size()} hosts"
652
+ elsif target_type == 'appliance'
653
+ target_desc = "appliance"
654
654
  end
655
655
  print_green_success "Executing workflow #{workflow['name']} on #{target_desc}"
656
- # todo: load job/execution
657
- # get([workflow['id']])
656
+ # todo: refresh, use get processId and load process record isntead? err
657
+ if json_response["jobExecution"] && json_response["jobExecution"]["id"]
658
+ get_args = [json_response["jobExecution"]["id"], "--details"] + (options[:remote] ? ["-r",options[:remote]] : [])
659
+ Morpheus::Logging::DarkPrinter.puts((['jobs', 'get-execution'] + get_args).join(' ')) if Morpheus::Logging.debug?
660
+ return ::Morpheus::Cli::JobsCommand.new.handle(['get-execution'] + get_args)
661
+ end
662
+ return json_response['success'] ? 0 : 1
658
663
  end
659
- return 0
660
664
  rescue RestClient::Exception => e
661
665
  print_rest_exception(e, options)
662
666
  return 1
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: morpheus-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.12
4
+ version: 4.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Estes
@@ -219,7 +219,8 @@ files:
219
219
  - lib/morpheus/api/library_container_upgrades_interface.rb
220
220
  - lib/morpheus/api/library_instance_types_interface.rb
221
221
  - lib/morpheus/api/library_layouts_interface.rb
222
- - lib/morpheus/api/library_resource_specs_interface.rb
222
+ - lib/morpheus/api/library_spec_template_types_interface.rb
223
+ - lib/morpheus/api/library_spec_templates_interface.rb
223
224
  - lib/morpheus/api/license_interface.rb
224
225
  - lib/morpheus/api/load_balancers_interface.rb
225
226
  - lib/morpheus/api/log_settings_interface.rb
@@ -349,7 +350,7 @@ files:
349
350
  - lib/morpheus/cli/library_layouts_command.rb
350
351
  - lib/morpheus/cli/library_option_lists_command.rb
351
352
  - lib/morpheus/cli/library_option_types_command.rb
352
- - lib/morpheus/cli/library_resource_specs_command.rb
353
+ - lib/morpheus/cli/library_spec_templates_command.rb
353
354
  - lib/morpheus/cli/library_upgrades_command.rb
354
355
  - lib/morpheus/cli/license.rb
355
356
  - lib/morpheus/cli/load_balancers.rb
@@ -438,7 +439,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
438
439
  - !ruby/object:Gem::Version
439
440
  version: '0'
440
441
  requirements: []
441
- rubygems_version: 3.0.6
442
+ rubyforge_project:
443
+ rubygems_version: 2.7.6
442
444
  signing_key:
443
445
  specification_version: 4
444
446
  summary: Provides CLI Interface to the Morpheus Public/Private Cloud Appliance
@@ -1,409 +0,0 @@
1
- require 'morpheus/cli/cli_command'
2
-
3
- class Morpheus::Cli::LibraryResourceSpecsCommand
4
- include Morpheus::Cli::CliCommand
5
- set_command_hidden # hidden until complete
6
- set_command_name :'library-spec-templates'
7
-
8
- register_subcommands :list, :get, :add, :update, :remove
9
-
10
- def connect(opts)
11
- @api_client = establish_remote_appliance_connection(opts)
12
- @resource_specs_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).library_resource_specs
13
- end
14
-
15
- def handle(args)
16
- handle_subcommand(args)
17
- end
18
-
19
- def list(args)
20
- options = {}
21
- params = {}
22
- optparse = Morpheus::Cli::OptionParser.new do |opts|
23
- opts.banner = subcommand_usage()
24
- build_common_options(opts, options, [:list, :query, :json, :yaml, :csv, :fields, :dry_run, :remote])
25
- opts.footer = "List resource spec templates."
26
- end
27
- optparse.parse!(args)
28
- connect(options)
29
- if args.count > 0
30
- print_error Morpheus::Terminal.angry_prompt
31
- puts_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args.inspect}\n#{optparse}"
32
- return 1
33
- end
34
- begin
35
- # construct payload
36
- params.merge!(parse_list_options(options))
37
- @resource_specs_interface.setopts(options)
38
- if options[:dry_run]
39
- print_dry_run @resource_specs_interface.dry.list(params)
40
- return
41
- end
42
- # do it
43
- json_response = @resource_specs_interface.list(params)
44
- render_result = render_with_format(json_response, options, 'specTemplates')
45
- return 0 if render_result
46
- resource_specs = json_response['specTemplates']
47
- title = "Morpheus Library - Resource Spec Templates"
48
- subtitles = []
49
- subtitles += parse_list_subtitles(options)
50
- print_h1 title, subtitles
51
- if resource_specs.empty?
52
- print cyan,"No resource specs found.",reset,"\n"
53
- else
54
- print_resource_specs_table(resource_specs, options)
55
- print_results_pagination(json_response)
56
- end
57
- print reset,"\n"
58
- rescue RestClient::Exception => e
59
- print_rest_exception(e, options)
60
- return 1
61
- end
62
- end
63
-
64
- def get(args)
65
- options = {}
66
- optparse = Morpheus::Cli::OptionParser.new do |opts|
67
- opts.banner = subcommand_usage("[name]")
68
- build_common_options(opts, options, [:json, :yaml, :csv, :fields, :dry_run, :remote])
69
- end
70
- optparse.parse!(args)
71
- if args.count < 1
72
- puts optparse
73
- return 1
74
- end
75
- connect(options)
76
- id_list = parse_id_list(args)
77
- return run_command_for_each_arg(id_list) do |arg|
78
- _get(arg, options)
79
- end
80
- end
81
-
82
- def _get(id, options)
83
-
84
- begin
85
- resource_spec = find_resource_spec_by_name_or_id(id)
86
- if resource_spec.nil?
87
- return 1
88
- end
89
- @resource_specs_interface.setopts(options)
90
- if options[:dry_run]
91
- print_dry_run @resource_specs_interface.dry.get(resource_spec['id'])
92
- return
93
- end
94
- json_response = @resource_specs_interface.get(resource_spec['id'])
95
- resource_spec = json_response['specTemplate']
96
- instances = json_response['instances'] || []
97
- servers = json_response['servers'] || []
98
- if options[:json]
99
- puts as_json(json_response, options, 'specTemplate')
100
- return 0
101
- elsif options[:yaml]
102
- puts as_yaml(json_response, options, 'specTemplate')
103
- return 0
104
- elsif options[:csv]
105
- puts records_as_csv([json_response['specTemplate']], options)
106
- return 0
107
- end
108
-
109
- print_h1 "Resource Spec Details"
110
- print cyan
111
- detemplateion_cols = {
112
- "ID" => lambda {|it| it['id'] },
113
- "Name" => lambda {|it| it['name'] },
114
- "Type" => lambda {|it| format_resource_spec_type(it['templateType']) },
115
- "Source" => lambda {|it| it['source'] },
116
- #"Owner" => lambda {|it| it['account'] ? it['account']['name'] : '' },
117
- "Created By" => lambda {|it| it['createdBy'] },
118
- "Updated By" => lambda {|it| it['updatedBy'] },
119
- "Created" => lambda {|it| format_local_dt(it['dateCreated']) },
120
- "Updated" => lambda {|it| format_local_dt(it['lastUpdated']) }
121
- }
122
- print_detemplateion_list(detemplateion_cols, resource_spec)
123
-
124
- print_h2 "Content"
125
-
126
- puts resource_spec['content']
127
-
128
- print reset,"\n"
129
- return 0
130
- rescue RestClient::Exception => e
131
- print_rest_exception(e, options)
132
- return 1
133
- end
134
- end
135
-
136
- def add(args)
137
- options = {}
138
- params = {'templateType' => 'bash', 'templatePhase' => 'provision'}
139
- optparse = Morpheus::Cli::OptionParser.new do |opts|
140
- opts.banner = subcommand_usage("[name]")
141
- opts.on('--name VALUE', String, "Name") do |val|
142
- params['name'] = val
143
- end
144
- opts.on('--type [kubernetes|helm|terraform]', String, "Spec Template Type.") do |val|
145
- params['templateType'] = val
146
- end
147
- # opts.on('--phase [provision|start|stop]', String, "Template Phase. Default is 'provision'") do |val|
148
- # params['templatePhase'] = val
149
- # end
150
- opts.on('--category VALUE', String, "Category") do |val|
151
- params['category'] = val
152
- end
153
- opts.on('--template TEXT', String, "Contents of the template.") do |val|
154
- params['template'] = val
155
- end
156
- opts.on('--file FILE', "File containing the template. This can be used instead of --template" ) do |filename|
157
- full_filename = File.expand_path(filename)
158
- if File.exists?(full_filename)
159
- params['template'] = File.read(full_filename)
160
- else
161
- print_red_alert "File not found: #{full_filename}"
162
- exit 1
163
- end
164
- # use the filename as the name by default.
165
- if !params['name']
166
- params['name'] = File.basename(full_filename)
167
- end
168
- end
169
- # opts.on('--enabled [on|off]', String, "Can be used to disable it") do |val|
170
- # options['enabled'] = !(val.to_s == 'off' || val.to_s == 'false')
171
- # end
172
- build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote, :quiet])
173
- opts.footer = "Create a new spec template." + "\n" +
174
- "[name] is required and can be passed as --name instead."
175
- end
176
- optparse.parse!(args)
177
- # support [name] as first argument
178
- if args[0]
179
- params['name'] = args[0]
180
- end
181
- if !params['name']
182
- print_error Morpheus::Terminal.angry_prompt
183
- puts_error "wrong number of arguments, expected 0-1 and got (#{args.count}) #{args.inspect}\n#{optparse}"
184
- return 1
185
- end
186
- connect(options)
187
- begin
188
- # construct payload
189
- payload = nil
190
- if options[:payload]
191
- payload = options[:payload]
192
- else
193
- # merge -O options into normally parsed options
194
- params.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]
195
- # todo: prompt?
196
- payload = {'specTemplate' => params}
197
- end
198
- @resource_specs_interface.setopts(options)
199
- if options[:dry_run]
200
- print_dry_run @resource_specs_interface.dry.create(payload)
201
- return
202
- end
203
- json_response = @resource_specs_interface.create(payload)
204
- if options[:json]
205
- puts as_json(json_response, options)
206
- elsif !options[:quiet]
207
- resource_spec = json_response['specTemplate']
208
- print_green_success "Added spec template #{resource_spec['name']}"
209
- _get(resource_spec['id'], {})
210
- end
211
- return 0
212
- rescue RestClient::Exception => e
213
- print_rest_exception(e, options)
214
- return 1
215
- end
216
- end
217
-
218
-
219
- def update(args)
220
- options = {}
221
- params = {}
222
- optparse = Morpheus::Cli::OptionParser.new do |opts|
223
- opts.banner = subcommand_usage("[name]")
224
- opts.on('--name VALUE', String, "Name") do |val|
225
- params['name'] = val
226
- end
227
- # opts.on('--code VALUE', String, "Code") do |val|
228
- # params['code'] = val
229
- # end
230
- # opts.on('--detemplateion VALUE', String, "Detemplateion") do |val|
231
- # params['detemplateion'] = val
232
- # end
233
- opts.on('--type [bash|powershell]', String, "Template Type") do |val|
234
- params['templateType'] = val
235
- end
236
- opts.on('--phase [start|stop]', String, "Template Phase") do |val|
237
- params['templatePhase'] = val
238
- end
239
- opts.on('--category VALUE', String, "Category") do |val|
240
- params['category'] = val
241
- end
242
- opts.on('--template TEXT', String, "Contents of the template.") do |val|
243
- params['template'] = val
244
- end
245
- opts.on('--file FILE', "File containing the template. This can be used instead of --template" ) do |filename|
246
- full_filename = File.expand_path(filename)
247
- if File.exists?(full_filename)
248
- params['template'] = File.read(full_filename)
249
- else
250
- print_red_alert "File not found: #{full_filename}"
251
- exit 1
252
- end
253
- end
254
- # opts.on('--enabled [on|off]', String, "Can be used to disable it") do |val|
255
- # options['enabled'] = !(val.to_s == 'off' || val.to_s == 'false')
256
- # end
257
- build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote, :quiet])
258
- opts.footer = "Update a spec template." + "\n" +
259
- "[name] is required. This is the name or id of a spec template."
260
- end
261
- optparse.parse!(args)
262
- if args.count != 1
263
- print_error Morpheus::Terminal.angry_prompt
264
- puts_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args.inspect}\n#{optparse}"
265
- return 1
266
- end
267
- connect(options)
268
- begin
269
- resource_spec = find_resource_spec_by_name_or_id(args[0])
270
- if resource_spec.nil?
271
- return 1
272
- end
273
- # construct payload
274
- payload = nil
275
- if options[:payload]
276
- payload = options[:payload]
277
- else
278
- # merge -O options into normally parsed options
279
- params.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]
280
- payload = {'specTemplate' => params}
281
- end
282
- @resource_specs_interface.setopts(options)
283
- if options[:dry_run]
284
- print_dry_run @resource_specs_interface.dry.update(resource_spec["id"], payload)
285
- return
286
- end
287
- json_response = @resource_specs_interface.update(resource_spec["id"], payload)
288
- if options[:json]
289
- puts as_json(json_response, options)
290
- elsif !options[:quiet]
291
- print_green_success "Updated spec template #{resource_spec['name']}"
292
- _get(resource_spec['id'], {})
293
- end
294
- return 0
295
- rescue RestClient::Exception => e
296
- print_rest_exception(e, options)
297
- return 1
298
- end
299
- end
300
-
301
- def remove(args)
302
- options = {}
303
- params = {}
304
- optparse = Morpheus::Cli::OptionParser.new do |opts|
305
- opts.banner = subcommand_usage("[name]")
306
- build_common_options(opts, options, [:json, :dry_run, :quiet, :auto_confirm])
307
- end
308
- optparse.parse!(args)
309
- if args.count < 1
310
- puts optparse
311
- return 127
312
- end
313
- connect(options)
314
-
315
- begin
316
- resource_spec = find_resource_spec_by_name_or_id(args[0])
317
- if resource_spec.nil?
318
- return 1
319
- end
320
-
321
- unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to delete spec template '#{resource_spec['name']}'?", options)
322
- return false
323
- end
324
-
325
- # payload = {
326
- # 'specTemplate' => {id: resource_spec["id"]}
327
- # }
328
- # payload['specTemplate'].merge!(resource_spec)
329
- payload = params
330
- @resource_specs_interface.setopts(options)
331
- if options[:dry_run]
332
- print_dry_run @resource_specs_interface.dry.destroy(resource_spec["id"])
333
- return
334
- end
335
-
336
- json_response = @resource_specs_interface.destroy(resource_spec["id"])
337
- if options[:json]
338
- puts as_json(json_response, options)
339
- elsif !options[:quiet]
340
- print_green_success "Deleted spec template #{resource_spec['name']}"
341
- end
342
- return 0, nil
343
- rescue RestClient::Exception => e
344
- print_rest_exception(e, options)
345
- return 1
346
- end
347
- end
348
-
349
-
350
- private
351
-
352
- def find_resource_spec_by_name_or_id(val)
353
- if val.to_s =~ /\A\d{1,}\Z/
354
- return find_resource_spec_by_id(val)
355
- else
356
- return find_resource_spec_by_name(val)
357
- end
358
- end
359
-
360
- def find_resource_spec_by_id(id)
361
- begin
362
- json_response = @resource_specs_interface.get(id.to_i)
363
- return json_response['specTemplate']
364
- rescue RestClient::Exception => e
365
- if e.response && e.response.code == 404
366
- print_red_alert "Spec Template not found by id #{id}"
367
- else
368
- raise e
369
- end
370
- end
371
- end
372
-
373
- def find_resource_spec_by_name(name)
374
- resource_specs = @resource_specs_interface.list({name: name.to_s})['specTemplates']
375
- if resource_specs.empty?
376
- print_red_alert "Spec Template not found by name #{name}"
377
- return nil
378
- elsif resource_specs.size > 1
379
- print_red_alert "#{resource_specs.size} spec templates found by name #{name}"
380
- print_resource_specs_table(resource_specs, {color: red})
381
- print_red_alert "Try using ID instead"
382
- print reset,"\n"
383
- return nil
384
- else
385
- return resource_specs[0]
386
- end
387
- end
388
-
389
- def print_resource_specs_table(resource_specs, opts={})
390
- columns = [
391
- {"ID" => lambda {|resource_spec| resource_spec['id'] } },
392
- {"NAME" => lambda {|resource_spec| resource_spec['name'] } },
393
- #{"OWNER" => lambda {|resource_spec| resource_spec['account'] ? resource_spec['account']['name'] : '' } },
394
- ]
395
- if opts[:include_fields]
396
- columns = opts[:include_fields]
397
- end
398
- print as_pretty_table(resource_specs, columns, opts)
399
- end
400
-
401
- def format_resource_spec_type(val)
402
- val.to_s # .capitalize
403
- end
404
-
405
- def format_resource_spec_phase(val)
406
- val.to_s # .capitalize
407
- end
408
-
409
- end