Class: Morpheus::Cli::NetworkPoolsCommand
- Inherits:
-
Object
- Object
- Morpheus::Cli::NetworkPoolsCommand
- Includes:
- CliCommand, InfrastructureHelper
- Defined in:
- lib/morpheus/cli/network_pools_command.rb
Instance Attribute Summary
Attributes included from CliCommand
Instance Method Summary collapse
- #add(args) ⇒ Object
- #add_ip(args) ⇒ Object
- #connect(opts) ⇒ Object
- #get(args) ⇒ Object
- #get_ip(args) ⇒ Object
- #handle(args) ⇒ Object
-
#initialize ⇒ NetworkPoolsCommand
constructor
A new instance of NetworkPoolsCommand.
- #list(args) ⇒ Object
- #list_ips(args) ⇒ Object
- #remove(args) ⇒ Object
- #remove_ip(args) ⇒ Object
- #update(args) ⇒ Object
- #update_ip(args) ⇒ Object
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, #get_available_cloud_types, #groups_interface, included
Methods included from CliCommand
#build_common_options, #build_option_type_options, #command_name, #default_refresh_interval, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_id_list, #parse_list_options, #parse_list_subtitles, #print, #print_error, #puts, #puts_error, #raise_command_error, #render_with_format, #run_command_for_each_arg, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!
Constructor Details
#initialize ⇒ NetworkPoolsCommand
Returns a new instance of NetworkPoolsCommand.
16 17 18 |
# File 'lib/morpheus/cli/network_pools_command.rb', line 16 def initialize() # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance end |
Instance Method Details
#add(args) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/morpheus/cli/network_pools_command.rb', line 166 def add(args) = {} ip_range_list = nil optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage() opts.on('--name VALUE', String, "Name for this network pool") do |val| ['name'] = val end opts.on('--type VALUE', String, "Type of network pool") do |val| ['type'] = 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 (opts, , [:options, :payload, :json, :dry_run, :quiet, :remote]) opts. = "Create a new network pool." + "\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() begin # merge -O options into normally parsed options .deep_merge!([:options].reject {|k,v| k.is_a?(Symbol) }) if [:options] # support [name] as first argument if args[0] ['name'] = args[0] end # construct payload payload = nil if [:payload] payload = [:payload] else # prompt for network options payload = { 'networkPool' => { # 'config' => {} } } # allow arbitrary -O options payload['networkPool'].deep_merge!([:options].reject {|k,v| k.is_a?(Symbol) }) if [:options] # Name if ['name'] payload['networkPool']['name'] = ['name'] else v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'description' => 'Name for this network pool.'}], ) payload['networkPool']['name'] = v_prompt['name'] end # Network Pool Type network_type_id = nil v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'type', 'fieldLabel' => 'Pool Type', 'type' => 'select', 'optionSource' => 'networkPoolTypes', 'required' => true, 'description' => 'Choose a network pool type.'}], , @api_client, {}) network_type_id = v_prompt['type'] if network_type_id.nil? || network_type_id.to_s.empty? print_red_alert "Pool Type not found by id '#{['type']}'" return 1 end payload['networkPool']['type'] = {'id' => network_type_id.to_i } # IP Ranges if ip_range_list payload['networkPool']['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 pool, comma separated list of ranges in the format start-end.'}], ) ip_range_list = v_prompt['ipRanges'].to_s.split(",").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("-") range = {startAddress: range_parts[0].to_s.strip, endAddress: range_parts[1].to_s.strip} range } payload['networkPool']['ipRanges'] = ip_range_list end end @network_pools_interface.setopts() if [:dry_run] print_dry_run @network_pools_interface.dry.create(payload) return end json_response = @network_pools_interface.create(payload) if [:json] print JSON.pretty_generate(json_response) print "\n" elsif ![:quiet] network_pool = json_response['networkPool'] print_green_success "Added network pool #{network_pool['name']}" get([network_pool['id']]) end return 0 rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#add_ip(args) ⇒ Object
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 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 |
# File 'lib/morpheus/cli/network_pools_command.rb', line 580 def add_ip(args) = {} params = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[network-pool] [ip]") opts.on('--ip-address VALUE', String, "IP Address for this network pool IP") do |val| [:options]['ipAddress'] = val end opts.on('--hostname VALUE', String, "Hostname for this network pool IP") do |val| [:options]['hostname'] = val end (opts, , [:options, :payload, :json, :dry_run, :quiet, :remote]) opts. = "Create a new network pool IP." + "\n" + "[network-pool] is required. This is the name or id of a network pool.\n" + "[ip] is required and can be passed as --ip-address 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() begin network_pool = find_network_pool_by_name_or_id(args[0]) return 1 if network_pool.nil? network_pool_id = network_pool['id'] # support [ip] as first argument if args[1] [:options]['ipAddress'] = args[1] end # construct payload payload = nil if [:payload] payload = [:payload] else # prompt for network options payload = { 'networkPoolIp' => { } } # allow arbitrary -O options payload['networkPoolIp'].deep_merge!([:options].reject {|k,v| k.is_a?(Symbol) }) if [:options] # IP Address v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'ipAddress', 'fieldLabel' => 'IP Address', 'type' => 'text', 'required' => true, 'description' => 'IP Address for this network pool IP.'}], [:options]) payload['networkPoolIp']['ipAddress'] = v_prompt['ipAddress'] unless v_prompt['ipAddress'].to_s.empty? # Hostname v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'hostname', 'fieldLabel' => 'Hostname', 'type' => 'text', 'required' => true, 'description' => 'Hostname for this network pool IP.'}], [:options]) payload['networkPoolIp']['hostname'] = v_prompt['hostname'] unless v_prompt['hostname'].to_s.empty? end @network_pool_ips_interface.setopts() if [:dry_run] print_dry_run @network_pool_ips_interface.dry.create(network_pool_id, payload) return end json_response = @network_pool_ips_interface.create(network_pool_id, payload) if [:json] print JSON.pretty_generate(json_response) print "\n" elsif ![:quiet] network_pool_ip = json_response['networkPoolIp'] print_green_success "Added network pool IP #{network_pool_ip['ipAddress']}" get_ip([network_pool['id'], network_pool_ip['id']]) end return 0 rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#connect(opts) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/morpheus/cli/network_pools_command.rb', line 20 def connect(opts) @api_client = establish_remote_appliance_connection(opts) @network_pools_interface = @api_client.network_pools @network_pool_ips_interface = @api_client.network_pool_ips @clouds_interface = @api_client.clouds @options_interface = @api_client. end |
#get(args) ⇒ Object
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 |
# File 'lib/morpheus/cli/network_pools_command.rb', line 98 def get(args) = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[network-pool]") (opts, , [:json, :yaml, :csv, :fields, :dry_run, :remote]) opts. = "Get details about a network pool." + "\n" + "[network-pool] is required. This is the name or id of a network pool." end optparse.parse!(args) if args.count != 1 print_error Morpheus::Terminal.angry_prompt puts_error "#{command_name} missing argument: [network-pool]\n#{optparse}" return 1 end connect() begin @network_pools_interface.setopts() if [:dry_run] if args[0].to_s =~ /\A\d{1,}\Z/ print_dry_run @network_pools_interface.dry.get(args[0].to_i) else print_dry_run @network_pools_interface.dry.list({name:args[0]}) end return end network_pool = find_network_pool_by_name_or_id(args[0]) return 1 if network_pool.nil? json_response = {'networkPool' => network_pool} # skip redundant request # json_response = @network_pools_interface.get(network_pool['id']) network_pool = json_response['networkPool'] if [:json] puts as_json(json_response, , "networkPool") return 0 elsif [:yaml] puts as_yaml(json_response, , "networkPool") return 0 elsif [:csv] puts records_as_csv([network_pool], ) return 0 end print_h1 "Network Pool Details" print cyan description_cols = { "ID" => 'id', "Name" => lambda {|it| !it['displayName'].to_s.empty? ? it['displayName'] : it['name'] }, "Network" => lambda {|it| it['name'] }, "Type" => lambda {|it| it['type'] ? it['type']['name'] : '' }, # "IP Ranges" => lambda {|it| it['ipRanges'] ? it['ipRanges'].collect {|r| r['startAddress'].to_s + " - " + r['endAddress'].to_s }.uniq.join(', ') : '' }, "Total IPs" => lambda {|it| it['ipCount'] }, "Free IPs" => lambda {|it| it['freeCount'] }, } print_description_list(description_cols, network_pool) print_h2 "IP Ranges" print cyan if network_pool['ipRanges'] network_pool['ipRanges'].each do |r| puts " * #{r['startAddress']} - #{r['endAddress']}" end end print reset,"\n" return 0 rescue RestClient::Exception => e print_rest_exception(e, ) return 1 end end |
#get_ip(args) ⇒ Object
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 |
# File 'lib/morpheus/cli/network_pools_command.rb', line 508 def get_ip(args) = {} params = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[network-pool] [ip]") (opts, , [:query, :json, :yaml, :csv, :fields, :dry_run, :remote]) opts. = "Get details about a network pool IP address.\n" + "[network-pool] is required. This is the name or id of a network pool.\n" + "[ip] is required. This is the ip address or id of a network pool IP." end optparse.parse!(args) connect() if args.count != 2 raise_command_error "wrong number of arguments, expected 2 and got (#{args.count}) #{args}\n#{optparse}" end begin network_pool = find_network_pool_by_name_or_id(args[0]) return 1 if network_pool.nil? network_pool_id = network_pool['id'] params.merge!(()) @network_pool_ips_interface.setopts() if [:dry_run] if args[1].to_s =~ /\A\d{1,}\Z/ print_dry_run @network_pool_ips_interface.dry.get(network_pool_id, args[1].to_i) else print_dry_run @network_pool_ips_interface.dry.list(network_pool_id, {ipAddress:args[1]}) end return end network_pool_ip = find_network_pool_ip_by_address_or_id(network_pool_id, args[1]) return 1 if network_pool_ip.nil? json_response = {'networkPoolIp' => network_pool_ip} # skip redundant request # json_response = @network_pool_ips_interface.get(network_pool_id, args[1]) #network_pool_ip = json_response['networkPoolIp'] if [:json] puts as_json(json_response, , "networkPoolIp") return 0 elsif [:yaml] puts as_yaml(json_response, , "networkPoolIp") return 0 elsif [:csv] puts records_as_csv([network_pool_ip], ) return 0 end print_h1 "Network Pool IP Details" print cyan description_cols = { "ID" => 'id', "IP Address" => lambda {|it| it['ipAddress'] }, "Hostname" => lambda {|it| it['hostname'] }, "Type" => lambda {|it| it['ipType'] ? it['ipType'] : '' }, # "Gateway" => lambda {|it| network_pool['gatewayAddress'] }, # "Subnet Mask" => lambda {|it| network_pool['subnetMask'] }, # "DNS Server" => lambda {|it| network_pool['dnsServer'] }, "Pool" => lambda {|it| network_pool['name'] }, #"Pool" => lambda {|it| it['networkPool'] ? it['networkPool']['name'] : '' }, "Interface" => lambda {|it| network_pool['interfaceName'] }, "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']) }, } print_description_list(description_cols, network_pool_ip) print reset,"\n" return 0 rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#handle(args) ⇒ Object
28 29 30 |
# File 'lib/morpheus/cli/network_pools_command.rb', line 28 def handle(args) handle_subcommand(args) end |
#list(args) ⇒ Object
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 96 |
# File 'lib/morpheus/cli/network_pools_command.rb', line 32 def list(args) = {} params = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage() (opts, , [:list, :json, :yaml, :csv, :fields, :dry_run, :remote]) opts. = "List network pools." end optparse.parse!(args) connect() begin params.merge!(()) @network_pools_interface.setopts() if [:dry_run] print_dry_run @network_pools_interface.dry.list(params) return end json_response = @network_pools_interface.list(params) network_pools = json_response["networkPools"] if [:json] puts as_json(json_response, , "networkPools") return 0 elsif [:yaml] puts as_yaml(json_response, , "networkPools") return 0 elsif [:csv] puts records_as_csv(network_pools, ) return 0 end title = "Morpheus Network Pools" subtitles = [] subtitles += parse_list_subtitles() print_h1 title, subtitles if network_pools.empty? print cyan,"No network pools found.",reset,"\n" else rows = network_pools.collect {|network_pool| row = { id: network_pool['id'], # matching UI, but huh?? name: !network_pool['displayName'].to_s.empty? ? network_pool['displayName'] : network_pool['name'], network: network_pool['name'], # network: network_pool['network'] ? network_pool['network']['name'] : '', type: network_pool['type'] ? network_pool['type']['name'] : '', ipRanges: network_pool['ipRanges'] ? network_pool['ipRanges'].collect {|it| it['startAddress'].to_s + " - " + it['endAddress'].to_s }.uniq.join(', ') : '', total: ("#{network_pool['ipCount']}/#{network_pool['freeCount']}") } row } columns = [:id, :name, :network, :type, {:ipRanges => {:display_name => "IP RANGES"} }, {:total => {:display_name => "TOTAL/FREE IPs"} }] if [:include_fields] columns = [:include_fields] end print cyan print as_pretty_table(rows, columns, ) print reset print_results_pagination(json_response, {:label => "network pool", :n_label => "network pools"}) end print reset,"\n" return 0 rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#list_ips(args) ⇒ Object
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'lib/morpheus/cli/network_pools_command.rb', line 441 def list_ips(args) = {} params = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[network-pool]") (opts, , [:list, :json, :yaml, :csv, :fields, :json, :dry_run, :remote]) opts. = "List network pool IP addresses.\n" + "[network-pool] is required. This is the name or id of a network pool." end optparse.parse!(args) connect() if args.count != 1 raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}" end begin network_pool = find_network_pool_by_name_or_id(args[0]) return 1 if network_pool.nil? network_pool_id = network_pool['id'] params.merge!(()) @network_pool_ips_interface.setopts() if [:dry_run] print_dry_run @network_pool_ips_interface.dry.list(network_pool_id, params) return end json_response = @network_pool_ips_interface.list(network_pool_id, params) network_pool_ips = json_response["networkPoolIps"] if [:json] puts as_json(json_response, , "networkPoolIps") return 0 elsif [:yaml] puts as_yaml(json_response, , "networkPoolIps") return 0 elsif [:csv] puts records_as_csv(network_pool_ips, ) return 0 end title = "Morpheus Network Pool IPs" subtitles = [] subtitles += parse_list_subtitles() print_h1 title, subtitles if network_pool_ips.empty? print cyan,"No network pool IPs found.",reset,"\n" else columns = [ {"ID" => lambda {|it| it['id'] } }, {"IP ADDRESS" => lambda {|it| it['ipAddress'] } }, {"HOSTNAME" => lambda {|it| it['hostname'] } }, {"TYPE" => lambda {|it| it['ipType'] } }, #{"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 [:include_fields] columns = [:include_fields] end print as_pretty_table(network_pool_ips, columns, ) print_results_pagination(json_response) end print reset,"\n" return 0 rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#remove(args) ⇒ Object
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 |
# File 'lib/morpheus/cli/network_pools_command.rb', line 397 def remove(args) = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[network-pool]") (opts, , [:account, :auto_confirm, :json, :dry_run, :remote]) opts. = "Delete a network pool." + "\n" + "[network-pool] is required. This is the name or id of a network pool." end optparse.parse!(args) if args.count < 1 print_error Morpheus::Terminal.angry_prompt puts_error "#{command_name} missing argument: [network-pool]\n#{optparse}" return 1 end connect() begin network_pool = find_network_pool_by_name_or_id(args[0]) return 1 if network_pool.nil? unless [:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the network pool: #{network_pool['name']}?") return 9, "aborted command" end @network_pools_interface.setopts() if [:dry_run] print_dry_run @network_pools_interface.dry.destroy(network_pool['id']) return 0 end json_response = @network_pools_interface.destroy(network_pool['id']) if [:json] print JSON.pretty_generate(json_response) print "\n" else print_green_success "Removed network pool #{network_pool['name']}" # list([]) end return 0 rescue RestClient::Exception => e print_rest_exception(e, ) return 1 end end |
#remove_ip(args) ⇒ Object
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 762 763 764 765 766 767 768 |
# File 'lib/morpheus/cli/network_pools_command.rb', line 725 def remove_ip(args) = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[network-pool] [ip]") (opts, , [:account, :auto_confirm, :json, :dry_run, :remote]) opts. = "Delete a network pool IP." + "\n" + "[network-pool] is required. This is the name or id of a network pool.\n" + "[ip] is required. This is the ip address or id of a network pool IP." 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() begin network_pool = find_network_pool_by_name_or_id(args[0]) return 1 if network_pool.nil? network_pool_id = network_pool['id'] network_pool_ip = find_network_pool_ip_by_address_or_id(network_pool_id, args[1]) return 1 if network_pool_ip.nil? unless [:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the network pool IP: #{network_pool_ip['ipAddress']} (#{network_pool_ip['hostname']})?") return 9, "aborted command" end @network_pool_ips_interface.setopts() if [:dry_run] print_dry_run @network_pool_ips_interface.dry.destroy(network_pool['id'], network_pool_ip['id']) return 0 end json_response = @network_pool_ips_interface.destroy(network_pool['id'], network_pool_ip['id']) if [:json] print JSON.pretty_generate(json_response) print "\n" else print_green_success "Removed network pool IP #{network_pool_ip['ipAddress']} (#{network_pool_ip['hostname']})" # list([]) end return 0 rescue RestClient::Exception => e print_rest_exception(e, ) return 1 end end |
#update(args) ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/morpheus/cli/network_pools_command.rb', line 278 def update(args) = {} ip_range_list = nil optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[network-pool] [options]") opts.on('--name VALUE', String, "Name for this network pool") do |val| ['name'] = val end opts.on('--type VALUE', String, "Type of network pool") do |val| ['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 (opts, , [:options, :payload, :json, :dry_run, :remote]) opts. = "Update a network pool." + "\n" + "[network-pool] is required. This is the id of a network pool." 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() begin network_pool = find_network_pool_by_name_or_id(args[0]) return 1 if network_pool.nil? # merge -O options into normally parsed options .deep_merge!([:options].reject {|k,v| k.is_a?(Symbol) }) if [:options] # construct payload payload = nil if [:payload] payload = [:payload] else # prompt for network options payload = { 'networkPool' => { } } # allow arbitrary -O options payload['networkPool'].deep_merge!([:options].reject {|k,v| k.is_a?(Symbol) }) if [:options] # Name if ['name'] payload['networkPool']['name'] = ['name'] else # v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'description' => 'Name for this network pool.'}], options) # payload['networkPool']['name'] = v_prompt['name'] end # Network Pool Type # network_type_id = nil # v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'type', 'fieldLabel' => 'Pool Type', 'type' => 'select', 'optionSource' => 'networkPoolTypes', 'required' => true, 'description' => 'Choose a network pool type.'}], options, @api_client, {}) # network_type_id = v_prompt['type'] # if network_type_id.nil? || network_type_id.to_s.empty? # print_red_alert "Pool Type not found by id '#{options['type']}'" # return 1 # end # payload['networkPool']['type'] = {'id' => network_type_id.to_i } if ['type'] payload['networkPool']['type'] = {'id' => ['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_pool['ipRanges'] existing_range = network_pool['ipRanges'].find {|r| range[:startAddress] == r['startAddress'] && range[:endAddress] == r['endAddress'] } if existing_range range[:id] = existing_range['id'] end end range } payload['networkPool']['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 pool, comma separated list of ranges in the format start-end.'}], options) # payload['networkPool']['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_pools_interface.setopts() if [:dry_run] print_dry_run @network_pools_interface.dry.update(network_pool["id"], payload) return end json_response = @network_pools_interface.update(network_pool["id"], payload) if [:json] puts as_json(json_response) else network_pool = json_response['networkPool'] print_green_success "Updated network pool #{network_pool['name']}" get([network_pool['id']]) end return 0 rescue RestClient::Exception => e print_rest_exception(e, ) return 1 end end |
#update_ip(args) ⇒ Object
656 657 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 |
# File 'lib/morpheus/cli/network_pools_command.rb', line 656 def update_ip(args) = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[network-pool] [ip] [options]") opts.on('--hostname VALUE', String, "Hostname for this network pool IP") do |val| [:options]['hostname'] = val end (opts, , [:options, :payload, :json, :dry_run, :quiet, :remote]) opts. = "Update a network pool IP." + "\n" + "[network-pool] is required. This is the name or id of a network pool.\n" + "[ip] is required. This is the ip address or id of a network pool IP." 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() begin network_pool = find_network_pool_by_name_or_id(args[0]) return 1 if network_pool.nil? network_pool_id = network_pool['id'] network_pool_ip = find_network_pool_ip_by_address_or_id(network_pool_id, args[1]) return 1 if network_pool_ip.nil? # merge -O options into normally parsed options .deep_merge!([:options].reject {|k,v| k.is_a?(Symbol) }) if [:options] # construct payload payload = nil if [:payload] payload = [:payload] else # prompt for network options payload = { 'networkPoolIp' => { } } # allow arbitrary -O options payload['networkPoolIp'].deep_merge!([:options].reject {|k,v| k.is_a?(Symbol) }) if [:options] if payload['networkPoolIp'].empty? raise_command_error "Specify at least one option to update.\n#{optparse}" end end @network_pool_ips_interface.setopts() if [:dry_run] print_dry_run @network_pool_ips_interface.dry.update(network_pool_id, network_pool_ip['id'], payload) return end json_response = @network_pool_ips_interface.update(network_pool_id, network_pool_ip['id'], payload) if [:json] print JSON.pretty_generate(json_response) print "\n" elsif ![:quiet] network_pool_ip = json_response['networkPoolIp'] print_green_success "Updated network pool IP #{network_pool_ip['ipAddress']}" get_ip([network_pool['id'], network_pool_ip['id']]) end return 0 rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |