Class: Morpheus::Cli::Roles

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

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from WhoamiHelper

#current_account, #current_user, #current_user_permissions, included, #is_master_account, #load_whoami

Methods included from ProvisioningHelper

#accounts_interface, #add_perms_options, #api_client, #apps_interface, #cloud_datastores_interface, #clouds_interface, #datastores_interface, #find_app_by_id, #find_app_by_name, #find_app_by_name_or_id, #find_cloud_by_id_for_provisioning, #find_cloud_by_name_for_provisioning, #find_cloud_by_name_or_id_for_provisioning, #find_cloud_resource_pool_by_name_or_id, #find_group_by_id_for_provisioning, #find_group_by_name_for_provisioning, #find_group_by_name_or_id_for_provisioning, #find_host_by_id, #find_host_by_name, #find_host_by_name_or_id, #find_instance_by_id, #find_instance_by_name, #find_instance_by_name_or_id, #find_instance_type_by_code, #find_instance_type_by_id, #find_instance_type_by_name, #find_instance_type_by_name_or_id, #find_instance_type_layout_by_id, #find_server_by_id, #find_server_by_name, #find_server_by_name_or_id, #find_workflow_by_id, #find_workflow_by_name, #find_workflow_by_name_or_id, #format_app_status, #format_blueprint_type, #format_container_connection_string, #format_container_status, #format_instance_connection_string, #format_instance_container_display_name, #format_instance_status, #format_metadata, #format_snapshot_status, #get_available_accounts, #get_available_clouds, #get_available_environments, #get_available_groups, #get_available_plans, #get_provision_type_for_zone_type, #get_static_environments, included, #instance_type_layouts_interface, #instance_types_interface, #instances_interface, #load_balance_protocols_dropdown, #options_interface, #parse_blueprint_type, #parse_host_id_list, #parse_instance_id_list, #parse_metadata, #parse_resource_id_list, #parse_server_id_list, #print_permissions, #prompt_evars, #prompt_exposed_ports, #prompt_instance_load_balancer, #prompt_metadata, #prompt_network_interfaces, #prompt_new_instance, #prompt_permissions, #prompt_resize_volumes, #prompt_security_groups, #prompt_volumes, #provision_types_interface, #reject_service_plan_option_types, #servers_interface

Methods included from AccountsHelper

#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

Instance Method Details

#_get(id, options = {}) ⇒ Object



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
248
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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/morpheus/cli/roles.rb', line 138

