Class: Morpheus::Cli::Clouds

Inherits:
Object
  • Object
show all
Includes:
CliCommand, InfrastructureHelper, ProvisioningHelper
Defined in:
lib/morpheus/cli/clouds.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

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 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_type_by_id, #find_network_type_by_name, #find_network_type_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, #get_available_cloud_types, #groups_interface, included, #network_groups_interface, #network_types_interface, #networks_interface, #prompt_for_network, #prompt_for_networks, #prompt_for_subnets, #subnet_types_interface, #subnets_interface

Methods included from CliCommand

#apply_options, #build_common_options, #build_option_type_options, #build_standard_add_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #get_subcommand_description, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_bytes_param, #parse_id_list, #parse_list_options, #parse_list_subtitles, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Constructor Details

#initializeClouds

Returns a new instance of Clouds.



21
22
23
# File 'lib/morpheus/cli/clouds.rb', line 21

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

Instance Method Details

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



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

def _get(arg, options={})
  begin
    if options[:dry_run]
      @clouds_interface.setopts(options)
      if arg.to_s =~ /\A\d{1,}\Z/
        print_dry_run @clouds_interface.dry.get(arg.to_i)
      else
        print_dry_run @clouds_interface.dry.list({name:arg})
      end
      return
    end
    cloud = find_cloud_by_name_or_id(arg)
    @clouds_interface.setopts(options)
    json_response = @clouds_interface.get(cloud['id'])
    cloud = json_response['zone']
    cloud_stats = cloud['stats']
    # serverCounts moved to zone.stats.serverCounts
    server_counts = nil
    if cloud_stats
      server_counts = cloud_stats['serverCounts']
    else
      server_counts = json_response['serverCounts'] # legacy
    end
    if options[:json]
      puts as_json(json_response, options, 'zone')
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, 'zone')
      return 0
    end
    if options[:csv]
      puts records_as_csv([json_response['zone']], options)
      return 0
    end
    cloud_type = cloud_type_for_id(cloud['zoneTypeId'])
    print_h1 "Cloud Details"
    print cyan
    description_cols = {
      "ID" => 'id',
      "Name" => 'name',
      "Type" => lambda {|it| cloud_type ? cloud_type['name'] : '' },
      "Code" => 'code',
      "Location" => 'location',
      "Visibility" => lambda {|it| it['visibility'].to_s.capitalize },
      "Groups" => lambda {|it| it['groups'].collect {|g| g.instance_of?(Hash) ? g['name'] : g.to_s }.join(', ') },
      #"Owner" => lambda {|it| it['owner'].instance_of?(Hash) ? it['owner']['name'] : it['ownerId'] },
      "Tenant" => lambda {|it| it['account'].instance_of?(Hash) ? it['account']['name'] : it['accountId'] },
      "Enabled" => lambda {|it| format_boolean(it['enabled']) },
      "Status" => lambda {|it| format_cloud_status(it) }
    }
    print_description_list(description_cols, cloud)
    
    print_h2 "Cloud Servers"
    print cyan
    if server_counts
      print "Container Hosts: #{server_counts['containerHost']}".center(20)
      print "Hypervisors: #{server_counts['hypervisor']}".center(20)
      print "Bare Metal: #{server_counts['baremetal']}".center(20)
      print "Virtual Machines: #{server_counts['vm']}".center(20)
      print "Unmanaged: #{server_counts['unmanaged']}".center(20)
      print "\n"
    end

    print reset,"\n"

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

#add(args) ⇒ Object



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

