Class: Morpheus::Cli::AccountGroupsCommand

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

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from InfrastructureHelper

#cloud_type_for_id, #cloud_type_for_name, #cloud_type_for_name_or_id, #clouds_interface, #find_cloud_by_id, #find_cloud_by_name, #find_cloud_by_name_or_id, #find_group_by_id, #find_group_by_name, #find_group_by_name_or_id, #find_network_by_id, #find_network_by_name, #find_network_by_name_or_id, #find_network_group_by_id, #find_network_group_by_name, #find_network_group_by_name_or_id, #find_network_server_type_by_id, #find_network_server_type_by_name, #find_network_server_type_by_name_or_code, #find_network_server_type_by_name_or_id, #find_network_type_by_id, #find_network_type_by_name, #find_network_type_by_name_or_id, #find_resource_pool_by_id, #find_resource_pool_by_name, #find_resource_pool_by_name_or_id, #find_resource_pool_group_by_id, #find_resource_pool_group_by_name, #find_resource_pool_group_by_name_or_id, #find_subnet_by_id, #find_subnet_by_name, #find_subnet_by_name_or_id, #find_subnet_type_by_id, #find_subnet_type_by_name, #find_subnet_type_by_name_or_id, #format_network_pool_server_status, #get_available_cloud_types, #groups_interface, included, #network_groups_interface, #network_pool_server_column_definitions, #network_pool_server_list_column_definitions, #network_pool_server_type_column_definitions, #network_pool_server_type_list_column_definitions, #network_server_type_column_definitions, #network_server_type_list_column_definitions, #network_types_interface, #networks_interface, #prompt_for_network, #prompt_for_networks, #prompt_for_pools, #prompt_for_subnets, #resource_pool_groups_interface, #resource_pools_interface, #subnet_types_interface, #subnets_interface

Methods included from AccountsHelper

#account_column_definitions, #account_users_interface, #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

Methods included from CliCommand

#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_many_options, #build_standard_add_options, #build_standard_api_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, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #execute_api, #execute_api_payload, #execute_api_request, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_each_payload, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_options, #parse_parameter_as_resource_id!, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Constructor Details

#initializeAccountGroupsCommand

Returns a new instance of AccountGroupsCommand.



17
18
19
# File 'lib/morpheus/cli/commands/account_groups_command.rb', line 17

def initialize()
  # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
end

Instance Method Details

#add(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/account_groups_command.rb', line 167

def add(args)
  options = {}
  params = {}
  use_it = false
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[tenant] [name]")
    build_option_type_options(opts, options, add_group_option_types())
    # opts.on( '-l', '--location LOCATION', "Location" ) do |val|
    #   params[:location] = val
    # end

    build_common_options(opts, options, [:options, :json, :dry_run, :remote])
    opts.footer = "Create a new tenant group."
  end
  optparse.parse!(args)
  if args.count == 1
    options[:account] = args[0]
  elsif args.count == 2
    options[:account] = args[0]
    options[:group] = args[1]
  else
    raise_command_error "wrong number of arguments, expected 1-2 and got (#{args.count}) #{args}\n#{optparse}"
  end
  connect(options)
  begin
     = (options)
    return 1 if .nil?
     = ['id']

    group_payload = {}
    if options[:group]
      group_payload[:name] = options[:group]
      options[:options]['name'] = options[:group] # to skip prompt
    end
    if params[:location]
      group_payload[:name] = params[:location]
      options[:options]['location'] = params[:location] # to skip prompt
    end
    all_option_types = add_group_option_types()
    params = Morpheus::Cli::OptionTypes.prompt(all_option_types, options[:options], @api_client, {})
    group_payload.merge!(params)
    payload = {group: group_payload}
    @account_groups_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @account_groups_interface.dry.create(['id'], payload)
      return
    end
    json_response = @account_groups_interface.create(['id'], payload)
    group = json_response['group']
    
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Added group #{group['name']} to account #{['name']}"
      list(["-A", ['id'].to_s])
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#add_cloud(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/account_groups_command.rb', line 289