def _get(id, options={})
  args = [id] # heh
  params = {}

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

    params.merge!(parse_query_options(options))

    @roles_interface.setopts(options)
    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']
    load_whoami()
    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

    render_response(json_response, options, 'role') do
    
    print cyan
    print_h1 "Role Details", options
    print cyan
    columns = @is_master_account ? role_column_definitions : subtenant_role_column_definitions
    print_description_list(columns, role, options)

    # print_h2 "Role Instance Limits", options
    # 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 "Permissions", options
    print cyan
    if options[:include_feature_access]
      rows = json_response['featurePermissions'].collect do |it|
        {
          code: it['code'],
          name: it['name'],
          access: format_access_string(it['access']),
        }
      end
      if options[:sort]
        rows.sort! {|a,b| a[options[:sort]] <=> b[options[:sort]] }
      end
      if options[:direction] == 'desc'
        rows.reverse!
      end
      if options[:phrase]
        phrase_regexp = /#{Regexp.escape(options[:phrase])}/i
        rows = rows.select {|row| row[:code].to_s =~ phrase_regexp || row[:name].to_s =~ phrase_regexp }
      end
      print as_pretty_table(rows, [:code, :name, :access], options)
      # print reset,"\n"
    else
      print cyan,"Use --permissions to list feature permissions","\n"
    end

    has_group_access = true
    has_cloud_access = true
    print_h2 "Global Access", options
    global_access_columns = {
      "Groups" => lambda {|it| get_access_string(it['globalSiteAccess']) },
      "Clouds" => lambda {|it| get_access_string(it['globalZoneAccess']) },
      "Instance Types" => lambda {|it| get_access_string(it['globalInstanceTypeAccess']) },
      "Blueprints" => lambda {|it| get_access_string(it['globalAppTemplateAccess'] || it['globalBlueprintAccess']) },
      "Catalog Item Types" => lambda {|it| get_access_string(it['globalCatalogItemTypeAccess']) },
      "VDI Pools" => lambda {|it| get_access_string(it['globalVdiPoolAccess']) },
    }
    if role['roleType'].to_s.downcase == 'account'
      global_access_columns.delete("Groups")
      has_group_access = false
    else
      global_access_columns.delete("Clouds")
      has_cloud_access = false
    end
    print as_pretty_table([json_response], global_access_columns, options)

    if has_group_access
      #print_h2 "Group Access: #{get_access_string(json_response['globalSiteAccess'])}", options
      print cyan
      if json_response['globalSiteAccess'] == 'custom'
        print_h2 "Group Access", options
        if options[:include_group_access]
          rows = json_response['sites'].collect do |it|
            {
              name: it['name'],
              access: format_access_string(it['access'], ["none","read","full"]),
            }
          end
          print as_pretty_table(rows, [:name, :access], options)
        else
          print cyan,"Use -g, --group-access to list custom access","\n"
        end
        # print reset,"\n"
      else
        # print "\n"
        # print cyan,bold,"Group Access: #{get_access_string(json_response['globalSiteAccess'])}",reset,"\n"
      end
    end
    
    if has_cloud_access
      print cyan
      #puts "Cloud Access: #{get_access_string(json_response['globalZoneAccess'])}"
      #print "\n"
      if json_response['globalZoneAccess'] == 'custom'
        print_h2 "Cloud Access", options
        if options[:include_cloud_access]
          rows = json_response['zones'].collect do |it|
            {
              name: it['name'],
              access: format_access_string(it['access'], ["none","read","full"]),
            }
          end
          print as_pretty_table(rows, [:name, :access], options)
        else
          print cyan,"Use -c, --cloud-access to list custom access","\n"
        end
        # print reset,"\n"
      else
        # print "\n"
        # print cyan,bold,"Cloud Access: #{get_access_string(json_response['globalZoneAccess'])}",reset,"\n"
      end
    end

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

    blueprint_global_access = json_response['globalAppTemplateAccess'] || json_response['globalBlueprintAccess']
    blueprint_permissions = json_response['appTemplatePermissions'] || json_response['blueprintPermissions'] || []
    print cyan
    # print_h2 "Blueprint Access: #{get_access_string(json_response['globalAppTemplateAccess'])}", options
    # print "\n"
    if blueprint_global_access == 'custom'
      print_h2 "Blueprint Access", options
      if options[:include_blueprint_access]
        rows = blueprint_permissions.collect do |it|
          {
            name: it['name'],
            access: format_access_string(it['access'], ["none","read","full"]),
          }
        end
        print as_pretty_table(rows, [:name, :access], options)
      else
        print cyan,"Use -b, --blueprint-access to list custom access","\n"
      end
      # print reset,"\n"
    else
      # print "\n"
      # print cyan,bold,"Blueprint Access: #{get_access_string(json_response['globalAppTemplateAccess'])}",reset,"\n"
    end

    
    catalog_item_type_global_access = json_response['globalCatalogItemTypeAccess']
    catalog_item_type_permissions = json_response['catalogItemTypePermissions'] || []
    print cyan
    # print_h2 "catalog_item_type Access: #{get_access_string(json_response['globalCatalogItemTypeAccess'])}", options
    # print "\n"
    if catalog_item_type_global_access == 'custom'
      print_h2 "Catalog Item Type Access", options
      if options[:include_catalog_item_type_access]
        rows = catalog_item_type_permissions.collect do |it|
          {
            name: it['name'],
            access: format_access_string(it['access'], ["none","read","full"]),
          }
        end
        print as_pretty_table(rows, [:name, :access], options)
      else
        print cyan,"Use --catalog-item-type-access to list custom access","\n"
      end
    else
      # print "\n"
      # print cyan,bold,"Catalog Item Type Access: #{get_access_string(json_response['globalCatalogItemTypeAccess'])}",reset,"\n"
    end
    

    persona_permissions = json_response['personaPermissions'] || json_response['personas'] || []
    # if options[:include_personas_access]
    print cyan
    if persona_permissions
      print_h2 "Persona Access", options
      rows = persona_permissions.collect do |it|
        {
          name: it['name'],
          access: format_access_string(it['access'], ["none","read","full"]),
        }
      end
      print as_pretty_table(rows, [:name, :access], options)        
    end

    # print reset,"\n"
    
    vdi_pool_global_access = json_response['globalVdiPoolAccess']
    vdi_pool_permissions = json_response['vdiPoolPermissions'] || []
    print cyan
    if vdi_pool_global_access == 'custom'
      print_h2 "VDI Pool Access", options
      if options[:include_vdi_pool_access]
        rows = vdi_pool_permissions.collect do |it|
          {
            name: it['name'],
            access: format_access_string(it['access'], ["none","read","full"]),
          }
        end
        print as_pretty_table(rows, [:name, :access], options)
      else
        print cyan,"Use --vdi-pool-access to list custom access","\n"
      end
    else
      # print "\n"
      # print cyan,bold,"VDI Pool Access: #{get_access_string(json_response['globalVdiPoolAccess'])}",reset,"\n"
    end

  end
  print reset,"\n"

  return 0, nil
end

#add(args) ⇒ Object



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

