Class: Morpheus::Cli::NetworkDomainsCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand, InfrastructureHelper
Defined in:
lib/morpheus/cli/commands/network_domains_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 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, #command_name, #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

#initializeNetworkDomainsCommand

set_default_subcommand :list



14
15
16
# File 'lib/morpheus/cli/commands/network_domains_command.rb', line 14

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

Instance Method Details

#add(args) ⇒ Object



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

def add(args)
  options = {}
  ip_range_list = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    opts.on('--name VALUE', String, "Name for this network domain") do |val|
      options['name'] = val
    end
    opts.on('--description VALUE', String, "Description for this network domain") do |val|
      options['description'] = val
    end
    opts.on('--public-zone [on|off]', String, "Public Zone") do |val|
      options['publicZone'] = val.to_s == 'on' || val.to_s == 'true'
    end
    opts.on('--domain-controller [on|off]', String, "Join Domain Controller") do |val|
      options['domainController'] = val.to_s == 'on' || val.to_s == 'true'
    end
    opts.on('--domain-username VALUE', String, "Domain Username") do |val|
      options['domainUsername'] = val
    end
    opts.on('--domain-password VALUE', String, "Domain Password") do |val|
      options['domainPassword'] = val
    end
    opts.on('--dc-server VALUE', String, "DC Server") do |val|
      options['dcServer'] = val
    end
    opts.on('--ou-path VALUE', String, "OU Path") do |val|
      options['ouPath'] = val
    end
    opts.on('--visibility [private|public]', String, "Visibility") do |val|
      options['visibility'] = val
    end
    opts.on('--tenant ID', String, "Tenant Account ID") do |val|
      options['tenant'] = val
    end
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :quiet, :remote])
    opts.footer = "Create a new network domain." + "\n" +
                  "[name] is required and can be passed as --name instead."
  end
  optparse.parse!(args)
  if args.count > 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "wrong number of arguments, expected 0-1 and got #{args.count}\n#{optparse}"
    return 1
  end
  connect(options)
  begin
    # merge -O options into normally parsed options
    options.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]

    # support [name] as first argument
    if args[0]
      options['name'] = args[0]
    end

    # construct payload
    payload = nil
    if options[:payload]
      payload = options[:payload]
    else
      # prompt for network options
      payload = {
        'networkDomain' => {
          # 'config' => {}
        }
      }
      
      # allow arbitrary -O options
      payload['networkDomain'].deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]

      # Name
      if options['name']
        payload['networkDomain']['name'] = options['name']
      else
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'description' => 'Name for this network domain.'}], options)
        payload['networkDomain']['name'] = v_prompt['name']
      end

      # Description
      if options['description']
        payload['networkDomain']['description'] = options['description']
      else
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'description', 'fieldLabel' => 'Description', 'type' => 'text', 'required' => false, 'description' => 'Description for this network domain.'}], options)
        payload['networkDomain']['description'] = v_prompt['description']
      end
      
      # Public Zone
      if options['publicZone'] != nil
        payload['networkDomain']['publicZone'] = options['publicZone']
      else
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'publicZone', 'fieldLabel' => 'Public Zone', 'type' => 'checkbox', 'required' => false, 'description' => ''}], options)
        payload['networkDomain']['publicZone'] = (v_prompt['publicZone'].to_s == 'on') unless v_prompt['publicZone'].nil?
      end

      # Domain Controller
      join_domain_controller = false
      if options['domainController'] != nil
        payload['networkDomain']['domainController'] = options['domainController']
      else
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'domainController', 'fieldLabel' => 'Join Domain Controller', 'type' => 'checkbox', 'required' => false, 'description' => ''}], options)
        payload['networkDomain']['domainController'] = (v_prompt['domainController'].to_s == 'on') unless v_prompt['domainController'].nil?
      end
      join_domain_controller = !!payload['networkDomain']['domainController']

      # Domain Username
      if options['domainUsername'] != nil
        payload['networkDomain']['domainUsername'] = options['domainUsername']
      elsif join_domain_controller
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'domainUsername', 'fieldLabel' => 'Domain Username', 'type' => 'text', 'required' => false, 'description' => ''}], options)
        payload['networkDomain']['domainUsername'] = v_prompt['domainUsername'] unless v_prompt['domainUsername'].nil?
      end

      # Domain Password
      if options['domainPassword'] != nil
        payload['networkDomain']['domainPassword'] = options['domainPassword']
      elsif join_domain_controller
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'domainPassword', 'fieldLabel' => 'Domain Password', 'type' => 'password', 'required' => false, 'description' => ''}], options)
        payload['networkDomain']['domainPassword'] = v_prompt['domainPassword'] unless v_prompt['domainPassword'].nil?
      end
      
      # DC Server
      if options['dcServer'] != nil
        payload['networkDomain']['dcServer'] = options['dcServer']
      elsif join_domain_controller
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'dcServer', 'fieldLabel' => 'DC Server', 'type' => 'text', 'required' => false, 'description' => ''}], options)
        payload['networkDomain']['dcServer'] = v_prompt['dcServer'] unless v_prompt['dcServer'].nil?
      end

      # OU Path
      if options['ouPath'] != nil
        payload['networkDomain']['ouPath'] = options['ouPath']
      elsif join_domain_controller
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'ouPath', 'fieldLabel' => 'OU Path', 'type' => 'text', 'required' => false, 'description' => ''}], options)
        payload['networkDomain']['ouPath'] = v_prompt['ouPath'] unless v_prompt['ouPath'].nil?
      end

      # Visibility
      if options['visibility']
        payload['networkDomain']['visibility'] = options['visibility'].to_s.downcase
      else
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'visibility', 'fieldLabel' => 'Visibility', 'type' => 'select', 'selectOptions' => [{'name' => 'Private', 'value' => 'private'},{'name' => 'Public', 'value' => 'public'}], 'required' => false, 'description' => 'Visibility', 'defaultValue' => 'private'}], options)
        payload['networkDomain']['visibility'] = v_prompt['visibility'].to_s.downcase
      end

      # Tenant
      if options['tenant']
        payload['networkDomain']['account'] = {'id' => options['tenant'].to_i}
      else
        begin
          available_accounts = @api_client.accounts.list({max:10000})['accounts'].collect {|it| {'name' => it['name'], 'value' => it['id'], 'id' => it['id']}}
           = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'tenant', 'fieldLabel' => 'Tenant', 'type' => 'select', 'selectOptions' => available_accounts, 'required' => false, 'description' => 'Tenant'}], options)
          if ['tenant']
            payload['networkDomain']['account'] = {'id' => ['tenant']}
          end
        rescue
          puts "failed to load list of available tenants: #{ex.message}"
        end
      end

    end

    @network_domains_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @network_domains_interface.dry.create(payload)
      return
    end
    json_response = @network_domains_interface.create(payload)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    elsif !options[:quiet]
      network_domain = json_response['networkDomain']
      print_green_success "Added network domain #{network_domain['name']}"
      get([network_domain['id']])
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#add_record(args) ⇒ Object



