Class: CloudstackClient::Connection
- Inherits:
-
Object
- Object
- CloudstackClient::Connection
- Defined in:
- lib/cloudstack-client/client.rb
Constant Summary collapse
- @@async_poll_interval =
2.0- @@async_timeout =
300
Instance Method Summary collapse
-
#assign_to_load_balancer_rule(name, vm_names) ⇒ Object
Assigns virtual machine or a list of virtual machines to a load balancer rule.
-
#associate_ip_address(network_id) ⇒ Object
Acquires and associates a public IP to an account.
-
#create_load_balancer_rule(name, publicip, private_port, public_port, options = {}) ⇒ Object
Creates a load balancing rule.
-
#create_offering(args) ⇒ Object
Create a service offering.
-
#create_port_forwarding_rule(ip_address_id, private_port, protocol, public_port, virtual_machine_id) ⇒ Object
Creates a port forwarding rule.
-
#create_server(host_name, service_name, template_name, zone_name = nil, network_names = [], project_name = nil) ⇒ Object
Deploys a new server using the specified parameters.
-
#delete_offering(id) ⇒ Object
Delete a service offering.
-
#destroy_router(id, async = false) ⇒ Object
Destroy virtual router.
-
#destroy_server(id) ⇒ Object
Destroy the server with the specified name.
-
#disassociate_ip_address(id) ⇒ Object
Disassociates an ip address from the account.
-
#get_default_network ⇒ Object
Finds the default network.
-
#get_default_zone ⇒ Object
Finds the default zone for your account.
-
#get_network(name, project_id = nil) ⇒ Object
Finds the network with the specified name.
-
#get_project(name) ⇒ Object
Get project by name.
-
#get_public_ip_address(ip_address) ⇒ Object
Finds the public ip address for a given ip address string.
-
#get_server(name, project_id = nil) ⇒ Object
Finds the server with the specified name.
- #get_server_default_nic(server) ⇒ Object
-
#get_server_fqdn(server) ⇒ Object
Returns the fully qualified domain name for a server.
-
#get_server_public_ip(server, cached_rules = nil) ⇒ Object
Finds the public ip for a server.
- #get_server_state(id) ⇒ Object
-
#get_service_offering(name) ⇒ Object
Finds the service offering with the specified name.
-
#get_ssh_port_forwarding_rule(server, cached_rules = nil) ⇒ Object
Gets the SSH port forwarding rule for the specified server.
-
#get_template(name) ⇒ Object
Finds the template with the specified name.
-
#get_zone(name) ⇒ Object
Finds the zone with the specified name.
-
#initialize(api_url, api_key, secret_key) ⇒ Connection
constructor
A new instance of Connection.
-
#list_accounts(args = { :name => nil }) ⇒ Object
Lists accounts.
-
#list_capacity(args = {}) ⇒ Object
List capacity.
-
#list_domains(name = nil) ⇒ Object
List domains.
-
#list_load_balancer_rules(options = {}) ⇒ Object
List loadbalancer rules.
-
#list_networks(args = {}) ⇒ Object
Lists all available networks.
-
#list_physical_networks ⇒ Object
Lists all physical networks.
-
#list_port_forwarding_rules(ip_address_id = nil, project_id) ⇒ Object
Lists all port forwarding rules.
-
#list_projects ⇒ Object
Lists projects.
-
#list_public_ip_addresses(args = {}) ⇒ Object
Lists the public ip addresses.
-
#list_routers(args = {:account => nil, :zone => nil, :projectid => nil, :status => nil, :name => nil}) ⇒ Object
Lists all virtual routers.
-
#list_servers(options = {}) ⇒ Object
Lists all the servers in your account.
-
#list_service_offerings(domain = nil) ⇒ Object
Lists all available service offerings.
-
#list_templates(args = {}) ⇒ Object
Lists all templates that match the specified filter.
-
#list_volumes(project_id = nil) ⇒ Object
Lists all volumes.
-
#list_zones ⇒ Object
Lists all available zones.
-
#reboot_server(name) ⇒ Object
Reboot the server with the specified name.
-
#send_async_request(params) ⇒ Object
Sends an asynchronous request and waits for the response.
-
#send_request(params) ⇒ Object
Sends a synchronous request to the CloudStack API and returns the response as a Hash.
-
#start_router(id, async = false) ⇒ Object
Start virtual router.
-
#start_server(name) ⇒ Object
Start the server with the specified name.
-
#stop_router(id, async = false) ⇒ Object
Stop virtual router.
-
#stop_server(name, forced = nil) ⇒ Object
Stops the server with the specified name.
- #update_offering(args) ⇒ Object
- #wait_for_server_state(id, state) ⇒ Object
Constructor Details
#initialize(api_url, api_key, secret_key) ⇒ Connection
Returns a new instance of Connection.
34 35 36 37 38 39 |
# File 'lib/cloudstack-client/client.rb', line 34 def initialize(api_url, api_key, secret_key) @api_url = api_url @api_key = api_key @secret_key = secret_key @use_ssl = api_url.start_with? "https" end |
Instance Method Details
#assign_to_load_balancer_rule(name, vm_names) ⇒ Object
Assigns virtual machine or a list of virtual machines to a load balancer rule.
756 757 758 759 760 761 762 763 764 765 766 767 768 769 |
# File 'lib/cloudstack-client/client.rb', line 756 def assign_to_load_balancer_rule(name, vm_names) id = list_load_balancer_rules(name).first['id'] vm_ids = vm_names.map do |vm| get_server(vm)['id'] end params = { 'command' => 'assignToLoadBalancerRule', 'id' => id, 'virtualmachineids' => vm_ids.join(',') } json = send_async_request(params) end |
#associate_ip_address(network_id) ⇒ Object
Acquires and associates a public IP to an account.
625 626 627 628 629 630 631 632 633 |
# File 'lib/cloudstack-client/client.rb', line 625 def associate_ip_address(network_id) params = { 'command' => 'associateIpAddress', 'networkid' => network_id } json = send_async_request(params) json['ipaddress'] end |
#create_load_balancer_rule(name, publicip, private_port, public_port, options = {}) ⇒ Object
Creates a load balancing rule.
738 739 740 741 742 743 744 745 746 747 748 749 750 751 |
# File 'lib/cloudstack-client/client.rb', line 738 def create_load_balancer_rule(name, publicip, private_port, public_port, = {}) params = { 'command' => 'createLoadBalancerRule', 'name' => name, 'privateport' => private_port, 'publicport' => public_port, 'publicipid' => get_public_ip_address(publicip)['id'] } params['algorithm'] = [:algorithm] || 'roundrobin' params['openfirewall'] = [:openfirewall] || true json = send_async_request(params) json['LoadBalancerRule'] end |
#create_offering(args) ⇒ Object
Create a service offering.
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/cloudstack-client/client.rb', line 334 def create_offering(args) params = { 'command' => 'createServiceOffering', 'name' => args[:name], 'cpunumber' => args[:cpunumber], 'cpuspeed' => args[:cpuspeed], 'displaytext' => args[:displaytext], 'memory' => args[:memory] } if args['domain'] params['domainid'] = list_domains(args['domain']).first["id"] end params['tags'] = args[:tags] if args[:tags] params['offerha'] = 'true' if args[:ha] json = send_request(params) json['serviceoffering'].first end |
#create_port_forwarding_rule(ip_address_id, private_port, protocol, public_port, virtual_machine_id) ⇒ Object
Creates a port forwarding rule.
679 680 681 682 683 684 685 686 687 688 689 690 |
# File 'lib/cloudstack-client/client.rb', line 679 def create_port_forwarding_rule(ip_address_id, private_port, protocol, public_port, virtual_machine_id) params = { 'command' => 'createPortForwardingRule', 'ipAddressId' => ip_address_id, 'privatePort' => private_port, 'protocol' => protocol, 'publicPort' => public_port, 'virtualMachineId' => virtual_machine_id } json = send_async_request(params) json['portforwardingrule'] end |
#create_server(host_name, service_name, template_name, zone_name = nil, network_names = [], project_name = nil) ⇒ Object
Deploys a new server using the specified parameters.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/cloudstack-client/client.rb', line 144 def create_server(host_name, service_name, template_name, zone_name=nil, network_names=[], project_name=nil) if host_name then if get_server(host_name) then puts "Error: Server '#{host_name}' already exists." exit 1 end end service = get_service_offering(service_name) if !service then puts "Error: Service offering '#{service_name}' is invalid" exit 1 end template = get_template(template_name) if !template then puts "Error: Template '#{template_name}' is invalid" exit 1 end zone = zone_name ? get_zone(zone_name) : get_default_zone if !zone then msg = zone_name ? "Zone '#{zone_name}' is invalid" : "No default zone found" puts "Error: #{msg}" exit 1 end if project_name project = get_project(project_name) if !project then msg = "Project '#{project_name}' is invalid" puts "Error: #{msg}" exit 1 end end networks = [] network_names.each do |name| network = project_name ? get_network(name, project['id']) : get_network(name) if !network then puts "Error: Network '#{name}' not found" exit 1 end networks << network end if networks.empty? then networks << get_default_network end if networks.empty? then puts "No default network found" exit 1 end network_ids = networks.map { |network| network['id'] } params = { 'command' => 'deployVirtualMachine', 'serviceOfferingId' => service['id'], 'templateId' => template['id'], 'zoneId' => zone['id'], 'networkids' => network_ids.join(',') } params['name'] = host_name if host_name params['projectid'] = project['id'] if project_name json = send_async_request(params) json['virtualmachine'] end |
#delete_offering(id) ⇒ Object
Delete a service offering.
358 359 360 361 362 363 364 365 366 |
# File 'lib/cloudstack-client/client.rb', line 358 def delete_offering(id) params = { 'command' => 'deleteServiceOffering', 'id' => id } json = send_request(params) json['success'] end |
#destroy_router(id, async = false) ⇒ Object
Destroy virtual router.
808 809 810 811 812 813 814 |
# File 'lib/cloudstack-client/client.rb', line 808 def destroy_router(id, async = false) params = { 'command' => 'destroyRouter', 'id' => id } async ? send_async_request(params) : send_request(params) end |
#destroy_server(id) ⇒ Object
Destroy the server with the specified name.
279 280 281 282 283 284 285 286 287 |
# File 'lib/cloudstack-client/client.rb', line 279 def destroy_server(id) params = { 'command' => 'destroyVirtualMachine', 'id' => id } json = send_async_request(params) json['virtualmachine'] end |
#disassociate_ip_address(id) ⇒ Object
Disassociates an ip address from the account.
Returns true if successful, false otherwise.
640 641 642 643 644 645 646 647 |
# File 'lib/cloudstack-client/client.rb', line 640 def disassociate_ip_address(id) params = { 'command' => 'disassociateIpAddress', 'id' => id } json = send_async_request(params) json['success'] end |
#get_default_network ⇒ Object
Finds the default network.
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
# File 'lib/cloudstack-client/client.rb', line 459 def get_default_network params = { 'command' => 'listNetworks', 'isDefault' => true } json = send_request(params) networks = json['network'] return nil if !networks || networks.empty? default = networks.first return default if networks.length == 1 networks.each { |n| if n['type'] == 'Direct' then default = n break end } default end |
#get_default_zone ⇒ Object
Finds the default zone for your account.
554 555 556 557 558 559 560 561 562 563 564 565 |
# File 'lib/cloudstack-client/client.rb', line 554 def get_default_zone params = { 'command' => 'listZones', 'available' => 'true' } json = send_request(params) zones = json['zone'] return nil unless zones zones.first end |
#get_network(name, project_id = nil) ⇒ Object
Finds the network with the specified name.
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/cloudstack-client/client.rb', line 437 def get_network(name, project_id = nil) params = { 'command' => 'listNetworks', 'listall' => true } params['projectid'] = project_id if project_id json = send_request(params) networks = json['network'] return nil unless networks networks.each { |n| if n['name'] == name then return n end } nil end |
#get_project(name) ⇒ Object
Get project by name.
695 696 697 698 699 700 701 702 703 |
# File 'lib/cloudstack-client/client.rb', line 695 def get_project(name) params = { 'command' => 'listProjects', 'name' => name, 'listall' => true, } json = send_request(params) json['project'] ? json['project'].first : nil end |
#get_public_ip_address(ip_address) ⇒ Object
Finds the public ip address for a given ip address string.
609 610 611 612 613 614 615 616 617 618 619 |
# File 'lib/cloudstack-client/client.rb', line 609 def get_public_ip_address(ip_address) params = { 'command' => 'listPublicIpAddresses', 'ipaddress' => ip_address } json = send_request(params) ip_address = json['publicipaddress'] return nil unless ip_address ip_address.first end |
#get_server(name, project_id = nil) ⇒ Object
Finds the server with the specified name.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/cloudstack-client/client.rb', line 44 def get_server(name, project_id=nil) params = { 'command' => 'listVirtualMachines', 'name' => name } params['projectid'] = project_id if project_id json = send_request(params) machines = json['virtualmachine'] if !machines || machines.empty? then return nil end machines.select {|m| m['name'] == name }.first end |
#get_server_default_nic(server) ⇒ Object
117 118 119 120 121 |
# File 'lib/cloudstack-client/client.rb', line 117 def get_server_default_nic(server) server['nic'].each do |nic| return nic if nic['isdefault'] end end |
#get_server_fqdn(server) ⇒ Object
Returns the fully qualified domain name for a server.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/cloudstack-client/client.rb', line 102 def get_server_fqdn(server) return nil unless server nic = get_server_default_nic(server) || {} networks = list_networks(project_id: server['projectid']) || {} id = nic['networkid'] network = networks.select { |net| net['id'] == id }.first return nil unless network "#{server['name']}.#{network['networkdomain']}" end |
#get_server_public_ip(server, cached_rules = nil) ⇒ Object
Finds the public ip for a server
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/cloudstack-client/client.rb', line 86 def get_server_public_ip(server, cached_rules=nil) return nil unless server # find the public ip nic = get_server_default_nic(server) || {} if nic['type'] == 'Virtual' then ssh_rule = get_ssh_port_forwarding_rule(server, cached_rules) ssh_rule ? ssh_rule['ipaddress'] : nil else nic['ipaddress'] end end |
#get_server_state(id) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/cloudstack-client/client.rb', line 60 def get_server_state(id) params = { 'command' => 'listVirtualMachines', 'id' => id } json = send_request(params) machine_state = json['virtualmachine'][0]['state'] if !machine_state || machine_state.empty? then return nil end machine_state end |
#get_service_offering(name) ⇒ Object
Finds the service offering with the specified name.
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/cloudstack-client/client.rb', line 292 def get_service_offering(name) # TODO: use name parameter # listServiceOfferings in CloudStack 2.2 doesn't seem to work # when the name parameter is specified. When this is fixed, # the name parameter should be added to the request. params = { 'command' => 'listServiceOfferings' } json = send_request(params) services = json['serviceoffering'] return nil unless services services.each { |s| if s['name'] == name then return s end } nil end |
#get_ssh_port_forwarding_rule(server, cached_rules = nil) ⇒ Object
Gets the SSH port forwarding rule for the specified server.
667 668 669 670 671 672 673 674 |
# File 'lib/cloudstack-client/client.rb', line 667 def get_ssh_port_forwarding_rule(server, cached_rules=nil) rules = cached_rules || list_port_forwarding_rules || [] rules.find_all { |r| r['virtualmachineid'] == server['id'] && r['privateport'] == '22'&& r['publicport'] == '22' }.first end |
#get_template(name) ⇒ Object
Finds the template with the specified name.
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 |
# File 'lib/cloudstack-client/client.rb', line 384 def get_template(name) # TODO: use name parameter # listTemplates in CloudStack 2.2 doesn't seem to work # when the name parameter is specified. When this is fixed, # the name parameter should be added to the request. params = { 'command' => 'listTemplates', 'templateFilter' => 'executable' } json = send_request(params) templates = json['template'] if !templates then return nil end templates.each { |t| if t['name'] == name then return t end } nil end |
#get_zone(name) ⇒ Object
Finds the zone with the specified name.
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
# File 'lib/cloudstack-client/client.rb', line 532 def get_zone(name) params = { 'command' => 'listZones', 'available' => 'true' } json = send_request(params) networks = json['zone'] return nil unless networks networks.each { |z| if z['name'] == name then return z end } nil end |
#list_accounts(args = { :name => nil }) ⇒ Object
Lists accounts.
841 842 843 844 845 846 847 848 849 850 851 |
# File 'lib/cloudstack-client/client.rb', line 841 def list_accounts(args = { :name => nil }) params = { 'command' => 'listAccounts', 'listall' => 'true', 'isrecursive' => 'true' } params['name'] = args[:name] if args[:name] json = send_request(params) json['account'] || [] end |
#list_capacity(args = {}) ⇒ Object
List capacity.
871 872 873 874 875 876 877 878 |
# File 'lib/cloudstack-client/client.rb', line 871 def list_capacity(args = {}) params = { 'command' => 'listCapacity', } json = send_request(params) json['capacity'] || [] end |
#list_domains(name = nil) ⇒ Object
List domains.
856 857 858 859 860 861 862 863 864 865 866 |
# File 'lib/cloudstack-client/client.rb', line 856 def list_domains(name = nil) params = { 'command' => 'listDomains', 'listall' => 'true', 'isrecursive' => 'true' } params['name'] = name if name json = send_request(params) json['domain'] || [] end |
#list_load_balancer_rules(options = {}) ⇒ Object
List loadbalancer rules
720 721 722 723 724 725 726 727 728 729 730 731 732 733 |
# File 'lib/cloudstack-client/client.rb', line 720 def list_load_balancer_rules( = {}) params = { 'command' => 'listLoadBalancerRules', } params['name'] = [:name] if [:name] if [:project_name] project = get_project([:project_name]) params['projectid'] = project['id'] end json = send_request(params) json['loadbalancerrule'] || [] end |
#list_networks(args = {}) ⇒ Object
Lists all available networks.
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 |
# File 'lib/cloudstack-client/client.rb', line 485 def list_networks(args = {}) params = { 'command' => 'listNetworks', 'listall' => true, } params['projectid'] = args[:project_id] if args[:project_id] params['zoneid'] = args[:zone_id] if args[:zone_id] if args[:account] domain = list_accounts(name: args[:account]) if domain.size > 0 params['account'] = args[:account] params['domainid'] = domain.first["domainid"] else puts "Account #{args[:account]} not found." end end json = send_request(params) json['network'] || [] end |
#list_physical_networks ⇒ Object
Lists all physical networks.
508 509 510 511 512 513 514 |
# File 'lib/cloudstack-client/client.rb', line 508 def list_physical_networks params = { 'command' => 'listPhysicalNetworks', } json = send_request(params) json['physicalnetwork'] || [] end |
#list_port_forwarding_rules(ip_address_id = nil, project_id) ⇒ Object
Lists all port forwarding rules.
652 653 654 655 656 657 658 659 660 661 662 |
# File 'lib/cloudstack-client/client.rb', line 652 def list_port_forwarding_rules(ip_address_id=nil, project_id) params = { 'command' => 'listPortForwardingRules', 'listall' => true, 'isrecursive' => true } params['ipAddressId'] = ip_address_id if ip_address_id params['projectid'] = project_id if project_id json = send_request(params) json['portforwardingrule'] || [] end |
#list_projects ⇒ Object
Lists projects.
708 709 710 711 712 713 714 715 |
# File 'lib/cloudstack-client/client.rb', line 708 def list_projects params = { 'command' => 'listProjects', 'listall' => true, } json = send_request(params) json['project'] || [] end |
#list_public_ip_addresses(args = {}) ⇒ Object
Lists the public ip addresses.
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
# File 'lib/cloudstack-client/client.rb', line 582 def list_public_ip_addresses(args = {}) params = { 'command' => 'listPublicIpAddresses', 'isrecursive' => true } if args[:project] project = get_project(args[:project]) params['projectid'] = project['id'] end if args[:account] account = list_accounts({name: args[:account]}).first unless account puts "Error: Account #{args[:account]} not found." exit 1 end params['domainid'] = account["domainid"] params['account'] = args[:account] end params['listall'] = args[:listall] if args[:listall] json = send_request(params) json['publicipaddress'] || [] end |
#list_routers(args = {:account => nil, :zone => nil, :projectid => nil, :status => nil, :name => nil}) ⇒ Object
Lists all virtual routers.
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 |
# File 'lib/cloudstack-client/client.rb', line 774 def list_routers(args = {:account => nil, :zone => nil, :projectid => nil, :status => nil, :name => nil}) params = { 'command' => 'listRouters', 'listall' => 'true', 'isrecursive' => 'true' } if args[:zone] zone = get_zone(args[:zone]) unless zone puts "Error: Zone #{args[:zone]} not found" exit 1 end params['zoneid'] = zone['id'] end params['projectid'] = args[:projectid] if args[:projectid] params['state'] = args[:status] if args[:status] params['name'] = args[:name] if args[:name] if args[:account] account = list_accounts({name: args[:account]}).first unless account puts "Error: Account #{args[:account]} not found." exit 1 end params['domainid'] = account["domainid"] params['account'] = args[:account] end json = send_request(params) json['router'] || [] end |
#list_servers(options = {}) ⇒ Object
Lists all the servers in your account.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/cloudstack-client/client.rb', line 126 def list_servers( = {}) params = { 'command' => 'listVirtualMachines', 'listAll' => true } params['projectid'] = [:project_id] if [:project_id] if [:account] params['domainid'] = list_accounts({name: [:account]}).first["domainid"] params['account'] = [:account] end json = send_request(params) json['virtualmachine'] || [] end |
#list_service_offerings(domain = nil) ⇒ Object
Lists all available service offerings.
318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/cloudstack-client/client.rb', line 318 def list_service_offerings(domain = nil) params = { 'command' => 'listServiceOfferings' } if domain params['domainid'] = list_domains(domain).first["id"] end json = send_request(params) json['serviceoffering'] || [] end |
#list_templates(args = {}) ⇒ Object
Lists all templates that match the specified filter.
Allowable filter values are:
-
featured - templates that are featured and are public
-
self - templates that have been registered/created by the owner
-
self-executable - templates that have been registered/created by the owner that can be used to deploy a new VM
-
executable - all templates that can be used to deploy a new VM
-
community - templates that are public
421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/cloudstack-client/client.rb', line 421 def list_templates(args = {}) filter = args[:filter] || 'featured' params = { 'command' => 'listTemplates', 'templateFilter' => filter } params['projectid'] = args[:project_id] if args[:project_id] params['zoneid'] = args[:zone_id] if args[:zone_id] json = send_request(params) json['template'] || [] end |
#list_volumes(project_id = nil) ⇒ Object
Lists all volumes.
519 520 521 522 523 524 525 526 527 |
# File 'lib/cloudstack-client/client.rb', line 519 def list_volumes(project_id = nil) params = { 'command' => 'listVolumes', 'listall' => true, } params['projectid'] = project_id if project_id json = send_request(params) json['network'] || [] end |
#list_zones ⇒ Object
Lists all available zones.
570 571 572 573 574 575 576 577 |
# File 'lib/cloudstack-client/client.rb', line 570 def list_zones params = { 'command' => 'listZones', 'available' => 'true' } json = send_request(params) json['zone'] || [] end |
#reboot_server(name) ⇒ Object
Reboot the server with the specified name.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/cloudstack-client/client.rb', line 259 def reboot_server(name) server = get_server(name) if !server || !server['id'] then puts "Error: Virtual machine '#{name}' does not exist" exit 1 end params = { 'command' => 'rebootVirtualMachine', 'id' => server['id'] } json = send_async_request(params) json['virtualmachine'] end |
#send_async_request(params) ⇒ Object
Sends an asynchronous request and waits for the response.
The contents of the ‘jobresult’ element are returned upon completion of the command.
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 |
# File 'lib/cloudstack-client/client.rb', line 930 def send_async_request(params) json = send_request(params) params = { 'command' => 'queryAsyncJobResult', 'jobId' => json['jobid'] } max_tries = (@@async_timeout / @@async_poll_interval).round max_tries.times do json = send_request(params) status = json['jobstatus'] print "." if status == 1 then return json['jobresult'] elsif status == 2 then print "\n" puts "Request failed (#{json['jobresultcode']}): #{json['jobresult']}" exit 1 end STDOUT.flush sleep @@async_poll_interval end print "\n" puts "Error: Asynchronous request timed out" exit 1 end |
#send_request(params) ⇒ Object
Sends a synchronous request to the CloudStack API and returns the response as a Hash.
The wrapper element of the response (e.g. mycommandresponse) is discarded and the contents of that element are returned.
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 |
# File 'lib/cloudstack-client/client.rb', line 886 def send_request(params) params['response'] = 'json' params['apiKey'] = @api_key params_arr = [] params.sort.each { |elem| params_arr << elem[0].to_s + '=' + CGI.escape(elem[1].to_s).gsub('+', '%20').gsub(' ','%20') } data = params_arr.join('&') signature = OpenSSL::HMAC.digest('sha1', @secret_key, data.downcase) signature = Base64.encode64(signature).chomp signature = CGI.escape(signature) url = "#{@api_url}?#{data}&signature=#{signature}" uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = @use_ssl http.verify_mode = OpenSSL::SSL::VERIFY_NONE begin response = http.request(Net::HTTP::Get.new(uri.request_uri)) rescue puts "Error connecting to API:" puts "#{@api_url} is not reachable" exit 1 end if !response.is_a?(Net::HTTPOK) then puts "Error #{response.code}: #{response.message}" puts JSON.pretty_generate(JSON.parse(response.body)) puts "URL: #{url}" exit 1 end json = JSON.parse(response.body) json[params['command'].downcase + 'response'] end |
#start_router(id, async = false) ⇒ Object
Start virtual router.
819 820 821 822 823 824 825 |
# File 'lib/cloudstack-client/client.rb', line 819 def start_router(id, async = false) params = { 'command' => 'startRouter', 'id' => id } async ? send_async_request(params) : send_request(params) end |
#start_server(name) ⇒ Object
Start the server with the specified name.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/cloudstack-client/client.rb', line 239 def start_server(name) server = get_server(name) if !server || !server['id'] then puts "Error: Virtual machine '#{name}' does not exist" exit 1 end params = { 'command' => 'startVirtualMachine', 'id' => server['id'] } json = send_async_request(params) json['virtualmachine'] end |
#stop_router(id, async = false) ⇒ Object
Stop virtual router.
830 831 832 833 834 835 836 |
# File 'lib/cloudstack-client/client.rb', line 830 def stop_router(id, async = false) params = { 'command' => 'stopRouter', 'id' => id } async ? send_async_request(params) : send_request(params) end |
#stop_server(name, forced = nil) ⇒ Object
Stops the server with the specified name.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/cloudstack-client/client.rb', line 218 def stop_server(name, forced=nil) server = get_server(name) if !server || !server['id'] then puts "Error: Virtual machine '#{name}' does not exist" exit 1 end params = { 'command' => 'stopVirtualMachine', 'id' => server['id'] } params['forced'] = true if forced json = send_async_request(params) json['virtualmachine'] end |
#update_offering(args) ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/cloudstack-client/client.rb', line 368 def update_offering(args) params = { 'command' => 'updateServiceOffering', 'id' => args['id'] } params['name'] = args['name'] if args['name'] params['displaytext'] = args['displaytext'] if args['displaytext'] params['sortkey'] = args['sortkey'] if args['sortkey'] json = send_request(params) json['serviceoffering'] end |
#wait_for_server_state(id, state) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/cloudstack-client/client.rb', line 75 def wait_for_server_state(id, state) while get_server_state(id) != state print '..' sleep 5 end state end |