def add(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] [options]")
    build_option_type_options(opts, options, add_role_option_types)
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote])
  end
  optparse.parse!(args)
  if args.count > 1
    raise_command_error "wrong number of arguments, expected 0-1 and got (#{args.count}) #{args}\n#{optparse}"
  end
  if args[0]
    options[:options]['authority'] = args[0]
  end
  connect(options)
  begin

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

    passed_options = options[:options] ? options[:options].reject {|k,v| k.is_a?(Symbol) } : {}
    payload = nil
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!({'role' => passed_options}) unless passed_options.empty?
    else
      # merge -O options into normally parsed options
      params.deep_merge!(passed_options)

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

      role_payload = params
      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)
          if role_payload['multitenant']
            v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'multitenantLocked', 'fieldLabel' => 'Multitenant Locked', 'type' => 'checkbox', 'defaultValue' => 'off', 'description' => 'Prevents subtenants from branching off this role/modifying it. '}], options[:options])
            role_payload['multitenantLocked'] = ['on','true'].include?(v_prompt['multitenantLocked'].to_s)
          end
        end
      end

      # v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'defaultPersona', 'fieldLabel' => 'Default Persona', 'type' => 'select', 'optionSource' => 'personas', 'description' => 'Default Persona'}], options[:options], @api_client)
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'defaultPersona', 'fieldLabel' => 'Default Persona', 'type' => 'select', 'selectOptions' => get_persona_select_options(), 'description' => 'Default Persona'}], options[:options], @api_client)
      role_payload['defaultPersona'] = {'code' => v_prompt['defaultPersona']} unless v_prompt['defaultPersona'].to_s.strip.empty?

      payload = {"role" => role_payload}
    end
    @roles_interface.setopts(options)
    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

    role = json_response['role']
    display_name = role['authority'] rescue ''
    if 
      print_green_success "Added role #{display_name} to account #{['name']}"
    else
      print_green_success "Added role #{display_name}"
    end

    get_args = [role['id']] + (options[:remote] ? ["-r",options[:remote]] : [])
    if 
      get_args.push "--account-id", ['id'].to_s
    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



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/morpheus/cli/roles.rb', line 20

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @whoami_interface = @api_client.whoami
  @users_interface = @api_client.users
  @accounts_interface = @api_client.accounts
  @roles_interface = @api_client.roles
  @groups_interface = @api_client.groups
  @options_interface = @api_client.options
  @instances_interface = @api_client.instances
  @instance_types_interface = @api_client.instance_types
  @blueprints_interface = @api_client.blueprints
  @active_group_id = Morpheus::Cli::Groups.active_groups[@appliance_name]
end

#get(args) ⇒ Object



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
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/morpheus/cli/roles.rb', line 81

def get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[role]")
    opts.on('-p','--permissions', "Display Permissions") do |val|
      options[:include_feature_access] = true
    end
    opts.on('-f','--feature-access', "Display Permissions [deprecated]") do |val|
      options[:include_feature_access] = true
    end
    opts.add_hidden_option('--feature-access')
    opts.on('-g','--group-access', "Display Group Access") do
      options[:include_group_access] = true
    end
    opts.on('-c','--cloud-access', "Display Cloud Access") do
      options[:include_cloud_access] = true
    end
    opts.on('-i','--instance-type-access', "Display Instance Type Access") do
      options[:include_instance_type_access] = true
    end
    opts.on('-b','--blueprint-access', "Display Blueprint Access") do
      options[:include_blueprint_access] = true
    end
    opts.on(nil,'--catalog-item-type-access', "Display Catalog Item Type Access") do
      options[:include_catalog_item_type_access] = true
    end
    opts.on(nil,'--personas', "Display Persona Access") do
      options[:include_personas_access] = true
    end
    opts.on(nil,'--vdi-pool-access', "Display VDI Pool Access") do
      options[:include_vdi_pool_access] = true
    end
    opts.on('-a','--all', "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
      options[:include_blueprint_access] = true
      options[:include_catalog_item_type_access] = true
      options[:include_personas_access] = true
      options[:include_vdi_pool_access] = true
    end
    build_standard_get_options(opts, options)
    opts.footer = <<-EOT
Get details about a role.
[role] is required. This is the name (authority) or id of a role.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:1)
  connect(options)
  id_list = parse_id_list(args)
  return run_command_for_each_arg(id_list) do |arg|
    _get(arg, options)
  end
end

#handle(args) ⇒ Object



34
35
36
# File 'lib/morpheus/cli/roles.rb', line 34

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



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