def add(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] --group GROUP --type TYPE")
    opts.on( '-g', '--group GROUP', "Group Name" ) do |val|
      params[:group] = val
    end
    opts.on( '-t', '--type TYPE', "Cloud Type" ) do |val|
      params[:zone_type] = val
    end
    opts.on( '-d', '--description DESCRIPTION', "Description (optional)" ) do |desc|
      params[:description] = desc
    end
    opts.on( '--certificate-provider CODE', String, "Certificate Provider. Default is 'internal'" ) do |val|
      params[:certificate_provider] = val
    end
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote])
  end
  optparse.parse!(args)
  # if args.count < 1
  #   puts optparse
  #   exit 1
  # end
  connect(options)

  begin
    payload = nil
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!({'zone' => parse_passed_options(options)})
    else
      cloud_payload = {name: args[0], description: params[:description]}
      cloud_payload.deep_merge!(parse_passed_options(options))
      # use active group by default
      params[:group] ||= @active_group_id

      # Group
      group_id = nil
      group = params[:group] ? find_group_by_name_or_id_for_provisioning(params[:group]) : nil
      if group
        group_id = group["id"]
      else
        # print_red_alert "Group not found or specified!"
        # exit 1
        #groups_dropdown = @groups_interface.list({})['groups'].collect {|it| {'name' => it["name"], 'value' => it["id"]} }
        group_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'group', 'type' => 'select', 'fieldLabel' => 'Group', 'optionSource' => 'groups', 'required' => true, 'description' => 'Select Group.'}],options[:options],@api_client,{})
        group_id = group_prompt['group']
      end
      cloud_payload['groupId'] = group_id
      # todo: pass groups as an array instead

      # Cloud Name

      if args[0]
        cloud_payload[:name] = args[0]
        options[:options]['name'] = args[0] # to skip prompt
      elsif !options[:no_prompt]
        # name_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true}], options[:options])
        # cloud_payload[:name] = name_prompt['name']
      end

      # Cloud Type

      cloud_type = nil
      if params[:zone_type]
        cloud_type = cloud_type_for_name(params[:zone_type])
      elsif !options[:no_prompt]
        # print_red_alert "Cloud Type not found or specified!"
        # exit 1
        cloud_types_dropdown = cloud_types_for_dropdown
        cloud_type_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'type', 'type' => 'select', 'fieldLabel' => 'Cloud Type', 'selectOptions' => cloud_types_dropdown, 'required' => true, 'description' => 'Select Cloud Type.'}],options[:options],@api_client,{})
        cloud_type_code = cloud_type_prompt['type']
        cloud_type = cloud_type_for_name(cloud_type_code) # this does work
      end
      if !cloud_type
        print_red_alert "A cloud type is required."
        exit 1
      end
      cloud_payload[:zoneType] = {code: cloud_type['code']}

      cloud_payload['config'] ||= {}
      if params[:certificate_provider]
        cloud_payload['config']['certificateProvider'] = params[:certificate_provider]
      else
        cloud_payload['config']['certificateProvider'] = 'internal'
      end

      all_option_types = add_cloud_option_types(cloud_type)

      params = Morpheus::Cli::OptionTypes.prompt(all_option_types, options[:options], @api_client, {zoneTypeId: cloud_type['id']})
      # some optionTypes have fieldContext='zone', so move those to the root level of the zone payload
      if params['zone'].is_a?(Hash)
        cloud_payload.deep_merge!(params.delete('zone'))
      end
      cloud_payload.deep_merge!(params)
      payload = {zone: cloud_payload}
    end
    @clouds_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @clouds_interface.dry.create(payload)
      return
    end
    json_response = @clouds_interface.create(payload)
    cloud = json_response['zone']
    if options[:json]
      puts as_json(json_response, options)
    else
      get([cloud['id']] + (options[:remote] ? ["-r",options[:remote]] : []))
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#apply_security_groups(args) ⇒ Object



666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
# File 'lib/morpheus/cli/clouds.rb', line 666

def apply_security_groups(args)
  options = {}
  clear_or_secgroups_specified = false
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] [-s] [--clear]")
    opts.on( '-c', '--clear', "Clear all security groups" ) do
      options[:securityGroupIds] = []
      clear_or_secgroups_specified = true
    end
    opts.on( '-s', '--secgroups SECGROUPS', "Apply the specified comma separated security group ids" ) do |secgroups|
      options[:securityGroupIds] = secgroups.split(",")
      clear_or_secgroups_specified = true
    end
    build_common_options(opts, options, [:json, :dry_run, :remote])
  end
  optparse.parse!(args)
  if !clear_or_secgroups_specified
    puts optparse
    exit
  end
  connect(options)
  begin
    cloud = find_cloud_by_name_or_id(args[0])
    @clouds_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @clouds_interface.dry.apply_security_groups(cloud['id'])
      return
    end
    json_response = @clouds_interface.apply_security_groups(cloud['id'], options)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
      return
    end
    security_groups([args[0]])
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#connect(opts) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/morpheus/cli/clouds.rb', line 25

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @clouds_interface = @api_client.clouds
  @groups_interface = @api_client.groups
  @active_group_id = Morpheus::Cli::Groups.active_groups[@appliance_name]
  # preload stuff
  get_available_cloud_types()
