Class: Morpheus::Cli::Roles

Inherits:
Object
  • Object
show all
Includes:
AccountsHelper, CliCommand
Defined in:
lib/morpheus/cli/roles.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from AccountsHelper

#accounts_interface, #find_account_by_id, #find_account_by_name, #find_account_by_name_or_id, #find_account_from_options, #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, #format_role_type, #format_user_role_names, #get_access_string, included, #print_accounts_table, #print_roles_table, #print_users_table, #roles_interface, #users_interface

Methods included from CliCommand

#build_common_options, #build_option_type_options, #command_name, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_id_list, #print, #print_error, #puts, #puts_error, #raise_command_error, #run_command_for_each_arg, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!

Instance Method Details

#add(args) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/morpheus/cli/roles.rb', line 249

def add(args)
  usage = "Usage: morpheus roles add [options]"
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[options]")
    build_option_type_options(opts, options, add_role_option_types)
    build_common_options(opts, options, [:options, :json, :dry_run])
  end
  optparse.parse!(args)

  connect(options)
  begin

    load_whoami()
     = (options)
     =  ? ['id'] : nil

    # argh, some options depend on others here...eg. multitenant is only available when roleType == 'user'
    #prompt_option_types = update_role_option_types()

    role_payload = {}
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'authority', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'displayOrder' => 1}], options[:options])
    role_payload['authority'] = v_prompt['authority']
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'description', 'fieldLabel' => 'Description', 'type' => 'text', 'displayOrder' => 2}], options[:options])
    role_payload['description'] = v_prompt['description']

    if @is_master_account
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'roleType', 'fieldLabel' => 'Type', 'type' => 'select', 'selectOptions' => role_type_options, 'defaultValue' => 'user', 'displayOrder' => 3}], options[:options])
      role_payload['roleType'] = v_prompt['roleType']
    else
      role_payload['roleType'] = 'user'
    end

    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'baseRole', 'fieldLabel' => 'Copy From Role', 'type' => 'text', 'displayOrder' => 4}], options[:options])
    if v_prompt['baseRole'].to_s != ''
      base_role = find_role_by_name_or_id(, v_prompt['baseRole'])
      exit 1 if base_role.nil?
      role_payload['baseRoleId'] = base_role['id']
    end

    if @is_master_account
      if role_payload['roleType'] == 'user'
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'multitenant', 'fieldLabel' => 'Multitenant', 'type' => 'checkbox', 'defaultValue' => 'off', 'description' => 'A Multitenant role is automatically copied into all existing subaccounts as well as placed into a subaccount when created. Useful for providing a set of predefined roles a Customer can use', 'displayOrder' => 5}], options[:options])
        role_payload['multitenant'] = ['on','true'].include?(v_prompt['multitenant'].to_s)
      end
    end

    role_payload['instanceLimits'] = {}
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'instanceLimits.maxStorage', 'fieldLabel' => 'Max Storage (bytes)', 'type' => 'text', 'displayOrder' => 8}], options[:options])
    if v_prompt['instanceLimits.maxStorage'].to_s.strip != ''
      role_payload['instanceLimits']['maxStorage'] = v_prompt['instanceLimits.maxStorage'].to_i
    end
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'instanceLimits.maxMemory', 'fieldLabel' => 'Max Memory (bytes)', 'type' => 'text', 'displayOrder' => 9}], options[:options])
    if v_prompt['instanceLimits.maxMemory'].to_s.strip != ''
      role_payload['instanceLimits']['maxMemory'] = v_prompt['instanceLimits.maxMemory'].to_i
    end
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'instanceLimits.maxCpu', 'fieldLabel' => 'CPU Count', 'type' => 'text', 'displayOrder' => 10}], options[:options])
    if v_prompt['instanceLimits.maxCpu'].to_s.strip != ''
      role_payload['instanceLimits']['maxCpu'] = v_prompt['instanceLimits.maxCpu'].to_i
    end

    payload = {role: role_payload}

    if options[:dry_run]
      print_dry_run @roles_interface.dry.create(, payload)
      return
    end
    json_response = @roles_interface.create(, payload)

    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
      return
    end

    if 
      print_green_success "Added role #{role_payload['authority']} to account #{['name']}"
    else
      print_green_success "Added role #{role_payload['authority']}"
    end

    details_options = [role_payload["authority"]]
    if 
      details_options.push "--account-id", ['id'].to_s
    end
    get(details_options)

  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#connect(opts) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/morpheus/cli/roles.rb', line 17

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @whoami_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).whoami
  @users_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).users
  @accounts_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).accounts
  @roles_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).roles
  @groups_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).groups
  @options_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).options
  #@clouds_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).instance_types
  @instance_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).instance_types
  @active_group_id = Morpheus::Cli::Groups.active_group