def list(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[search phrase]")
    build_standard_list_options(opts, options)
    opts.footer = "List roles."
  end
  optparse.parse!(args)
  # verify_args!(args:args, optparse:optparse, count:0)
  options[:phrase] = args.join(" ") if args.count > 0
  connect(options)

   = (options)
   =  ? ['id'] : nil
  params = {}
  params.merge!(parse_list_options(options))
  @roles_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @roles_interface.dry.list(, params), options
    return 0, nil
  end
  load_whoami()
  json_response = @roles_interface.list(, params)

  render_response(json_response, options, "roles") do
    roles = json_response['roles']
    title = "Morpheus Roles"
    subtitles = []
    subtitles += parse_list_subtitles(options)
    print_h1 title, subtitles, options
    if roles.empty?
      print cyan,"No roles found.",reset,"\n"
    else
      print cyan
      columns = @is_master_account ? role_column_definitions : subtenant_role_column_definitions
      print as_pretty_table(roles, columns.upcase_keys!, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
  end
  return 0, nil
end

#list_permissions(args) ⇒ Object



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

def list_permissions(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[role]")
    build_common_options(opts, options, [:list, :json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "List the permissions for a role.\n" +
                  "[role] is required. This is the name or id of a role."
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  
     = (options)
     =  ? ['id'] : nil

    # role = find_role_by_name_or_id(account_id, args[0])
    # exit 1 if role.nil?

    @roles_interface.setopts(options)
    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

    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

    role_permissions = json_response['featurePermissions']

    if options[:json]
      puts as_json(role_permissions, options)
      return 0
    elsif options[:yaml]
      puts as_yaml(role_permissions, options)
      return 0
    elsif options[:csv]
      puts records_as_csv(role_permissions)
      return 0
    end

    print cyan
    print_h1 "Role Permissions: [#{role['id']}] #{role['authority']}", options

    print cyan
    if role_permissions && role_permissions.size > 0
      rows = role_permissions.collect do |it|
        {
          code: it['code'],
          name: it['name'],
          access: format_access_string(it['access']),
        }
      end
      if options[:sort]
        rows.sort! {|a,b| a[options[:sort]] <=> b[options[:sort]] }
      end
      if options[:direction] == 'desc'
        rows.reverse!
      end
      if options[:phrase]
        phrase_regexp = /#{Regexp.escape(options[:phrase])}/i
        rows = rows.select {|row| row[:code].to_s =~ phrase_regexp || row[:name].to_s =~ phrase_regexp }
      end
      print as_pretty_table(rows, [:code, :name, :access], options)
    else
      puts "No permissions found"
    end

    print reset,"\n"
    return 0
  
end

#remove(args) ⇒ Object



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

def remove(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[role]")
    build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
  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
    @roles_interface.setopts(options)
    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



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
619
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
665
666
667
668
669
670
# File 'lib/morpheus/cli/roles.rb', line 593

def update(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[role] [options]")
    build_option_type_options(opts, options, update_role_option_types)
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote])
  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?

    passed_options = options[:options] ? options[:options].reject {|k,v| k.is_a?(Symbol) } : {}
    payload = nil
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!({'role' => passed_options}) unless passed_options.empty?
    else
      # merge -O options into normally parsed options
      params.deep_merge!(passed_options)
      prompt_option_types = update_role_option_types()
      if !@is_master_account
        prompt_option_types = prompt_option_types.reject {|it| ['roleType', 'multitenant','multitenantLocked'].include?(it['fieldName']) }
      end
      if role['roleType'] != 'user'
        prompt_option_types = prompt_option_types.reject {|it| ['multitenant','multitenantLocked'].include?(it['fieldName']) }
      end
      #params = Morpheus::Cli::OptionTypes.prompt(prompt_option_types, options[:options], @api_client, options[:params])

      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

      payload = {"role" => params}
    end
    @roles_interface.setopts(options)
    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
    role = json_response['role']
    display_name = role['authority'] rescue ''
    print_green_success "Updated role #{display_name}"

    get_args = [role['id']] + (options[:remote] ? ["-r",options[:remote]] : [])
    if 
      get_args.push "--account-id", ['id'].to_s
    end
    get(get_args)

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

#update_blueprint_access(args) ⇒ Object



1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
# File 'lib/morpheus/cli/roles.rb', line 1254

def update_blueprint_access(args)
  options = {}
  blueprint_id = nil
  access_value = nil
  do_all = false
  allowed_access_values = ['full', 'none']
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[role] [blueprint] [access]")
    opts.on( '--blueprint ID', String, "Blueprint ID or Name" ) do |val|
      blueprint_id = val
    end
    opts.on( nil, '--all', "Update all blueprints at once." ) do
      do_all = true
    end
    opts.on( '--access VALUE', String, "Access value [#{allowed_access_values.join('|')}]" ) do |val|
      access_value = val
    end
    build_common_options(opts, options, [:json, :dry_run, :remote])
    opts.footer = "Update role access for an blueprint or all blueprints.\n" +
                  "[role] is required. This is the name or id of a role.\n" + 
                  "--blueprint or --all is required. This is the name or id of a blueprint.\n" + 
                  "--access is required. This is the new access value. #{anded_list(allowed_access_values)}"
  end
  optparse.parse!(args)

  # usage: update-blueprint-access [role] [access] --all
  #        update-blueprint-access [role] [blueprint] [access]
  name = args[0]
  if do_all
    verify_args!(args:args, optparse:optparse, min:1, max:2)
    access_value = args[1] if args[1]
  else
    verify_args!(args:args, optparse:optparse, min:1, max:3)
    blueprint_id = args[1] if args[1]
    access_value = args[2] if args[2]
  end
  if !blueprint_id && !do_all
    raise_command_error("missing required argument: [blueprint] or --all", optparse)
  end
  if !access_value
    raise_command_error("missing required argument: [access]", optparse)
  end
  access_value = access_value.to_s.downcase
  if !allowed_access_values.include?(access_value)
    raise_command_error("invalid access value: #{access_value}", optparse)
    puts optparse
    return 1
  end

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

    role_json = @roles_interface.get(, role['id'])
    blueprint_global_access = role_json['globalAppTemplateAccess'] || role_json['globalBlueprintAccess']
    blueprint_permissions = role_json['appTemplatePermissions'] || role_json['blueprintPermissions'] || []
    if blueprint_global_access != 'custom'
      print "\n", red, "Global Blueprint Access is currently: #{blueprint_global_access.to_s.capitalize}"
      print "\n", "You must first set it to Custom via `morpheus roles update-global-blueprint-access \"#{name}\" custom`"
      print "\n\n", reset
      return 1
    end

    # hacky, but support name or code lookup via the list returned in the show payload
    blueprint = nil
    if !do_all
      if blueprint_id.to_s =~ /\A\d{1,}\Z/
        blueprint = blueprint_permissions.find {|b| b['id'] == blueprint_id.to_i }
      else
        blueprint = blueprint_permissions.find {|b| b['name'] == blueprint_id || b['code'] == blueprint_id }
      end
      if blueprint.nil?
        print_red_alert "Blueprint not found: '#{blueprint_id}'"
        return 1
      end
    end

    params = {}
    if do_all
      params['allAppTemplates'] = true
      #params['allBlueprints'] = true
    else
      params['appTemplateId'] = blueprint['id']
      # params['blueprintId'] = blueprint['id']
    end
    params['access'] = access_value
    @roles_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @roles_interface.dry.update_blueprint(, role['id'], params)
      return
    end
    json_response = @roles_interface.update_blueprint(, role['id'], params)

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

#update_catalog_item_type_access(args) ⇒ Object



1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
# File 'lib/morpheus/cli/roles.rb', line 1415

def update_catalog_item_type_access(args)
  options = {}
  catalog_item_type_id = nil
  access_value = nil
  do_all = false
  allowed_access_values = ['full', 'none']
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[role] [catalog-item-type] [access]")
    opts.on( '--catalog-item-type ID', String, "Catalog Item Type ID or Name" ) do |val|
      catalog_item_type_id = val
    end
    opts.on( nil, '--all', "Update all catalog item types at once." ) do
      do_all = true
    end
    opts.on( '--access VALUE', String, "Access value [#{allowed_access_values.join('|')}]" ) do |val|
      access_value = val
    end
    build_common_options(opts, options, [:json, :dry_run, :remote])
    opts.footer = "Update role access for an catalog item type or all types.\n" +
                  "[role] is required. This is the name or id of a role.\n" + 
                  "--catalog-item-type or --all is required. This is the name or id of a catalog item type.\n" + 
                  "--access is required. This is the new access value. #{anded_list(allowed_access_values)}"
  end
  optparse.parse!(args)

  # usage: update-catalog_item_type-access [role] [access] --all
  #        update-catalog_item_type-access [role] [catalog-item-type] [access]
  name = args[0]
  if do_all
    verify_args!(args:args, optparse:optparse, min:1, max:2)
    access_value = args[1] if args[1]
  else
    verify_args!(args:args, optparse:optparse, min:1, max:3)
    catalog_item_type_id = args[1] if args[1]
    access_value = args[2] if args[2]
  end
  if !catalog_item_type_id && !do_all
    raise_command_error("missing required argument: [catalog-item-type] or --all", optparse)
  end
  if !access_value
    raise_command_error("missing required argument: [access]", optparse)
  end
  access_value = access_value.to_s.downcase
  if !allowed_access_values.include?(access_value)
    raise_command_error("invalid access value: #{access_value}", optparse)
    puts optparse
    return 1
  end

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

    role_json = @roles_interface.get(, role['id'])
    catalog_item_type_global_access = role_json['globalCatalogItemTypeAccess']
    catalog_item_type_permissions = role_json['catalogItemTypePermissions'] || []
    if catalog_item_type_global_access != 'custom'
      print "\n", red, "Global Catalog Item Type Access is currently: #{catalog_item_type_global_access.to_s.capitalize}"
      print "\n", "You must first set it to Custom via `morpheus roles update-global-catalog-item-type-access \"#{name}\" custom`"
      print "\n\n", reset
      return 1
    end

    # hacky, but support name or code lookup via the list returned in the show payload
    catalog_item_type = nil
    if !do_all
      if catalog_item_type_id.to_s =~ /\A\d{1,}\Z/
        catalog_item_type = catalog_item_type_permissions.find {|b| b['id'] == catalog_item_type_id.to_i }
      else
        catalog_item_type = catalog_item_type_permissions.find {|b| b['name'] == catalog_item_type_id }
      end
      if catalog_item_type.nil?
        print_red_alert "Catalog Item Type not found: '#{catalog_item_type_id}'"
        return 1
      end
    end

    params = {}
    if do_all
      params['allCatalogItemTypes'] = true
    else
      params['catalogItemTypeId'] = catalog_item_type['id']
    end
    params['access'] = access_value
    @roles_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @roles_interface.dry.update_catalog_item_type(, role['id'], params)
      return
    end
    json_response = @roles_interface.update_catalog_item_type(, role['id'], params)

    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      if do_all
        print_green_success "Role #{role['authority']} access updated for all catalog item types"
      else
        print_green_success "Role #{role['authority']} access updated for catalog item type #{catalog_item_type['name']}"
      end
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update_cloud_access(args) ⇒ Object



959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
# File 'lib/morpheus/cli/roles.rb', line 959

def update_cloud_access(args)
  options = {}
  cloud_id = nil
  access_value = nil
  do_all = false
  allowed_access_values = ['full', 'read', 'none']
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    opts.on( '-c', '--cloud CLOUD', "Cloud name or id" ) do |val|
      cloud_id = val
    end
    opts.on( nil, '--all', "Update all clouds at once." ) do
      do_all = true
    end
    opts.on( '--access VALUE', String, "Access value [#{allowed_access_values.join('|')}]" ) do |val|
      access_value = val
    end
    build_common_options(opts, options, [:json, :dry_run, :remote])
    opts.footer = "Update role access for a cloud or all clouds.\n" +
                  "[role] is required. This is the name or id of a role.\n" + 
                  "--cloud or --all is required. This is the name or id of a cloud.\n" + 
                  "--access is required. This is the new access value. #{anded_list(allowed_access_values)}"
  end
  optparse.parse!(args)

  # usage: update-cloud-access [role] [access] --all
  #        update-cloud-access [role] [cloud] [access]
  name = args[0]
  if do_all
    verify_args!(args:args, optparse:optparse, min:1, max:2)
    access_value = args[1] if args[1]
  else
    verify_args!(args:args, optparse:optparse, min:1, max:3)
    cloud_id = args[1] if args[1]
    access_value = args[2] if args[2]
  end
  if !cloud_id && !do_all
    raise_command_error("missing required argument: [cloud] or --all", optparse)
  end
  if !access_value
    raise_command_error("missing required argument: [access]", optparse)
  end
  access_value = access_value.to_s.downcase
  if !allowed_access_values.include?(access_value)
    raise_command_error("invalid access value: #{access_value}", optparse)
    puts optparse
    return 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

    cloud = nil
    if !do_all
      cloud = find_cloud_by_name_or_id_for_provisioning(nil, cloud_id)
      return 1 if cloud.nil?
      cloud_id = cloud['id']
    end
    params = {}
    if do_all
      params['allClouds'] = true
    else
      params['cloudId'] = cloud_id
    end
    params['access'] = access_value
    @roles_interface.setopts(options)
    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
      if do_all
        print_green_success "Role #{role['authority']} access updated for all clouds"
      else
        print_green_success "Role #{role['authority']} access updated for cloud #{cloud['name']}"
      end
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update_feature_access(args) ⇒ Object



713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
# File 'lib/morpheus/cli/roles.rb', line 713

def update_feature_access(args)
  usage = "Usage: morpheus roles update-feature-access [name] [code] [full|read|user|yes|no|none]"
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] [code] [full|read|user|yes|no|none]")
    build_common_options(opts, options, [:json, :dry_run, :remote])
  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}
    @roles_interface.setopts(options)
    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']} permission #{permission_code} set to #{access_value}"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update_global_blueprint_access(args) ⇒ Object



1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
# File 'lib/morpheus/cli/roles.rb', line 1206

def update_global_blueprint_access(args)
  usage = "Usage: morpheus roles update-global-blueprint-access [role] [full|custom|none]"
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[role] [full|custom|none]")
    build_common_options(opts, options, [:json, :dry_run, :remote])
  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: 'AppTemplate', access: access_value}
    @roles_interface.setopts(options)
    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 blueprint access updated"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update_global_catalog_item_type_access(args) ⇒ Object



1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
# File 'lib/morpheus/cli/roles.rb', line 1367

def update_global_catalog_item_type_access(args)
  usage = "Usage: morpheus roles update-global-catalog-item-type-access [role] [full|custom|none]"
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[role] [full|custom|none]")
    build_common_options(opts, options, [:json, :dry_run, :remote])
  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: 'CatalogItemType', access: access_value}
    @roles_interface.setopts(options)
    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 catalog item type access updated"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update_global_cloud_access(args) ⇒ Object



912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
# File 'lib/morpheus/cli/roles.rb', line 912

def update_global_cloud_access(args)
  usage = "Usage: morpheus roles update-global-cloud-access [name] [full|custom|none]"
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] [full|custom|none]")
    build_common_options(opts, options, [:json, :dry_run, :remote])
  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}
    @roles_interface.setopts(options)
    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