658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
# File 'lib/morpheus/cli/commands/network_domains_command.rb', line 658

def add_record(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[network-domain] [record]")
    opts.on('--name VALUE', String, "Name") do |val|
      options[:options]['name'] = val
    end
    opts.on('--type VALUE', String, "Domain Record Type. Default is 'A'") do |val|
      options[:options]['type'] = val
    end
    # opts.on('--fqdn VALUE', String, "FQDN") do |val|
    #   options[:options]['hostname'] = val
    # end
    opts.on('--content VALUE', String, "Content") do |val|
      options[:options]['content'] = val
    end
    opts.on('--comment VALUE', String, "Comment") do |val|
      options[:options]['comment'] = val
    end
    opts.on('--ttl SECONDS', String, "TTL in seconds. Default is 86400.") do |val|
      options[:options]['ttl'] = val
    end
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :quiet, :remote])
    opts.footer = "Create a new network domain record." + "\n" +
                  "[network-domain] is required. This is the name or id of a network domain.\n" +
                  "[record] is required. This is the name of the domain record and can be passed as --name instead."
  end
  optparse.parse!(args)
  if args.count < 1 || args.count > 2
    raise_command_error "wrong number of arguments, expected 1-2 and got (#{args.count}) #{args}\n#{optparse}"
  end
  connect(options)
  begin
    network_domain = find_network_domain_by_name_or_id(args[0])
    return 1 if network_domain.nil?
    network_domain_id = network_domain['id']

    # support [name] as first argument
    if args[1]
      options[:options]['name'] = args[1]
    end

    # construct payload
    payload = nil
    if options[:payload]
      payload = options[:payload]
    else
      # prompt for network options
      payload = {
        'networkDomainRecord' => {
          #'networkDomain' => {'id' => network_domain['id']}
        }
      }
      
      # allow arbitrary -O options
      payload['networkDomainRecord'].deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]

      # Name
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'description' => 'Name of this domain record.'}], options[:options])
      payload['networkDomainRecord']['name'] = v_prompt['name'] unless v_prompt['name'].to_s.empty?

      # Type
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'type', 'fieldLabel' => 'Type', 'type' => 'select', 'required' => true, 'optionSource' => 'dnsRecordType', 'description' => 'Type for this domain record.', 'defaultValue' => 'A'}], options[:options], @api_client)
      payload['networkDomainRecord']['type'] = v_prompt['type'] unless v_prompt['type'].to_s.empty?

      # Content
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'content', 'fieldLabel' => 'Content', 'type' => 'textarea', 'required' => true, 'description' => 'Content for this domain record.'}], options[:options])
      payload['networkDomainRecord']['content'] = v_prompt['content'] unless v_prompt['content'].to_s.empty?

      # TTL
      if options[:options]['ttl'] == 'null'
        payload['networkDomainRecord']['ttl'] = nil
      else
        v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'ttl', 'fieldLabel' => 'TTL', 'type' => 'text', 'required' => false, 'description' => 'TTL in seconds for this domain record. Default is 86400.'}], options[:options])
        payload['networkDomainRecord']['ttl'] = v_prompt['ttl'].to_i unless v_prompt['ttl'].to_s.empty?
      end

      # Comment
      # v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'comment', 'fieldLabel' => 'Comment', 'type' => 'text', 'required' => true, 'description' => 'Comment for this domain record.'}], options[:options])
      # payload['networkDomainRecord']['comment'] = v_prompt['comment'] unless v_prompt['comment'].to_s.empty?

    end

    @network_domain_records_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @network_domain_records_interface.dry.create(network_domain_id, payload)
      return
    end
    json_response = @network_domain_records_interface.create(network_domain_id, payload)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    elsif !options[:quiet]
      network_domain_record = json_response['networkDomainRecord']
      print_green_success "Added network domain record #{network_domain_record['name']}"
      get_record([network_domain['id'], network_domain_record['id']])
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#connect(opts) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/morpheus/cli/commands/network_domains_command.rb', line 18

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @network_domains_interface = @api_client.network_domains
  @network_domain_records_interface = @api_client.network_domain_records
  @clouds_interface = @api_client.clouds
  @options_interface = @api_client.options