end

#get(args) ⇒ Object



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
126
127
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
194
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
# File 'lib/morpheus/cli/roles.rb', line 83

def get(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name]")
    opts.on(nil,'--feature-access', "Display Feature Access") do |val|
      options[:include_feature_access] = true
    end
    opts.on(nil,'--group-access', "Display Group Access") do
      options[:include_group_access] = true
    end
    opts.on(nil,'--cloud-access', "Display Cloud Access") do
      options[:include_cloud_access] = true
    end
    opts.on(nil,'--instance-type-access', "Display Instance Type Access") do
      options[:include_instance_type_access] = true
    end
    opts.on(nil,'--all-access', "Display All Access Lists") do
      options[:include_feature_access] = true
      options[:include_group_access] = true
      options[:include_cloud_access] = true
      options[:include_instance_type_access] = true
    end
    build_common_options(opts, options, [:json, :dry_run])
  end
  optparse.parse!(args)

  if args.count < 1
    puts optparse
    exit 1
  end

  connect(options)
  begin
     = (options)
     =  ? ['id'] : nil
    if options[:dry_run]
      if args[0].to_s =~ /\A\d{1,}\Z/
        print_dry_run @roles_interface.dry.get(, args[0].to_i)
      else
        print_dry_run @roles_interface.dry.list(, {name: args[0]})
      end
      return
    end

    # role = find_role_by_name_or_id(account_id, args[0])
    # exit 1 if role.nil?
    # refetch from show action, argh
    # json_response = @roles_interface.get(account_id, role['id'])
    # role = json_response['role']

    json_response = nil
    if args[0].to_s =~ /\A\d{1,}\Z/
      json_response = @roles_interface.get(, args[0].to_i)
      role = json_response['role']
    else
      role = find_role_by_name_or_id(, args[0])
      exit 1 if role.nil?
      # refetch from show action, argh
      json_response = @roles_interface.get(, role['id'])
      role = json_response['role']
    end

    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print cyan
      print_h1 "Role Details"
      print cyan
      description_cols = {
        "ID" => 'id',
        "Name" => 'authority',
        "Description" => 'description',
        "Scope" => lambda {|it| it['scope'] },
        "Type" => lambda {|it| format_role_type(it) },
        "Multitenant" => lambda {|it| format_boolean(it['multitenant']) },
        "Owner" => lambda {|it| role['owner'] ? role['owner']['name'] : '' },
        #"Account" => lambda {|it| it['account'] ? it['account']['name'] : '' },
        "Created" => lambda {|it| format_local_dt(it['dateCreated']) },
        "Updated" => lambda {|it| format_local_dt(it['lastUpdated']) }
      }
      print_description_list(description_cols, role)

      print_h2 "Role Instance Limits"
      print cyan
      print_description_list({
        "Max Storage"  => lambda {|it| (it && it['maxStorage'].to_i != 0) ? Filesize.from("#{it['maxStorage']} B").pretty : "no limit" },
        "Max Memory"  => lambda {|it| (it && it['maxMemory'].to_i != 0) ? Filesize.from("#{it['maxMemory']} B").pretty : "no limit" },
        "CPU Count"  => lambda {|it| (it && it['maxCpu'].to_i != 0) ? it['maxCpu'] : "no limit" }
      }, role['instanceLimits'])

      print_h2 "Feature Access"
      print cyan

      if options[:include_feature_access]
        rows = json_response['featurePermissions'].collect do |it|
          {
            code: it['code'],
            name: it['name'],
            access: get_access_string(it['access']),
          }
        end
        tp rows, [:code, :name, :access]
      else
        puts "Use --feature-access to list feature access"
      end

      print_h2 "Group Access"
      print cyan
      puts "Global Group Access: #{get_access_string(json_response['globalSiteAccess'])}\n\n"
      if json_response['globalSiteAccess'] == 'custom'
        if options[:include_group_access]
          rows = json_response['sites'].collect do |it|
            {
              name: it['name'],
              access: get_access_string(it['access']),
            }
          end
          tp rows, [:name, :access]
        else
          puts "Use --group-access to list custom access"
        end
      end

      print_h2 "Cloud Access"
      print cyan
      puts "Global Cloud Access: #{get_access_string(json_response['globalZoneAccess'])}\n\n"
      if json_response['globalZoneAccess'] == 'custom'
        if options[:include_cloud_access]
          rows = json_response['zones'].collect do |it|
            {
              name: it['name'],
              access: get_access_string(it['access']),
            }
          end
          tp rows, [:name, :access]
        else
          puts "Use --cloud-access to list custom access"
        end
      end

      print_h2 "Instance Type Access"
      print cyan
      puts "Global Instance Type Access: #{get_access_string(json_response['globalInstanceTypeAccess'])}\n\n"
      if json_response['globalInstanceTypeAccess'] == 'custom'
        if options[:include_instance_type_access]
          rows = json_response['instanceTypePermissions'].collect do |it|
            {
              name: it['name'],
              access: get_access_string(it['access']),
            }
          end
          tp rows, [:name, :access]
        else
          puts "Use --instance-type-access to list custom access"
        end
      end

      print reset,"\n"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#handle(args) ⇒ Object