762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
# File 'lib/morpheus/cli/roles.rb', line 762

def update_global_group_access(args)
  usage = "Usage: morpheus roles update-global-group-access [name] [full|read|custom|none]"
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] [full|read|custom|none]")
    build_common_options(opts, options, [:json, :dry_run, :remote])
  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}
    @roles_interface.setopts(options)
    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



1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
# File 'lib/morpheus/cli/roles.rb', line 1060

def update_global_instance_type_access(args)
  usage = "Usage: morpheus roles update-global-instance-type-access [role] [full|custom|none]"
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[role] [full|custom|none]")
    build_common_options(opts, options, [:json, :dry_run, :remote])
  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}
    @roles_interface.setopts(options)
    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_global_vdi_pool_access(args) ⇒ Object



1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
# File 'lib/morpheus/cli/roles.rb', line 1619

def update_global_vdi_pool_access(args)
  usage = "Usage: morpheus roles update-global-vdi-pool-access [role] [full|custom|none]"
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[role] [full|custom|none]")
    build_common_options(opts, options, [:json, :dry_run, :remote])
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count: 2)
  name = args[0]
  access_value = args[1].to_s.downcase
  if !['full', 'custom', 'none'].include?(access_value)
    raise_command_error("invalid access value: #{args[1]}", optparse)
  end


  connect(options)
  begin
     = (options)
     =  ? ['id'] : nil
    role = find_role_by_name_or_id(, name)
    exit 1 if role.nil?
    # note: VdiPools being plural is odd, the others are singular
    params = {permissionCode: 'VdiPools', access: access_value}
    @roles_interface.setopts(options)
    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 vdi pool access updated"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update_group_access(args) ⇒ Object



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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
# File 'lib/morpheus/cli/roles.rb', line 809