end

#get(args) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/morpheus/cli/commands/network_domains_command.rb', line 97

def get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[network-domain]")
    build_common_options(opts, options, [:json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "Get details about a network domain." + "\n" +
                  "[network-domain] is required. This is the name or id of a network domain."
  end
  optparse.parse!(args)
  if args.count != 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} missing argument: [network-domain]\n#{optparse}"
    return 1
  end
  connect(options)
  begin
    @network_domains_interface.setopts(options)
    if options[:dry_run]
      if args[0].to_s =~ /\A\d{1,}\Z/
        print_dry_run @network_domains_interface.dry.get(args[0].to_i)
      else
        print_dry_run @network_domains_interface.dry.list({name:args[0]})
      end
      return
    end
    network_domain = find_network_domain_by_name_or_id(args[0])
    return 1 if network_domain.nil?
    json_response = {'networkDomain' => network_domain}  # skip redundant request
    # json_response = @network_domains_interface.get(network_domain['id'])
    network_domain = json_response['networkDomain']
    
    if options[:json]
      puts as_json(json_response, options, 'networkDomain')
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, 'networkDomain')
      return 0
    elsif options[:csv]
      puts records_as_csv([network_domain], options)
      return 0
    end
    print_h1 "Network Domain Details"
    print cyan
    description_cols = {
      "ID" => 'id',
      "Name" => lambda {|it| it['name'] },
      "Description" => lambda {|it| it['description'] },
      # "Source" => lambda {|it| it['refSource'] }, showReferenceName(refType, refId)
      "Domain Controller" => lambda {|it| it['domainController'] ? 'Yes' : 'No' },
      "Public Zone" => lambda {|it| it['publicZone'] ? 'Yes' : 'No' },
      "Domain Username" => lambda {|it| it['domainUsername'] },
      "Domain Password" => lambda {|it| it['domainPassword'] },
      "DC Server" => lambda {|it| it['dcServer'] },
      "OU Path" => lambda {|it| it['ouPath'] },
      "Visibility" => lambda {|it| it['visibility'].to_s.capitalize },
      "Tenant" => lambda {|it| it['account'] ? it['account']['name'] : '' },
    }
    print_description_list(description_cols, network_domain)
    
    # print_h2 "Domain Records"
    # print cyan
    # if network_domain['records']
    #   network_domain['records'].each do |r|
    #     puts " * #{r['name']}\t#{r['fqdn']}\t#{r['type']}\t#{r['ttl']}"
    #   end
    # end
    print reset,"\n"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#get_record(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
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
# File 'lib/morpheus/cli/commands/network_domains_command.rb', line 586