end

#count(args) ⇒ Object



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

def count(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[options]")
    build_common_options(opts, options, [:query, :remote, :dry_run])
    opts.footer = "Get the number of clouds."
  end
  optparse.parse!(args)
  connect(options)
  begin
    params = {}
    params.merge!(parse_list_options(options))
    @clouds_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @clouds_interface.dry.list(params)
      return
    end
    json_response = @clouds_interface.list(params)
    # print number only
    if json_response['meta'] && json_response['meta']['total']
      print cyan, json_response['meta']['total'], reset, "\n"
    else
      print yellow, "unknown", reset, "\n"
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#firewall_disable(args) ⇒ Object



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

def firewall_disable(args)
  options = {}
  clear_or_secgroups_specified = false
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:json, :dry_run, :remote])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    return
  end
  connect(options)
  begin
    cloud = find_cloud_by_name_or_id(args[0])
    @clouds_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @clouds_interface.dry.firewall_disable(cloud['id'])
      return
    end
    json_response = @clouds_interface.firewall_disable(cloud['id'])
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
      return
    end
    security_groups([args[0]])
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#firewall_enable(args) ⇒ Object



586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'lib/morpheus/cli/clouds.rb', line 586

def firewall_enable(args)
  options = {}
  clear_or_secgroups_specified = false
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:json, :dry_run, :remote])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    return
  end
  connect(options)
  begin
    cloud = find_cloud_by_name_or_id(args[0])
    @clouds_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @clouds_interface.dry.firewall_enable(cloud['id'])
      return
    end
    json_response = @clouds_interface.firewall_enable(cloud['id'])
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
      return
    end
    security_groups([args[0]])
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#get(args) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/morpheus/cli/clouds.rb', line 135

def get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "Get details about a cloud.\n" +
                  "[name] is required. This is the name or id of a cloud."
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  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/clouds.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/morpheus/cli/clouds.rb', line 38

def list(args)
  options={}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    opts.on( '-g', '--group GROUP', "Group Name" ) do |group|
      options[:group] = group
    end
    opts.on( '-t', '--type TYPE', "Cloud Type" ) do |val|
      options[:zone_type] = val
    end
    build_standard_list_options(opts, options)
    opts.footer = "List clouds."
  end
  optparse.parse!(args)
  connect(options)
  # verify_args!(args:args, optparse:optparse, count:0)
  if args.count > 0
    options[:phrase] = args.join(" ")
  end
  begin
    if options[:zone_type]
      cloud_type = cloud_type_for_name(options[:zone_type])
      params[:type] = cloud_type['code']
    end
    if !options[:group].nil?
      group = find_group_by_name(options[:group])
      if !group.nil?
        params['groupId'] = group['id']
      end
    end

    params.merge!(parse_list_options(options))
    @clouds_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @clouds_interface.dry.list(params)
      return 0
    end

    json_response = @clouds_interface.list(params)
    render_response(json_response, options, 'zones') do
      clouds = json_response['zones']
      title = "Morpheus Clouds"
      subtitles = []
      if group
        subtitles << "Group: #{group['name']}".strip
      end
      if cloud_type
        subtitles << "Type: #{cloud_type['name']}".strip
      end
      subtitles += parse_list_subtitles(options)
      print_h1 title, subtitles
      if clouds.empty?
        print cyan,"No clouds found.",reset,"\n"
      else
        print_clouds_table(clouds, options)
        print_results_pagination(json_response)
      end
      print reset,"\n"
    end
    return 0, nil
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#list_cloud_types(args) ⇒ Object



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