30
31
32
# File 'lib/morpheus/cli/roles.rb', line 30

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



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
# File 'lib/morpheus/cli/roles.rb', line 34

def list(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:list, :json, :dry_run])
  end
  optparse.parse!(args)

  connect(options)
  begin
    load_whoami()

     = (options)
     =  ? ['id'] : nil
    params = {}
    [:phrase, :offset, :max, :sort, :direction].each do |k|
      params[k] = options[k] unless options[k].nil?
    end

    if options[:dry_run]
      print_dry_run @roles_interface.dry.list(, params)
      return
    end
    json_response = @roles_interface.list(, params)
    roles = json_response['roles']
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      title = "Morpheus Roles"
      subtitles = []
      if params[:phrase]
        subtitles << "Search: #{params[:phrase]}".strip
      end
      print_h1 title, subtitles
      if roles.empty?
        print cyan,"No roles found.",reset,"\n"
      else
        print_roles_table(roles, {is_master_account: @is_master_account})
        print_results_pagination(json_response)
      end
      print reset,"\n"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#remove(args) ⇒ Object



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/morpheus/cli/roles.rb', line 424

def remove(args)
  usage = "Usage: morpheus roles remove [name]"
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:auto_confirm, :json, :dry_run])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  name = args[0]
  connect(options)
  begin

     = (options)
     =  ? ['id'] : nil

    role = find_role_by_name_or_id(, name)
    exit 1 if role.nil?
    unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the role #{role['authority']}?")
      exit
    end
    if options[:dry_run]
      print_dry_run @roles_interface.dry.destroy(, role['id'])
      return
    end
    json_response = @roles_interface.destroy(, role['id'])
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Role #{role['authority']} removed"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update(args) ⇒ Object



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
374
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
# File 'lib/morpheus/cli/roles.rb', line 342