def get_record(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[network-domain] [record]")
    build_common_options(opts, options, [:query, :json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "Get details about a network domain record.\n" +
                  "[network-domain] is required. This is the name or id of a network domain.\n" +
                  "[record] is required. This is the name or id of a network domain record."
  end
  optparse.parse!(args)
  connect(options)
  if args.count != 2
    raise_command_error "wrong number of arguments, expected 2 and got (#{args.count}) #{args}\n#{optparse}"
  end
  begin
    network_domain = find_network_domain_by_name_or_id(args[0])
    return 1 if network_domain.nil?
    network_domain_id = network_domain['id']

    params.merge!(parse_list_options(options))
    @network_domain_records_interface.setopts(options)
    if options[:dry_run]
      if args[1].to_s =~ /\A\d{1,}\Z/
        print_dry_run @network_domain_records_interface.dry.get(network_domain_id, args[1].to_i)
      else
        print_dry_run @network_domain_records_interface.dry.list(network_domain_id, {name:args[1]})
      end
      return
    end
    network_domain_record = find_network_domain_record_by_name_or_id(network_domain_id, args[1])
    return 1 if network_domain_record.nil?
    json_response = {'networkDomainRecord' => network_domain_record}  # skip redundant request
    # json_response = @network_domain_records_interface.get(network_domain_id, args[1])
    #network_domain_record = json_response['networkDomainRecord']
    if options[:json]
      puts as_json(json_response, options, "networkDomainRecord")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "networkDomainRecord")
      return 0
    elsif options[:csv]
      puts records_as_csv([network_domain_record], options)
      return 0
    end
    print_h1 "Network Domain Record Details"
    print cyan
    description_cols = {
      "ID" => 'id',
      "Name" => lambda {|it| it['name'] },
      "FQDN" => lambda {|it| it['fqdn'] },
      "Type" => lambda {|it| it['type'] },
      "Content" => lambda {|it| it['content'] },
      "Comment" => lambda {|it| it['Comment'] },
      "TTL" => lambda {|it| it['ttl'] },
      "Domain" => lambda {|it| network_domain['name'] },
      "Source" => lambda {|it| it['source'] },
      "Created By" => lambda {|it| it['createdBy'] ? it['createdBy']['username'] : '' },
      #"Created" => lambda {|it| format_local_dt(it['dateCreated']) },
      #"Updated" => lambda {|it| format_local_dt(it['lastUpdated']) },
      #"Status" => lambda {|it| it['statusMessage'].to_s.empty? ? it['status'] : "#{it['status']} - #{it['statusMessage']}" },
    }
    print_description_list(description_cols, network_domain_record)

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

#handle(args) ⇒ Object



26
27
28
# File 'lib/morpheus/cli/commands/network_domains_command.rb', line 26

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/morpheus/cli/commands/network_domains_command.rb', line 30

def list(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:list, :json, :yaml, :csv, :fields, :json, :dry_run, :remote])
    opts.footer = "List network domains."
  end
  optparse.parse!(args)
  connect(options)
  begin
    params.merge!(parse_list_options(options))
    @network_domains_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @network_domains_interface.dry.list(params)
      return
    end
    json_response = @network_domains_interface.list(params)
    network_domains = json_response["networkDomains"]
    if options[:json]
      puts as_json(json_response, options, "networkDomains")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "networkDomains")
      return 0
    elsif options[:csv]
      puts records_as_csv(network_domains, options)
      return 0
    end
    title = "Morpheus Network Domains"
    subtitles = []
    subtitles += parse_list_subtitles(options)
    print_h1 title, subtitles
    if network_domains.empty?
      print cyan,"No network domains found.",reset,"\n"
    else
      rows = network_domains.collect {|network_domain| 
        row = {
          id: network_domain['id'],
          name: network_domain['name'],
          description: network_domain['description'], 
          source: network_domain['refType'] ? "#{network_domain['refType']} #{network_domain['id']}" : '', # showReferenceName(refType, refId)
          domainController: network_domain['domainController'] ? 'Yes' : 'No', 
          visibility: network_domain['visibility'].to_s.capitalize, 
          tenant: network_domain['account'] ? network_domain['account']['name'] : '', 
          owner: network_domain['owner'] ? network_domain['owner']['name'] : '', 
        }
        row
      }
      columns = [:id, :name, :description, {:domainController => {:display_name => "DOMAIN CONTROLLER"} }, :visibility, :tenant]
      if options[:include_fields]
        columns = options[:include_fields]
        rows = network_domains
      end
      print cyan
      print as_pretty_table(rows, columns, options)
      print reset
      print_results_pagination(json_response, {:label => "network domain", :n_label => "network domains"})
    end
    print reset,"\n"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#list_records(args) ⇒ Object



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