def list_cloud_types(args)
  options={}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:list, :query, :json, :yaml, :csv, :fields, :dry_run, :remote])
  end
  optparse.parse!(args)
  connect(options)
  begin
    params.merge!(parse_list_options(options))
    @clouds_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @clouds_interface.dry.cloud_types({})
      return 0
    end
    json_response = @clouds_interface.cloud_types(params)
    
    render_result = render_with_format(json_response, options, 'zoneTypes')
    return 0 if render_result

    #cloud_types = get_available_cloud_types()
    cloud_types = json_response['zoneTypes']

    title = "Morpheus Cloud Types"
    subtitles = []
      
    subtitles += parse_list_subtitles(options)
    print_h1 title, subtitles

    if cloud_types.empty?
      print cyan,"No cloud types found.",reset,"\n"
    else
      print cyan
      cloud_types = cloud_types.select {|it| it['enabled'] }
      rows = cloud_types.collect do |cloud_type|
        {id: cloud_type['id'], name: cloud_type['name'], code: cloud_type['code']}
      end
      #print "\n"
      columns = [:id, :name, :code]
      columns = options[:include_fields] if options[:include_fields]
      print as_pretty_table(rows, columns, options)
      print_results_pagination(json_response)
      print reset,"\n"
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#refresh(args) ⇒ Object



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/morpheus/cli/clouds.rb', line 454

def refresh(args)
  options = {}
  query_params = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] [options]")
    opts.on( '-f', '--force', "Force refresh. Useful if the cloud is disabled." ) do
      query_params[:force] = 'true'
    end
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote])
    opts.footer = "Refresh a cloud." + "\n" +
                  "[cloud] is required. This is the name or id of a cloud."
  end
  optparse.parse!(args)
  if args.count != 1
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
  end
  connect(options)
  begin
    cloud = find_cloud_by_name_or_id(args[0])
    return 1 if cloud.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!(passed_options) unless passed_options.empty?
    else
      payload = {}
      payload.deep_merge!(passed_options) unless passed_options.empty?
    end
    @clouds_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @clouds_interface.dry.refresh(cloud['id'], query_params, payload)
      return
    end
    json_response = @clouds_interface.refresh(cloud['id'], query_params, payload)
    if options[:json]
      puts as_json(json_response, options)
    else
      print_green_success "Refreshing cloud #{cloud['name']}..."
      #get([cloud['id']] + (options[:remote] ? ["-r",options[:remote]] : []))
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#remove(args) ⇒ Object



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

def remove(args)
  options = {}
  query_params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    opts.on( '-f', '--force', "Force Remove" ) do
      query_params[:force] = 'on'
    end
    build_common_options(opts, options, [:auto_confirm, :quiet, :json, :dry_run, :remote])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    return
  end
  connect(options)
  begin
    cloud = find_cloud_by_name_or_id(args[0])
    unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the cloud #{cloud['name']}?")
      exit
    end
    @clouds_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @clouds_interface.dry.destroy(cloud['id'], query_params)
      return 0
    end
    json_response = @clouds_interface.destroy(cloud['id'], query_params)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    elsif !options[:quiet]
      print_green_success "Removed cloud #{cloud['name']}"
      #list([])
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#security_groups(args) ⇒ Object



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

def security_groups(args)
  options = {}
  clear_or_secgroups_specified = false
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:json, :dry_run, :remote])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    return
  end
  connect(options)
  begin
    cloud = find_cloud_by_name_or_id(args[0])
    zone_id = cloud['id']
    @clouds_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @clouds_interface.dry.security_groups(zone_id)
      return
    end
    json_response = @clouds_interface.security_groups(zone_id)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
      return
    end
    securityGroups = json_response['securityGroups']
    print_h1 "Morpheus Security Groups for Cloud: #{cloud['name']}"
    print cyan
    print_description_list({"Firewall Enabled" => lambda {|it| format_boolean it['firewallEnabled'] } }, json_response)
    if securityGroups.empty?
      print yellow,"\n","No security groups currently applied.",reset,"\n"
    else
      print "\n"
      securityGroups.each do |securityGroup|
        print cyan, "=  #{securityGroup['id']} (#{securityGroup['name']}) - (#{securityGroup['description']})\n"
      end
    end
    print reset,"\n"

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

#sync(args) ⇒ Object

not exposed yet, refresh should be all that’s needed.



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