def update(args)
  usage = "Usage: morpheus roles update [name] [options]"
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name] [options]")
    build_option_type_options(opts, options, update_role_option_types)
    build_common_options(opts, options, [:options, :json, :dry_run])
  end
  optparse.parse!(args)

  if args.count < 1
    puts optparse
    exit 1
  end
  name = args[0]
  connect(options)
  begin

    load_whoami()

     = (options)
     =  ? ['id'] : nil

    role = find_role_by_name_or_id(, name)
    exit 1 if role.nil?

    prompt_option_types = update_role_option_types()
    if !@is_master_account
      prompt_option_types = prompt_option_types.reject {|it| ['roleType', 'multitenant'].include?(it['fieldName']) }
    end
    if role['roleType'] != 'user'
      prompt_option_types = prompt_option_types.reject {|it| ['multitenant'].include?(it['fieldName']) }
    end
    #params = Morpheus::Cli::OptionTypes.prompt(prompt_option_types, options[:options], @api_client, options[:params])
    params = options[:options] || {}

    if params.empty?
      puts optparse
      option_lines = prompt_option_types.collect {|it| "\t-O #{it['fieldName']}=\"value\"" }.join("\n")
      puts "\nAvailable Options:\n#{option_lines}\n\n"
      exit 1
    end

    #puts "parsed params is : #{params.inspect}"
    role_keys = ['authority', 'description', 'instanceLimits']
    role_payload = params.select {|k,v| role_keys.include?(k) }
    if !role_payload['instanceLimits']
      role_payload['instanceLimits'] = {}
      role_payload['instanceLimits']['maxStorage'] = params['instanceLimits.maxStorage'].to_i if params['instanceLimits.maxStorage'].to_s.strip != ''
      role_payload['instanceLimits']['maxMemory'] = params['instanceLimits.maxMemory'].to_i if params['instanceLimits.maxMemory'].to_s.strip != ''
      role_payload['instanceLimits']['maxCpu'] = params['instanceLimits.maxCpu'].to_i if params['instanceLimits.maxCpu'].to_s.strip != ''
    end

    if params['multitenant'].to_s != ''
      role_payload['multitenant'] = ['on','true'].include?(v_prompt['multitenant'].to_s)
    end
    payload = {role: role_payload}
    if options[:dry_run]
      print_dry_run @roles_interface.dry.update(, role['id'], payload)
      return
    end
    json_response = @roles_interface.update(, role['id'], payload)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
      return
    end

    print_green_success "Updated role #{role_payload['authority']}"

    details_options = [role_payload["authority"] || role['authority']]
    if 
      details_options.push "--account-id", ['id'].to_s
    end
    get(details_options)

  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update_cloud_access(args) ⇒ Object



666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
# File 'lib/morpheus/cli/roles.rb', line 666

def update_cloud_access(args)
  usage = "Usage: morpheus roles update-cloud-access [name] [cloud_name] [full|none]"
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name] [cloud] [full|none]")
    opts.on( '-g', '--group GROUP', "Group to find cloud in" ) do |val|
      options[:group] = val
    end
    build_common_options(opts, options, [:json, :dry_run])
  end
  optparse.parse!(args)

  if args.count < 2
    puts optparse
    exit 1
  end
  name = args[0]
  cloud_name = args[1]
  access_value = args[2].to_s.downcase
  if !['full', 'none'].include?(access_value)
    puts optparse
    exit 1
  end

  connect(options)
  begin
     = (options)
     =  ? ['id'] : nil
    role = find_role_by_name_or_id(, name)
    exit 1 if role.nil?

    role_json = @roles_interface.get(, role['id'])
    if role_json['globalZoneAccess'] != 'custom'
      print "\n", red, "Global Cloud Access is currently: #{role_json['globalZoneAccess'].capitalize}"
      print "\n", "You must first set it to Custom via `morpheus roles update-global-cloud-access \"#{name}\" custom`"
      print "\n\n", reset
      exit 1
    end

    group_id = nil
    if !options[:group].nil?
      group_id = find_group_id_by_name(options[:group])
    else
      group_id = @active_group_id
    end

    if group_id.nil?
      print_red_alert "Group not found or specified!"
      exit 1
    end

    cloud_id = find_cloud_id_by_name(group_id, cloud_name)
    exit 1 if cloud_id.nil?
    params = {cloudId: cloud_id, access: access_value}
    if options[:dry_run]
      print_dry_run @roles_interface.dry.update_cloud(, role['id'], params)
      return
    end
    json_response = @roles_interface.update_cloud(, role['id'], params)

    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Role #{role['authority']} global cloud access updated"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update_feature_access(args) ⇒ Object



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
# File 'lib/morpheus/cli/roles.rb', line 465