def update_group_access(args)
  options = {}
  name = nil
  group_id = nil
  access_value = nil
  do_all = false
  allowed_access_values = ['full', 'read', 'none']
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[role] [group] [access]")
    opts.on( '-g', '--group GROUP', "Group name or id" ) do |val|
      group_id = val
    end
    opts.on( nil, '--all', "Update all groups at once." ) do
      do_all = true
    end
    opts.on( '--access VALUE', String, "Access value [#{allowed_access_values.join('|')}]" ) do |val|
      access_value = val
    end
    build_common_options(opts, options, [:json, :dry_run, :remote])
    opts.footer = "Update role access for a group or all groups.\n" +
                  "[role] is required. This is the name or id of a role.\n" + 
                  "--group or --all is required. This is the name or id of a group.\n" + 
                  "--access is required. This is the new access value. #{anded_list(allowed_access_values)}"
  end
  optparse.parse!(args)

  # usage: update-group-access [role] [access] --all
  #        update-group-access [role] [group] [access]
  name = args[0]
  if do_all
    verify_args!(args:args, optparse:optparse, min:1, max:2)
    access_value = args[1] if args[1]
  else
    verify_args!(args:args, optparse:optparse, min:1, max:3)
    group_id = args[1] if args[1]
    access_value = args[2] if args[2]
  end
  if !group_id && !do_all
    raise_command_error("missing required argument: [group] or --all", optparse)
  end
  if !access_value
    raise_command_error("missing required argument: [access]", optparse)
  end
  access_value = access_value.to_s.downcase
  if !allowed_access_values.include?(access_value)
    raise_command_error("invalid access value: #{access_value}", optparse)
    puts optparse
    return 1
  end

  connect(options)
  begin
     = (options)
     =  ? ['id'] : nil
    role = find_role_by_name_or_id(, name)
    return 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 = nil
    if !do_all
      group = find_group_by_name_or_id_for_provisioning(group_id)
      return 1 if group.nil?
      group_id = group['id']
    end

    params = {}
    if do_all
      params['allGroups'] = true
    else
      params['groupId'] = group_id
    end
    params['access'] = access_value
    @roles_interface.setopts(options)
    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
      if do_all
        print_green_success "Role #{role['authority']} access updated for all groups"
      else
        print_green_success "Role #{role['authority']} access updated for group #{group['name']}"
      end
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update_instance_type_access(args) ⇒ Object



