Class: Morpheus::Cli::UserSettingsCommand
Instance Attribute Summary
Attributes included from CliCommand
#no_prompt
Instance Method Summary
collapse
#account_column_definitions, #accounts_interface, #find_account_by_id, #find_account_by_name, #find_account_by_name_or_id, #find_account_from_options, #find_all_user_ids, #find_role_by_id, #find_role_by_name, #find_role_by_name_or_id, #find_user_by_id, #find_user_by_username, #find_user_by_username_or_id, #find_user_group_by_id, #find_user_group_by_name, #find_user_group_by_name_or_id, #format_access_string, #format_role_type, #format_user_role_names, #format_user_status, #get_access_color, #get_access_string, included, #list_account_column_definitions, #list_user_column_definitions, #list_user_group_column_definitions, #role_column_definitions, #roles_interface, #subtenant_role_column_definitions, #user_column_definitions, #user_group_column_definitions, #user_groups_interface, #users_interface
Methods included from CliCommand
#apply_options, #build_common_options, #build_option_type_options, #build_standard_add_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #get_subcommand_description, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_bytes_param, #parse_id_list, #parse_list_options, #parse_list_subtitles, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands
Constructor Details
Returns a new instance of UserSettingsCommand.
13
14
15
|
# File 'lib/morpheus/cli/user_settings_command.rb', line 13
def initialize()
end
|
Instance Method Details
#clear_access_token(args) ⇒ Object
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
|
# File 'lib/morpheus/cli/user_settings_command.rb', line 443
def clear_access_token(args)
raw_args = args
options = {}
params = {}
client_id = nil
all_clients = false
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage("[client-id]")
opts.on("--all", "--all", "Clear tokens for all Client IDs instead of a specific client.") do
all_clients = true
end
opts.on("-u", "--user USER", "User username or ID") do |val|
options[:user] = val.to_s
end
opts.on("--user-id ID", String, "User ID") do |val|
params['userId'] = val.to_s
end
build_common_options(opts, options, [:payload, :options, :json, :dry_run, :quiet, :remote])
opts. = <<-EOT
Clear API access token for a specific client.
[client-id] or --all is required. This is the id of an api client.
Done for the current user by default, unless a user is specified with the --user option.
EOT
end
optparse.parse!(args)
connect(options)
if args.count > 1 || (args.count == 0 && all_clients == false)
print_error Morpheus::Terminal.angry_prompt
puts_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args.inspect}\n#{optparse}"
return 1
end
if args[0]
params['clientId'] = args[0]
end
if params['clientId'] == 'all'
params.delete('clientId')
all_clients = true
end
begin
if options[:user]
user = find_user_by_username_or_id(nil, options[:user], {global:true})
return 1 if user.nil?
params['userId'] = user['id']
end
payload = {}
@user_settings_interface.setopts(options)
if options[:dry_run]
print_dry_run @user_settings_interface.dry.clear_access_token(params, payload)
return
end
json_response = @user_settings_interface.clear_access_token(params, payload)
if options[:quiet]
return 0
elsif options[:json]
puts as_json(json_response, options)
return 0
end
new_access_token = json_response['token']
success_msg = "Success"
if all_clients
success_msg = "Cleared all access tokens"
else
success_msg = "Cleared #{params['clientId']} access token"
end
if params['userId']
success_msg << " for user #{params['userId']}"
end
print_green_success success_msg
if params['clientId'] == Morpheus::APIClient::CLIENT_ID
if params['userId'].nil? print yellow,"Your current access token is no longer valid, you will need to login again.",reset,"\n"
end
end
return 0
rescue RestClient::Exception => e
print_rest_exception(e, options)
return 1
end
end
|
#connect(opts) ⇒ Object
17
18
19
20
21
|
# File 'lib/morpheus/cli/user_settings_command.rb', line 17
def connect(opts)
@api_client = establish_remote_appliance_connection(opts)
@user_settings_interface = @api_client.user_settings
@users_interface = @api_client.users
end
|
#get(args) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/morpheus/cli/user_settings_command.rb', line 27
def get(args)
raw_args = args
options = {}
params = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage()
opts.on("-u", "--user USER", "User username or ID") do |val|
options[:user] = val.to_s
end
opts.on("--user-id ID", String, "User ID") do |val|
params['userId'] = val.to_s
end
build_common_options(opts, options, [:query, :json, :yaml, :csv, :fields, :dry_run, :remote])
opts. = <<-EOT
Get user settings.
Done for the current user by default, unless a user is specified with the --user option.
EOT
end
optparse.parse!(args)
connect(options)
if args.count != 0
print_error Morpheus::Terminal.angry_prompt
puts_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args.inspect}\n#{optparse}"
return 1
end
begin
if options[:user]
user = find_user_by_username_or_id(nil, options[:user], {global:true})
return 1 if user.nil?
params['userId'] = user['id']
end
params.merge!(parse_list_options(options))
@user_settings_interface.setopts(options)
if options[:dry_run]
print_dry_run @user_settings_interface.dry.get(params)
return
end
json_response = @user_settings_interface.get(params)
if options[:json]
puts as_json(json_response, options, "user")
return 0
elsif options[:yaml]
puts as_yaml(json_response, options, "user")
return 0
elsif options[:csv]
puts records_as_csv([json_response['user']], options)
return 0
end
user_settings = json_response['user'] || json_response['userSettings']
access_tokens = user_settings['accessTokens'] || json_response['accessTokens'] || json_response['apiAccessTokens'] || []
print_h1 "User Settings"
print cyan
description_cols = {
"ID" => lambda {|it| it['id'] },
"Username" => lambda {|it| it['username'] },
"First Name" => lambda {|it| it['firstName'] },
"Last Name" => lambda {|it| it['lastName'] },
"Email" => lambda {|it| it['email'] },
"Avatar" => lambda {|it| it['avatar'] ? it['avatar'].split('/').last : '' },
"Notifications" => lambda {|it| format_boolean(it['receiveNotifications']) },
"Linux Username" => lambda {|it| it['linuxUsername'] },
"Linux Password" => lambda {|it| it['linuxPassword'] },
"Linux Key Pair" => lambda {|it| it['linuxKeyPairId'] },
"Windows Username" => lambda {|it| it['windowsUsername'] },
"Windows Password" => lambda {|it| it['windowsPassword'] },
"Default Persona" => lambda {|it| it['defaultPersona'] ? it['defaultPersona']['name'] : '' },
}
print_description_list(description_cols, user_settings)
if access_tokens && !access_tokens.empty?
print_h2 "API Access Tokens"
cols = {
"CLIENT ID" => lambda {|it| it['clientId'] },
"USERNAME" => lambda {|it| it['username'] },
"ACCESS TOKEN" => lambda {|it| it['maskedAccessToken'] },
"REFRESH TOKEN" => lambda {|it| it['maskedRefreshToken'] },
"EXPIRATION" => lambda {|it| format_local_dt(it['expiration']) },
"TTL" => lambda {|it| it['expiration'] ? (format_duration(it['expiration']) rescue '') : '' }
}
print cyan
puts as_pretty_table(access_tokens, cols)
else
print cyan, "\n", "No API access tokens found", "\n\n"
end
print reset return 0
rescue RestClient::Exception => e
print_rest_exception(e, options)
return 1
end
end
|
#handle(args) ⇒ Object
23
24
25
|
# File 'lib/morpheus/cli/user_settings_command.rb', line 23
def handle(args)
handle_subcommand(args)
end
|
#list_clients(args) ⇒ Object
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
|
# File 'lib/morpheus/cli/user_settings_command.rb', line 534
def list_clients(args)
raw_args = args
options = {}
params = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage()
build_common_options(opts, options, [:query, :json, :yaml, :csv, :fields, :dry_run, :remote])
opts. = <<-EOT
List available api clients.
EOT
end
optparse.parse!(args)
connect(options)
if args.count != 0
print_error Morpheus::Terminal.angry_prompt
puts_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args.inspect}\n#{optparse}"
return 1
end
begin
params.merge!(parse_list_options(options))
@user_settings_interface.setopts(options)
if options[:dry_run]
print_dry_run @user_settings_interface.dry.available_clients(params)
return
end
json_response = @user_settings_interface.available_clients(params)
if options[:json]
puts as_json(json_response, options, "clients")
return 0
elsif options[:yaml]
puts as_yaml(json_response, options, "clients")
return 0
elsif options[:csv]
puts records_as_csv(json_response['clients'], options)
return 0
end
clients = json_response['clients'] || json_response['apiClients']
print_h1 "Morpheus API Clients"
columns = {
"CLIENT ID" => lambda {|it| it['clientId'] },
"NAME" => lambda {|it| it['name'] },
"TTL" => lambda {|it| it['accessTokenValiditySeconds'] ? "#{it['accessTokenValiditySeconds']}" : '' },
"DURATION" => lambda {|it| it['accessTokenValiditySeconds'] ? (format_duration_seconds(it['accessTokenValiditySeconds']) rescue '') : '' },
}
print cyan
puts as_pretty_table(clients, columns)
print reset return 0
rescue RestClient::Exception => e
print_rest_exception(e, options)
return 1
end
end
|
#regenerate_access_token(args) ⇒ Object
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
# File 'lib/morpheus/cli/user_settings_command.rb', line 375
def regenerate_access_token(args)
raw_args = args
options = {}
params = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage("[client-id]")
opts.on("-u", "--user USER", "User username or ID") do |val|
options[:user] = val.to_s
end
opts.on("--user-id ID", String, "User ID") do |val|
params['userId'] = val.to_s
end
build_common_options(opts, options, [:payload, :options, :json, :dry_run, :quiet, :remote])
opts. = <<-EOT
Regenerate API access token for a specific client.
[client-id] is required. This is the id of an api client.
Done for the current user by default, unless a user is specified with the --user option.
EOT
end
optparse.parse!(args)
connect(options)
if args.count != 1
print_error Morpheus::Terminal.angry_prompt
puts_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args.inspect}\n#{optparse}"
return 1
end
params['clientId'] = args[0]
begin
if options[:user]
user = find_user_by_username_or_id(nil, options[:user], {global:true})
return 1 if user.nil?
params['userId'] = user['id']
end
payload = {}
@user_settings_interface.setopts(options)
if options[:dry_run]
print_dry_run @user_settings_interface.dry.regenerate_access_token(params, payload)
return
end
json_response = @user_settings_interface.regenerate_access_token(params, payload)
new_access_token = json_response['access_token'] || json_response['token']
if params['clientId'] == Morpheus::APIClient::CLIENT_ID
if params['userId'].nil? if new_access_token
login_opts = {:remote_token => new_access_token}
login_result = Morpheus::Cli::Credentials.new(@appliance_name, @appliance_url).login(login_opts)
end
end
end
if options[:quiet]
return 0
elsif options[:json]
puts as_json(json_response, options)
return 0
end
print_green_success "Regenerated #{params['clientId']} access token: #{new_access_token}"
get_args = [] + (options[:remote] ? ["-r",options[:remote]] : []) + (params['userId'] ? ['--user-id', params['userId'].to_s] : [])
get(get_args)
return 0
rescue RestClient::Exception => e
print_rest_exception(e, options)
return 1
end
end
|
#remove_avatar(args) ⇒ Object
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
# File 'lib/morpheus/cli/user_settings_command.rb', line 261
def remove_avatar(args)
options = {}
params = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage()
opts.on("-u", "--user USER", "User username or ID") do |val|
options[:user] = val.to_s
end
opts.on("--user-id ID", String, "User ID") do |val|
params['userId'] = val.to_s
end
build_common_options(opts, options, [:json, :dry_run, :quiet, :remote])
opts. = <<-EOT
Remove avatar profile image.
[file] is required. This is the local path of a file to upload [png|jpg|svg].
Done for the current user by default, unless a user is specified with the --user option.
EOT
end
optparse.parse!(args)
connect(options)
if args.count != 0
print_error Morpheus::Terminal.angry_prompt
puts_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args.inspect}\n#{optparse}"
return 1
end
begin
if options[:user]
user = find_user_by_username_or_id(nil, options[:user], {global:true})
return 1 if user.nil?
params['userId'] = user['id']
end
@user_settings_interface.setopts(options)
if options[:dry_run]
print_dry_run @user_settings_interface.dry.remove_avatar(params)
return
end
json_response = @user_settings_interface.remove_avatar(params)
if options[:quiet]
return 0
elsif options[:json]
puts as_json(json_response, options)
return 0
end
print_green_success "Removed avatar"
get_args = [] + (options[:remote] ? ["-r",options[:remote]] : []) + (params['userId'] ? ['--user-id', params['userId'].to_s] : [])
get(get_args)
return 0
rescue RestClient::Exception => e
print_rest_exception(e, options)
return 1
end
end
|
#update(args) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/morpheus/cli/user_settings_command.rb', line 128
def update(args)
raw_args = args
options = {}
params = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage("[options]")
opts.on("-u", "--user USER", "User username or ID") do |val|
options[:user] = val.to_s
end
opts.on("--user-id ID", String, "User ID") do |val|
params['userId'] = val.to_s
end
build_common_options(opts, options, [:payload, :options, :json, :dry_run, :quiet, :remote])
opts. = <<-EOT
Update user settings.
Done for the current user by default, unless a user is specified with the --user option.
EOT
end
optparse.parse!(args)
connect(options)
if args.count != 0
print_error Morpheus::Terminal.angry_prompt
puts_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args.inspect}\n#{optparse}"
return 1
end
begin
if options[:user]
user = find_user_by_username_or_id(nil, options[:user], {global:true})
return 1 if user.nil?
params['userId'] = user['id']
end
payload = {}
if options[:payload]
payload = options[:payload]
else
end
if options[:options]
payload['user'] ||= {}
payload['user'].deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) })
end
@user_settings_interface.setopts(options)
if options[:dry_run]
print_dry_run @user_settings_interface.dry.update(params, payload)
return
end
json_response = @user_settings_interface.update(params, payload)
if options[:quiet]
return 0
elsif options[:json]
puts as_json(json_response, options)
return 0
end
print_green_success "Updated user settings"
get_args = [] + (options[:remote] ? ["-r",options[:remote]] : []) + (params['userId'] ? ['--user-id', params['userId'].to_s] : [])
get(get_args)
return 0
rescue RestClient::Exception => e
print_rest_exception(e, options)
return 1
end
end
|
#update_avatar(args) ⇒ Object
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
# File 'lib/morpheus/cli/user_settings_command.rb', line 195
def update_avatar(args)
options = {}
params = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage("[file]")
opts.on("-u", "--user USER", "User username or ID") do |val|
options[:user] = val.to_s
end
opts.on("--user-id ID", String, "User ID") do |val|
params['userId'] = val.to_s
end
build_common_options(opts, options, [:json, :dry_run, :quiet, :remote])
opts. = <<-EOT
Update avatar profile image.
[file] is required. This is the local path of a file to upload [png|jpg|svg].
Done for the current user by default, unless a user is specified with the --user option.
EOT
end
optparse.parse!(args)
connect(options)
if args.count != 1
print_error Morpheus::Terminal.angry_prompt
puts_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args.inspect}\n#{optparse}"
return 1
end
filename = File.expand_path(args[0].to_s)
image_file = nil
if filename && File.file?(filename)
image_file = File.new(filename, 'rb')
else
puts_error "#{Morpheus::Terminal.angry_prompt}File not found: #{filename}"
return 1
end
begin
if options[:user]
user = find_user_by_username_or_id(nil, options[:user], {global:true})
return 1 if user.nil?
params['userId'] = user['id']
end
@user_settings_interface.setopts(options)
if options[:dry_run]
print_dry_run @user_settings_interface.dry.update_avatar(image_file, params)
return
end
json_response = @user_settings_interface.update_avatar(image_file, params)
if options[:quiet]
return 0
elsif options[:json]
puts as_json(json_response, options)
return 0
end
print_green_success "Updated avatar"
get_args = [] + (options[:remote] ? ["-r",options[:remote]] : []) + (params['userId'] ? ['--user-id', params['userId'].to_s] : [])
get(get_args)
return 0
rescue RestClient::Exception => e
print_rest_exception(e, options)
return 1
end
end
|
#view_avatar(args) ⇒ Object
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
# File 'lib/morpheus/cli/user_settings_command.rb', line 317
def view_avatar(args)
raw_args = args
options = {}
params = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage()
opts.on("-u", "--user USER", "User username or ID") do |val|
options[:user] = val.to_s
end
opts.on("--user-id ID", String, "User ID") do |val|
params['userId'] = val.to_s
end
build_common_options(opts, options, [:remote])
opts. = <<-EOT
View avatar profile image.
This opens the avatar image url with a web browser.
Done for the current user by default, unless a user is specified with the --user option.
EOT
end
optparse.parse!(args)
connect(options)
if args.count != 0
print_error Morpheus::Terminal.angry_prompt
puts_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args.inspect}\n#{optparse}"
return 1
end
begin
if options[:user]
user = find_user_by_username_or_id(nil, options[:user], {global:true})
return 1 if user.nil?
params['userId'] = user['id']
end
json_response = @user_settings_interface.get(params)
user_settings = json_response['user'] || json_response['userSettings']
if user_settings['avatar']
link = user_settings['avatar']
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
system "start #{link}"
elsif RbConfig::CONFIG['host_os'] =~ /darwin/
system "open #{link}"
elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/
system "xdg-open #{link}"
end
return 0, nil
else
print_error red,"No avatar image found.",reset,"\n"
return 1
end
rescue RestClient::Exception => e
print_rest_exception(e, options)
return 1
end
end
|