def update_feature_access(args)
  usage = "Usage: morpheus roles update-feature-access [name] [code] [full|read|user|yes|no|none]"
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name] [code] [full|read|user|yes|no|none]")
    build_common_options(opts, options, [:json, :dry_run])
  end
  optparse.parse!(args)

  if args.count < 3
    puts optparse
    exit 1
  end
  name = args[0]
  permission_code = args[1]
  access_value = args[2].to_s.downcase

  # if !['full_decrypted','full', 'read', 'custom', 'none'].include?(access_value)
  #   puts optparse
  #   exit 1
  # end

  connect(options)
  begin
     = (options)
     =  ? ['id'] : nil
    role = find_role_by_name_or_id(, name)
    exit 1 if role.nil?

    params = {permissionCode: permission_code, access: access_value}
    if options[:dry_run]
      print_dry_run @roles_interface.dry.update_permission(, role['id'], params)
      return
    end
    json_response = @roles_interface.update_permission(, role['id'], params)

    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Role #{role['authority']} feature access updated"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update_global_cloud_access(args) ⇒ Object



620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'lib/morpheus/cli/roles.rb', line 620

def update_global_cloud_access(args)
  usage = "Usage: morpheus roles update-global-cloud-access [name] [full|custom|none]"
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name] [full|custom|none]")
    build_common_options(opts, options, [:json, :dry_run])
  end
  optparse.parse!(args)

  if args.count < 2
    puts optparse
    exit 1
  end
  name = args[0]
  access_value = args[1].to_s.downcase
  if !['full', 'custom', 'none'].include?(access_value)
    puts optparse
    exit 1
  end

  connect(options)
  begin
     = (options)
     =  ? ['id'] : nil
    role = find_role_by_name_or_id(, name)
    exit 1 if role.nil?

    params = {permissionCode: 'ComputeZone', access: access_value}
    if options[:dry_run]
      print_dry_run @roles_interface.dry.update_permission(, role['id'], params)
      return
    end
    json_response = @roles_interface.update_permission(, role['id'], params)

    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Role #{role['authority']} global cloud access updated"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update_global_group_access(args) ⇒ Object



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/morpheus/cli/roles.rb', line 513

def update_global_group_access(args)
  usage = "Usage: morpheus roles update-global-group-access [name] [full|read|custom|none]"
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name] [code] [full|read|custom|none]")
    build_common_options(opts, options, [:json, :dry_run])
  end
  optparse.parse!(args)

  if args.count < 2
    puts optparse
    exit 1
  end
  name = args[0]
  access_value = args[1].to_s.downcase
  if !['full', 'read', 'custom', 'none'].include?(access_value)
    puts optparse
    exit 1
  end

  connect(options)
  begin
     = (options)
     =  ? ['id'] : nil
    role = find_role_by_name_or_id(, name)
    exit 1 if role.nil?

    params = {permissionCode: 'ComputeSite', access: access_value}
    if options[:dry_run]
      print_dry_run @roles_interface.dry.update_permission(, role['id'], params)
      return
    end
    json_response = @roles_interface.update_permission(, role['id'], params)

    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Role #{role['authority']} global group access updated"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update_global_instance_type_access(args) ⇒ Object



738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# File 'lib/morpheus/cli/roles.rb', line 738