1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
# File 'lib/morpheus/cli/roles.rb', line 1108

def update_instance_type_access(args)
  options = {}
  instance_type_name = nil
  access_value = nil
  do_all = false
  allowed_access_values = ['full', 'none']
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[role] [type] [access]")
    opts.on( '--instance-type INSTANCE_TYPE', String, "Instance Type name" ) do |val|
      instance_type_name = val
    end
    opts.on( nil, '--all', "Update all instance types at once." ) do
      do_all = true
    end
    opts.on( '--access VALUE', String, "Access value [#{allowed_access_values.join('|')}]" ) do |val|
      access_value = val
    end
    build_common_options(opts, options, [:json, :dry_run, :remote])
    opts.footer = "Update role access for an instance type or all instance types.\n" +
                  "[role] is required. This is the name or id of a role.\n" + 
                  "--instance-type or --all is required. This is the name of an instance type.\n" + 
                  "--access is required. This is the new access value. #{anded_list(allowed_access_values)}"
  end
  optparse.parse!(args)

  if args.count < 1
    puts optparse
    return 1
  end
  name = args[0]
  # support old usage: [role] [instance-type] [access]
  instance_type_name ||= args[1]
  access_value ||= args[2]

  if (!instance_type_name && !do_all) || !access_value
    puts optparse
    return 1
  end
  
  access_value = access_value.to_s.downcase

  if !['full', 'none'].include?(access_value)
    puts optparse
    return 1
  end

  connect(options)
  begin
     = (options)
     =  ? ['id'] : nil
    role = find_role_by_name_or_id(, name)
    return 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
      return 1
    end

    instance_type = nil
    if !do_all
      instance_type = find_instance_type_by_name(instance_type_name)
      return 1 if instance_type.nil?
    end

    params = {}
    if do_all
      params['allInstanceTypes'] = true
    else
      params['instanceTypeId'] = instance_type['id']
    end
    params['access'] = access_value
    @roles_interface.setopts(options)
    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
      if do_all
        print_green_success "Role #{role['authority']} access updated for all instance types"
      else
        print_green_success "Role #{role['authority']} access updated for instance type #{instance_type['name']}"
      end
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update_persona_access(args) ⇒ Object



1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
# File 'lib/morpheus/cli/roles.rb', line 1526