def sync(args)
  options = {}
  query_params = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] [options]")
    opts.on( '-f', '--force', "Force Delete" ) do
      query_params[:force] = 'true'
    end
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote])
    opts.footer = "Sync a cloud." + "\n" +
                  "[cloud] is required. This is the name or id of a cloud."
  end
  optparse.parse!(args)
  if args.count != 1
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
  end
  connect(options)
  begin
    cloud = find_cloud_by_name_or_id(args[0])
    return 1 if cloud.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!(passed_options) unless passed_options.empty?
    else
      payload = {}
      payload.deep_merge!(passed_options) unless passed_options.empty?
    end
    @clouds_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @clouds_interface.dry.sync(cloud['id'], query_params, payload)
      return
    end
    json_response = @clouds_interface.sync(cloud['id'], query_params, payload)
    if options[:json]
      puts as_json(json_response, options)
    else
      print_green_success "Syncing cloud #{cloud['name']}..."
      #get([cloud['id']] + (options[:remote] ? ["-r",options[:remote]] : []))
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update(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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/morpheus/cli/clouds.rb', line 344

def update(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] [options]")
    # opts.on( '-g', '--group GROUP', "Group Name" ) do |val|
    #   params[:group] = val
    # end
    # opts.on( '-t', '--type TYPE', "Cloud Type" ) do |val|
    #   params[:zone_type] = val
    # end
    # opts.on( '-d', '--description DESCRIPTION', "Description (optional)" ) do |desc|
    #   params[:description] = desc
    # end
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  connect(options)
  begin
    cloud = find_cloud_by_name_or_id(args[0])
    return 1 if cloud.nil?
    payload = nil
    if options[:payload]
      payload = options[:payload]
      # support -O OPTION switch on top of --payload
      if options[:options]
        payload['zone'] ||= {}
        payload['zone'].deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) })
      end
    else
      cloud_type = cloud_type_for_id(cloud['zoneTypeId'])
      cloud_payload = {}
      all_option_types = update_cloud_option_types(cloud_type)
      #params = Morpheus::Cli::OptionTypes.prompt(all_option_types, options[:options], @api_client, {zoneTypeId: cloud_type['id']})
      params = options[:options] || {}
      if params.empty?
        puts_error optparse.banner
        puts_error format_available_options(all_option_types)
        exit 1
      end
      # some optionTypes have fieldContext='zone', so move those to the root level of the zone payload
      if params['zone'].is_a?(Hash)
        cloud_payload.merge!(params.delete('zone'))
      end
      cloud_payload.merge!(params)
      payload = {zone: cloud_payload}
    end
    @clouds_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @clouds_interface.dry.update(cloud['id'], payload)
      return
    end
    json_response = @clouds_interface.update(cloud['id'], payload)
    if options[:json]
      puts as_json(json_response, options)
    else
      get([cloud['id']] + (options[:remote] ? ["-r",options[:remote]] : []))
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#update_wiki(args) ⇒ Object



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
911
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
# File 'lib/morpheus/cli/clouds.rb', line 868

def update_wiki(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[cloud] [options]")
    build_option_type_options(opts, options, update_wiki_page_option_types)
    opts.on('--file FILE', "File containing the wiki content. This can be used instead of --content") do |filename|
      full_filename = File.expand_path(filename)
      if File.exists?(full_filename)
        params['content'] = File.read(full_filename)
      else
        print_red_alert "File not found: #{full_filename}"
        return 1
      end
      # use the filename as the name by default.
      if !params['name']
        params['name'] = File.basename(full_filename)
      end
    end
    opts.on(nil, '--clear', "Clear current page content") do |val|
      params['content'] = ""
    end
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote])
  end
  optparse.parse!(args)
  if args.count != 1
    puts_error  "#{Morpheus::Terminal.angry_prompt}wrong number of arguments. Expected 1 and received #{args.count} #{args.inspect}\n#{optparse}"
    return 1
  end
  connect(options)

  begin
    cloud = find_cloud_by_name_or_id(args[0])
    return 1 if cloud.nil?
    # construct payload
    passed_options = options[:options] ? options[:options].reject {|k,v| k.is_a?(Symbol) } : {}
    payload = nil
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!({'page' => passed_options}) unless passed_options.empty?
    else
      payload = {
        'page' => {
        }
      }
      # allow arbitrary -O options
      payload.deep_merge!({'page' => passed_options}) unless passed_options.empty?
      # prompt for options
      #params = Morpheus::Cli::OptionTypes.prompt(update_wiki_page_option_types, options[:options], @api_client, options[:params])
      #params = passed_options
      params.deep_merge!(passed_options)

      if params.empty?
        raise_command_error "Specify at least one option to update.\n#{optparse}"
      end

      payload.deep_merge!({'page' => params}) unless params.empty?
    end
    @clouds_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @clouds_interface.dry.update_wiki(cloud["id"], payload)
      return
    end
    json_response = @clouds_interface.update_wiki(cloud["id"], payload)

    if options[:json]
      puts as_json(json_response, options)
    else
      print_green_success "Updated wiki page for cloud #{cloud['name']}"
      wiki([cloud['id']])
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#view_wiki(args) ⇒ Object



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