def add_cloud(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[tenant] [group] [cloud]")
    build_common_options(opts, options, [:account, :json, :dry_run, :remote])
    opts.footer = "Add a cloud to a tenant group."
  end
  optparse.parse!(args)
  if args.count == 3
    options[:account] = args[0]
    options[:group] = args[1]
    options[:cloud] = args[2]
  else
    raise_command_error "wrong number of arguments, expected 3 and got (#{args.count}) #{args}\n#{optparse}"
  end
  connect(options)
  begin
     = (options)
    return 1 if .nil?
     = ['id']

    group = (, options[:group])
    return 1 if group.nil?

    # err, this is going to find public clouds only, not those in the subaccount
    # good enough for now?
    cloud = find_cloud_by_name_or_id(options[:cloud])
    current_zones = group['zones']
    found_zone = current_zones.find {|it| it["id"] == cloud["id"] }
    if found_zone
      print_red_alert "Cloud #{cloud['name']} is already in group #{group['name']}."
      exit 1
    end
    new_zones = current_zones + [{'id' => cloud['id']}]
    payload = {group: {id: group["id"], zones: new_zones}}
    @account_groups_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @account_groups_interface.dry.update_zones(['id'], group["id"], payload)
      return
    end
    json_response = @account_groups_interface.update_zones(['id'], group["id"], payload)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Added cloud #{cloud["name"]} to group #{group['name']}"
      #list(["-A", account['id'].to_s])
      get([group["id"].to_s, "-A", ['id'].to_s])
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#command_nameObject



13
14
15
# File 'lib/morpheus/cli/commands/account_groups_command.rb', line 13

def command_name
  "tenants groups"
end

#connect(opts) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/morpheus/cli/commands/account_groups_command.rb', line 21

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @account_groups_interface = @api_client.
  @accounts_interface = @api_client.accounts

  @groups_interface = @api_client.groups
  @clouds_interface = @api_client.clouds
end

#get(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/account_groups_command.rb', line 90

def get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[tenant] [group]")
    build_common_options(opts, options, [:json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "Get details about a tenant group."
  end
  optparse.parse!(args)
  if args.count == 2
    options[:account] = args[0]
    options[:group] = args[1]
  else
    raise_command_error "wrong number of arguments, expected 2 and got (#{args.count}) #{args}\n#{optparse}"
  end
  connect(options)
  begin

     = (options)
    return 1 if .nil?
     = ['id']

    if options[:dry_run]
      @account_groups_interface.setopts(options)
      if options[:group].to_s =~ /\A\d{1,}\Z/
        print_dry_run @account_groups_interface.dry.get(, options[:group].to_i)
      else
        print_dry_run @account_groups_interface.dry.get(, {name: options[:group]})
      end
      return
    end

    group = (, options[:group])
    @account_groups_interface.setopts(options)
    return 1 if group.nil?
    # skip redundant request
    # json_response = @account_groups_interface.dry.get(account_id, options[:group].to_i)
    json_response = {"group" => group}
    
    if options[:json]
      puts as_json(json_response, options, "group")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "group")
      return 0
    elsif options[:csv]
      puts records_as_csv(json_response["group"], options)
      return 0
    end

    print_h1 "Group Details", options
    print cyan
    description_cols = {
      "ID" => 'id',
      "Name" => 'name',
      "Code" => 'code',
      "Location" => 'location',
      "Clouds" => lambda {|it| it['zones'].collect {|z| z['name'] }.join(', ') },
      "Hosts" => 'serverCount',
      "Tenant" => lambda {|it| ['name'] },
    }
    print_description_list(description_cols, group)
    # puts "ID: #{group['id']}"
    # puts "Name: #{group['name']}"
    # puts "Code: #{group['code']}"
    # puts "Location: #{group['location']}"
    # puts "Clouds: #{group['zones'].collect {|it| it['name'] }.join(', ')}"
    # puts "Hosts: #{group['serverCount']}"

    print reset,"\n"

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

#handle(args) ⇒ Object



30
31
32
# File 'lib/morpheus/cli/commands/account_groups_command.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
82
83
84
85
86
87
88
# File 'lib/morpheus/cli/commands/account_groups_command.rb', line 34

def list(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[tenant]")
    build_common_options(opts, options, [:list, :json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "List tenant groups."
  end
  optparse.parse!(args)
  if args.count == 1
    options[:account] = args[0]
  else
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
  end
  connect(options)
  begin
    # load account
     = (options)
    return 1 if .nil?
     = ['id']

    params.merge!(parse_list_options(options))
    @account_groups_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @account_groups_interface.dry.list(['id'], params)
      return
    end
    json_response = @account_groups_interface.list(['id'], params)
    if options[:json]
      puts as_json(json_response, options, "groups")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "groups")
      return 0
    elsif options[:csv]
      puts records_as_csv(json_response["groups"], options)
      return 0
    end
    groups = json_response['groups']
    title = "Morpheus Groups - Tenant: #{['name']}"
    subtitles = []
    subtitles += parse_list_subtitles(options)
    print_h1 title, subtitles, options
    if groups.empty?
      print cyan,"No groups found.",reset,"\n"
    else
      print_groups_table(groups, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#remove(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
# File 'lib/morpheus/cli/commands/account_groups_command.rb', line 399

def remove(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[tenant] [group]")
    build_common_options(opts, options, [:json, :dry_run, :auto_confirm, :remote])
    opts.footer = "Delete a tenant group."
  end
  optparse.parse!(args)
  if args.count == 2
    options[:account] = args[0]
    options[:group] = args[1]
  else
    raise_command_error "wrong number of arguments, expected 2 and got (#{args.count}) #{args}\n#{optparse}"
  end
  connect(options)

  begin
     = (options)
    return 1 if .nil?
     = ['id']

    group = (, options[:group])
    return 1 if group.nil?
    
    unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the group #{group['name']}?")
      exit
    end
    @account_groups_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @account_groups_interface.dry.destroy(['id'], group['id'])
      return
    end
    json_response = @account_groups_interface.destroy(['id'], group['id'])
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    elsif !options[:quiet]
      print_green_success "Removed group #{group['name']}"
      #list(["-A", account['id']])
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#remove_cloud(args) ⇒ Object



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/commands/account_groups_command.rb', line 344

def remove_cloud(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[tenant] [group] [cloud]")
    build_common_options(opts, options, [:account, :json, :dry_run, :remote])
    opts.footer = "Remove a cloud from a tenant group."
  end
  optparse.parse!(args)
  if args.count == 3
    options[:account] = args[0]
    options[:group] = args[1]
    options[:cloud] = args[2]
  else
    raise_command_error "wrong number of arguments, expected 3 and got (#{args.count}) #{args}\n#{optparse}"
  end
  connect(options)
  begin
     = (options)
    return 1 if .nil?
     = ['id']

    group = (, options[:group])
    return 1 if group.nil?

    # err, this is going to find public clouds only, not those in the subaccount
    # good enough for now?
    cloud = find_cloud_by_name_or_id(options[:cloud])
    current_zones = group['zones']
    found_zone = current_zones.find {|it| it["id"] == cloud["id"] }
    if !found_zone
      print_red_alert "Cloud #{cloud['name']} is not in group #{group['name']}."
      exit 1
    end
    new_zones = current_zones.reject {|it| it["id"] == cloud["id"] }
    payload = {group: {id: group["id"], zones: new_zones}}
    @account_groups_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @account_groups_interface.dry.update_zones(['id'], group["id"], payload)
      return
    end
    json_response = @account_groups_interface.update_zones(['id'], group["id"], payload)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Removed cloud #{cloud['name']} from group #{group['name']}"
      # list(["-A", account['id'].to_s])
      get([group["id"].to_s, "-A", ['id'].to_s])
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/account_groups_command.rb', line 230

def update(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[tenant] [group] [options]")
    build_option_type_options(opts, options, update_group_option_types())
    # opts.on( '-l', '--location LOCATION', "Location" ) do |val|
    #   params[:location] = val
    # end
    build_common_options(opts, options, [:account, :options, :json, :dry_run, :remote])
    opts.footer = "Update an existing tenant group."
  end
  optparse.parse!(args)
  if args.count == 2
    options[:account] = args[0]
    options[:group] = args[1]
  else
    raise_command_error "wrong number of arguments, expected 2 and got (#{args.count}) #{args}\n#{optparse}"
  end
  connect(options)
  begin
     = (options)
    return 1 if .nil?
     = ['id']

    group = (, options[:group])
    return 1 if group.nil?

    group_payload = {id: group['id']}

    params.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]

    if params.empty?
      puts optparse
      return 1
    end

    group_payload.merge!(params)

    payload = {group: group_payload}
    @account_groups_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @account_groups_interface.dry.update(['id'], group['id'], payload)
      return
    end
    json_response = @account_groups_interface.update(['id'], group['id'], payload)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      #list(["-A", account['id'].to_s])
      get([group["id"].to_s, "-A", ['id'].to_s])
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end