def update_persona_access(args)
  options = {}
  persona_id = nil
  name = nil
  access_value = nil
  do_all = false
  allowed_access_values = ['full', 'none']
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[role] [persona] [access]")
    opts.on( '--persona CODE', String, "Persona Code" ) do |val|
      persona_id = val
    end
    opts.on( nil, '--all', "Update all personas at once." ) do
      do_all = true
    end
    opts.on( '--access VALUE', String, "Access value [#{allowed_access_values.join('|')}]" ) do |val|
      access_value = val
    end
    build_common_options(opts, options, [:json, :dry_run, :remote])
    opts.footer = "Update role access for a persona or all personas.\n" +
                  "[role] is required. This is the name or id of a role.\n" + 
                  "--persona or --all is required. This is the code of a persona. Service Catalog, Standard, or Virtual Desktop\n" + 
                  "--access is required. This is the new access value. #{anded_list(allowed_access_values)}"
  end
  optparse.parse!(args)

  # usage: update-persona-access [role] [access] --all
  #        update-persona-access [role] [persona] [access]
  name = args[0]
  if do_all
    verify_args!(args:args, optparse:optparse, min:1, max:2)
    access_value = args[1] if args[1]
  else
    verify_args!(args:args, optparse:optparse, min:1, max:3)
    persona_id = args[1] if args[1]
    access_value = args[2] if args[2]
  end
  if !persona_id && !do_all
    raise_command_error("missing required argument: [persona] or --all", optparse)
  end
  if !access_value
    raise_command_error("missing required argument: [access]", optparse)
  end
  access_value = access_value.to_s.downcase
  if !allowed_access_values.include?(access_value)
    raise_command_error("invalid access value: #{access_value}", optparse)
    puts optparse
    return 1
  end

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

    role_json = @roles_interface.get(, role['id'])
    
    # no lookup right now, pass the code serviceCatalog|standard
    persona_code = persona_id

    params = {}
    if do_all
      params['allPersonas'] = true
    else
      params['personaCode'] = persona_code
    end
    params['access'] = access_value
    @roles_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @roles_interface.dry.update_persona(, role['id'], params)
      return
    end
    json_response = @roles_interface.update_persona(, role['id'], params)

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

#update_vdi_pool_access(args) ⇒ Object



1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
# File 'lib/morpheus/cli/roles.rb', line 1662

def update_vdi_pool_access(args)
  options = {}
  vdi_pool_id = nil
  access_value = nil
  do_all = false
  allowed_access_values = ['full', 'none']
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[role] [vdi-pool] [access]")
    opts.on( '--vdi-pool ID', String, "VDI Pool ID or Name" ) do |val|
      vdi_pool_id = val
    end
    opts.on( nil, '--all', "Update all VDI pools at once." ) do
      do_all = true
    end
    opts.on( '--access VALUE', String, "Access value [#{allowed_access_values.join('|')}]" ) do |val|
      access_value = val
    end
    build_common_options(opts, options, [:json, :dry_run, :remote])
    opts.footer = "Update role access for a VDI pool or all VDI pools.\n" +
                  "[role] is required. This is the name or id of a role.\n" + 
                  "--vdi-pool or --all is required. This is the name or id of a VDI pool.\n" + 
                  "--access is required. This is the new access value. #{anded_list(allowed_access_values)}"
  end
  optparse.parse!(args)

  # usage: update-vdi-pool-access [role] [access] --all
  #        update-vdi-pool-access [role] [vdi-pool] [access]
  name = args[0]
  if do_all
    verify_args!(args:args, optparse:optparse, min:1, max:2)
    access_value = args[1] if args[1]
  else
    verify_args!(args:args, optparse:optparse, min:1, max:3)
    vdi_pool_id = args[1] if args[1]
    access_value = args[2] if args[2]
  end
  if !vdi_pool_id && !do_all
    raise_command_error("missing required argument: [vdi-pool] or --all", optparse)
  end
  if !access_value
    raise_command_error("missing required argument: [access]", optparse)
  end
  access_value = access_value.to_s.downcase
  if !allowed_access_values.include?(access_value)
    raise_command_error("invalid access value: #{access_value}", optparse)
    puts optparse
    return 1
  end

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

    role_json = @roles_interface.get(, role['id'])
    vdi_pool_global_access = role_json['globalVdiPoolAccess']
    vdi_pool_permissions = role_json['vdiPoolPermissions'] || []
    if vdi_pool_global_access != 'custom'
      print "\n", red, "Global VDI Pool Access is currently: #{vdi_pool_global_access.to_s.capitalize}"
      print "\n", "You must first set it to Custom via `morpheus roles update-global-vdi-pool-access \"#{name}\" custom`"
      print "\n\n", reset
      return 1
    end

    # hacky, but support name or code lookup via the list returned in the show payload
    vdi_pool = nil
    if !do_all
      if vdi_pool_id.to_s =~ /\A\d{1,}\Z/
        vdi_pool = vdi_pool_permissions.find {|b| b['id'] == vdi_pool_id.to_i }
      else
        vdi_pool = vdi_pool_permissions.find {|b| b['name'] == vdi_pool_id }
      end
      if vdi_pool.nil?
        print_red_alert "VDI Pool not found: '#{vdi_pool_id}'"
        return 1
      end
    end

    params = {}
    if do_all
      params['allVdiPools'] = true
    else
      params['vdiPoolId'] = vdi_pool['id']
    end
    params['access'] = access_value
    @roles_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @roles_interface.dry.update_vdi_pool(, role['id'], params)
      return
    end
    json_response = @roles_interface.update_vdi_pool(, role['id'], params)

    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      if do_all
        print_green_success "Role #{role['authority']} access updated for all VDI pools"
      else
        print_green_success "Role #{role['authority']} access updated for VDI pool #{vdi_pool['name']}"
      end
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end