def list_records(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[network-domain]")
    build_common_options(opts, options, [:list, :json, :yaml, :csv, :fields, :json, :dry_run, :remote])
    opts.footer = "List network domain records.\n" +
                  "[network-domain] is required. This is the name or id of a network domain."
  end
  optparse.parse!(args)
  connect(options)
  if args.count != 1
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
  end
  begin
    network_domain = find_network_domain_by_name_or_id(args[0])
    return 1 if network_domain.nil?
    network_domain_id = network_domain['id']

    params.merge!(parse_list_options(options))
    @network_domain_records_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @network_domain_records_interface.dry.list(network_domain_id, params)
      return
    end
    json_response = @network_domain_records_interface.list(network_domain_id, params)
    network_domain_records = json_response["networkDomainRecords"]
    if options[:json]
      puts as_json(json_response, options, "networkDomainRecords")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "networkDomainRecords")
      return 0
    elsif options[:csv]
      puts records_as_csv(network_domain_records, options)
      return 0
    end
    title = "Morpheus Network Domain Records"
    subtitles = []
    subtitles += parse_list_subtitles(options)
    print_h1 title, subtitles
    if network_domain_records.empty?
      print cyan,"No network domain records found.",reset,"\n"
    else
      columns = [
        {"ID" => lambda {|it| it['id'] } },
        {"NAME" => lambda {|it| it['name'] } },
        #{"FQDN" => lambda {|it| it['fqdn'] } },
        {"TYPE" => lambda {|it| it['type'] } },
        # {"CONTENT" => lambda {|it| it['content'] } },
        {"CONTENT" => lambda {|it| it['content'].to_s.split.join(",") } },
        # {"COMMENT" => lambda {|it| it['comment'] } },
        #{"CREATED BY" => lambda {|it| it['createdBy'] ? it['createdBy']['username'] : '' } },
        #{"CREATED" => lambda {|it| format_local_dt(it['dateCreated']) } },
        #{"UPDATED" => lambda {|it| format_local_dt(it['lastUpdated']) } },
      ]
      if options[:include_fields]
        columns = options[:include_fields]
      end
      print as_pretty_table(network_domain_records, columns, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#remove(args) ⇒ Object



472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/morpheus/cli/commands/network_domains_command.rb', line 472

def remove(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[network-domain]")
    build_common_options(opts, options, [:account, :auto_confirm, :json, :dry_run, :remote])
    opts.footer = "Delete a network domain." + "\n" +
                  "[network-domain] is required. This is the name or id of a network domain."
  end
  optparse.parse!(args)

  if args.count < 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} missing argument: [network-domain]\n#{optparse}"
    return 1
  end

  connect(options)
  begin
    network_domain = find_network_domain_by_name_or_id(args[0])
    return 1 if network_domain.nil?

    unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the network domain: #{network_domain['name']}?")
      return 9, "aborted command"
    end
    @network_domains_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @network_domains_interface.dry.destroy(network_domain['id'])
      return 0
    end
    json_response = @network_domains_interface.destroy(network_domain['id'])
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Removed network domain #{network_domain['name']}"
      # list([])
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#remove_record(args) ⇒ Object



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

def remove_record(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[network-domain] [record]")
    build_common_options(opts, options, [:account, :auto_confirm, :json, :dry_run, :remote])
    opts.footer = "Delete a network domain record." + "\n" +
                  "[network-domain] is required. This is the name or id of a network domain.\n" +
                  "[record] is required. This is the name or id of a network domain record."
  end
  optparse.parse!(args)
  if args.count != 2
    raise_command_error "wrong number of arguments, expected 2 and got (#{args.count}) #{args}\n#{optparse}"
  end
  connect(options)
  begin
    network_domain = find_network_domain_by_name_or_id(args[0])
    return 1 if network_domain.nil?
    network_domain_id = network_domain['id']

    network_domain_record = find_network_domain_record_by_name_or_id(network_domain_id, args[1])
    return 1 if network_domain_record.nil?

    unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the networkd domain record: #{network_domain_record['name']}?")
      return 9, "aborted command"
    end
    @network_domain_records_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @network_domain_records_interface.dry.destroy(network_domain['id'], network_domain_record['id'])
      return 0
    end
    json_response = @network_domain_records_interface.destroy(network_domain['id'], network_domain_record['id'])
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Removed network domain record #{network_domain_record['name']}"
      # list([])
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#update(args) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
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
# File 'lib/morpheus/cli/commands/network_domains_command.rb', line 353

def update(args)
  options = {}
  ip_range_list = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[network-domain] [options]")
    opts.on('--name VALUE', String, "Name for this network domain") do |val|
      options['name'] = val
    end
    opts.on('--type VALUE', String, "Type of network domain") do |val|
      options['description'] = val
    end
    opts.on('--ip-ranges LIST', Array, "IP Ranges, comma separated list IP ranges in the format start-end.") do |list|
      if list.size == 1 && list[0] == 'null' # hacky way to clear it
        ip_range_list = []
      else
        ip_range_list = list.collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq
        ip_range_list = ip_range_list.collect {|it|
          range_parts = it.split("-")
          {startAddress: range_parts[0].to_s.strip, endAddress: range_parts[1].to_s.strip}
        }
      end
    end
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote])
    opts.footer = "Update a network domain." + "\n" +
                  "[network-domain] is required. This is the id of a network domain."
  end
  optparse.parse!(args)
  if args.count != 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "wrong number of arguments, expected 1 and got #{args.count}\n#{optparse}"
    return 1
  end
  connect(options)

  begin
    network_domain = find_network_domain_by_name_or_id(args[0])
    return 1 if network_domain.nil?
    
    # merge -O options into normally parsed options
    options.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]

    # construct payload
    payload = nil
    if options[:payload]
      payload = options[:payload]
    else
      # prompt for network options
      payload = {
        'networkDomain' => {
        }
      }
      
      # allow arbitrary -O options
      payload['networkDomain'].deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]

      # Name
      if options['name']
        payload['networkDomain']['name'] = options['name']
      else
        # v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'description' => 'Name for this network domain.'}], options)
        # payload['networkDomain']['name'] = v_prompt['name']
      end
      
      # Network Domain Type
      # network_type_id = nil
      # v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'type', 'fieldLabel' => 'Domain Type', 'type' => 'select', 'optionSource' => 'networkDomainTypes', 'required' => true, 'description' => 'Choose a network domain type.'}], options, @api_client, {})
      # network_type_id = v_prompt['type']
      # if network_type_id.nil? || network_type_id.to_s.empty?
      #   print_red_alert "Domain Type not found by id '#{options['type']}'"
      #   return 1
      # end
      # payload['networkDomain']['type'] = {'id' => network_type_id.to_i }
      if options['type']
        payload['networkDomain']['type'] = {'id' => options['type'].to_i }
      end

      # IP Ranges
      if ip_range_list
        ip_range_list = ip_range_list.collect {|range|
          # ugh, need to allow changing an existing range by id too
          if network_domain['ipRanges']
            existing_range = network_domain['ipRanges'].find {|r|
              range[:startAddress] == r['startAddress'] && range[:endAddress] == r['endAddress']
            }
            if existing_range
              range[:id] = existing_range['id']
            end
          end
          range
        }
        payload['networkDomain']['ipRanges'] = ip_range_list
      else
        # v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'ipRanges', 'fieldLabel' => 'IP Ranges', 'type' => 'text', 'required' => true, 'description' => 'IP Ranges in the domain, comma separated list of ranges in the format start-end.'}], options)
        # payload['networkDomain']['ipRanges'] = v_prompt['ipRanges'].to_s.split(",").collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq.collect {|it| 
        #   it
        # }
      end

    end
    @network_domains_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @network_domains_interface.dry.update(network_domain["id"], payload)
      return
    end
    json_response = @network_domains_interface.update(network_domain["id"], payload)
    if options[:json]
      puts as_json(json_response)
    else
      network_domain = json_response['networkDomain']
      print_green_success "Updated network domain #{network_domain['name']}"
      get([network_domain['id']])
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end