def view_wiki(args)
  params = {}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[id]")
    build_common_options(opts, options, [:dry_run, :remote])
    opts.footer = "View cloud wiki page in a web browser" + "\n" +
                  "[cloud] is required. This is the name or id of a cloud."
  end
  optparse.parse!(args)
  if args.count != 1
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args.join(' ')}\n#{optparse}"
  end
  connect(options)
  begin
    cloud = find_cloud_by_name_or_id(args[0])
    return 1 if cloud.nil?

    link = "#{@appliance_url}/login/oauth-redirect?access_token=#{@access_token}\\&redirectUri=/infrastructure/clouds/#{cloud['id']}#!wiki"

    if options[:dry_run]
      puts Morpheus::Util.open_url_command(link)
      return 0
    end
    return Morpheus::Util.open_url(link)
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#wiki(args) ⇒ Object



759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
# File 'lib/morpheus/cli/clouds.rb', line 759

def wiki(args)
  options = {}
  params = {}
  open_wiki_link = false
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[cloud]")
    opts.on('--view', '--view', "View wiki page in web browser.") do
      open_wiki_link = true
    end
    build_common_options(opts, options, [:json, :dry_run, :remote])
    opts.footer = "View wiki page details for a cloud." + "\n" +
                  "[cloud] is required. This is the name or id of a cloud."
  end
  optparse.parse!(args)
  if args.count != 1
    puts_error  "#{Morpheus::Terminal.angry_prompt}wrong number of arguments. Expected 1 and received #{args.count} #{args.inspect}\n#{optparse}"
    return 1
  end
  connect(options)

  begin
    cloud = find_cloud_by_name_or_id(args[0])
    return 1 if cloud.nil?


    @clouds_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @clouds_interface.dry.wiki(cloud["id"], params)
      return
    end
    json_response = @clouds_interface.wiki(cloud["id"], params)
    page = json_response['page']

    render_result = render_with_format(json_response, options, 'page')
    return 0 if render_result

    if page

      # my_terminal.exec("wiki get #{page['id']}")

      print_h1 "Cloud Wiki Page: #{cloud['name']}"
      # print_h1 "Wiki Page Details"
      print cyan

      print_description_list({
        "Page ID" => 'id',
        "Name" => 'name',
        #"Category" => 'category',
        #"Ref Type" => 'refType',
        #"Ref ID" => 'refId',
        #"Owner" => lambda {|it| it['account'] ? it['account']['name'] : '' },
        "Created" => lambda {|it| format_local_dt(it['dateCreated']) },
        "Created By" => lambda {|it| it['createdBy'] ? it['createdBy']['username'] : '' },
        "Updated" => lambda {|it| format_local_dt(it['lastUpdated']) },
        "Updated By" => lambda {|it| it['updatedBy'] ? it['updatedBy']['username'] : '' }
      }, page)
      print reset,"\n"

      print_h2 "Page Content"
      print cyan, page['content'], reset, "\n"

    else
      print "\n"
      print cyan, "No wiki page found.", reset, "\n"
    end
    print reset,"\n"

    if open_wiki_link
      return view_wiki([args[0]])
    end

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