def update_global_instance_type_access(args)
  usage = "Usage: morpheus roles update-global-instance-type-access [name] [full|custom|none]"
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name] [full|custom|none]")
    build_common_options(opts, options, [:json, :dry_run])
  end
  optparse.parse!(args)

  if args.count < 2
    puts optparse
    exit 1
  end
  name = args[0]
  access_value = args[1].to_s.downcase
  if !['full', 'custom', 'none'].include?(access_value)
    puts optparse
    exit 1
  end


  connect(options)
  begin
     = (options)
     =  ? ['id'] : nil
    role = find_role_by_name_or_id(, name)
    exit 1 if role.nil?

    params = {permissionCode: 'InstanceType', access: access_value}
    if options[:dry_run]
      print_dry_run @roles_interface.dry.update_permission(, role['id'], params)
      return
    end
    json_response = @roles_interface.update_permission(, role['id'], params)

    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Role #{role['authority']} global instance type access updated"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update_group_access(args) ⇒ Object



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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# File 'lib/morpheus/cli/roles.rb', line 559

def update_group_access(args)
  usage = "Usage: morpheus roles update-group-access [name] [group_name] [full|read|none]"
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name] [group] [full|read|none]")
    build_common_options(opts, options, [:json, :dry_run])
  end
  optparse.parse!(args)

  if args.count < 2
    puts optparse
    exit 1
  end
  name = args[0]
  group_name = args[1]
  access_value = args[2].to_s.downcase
  if !['full', 'read', 'none'].include?(access_value)
    puts optparse
    exit 1
  end

  connect(options)
  begin
     = (options)
     =  ? ['id'] : nil
    role = find_role_by_name_or_id(, name)
    exit 1 if role.nil?

    role_json = @roles_interface.get(, role['id'])
    if role_json['globalSiteAccess'] != 'custom'
      print "\n", red, "Global Group Access is currently: #{role_json['globalSiteAccess'].capitalize}"
      print "\n", "You must first set it to Custom via `morpheus roles update-global-group-access \"#{name}\" custom`"
      print "\n\n", reset
      exit 1
    end

    # group_id = find_group_id_by_name(group_name)
    # exit 1 if group_id.nil?
    group = find_group_by_name(group_name)
    exit 1 if group.nil?
    group_id = group['id']

    params = {groupId: group_id, access: access_value}
    if options[:dry_run]
      print_dry_run @roles_interface.dry.update_group(, role['id'], params)
      return
    end
    json_response = @roles_interface.update_group(, role['id'], params)

    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Role #{role['authority']} global group access updated"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update_instance_type_access(args) ⇒ Object



785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
# File 'lib/morpheus/cli/roles.rb', line 785

def update_instance_type_access(args)
  usage = "Usage: morpheus roles update-instance-type-access [name] [instance_type_name] [full|none]"
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name] [instance-type] [full|none]")
    build_common_options(opts, options, [:json, :dry_run])
  end
  optparse.parse!(args)

  if args.count < 2
    puts optparse
    exit 1
  end
  name = args[0]
  instance_type_name = args[1]
  access_value = args[2].to_s.downcase
  if !['full', 'none'].include?(access_value)
    puts optparse
    exit 1
  end

  connect(options)
  begin
     = (options)
     =  ? ['id'] : nil
    role = find_role_by_name_or_id(, name)
    exit 1 if role.nil?

    role_json = @roles_interface.get(, role['id'])
    if role_json['globalInstanceTypeAccess'] != 'custom'
      print "\n", red, "Global Instance Type Access is currently: #{role_json['globalInstanceTypeAccess'].capitalize}"
      print "\n", "You must first set it to Custom via `morpheus roles update-global-instance-type-access \"#{name}\" custom`"
      print "\n\n", reset
      exit 1
    end

    instance_type = find_instance_type_by_name(instance_type_name)
    exit 1 if instance_type.nil?

    params = {instanceTypeId: instance_type['id'], access: access_value}
    if options[:dry_run]
      print_dry_run @roles_interface.dry.update_instance_type(, role['id'], params)
      return
    end
    json_response = @roles_interface.update_instance_type(, role['id'], params)

    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Role #{role['authority']} global instance type access updated"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end