Class: VagrantPlugins::VCloud::Driver::Version_5_1
- Defined in:
- lib/vagrant-vcloud/driver/version_5_1.rb
Overview
Main class to access vCloud rest APIs
Instance Attribute Summary collapse
-
#auth_key ⇒ Object
readonly
Returns the value of attribute auth_key.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
-
#add_vapp_port_forwarding_rules(vappid, network_name, config = {}) ⇒ Object
nat_rules << { :nat_external_port => j.to_s, :nat_internal_port => “22”, :nat_protocol => “TCP”, :vm_scoped_local_id => value}.
-
#compose_vapp_from_vm(vdc, vapp_name, vapp_description, vm_list = {}, network_config = {}) ⇒ Object
Compose a vapp using existing virtual machines.
-
#create_catalog(orgId, catalogName, catalogDescription) ⇒ Object
Create a catalog in an organization.
-
#create_vapp_from_template(vdc, vapp_name, vapp_description, vapp_templateid, poweron = false) ⇒ Object
Create a vapp starting from a template.
-
#delete_vapp(vAppId) ⇒ Object
Delete a given vapp NOTE: It doesn’t verify that the vapp is shutdown.
-
#delete_vm(vmId) ⇒ Object
VM operations ####.
- #find_edge_gateway_id(edge_gateway_name, vdc_id) ⇒ Object
- #find_edge_gateway_network(edge_gateway_name, vdc_id, edge_gateway_ip) ⇒ Object
-
#get_catalog(catalogId) ⇒ Object
Fetch details about a given catalog.
-
#get_catalog_by_name(organization, catalogName) ⇒ Object
Friendly helper method to fetch an catalog by name - organization hash (from get_organization/get_organization_by_name) - catalog name.
-
#get_catalog_id_by_name(organization, catalogName) ⇒ Object
Friendly helper method to fetch an catalog id by name - organization hash (from get_organization/get_organization_by_name) - catalog name.
-
#get_catalog_item(catalogItemId) ⇒ Object
Fetch details about a given catalog item: - description - vApp templates.
-
#get_catalog_item_by_name(catalogId, catalogItemName) ⇒ Object
friendly helper method to fetch an catalogItem by name - catalogId (use get_catalog_name(org, name)) - catalagItemName.
-
#get_organization(orgId) ⇒ Object
Fetch details about an organization: - catalogs - vdcs - networks.
-
#get_organization_by_name(name) ⇒ Object
friendly helper method to fetch an Organization by name - name (this isn’t case sensitive).
-
#get_organization_id_by_name(name) ⇒ Object
friendly helper method to fetch an Organization Id by name - name (this isn’t case sensitive).
-
#get_organizations ⇒ Object
Fetch existing organizations and their IDs.
-
#get_task(taskid) ⇒ Object
Fetch information for a given task.
-
#get_vapp(vAppId) ⇒ Object
Fetch details about a given vapp: - name - description - status - IP - Children VMs: – IP addresses – status – ID.
-
#get_vapp_edge_public_ip(vAppId) ⇒ Object
get vApp edge public IP from the vApp ID Only works when: - vApp needs to be poweredOn - FenceMode is set to “natRouted” - NatType“ is set to ”portForwarding This will be required to know how to connect to VMs behind the Edge device.
-
#get_vapp_port_forwarding_external_ports(vAppId) ⇒ Object
Get vApp port forwarding rules external ports used and returns a set instead of an HASH.
-
#get_vapp_port_forwarding_rules(vAppId) ⇒ Object
nat_rules << { :nat_external_port => j.to_s, :nat_internal_port => “22”, :nat_protocol => “TCP”, :vm_scoped_local_id => value}.
-
#get_vapp_template(vAppId) ⇒ Object
Fetch details about a given vapp template: - name - description - Children VMs: – ID.
-
#get_vdc(vdcId) ⇒ Object
Fetch details about a given vdc: - description - vapps - networks.
-
#get_vdc_by_name(organization, vdcName) ⇒ Object
Friendly helper method to fetch a Organization VDC by name - Organization object - Organization VDC Name.
-
#get_vdc_id_by_name(organization, vdcName) ⇒ Object
Friendly helper method to fetch a Organization VDC Id by name - Organization object - Organization VDC Name.
-
#get_vm(vmId) ⇒ Object
Fetch details about a given VM.
-
#initialize(host, username, password, org_name) ⇒ Version_5_1
constructor
A new instance of Version_5_1.
-
#login ⇒ Object
Authenticate against the specified server.
-
#logout ⇒ Object
Destroy the current session.
-
#poweroff_vapp(vAppId) ⇒ Object
Shutdown a given vapp.
-
#poweroff_vm(vmId) ⇒ Object
Shutdown a given VM Using undeploy as a REAL powerOff Only poweroff will put the VM into a partially powered off state.
-
#poweron_vapp(vAppId) ⇒ Object
Boot a given vapp.
-
#poweron_vm(vmId) ⇒ Object
Boot a given vm.
-
#reboot_vapp(vAppId) ⇒ Object
reboot a given vapp This will basically initial a guest OS reboot, and will only work if VMware-tools are installed on the underlying VMs.
-
#reboot_vm(vmId) ⇒ Object
reboot a given VM This will basically initial a guest OS reboot, and will only work if VMware-tools are installed on the underlying VMs.
-
#recompose_vapp_from_vm(vAppId, vm_list = {}, network_config = {}) ⇒ Object
Recompose an existing vapp using existing virtual machines.
- #remove_edge_gateway_rules(edge_gateway_name, vdc_id, edge_gateway_ip, vAppId) ⇒ Object
-
#reset_vapp(vAppId) ⇒ Object
reset a given vapp This will basically reset the VMs within the vApp vShield Edge devices are not affected.
-
#reset_vm(vmId) ⇒ Object
reset a given VM This will basically reset the VMs within the vApp vShield Edge devices are not affected.
-
#set_edge_gateway_rules(edge_gateway_name, vdc_id, edge_gateway_ip, vAppId) ⇒ Object
Set Org Edge port forwarding and firewall rules.
-
#set_vapp_network_config(vappid, network_name, config = {}) ⇒ Object
Set vApp Network Config.
-
#set_vapp_port_forwarding_rules(vappid, network_name, config = {}) ⇒ Object
Set vApp port forwarding rules.
-
#set_vm_guest_customization(vmid, computer_name, config = {}) ⇒ Object
Set VM Guest Customization Config.
-
#set_vm_network_config(vmid, network_name, config = {}) ⇒ Object
Set VM Network Config.
-
#suspend_vapp(vAppId) ⇒ Object
Suspend a given vapp.
-
#suspend_vm(vmId) ⇒ Object
Suspend a given VM.
-
#upload_ovf(vdcId, vappName, vappDescription, ovfFile, catalogId, uploadOptions = {}) ⇒ Object
Upload an OVF package - vdcId - vappName - vappDescription - ovfFile - catalogId - uploadOptions {}.
-
#wait_task_completion(taskid) ⇒ Object
Poll a given task until completion.
Constructor Details
#initialize(host, username, password, org_name) ⇒ Version_5_1
Returns a new instance of Version_5_1.
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 30 def initialize(host, username, password, org_name) @logger = Log4r::Logger.new("vagrant::provider::vcloud::driver_5_1") @host = host @api_url = "#{host}/api" @host_url = "#{host}" @username = username @password = password @org_name = org_name @api_version = "5.1" @id = nil end |
Instance Attribute Details
#auth_key ⇒ Object (readonly)
Returns the value of attribute auth_key.
28 29 30 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 28 def auth_key @auth_key end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
28 29 30 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 28 def id @id end |
Instance Method Details
#add_vapp_port_forwarding_rules(vappid, network_name, config = {}) ⇒ Object
nat_rules << { :nat_external_port => j.to_s, :nat_internal_port => “22”, :nat_protocol => “TCP”, :vm_scoped_local_id => value}
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 916 def add_vapp_port_forwarding_rules(vappid, network_name, config={}) builder = Nokogiri::XML::Builder.new do |xml| xml.NetworkConfigSection( "xmlns" => "http://www.vmware.com/vcloud/v1.5", "xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1") { xml['ovf'].Info "Network configuration" xml.NetworkConfig("networkName" => network_name) { xml.Configuration { xml.ParentNetwork("href" => "#{@api_url}/network/#{config[:parent_network]}") xml.FenceMode(config[:fence_mode] || 'isolated') xml.Features { xml.NatService { xml.IsEnabled "true" xml.NatType "portForwarding" xml.Policy(config[:nat_policy_type] || "allowTraffic") preExisting = get_vapp_port_forwarding_rules(vappid) config[:nat_rules].concat(preExisting) config[:nat_rules].each do |nat_rule| xml.NatRule { xml.VmRule { xml.ExternalPort nat_rule[:nat_external_port] xml.VAppScopedVmId nat_rule[:vapp_scoped_local_id] xml.VmNicId(nat_rule[:nat_vmnic_id] || "0") xml.InternalPort nat_rule[:nat_internal_port] xml.Protocol(nat_rule[:nat_protocol] || "TCP") } } end } } } } } end params = { 'method' => :put, 'command' => "/vApp/vapp-#{vappid}/networkConfigSection" } response, headers = send_request(params, builder.to_xml, "application/vnd.vmware.vcloud.networkConfigSection+xml") task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#compose_vapp_from_vm(vdc, vapp_name, vapp_description, vm_list = {}, network_config = {}) ⇒ Object
Compose a vapp using existing virtual machines
Params:
-
vdc: the associated VDC
-
vapp_name: name of the target vapp
-
vapp_description: description of the target vapp
-
vm_list: hash with IDs of the VMs to be used in the composing process
-
network_config: hash of the network configuration for the vapp
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 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 670 def compose_vapp_from_vm(vdc, vapp_name, vapp_description, vm_list={}, network_config={}) builder = Nokogiri::XML::Builder.new do |xml| xml.ComposeVAppParams( "xmlns" => "http://www.vmware.com/vcloud/v1.5", "xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1", "name" => vapp_name, "deploy" => "false", "powerOn" => "false") { xml.Description vapp_description xml.InstantiationParams { xml.NetworkConfigSection { xml['ovf'].Info "Configuration parameters for logical networks" xml.NetworkConfig("networkName" => network_config[:name]) { xml.Configuration { xml.IpScopes { xml.IpScope { xml.IsInherited(network_config[:is_inherited] || "false") xml.Gateway network_config[:gateway] xml.Netmask network_config[:netmask] xml.Dns1 network_config[:dns1] if network_config[:dns1] xml.Dns2 network_config[:dns2] if network_config[:dns2] xml.DnsSuffix network_config[:dns_suffix] if network_config[:dns_suffix] xml.IpRanges { xml.IpRange { xml.StartAddress network_config[:start_address] xml.EndAddress network_config[:end_address] } } } } xml.ParentNetwork("href" => "#{@api_url}/network/#{network_config[:parent_network]}") xml.FenceMode network_config[:fence_mode] xml.Features { xml.FirewallService { xml.IsEnabled(network_config[:enable_firewall] || "false") } xml.NatService { xml.IsEnabled "true" xml.NatType "portForwarding" xml.Policy(network_config[:nat_policy_type] || "allowTraffic") } } } } } } vm_list.each do |vm_name, vm_id| xml.SourcedItem { xml.Source("href" => "#{@api_url}/vAppTemplate/vm-#{vm_id}", "name" => vm_name) xml.InstantiationParams { xml.NetworkConnectionSection( "xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1", "type" => "application/vnd.vmware.vcloud.networkConnectionSection+xml", "href" => "#{@api_url}/vAppTemplate/vm-#{vm_id}/networkConnectionSection/") { xml['ovf'].Info "Network config for sourced item" xml.PrimaryNetworkConnectionIndex "0" xml.NetworkConnection("network" => network_config[:name]) { xml.NetworkConnectionIndex "0" xml.IsConnected "true" xml.IpAddressAllocationMode(network_config[:ip_allocation_mode] || "POOL") } } } xml.NetworkAssignment("containerNetwork" => network_config[:name], "innerNetwork" => network_config[:name]) } end xml.AllEULAsAccepted "true" } end params = { "method" => :post, "command" => "/vdc/#{vdc}/action/composeVApp" } response, headers = send_request(params, builder.to_xml, "application/vnd.vmware.vcloud.composeVAppParams+xml") vapp_id = headers["Location"].gsub("#{@api_url}/vApp/vapp-", "") task = response.css("VApp Task[operationName='vdcComposeVapp']").first task_id = task["href"].gsub("#{@api_url}/task/", "") { :vapp_id => vapp_id, :task_id => task_id } end |
#create_catalog(orgId, catalogName, catalogDescription) ⇒ Object
Create a catalog in an organization
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 596 def create_catalog(orgId, catalogName, catalogDescription) builder = Nokogiri::XML::Builder.new do |xml| xml.AdminCatalog( "xmlns" => "http://www.vmware.com/vcloud/v1.5", "name" => catalogName ) { xml.Description catalogDescription } end params = { 'method' => :post, 'command' => "/admin/org/#{orgId}/catalogs" } response, headers = send_request(params, builder.to_xml, "application/vnd.vmware.admin.catalog+xml") task_id = response.css("AdminCatalog Tasks Task[operationName='catalogCreateCatalog']").first[:href].gsub("#{@api_url}/task/","") catalog_id = response.css("AdminCatalog Link [type='application/vnd.vmware.vcloud.catalog+xml']").first[:href].gsub("#{@api_url}/catalog/","") { :task_id => task_id, :catalog_id => catalog_id } end |
#create_vapp_from_template(vdc, vapp_name, vapp_description, vapp_templateid, poweron = false) ⇒ Object
Create a vapp starting from a template
Params:
-
vdc: the associated VDC
-
vapp_name: name of the target vapp
-
vapp_description: description of the target vapp
-
vapp_templateid: ID of the vapp template
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 632 def create_vapp_from_template(vdc, vapp_name, vapp_description, vapp_templateid, poweron=false) builder = Nokogiri::XML::Builder.new do |xml| xml.InstantiateVAppTemplateParams( "xmlns" => "http://www.vmware.com/vcloud/v1.5", "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", "xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1", "name" => vapp_name, "deploy" => "true", "powerOn" => poweron) { xml.Description vapp_description xml.Source("href" => "#{@api_url}/vAppTemplate/#{vapp_templateid}") } end params = { "method" => :post, "command" => "/vdc/#{vdc}/action/instantiateVAppTemplate" } response, headers = send_request(params, builder.to_xml, "application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml") vapp_id = headers["Location"].gsub("#{@api_url}/vApp/vapp-", "") task = response.css("VApp Task[operationName='vdcInstantiateVapp']").first task_id = task["href"].gsub("#{@api_url}/task/", "") { :vapp_id => vapp_id, :task_id => task_id } end |
#delete_vapp(vAppId) ⇒ Object
Delete a given vapp NOTE: It doesn’t verify that the vapp is shutdown
384 385 386 387 388 389 390 391 392 393 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 384 def delete_vapp(vAppId) params = { 'method' => :delete, 'command' => "/vApp/vapp-#{vAppId}" } response, headers = send_request(params) task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#delete_vm(vmId) ⇒ Object
VM operations ####
Delete a given vm NOTE: It doesn’t verify that the vm is shutdown
477 478 479 480 481 482 483 484 485 486 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 477 def delete_vm(vmId) params = { 'method' => :delete, 'command' => "/vApp/vm-#{vmId}" } response, headers = send_request(params) task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#find_edge_gateway_id(edge_gateway_name, vdc_id) ⇒ Object
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 1043 def find_edge_gateway_id(edge_gateway_name, vdc_id) params = { 'method' => :get, 'command' => "/query?type=edgeGateway&format=records&filter=vdc==#{@api_url}/vdc/#{vdc_id}&filter=name==#{edge_gateway_name}" } response, headers = send_request(params) edgeGateway = response.css('EdgeGatewayRecord').first if edgeGateway return edgeGateway['href'].gsub("#{@api_url}/admin/edgeGateway/", "") else return nil end end |
#find_edge_gateway_network(edge_gateway_name, vdc_id, edge_gateway_ip) ⇒ Object
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 1060 def find_edge_gateway_network(edge_gateway_name, vdc_id, edge_gateway_ip) params = { 'method' => :get, 'command' => "/query?type=edgeGateway&format=records&filter=vdc==#{@api_url}/vdc/#{vdc_id}&filter=name==#{edge_gateway_name}" } response, headers = send_request(params) edgeGateway = response.css('EdgeGatewayRecord').first if edgeGateway edgeGatewayId = edgeGateway['href'].gsub("#{@api_url}/admin/edgeGateway/", "") end params = { 'method' => :get, 'command' => "/admin/edgeGateway/#{edgeGatewayId}" } response, headers = send_request(params) response.css("EdgeGateway Configuration GatewayInterfaces GatewayInterface").each do |gw| # Only check uplinks, avoid another check if gw.css("InterfaceType").text == "uplink" # Loop on all sub-allocation pools gw.css("SubnetParticipation IpRanges IpRange").each do |currentRange| lowip = currentRange.css("StartAddress").first.text highip = currentRange.css("EndAddress").first.text rangeIpLow = NetAddr.ip_to_i(lowip) rangeIpHigh = NetAddr.ip_to_i(highip) testIp = NetAddr.ip_to_i(edge_gateway_ip) if (rangeIpLow..rangeIpHigh) === testIp return gw.css("Network").first[:href] end end end end end |
#get_catalog(catalogId) ⇒ Object
Fetch details about a given catalog
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 164 def get_catalog(catalogId) params = { 'method' => :get, 'command' => "/catalog/#{catalogId}" } response, headers = send_request(params) description = response.css("Description").first description = description.text unless description.nil? items = {} response.css("CatalogItem[type='application/vnd.vmware.vcloud.catalogItem+xml']").each do |item| items[item['name']] = item['href'].gsub("#{@api_url}/catalogItem/", "") end { :description => description, :items => items } end |
#get_catalog_by_name(organization, catalogName) ⇒ Object
Friendly helper method to fetch an catalog by name
-
organization hash (from get_organization/get_organization_by_name)
-
catalog name
201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 201 def get_catalog_by_name(organization, catalogName) result = nil organization[:catalogs].each do |catalog| if catalog[0].downcase == catalogName.downcase result = get_catalog(catalog[1]) end end result end |
#get_catalog_id_by_name(organization, catalogName) ⇒ Object
Friendly helper method to fetch an catalog id by name
-
organization hash (from get_organization/get_organization_by_name)
-
catalog name
185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 185 def get_catalog_id_by_name(organization, catalogName) result = nil organization[:catalogs].each do |catalog| if catalog[0].downcase == catalogName.downcase result = catalog[1] end end result end |
#get_catalog_item(catalogItemId) ⇒ Object
Fetch details about a given catalog item:
-
description
-
vApp templates
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 276 def get_catalog_item(catalogItemId) params = { 'method' => :get, 'command' => "/catalogItem/#{catalogItemId}" } response, headers = send_request(params) description = response.css("Description").first description = description.text unless description.nil? items = {} response.css("Entity[type='application/vnd.vmware.vcloud.vAppTemplate+xml']").each do |item| items[item['name']] = item['href'].gsub("#{@api_url}/vAppTemplate/vappTemplate-", "") end { :description => description, :items => items } end |
#get_catalog_item_by_name(catalogId, catalogItemName) ⇒ Object
friendly helper method to fetch an catalogItem by name
-
catalogId (use get_catalog_name(org, name))
-
catalagItemName
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 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 297 def get_catalog_item_by_name(catalogId, catalogItemName) result = nil catalogElems = get_catalog(catalogId) catalogElems[:items].each do |catalogElem| catalogItem = get_catalog_item(catalogElem[1]) if catalogItem[:items][catalogItemName] # This is a vApp Catalog Item # fetch CatalogItemId catalogItemId = catalogItem[:items][catalogItemName] # Fetch the catalogItemId information params = { 'method' => :get, 'command' => "/vAppTemplate/vappTemplate-#{catalogItemId}" } response, headers = send_request(params) # VMs Hash for all the vApp VM entities vms_hash = {} response.css("/VAppTemplate/Children/Vm").each do |vmElem| vmName = vmElem["name"] vmId = vmElem["href"].gsub("#{@api_url}/vAppTemplate/vm-", "") # Add the VM name/id to the VMs Hash vms_hash[vmName] = { :id => vmId } end result = { catalogItemName => catalogItemId, :vms_hash => vms_hash } end end result end |
#get_organization(orgId) ⇒ Object
Fetch details about an organization:
-
catalogs
-
vdcs
-
networks
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 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 132 def get_organization(orgId) params = { 'method' => :get, 'command' => "/org/#{orgId}" } response, headers = send_request(params) catalogs = {} response.css("Link[type='application/vnd.vmware.vcloud.catalog+xml']").each do |item| catalogs[item['name']] = item['href'].gsub("#{@api_url}/catalog/", "") end vdcs = {} response.css("Link[type='application/vnd.vmware.vcloud.vdc+xml']").each do |item| vdcs[item['name']] = item['href'].gsub("#{@api_url}/vdc/", "") end networks = {} response.css("Link[type='application/vnd.vmware.vcloud.orgNetwork+xml']").each do |item| networks[item['name']] = item['href'].gsub("#{@api_url}/network/", "") end tasklists = {} response.css("Link[type='application/vnd.vmware.vcloud.tasksList+xml']").each do |item| tasklists[item['name']] = item['href'].gsub("#{@api_url}/tasksList/", "") end { :catalogs => catalogs, :vdcs => vdcs, :networks => networks, :tasklists => tasklists } end |
#get_organization_by_name(name) ⇒ Object
friendly helper method to fetch an Organization by name
-
name (this isn’t case sensitive)
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 113 def get_organization_by_name(name) result = nil # Fetch all organizations organizations = get_organizations() organizations.each do |organization| if organization[0].downcase == name.downcase result = get_organization(organization[1]) end end result end |
#get_organization_id_by_name(name) ⇒ Object
friendly helper method to fetch an Organization Id by name
-
name (this isn’t case sensitive)
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 95 def get_organization_id_by_name(name) result = nil # Fetch all organizations organizations = get_organizations() organizations.each do |organization| if organization[0].downcase == name.downcase result = organization[1] end end result end |
#get_organizations ⇒ Object
Fetch existing organizations and their IDs
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 76 def get_organizations params = { 'method' => :get, 'command' => '/org' } response, headers = send_request(params) orgs = response.css('OrgList Org') results = {} orgs.each do |org| results[org['name']] = org['href'].gsub("#{@api_url}/org/", "") end results end |
#get_task(taskid) ⇒ Object
Fetch information for a given task
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 1503 def get_task(taskid) params = { 'method' => :get, 'command' => "/task/#{taskid}" } response, headers = send_request(params) task = response.css('Task').first status = task['status'] start_time = task['startTime'] end_time = task['endTime'] { :status => status, :start_time => start_time, :end_time => end_time, :response => response } end |
#get_vapp(vAppId) ⇒ Object
Fetch details about a given vapp:
-
name
-
description
-
status
-
IP
-
Children VMs: – IP addresses – status – ID
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 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 342 def get_vapp(vAppId) params = { 'method' => :get, 'command' => "/vApp/vapp-#{vAppId}" } response, headers = send_request(params) vapp_node = response.css('VApp').first if vapp_node name = vapp_node['name'] status = convert_vapp_status(vapp_node['status']) end description = response.css("Description").first description = description.text unless description.nil? ip = response.css('IpAddress').first ip = ip.text unless ip.nil? vms = response.css('Children Vm') vms_hash = {} # ipAddress could be namespaced or not: see https://github.com/astratto/vcloud-rest/issues/3 vms.each do |vm| vapp_local_id = vm.css('VAppScopedLocalId') addresses = vm.css('rasd|Connection').collect{|n| n['vcloud:ipAddress'] || n['ipAddress'] } vms_hash[vm['name'].to_sym] = { :addresses => addresses, :status => convert_vapp_status(vm['status']), :id => vm['href'].gsub("#{@api_url}/vApp/vm-", ''), :vapp_scoped_local_id => vapp_local_id.text } end # TODO: EXPAND INFO FROM RESPONSE { :name => name, :description => description, :status => status, :ip => ip, :vms_hash => vms_hash } end |
#get_vapp_edge_public_ip(vAppId) ⇒ Object
get vApp edge public IP from the vApp ID Only works when:
-
vApp needs to be poweredOn
-
FenceMode is set to “natRouted”
-
NatType“ is set to ”portForwarding
This will be required to know how to connect to VMs behind the Edge device.
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 1332 def get_vapp_edge_public_ip(vAppId) # Check the network configuration section params = { 'method' => :get, 'command' => "/vApp/vapp-#{vAppId}/networkConfigSection" } response, headers = send_request(params) # FIXME: this will return nil if the vApp uses multiple vApp Networks # with Edge devices in natRouted/portForwarding mode. config = response.css('NetworkConfigSection/NetworkConfig/Configuration') fenceMode = config.css('/FenceMode').text natType = config.css('/Features/NatService/NatType').text raise InvalidStateError, "Invalid request because FenceMode must be set to natRouted." unless fenceMode == "natRouted" raise InvalidStateError, "Invalid request because NatType must be set to portForwarding." unless natType == "portForwarding" # Check the routerInfo configuration where the global external IP is defined edgeIp = config.css('/RouterInfo/ExternalIp').text if edgeIp == "" return nil else return edgeIp end end |
#get_vapp_port_forwarding_external_ports(vAppId) ⇒ Object
Get vApp port forwarding rules external ports used and returns a set instead of an HASH.
-
vappid: id of the vApp
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 1014 def get_vapp_port_forwarding_external_ports(vAppId) params = { 'method' => :get, 'command' => "/vApp/vapp-#{vAppId}/networkConfigSection" } @logger.debug("these are the params: #{params.inspect}") response, headers = send_request(params) # FIXME: this will return nil if the vApp uses multiple vApp Networks # with Edge devices in natRouted/portForwarding mode. config = response.css('NetworkConfigSection/NetworkConfig/Configuration') fenceMode = config.css('/FenceMode').text natType = config.css('/Features/NatService/NatType').text raise InvalidStateError, "Invalid request because FenceMode must be set to natRouted." unless fenceMode == "natRouted" raise InvalidStateError, "Invalid request because NatType must be set to portForwarding." unless natType == "portForwarding" nat_rules = Set.new config.css('/Features/NatService/NatRule').each do |rule| # portforwarding rules information vmRule = rule.css('VmRule') nat_rules.add(vmRule.css('ExternalPort').text.to_i) end nat_rules end |
#get_vapp_port_forwarding_rules(vAppId) ⇒ Object
nat_rules << { :nat_external_port => j.to_s, :nat_internal_port => “22”, :nat_protocol => “TCP”, :vm_scoped_local_id => value}
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 974 def get_vapp_port_forwarding_rules(vAppId) params = { 'method' => :get, 'command' => "/vApp/vapp-#{vAppId}/networkConfigSection" } response, headers = send_request(params) # FIXME: this will return nil if the vApp uses multiple vApp Networks # with Edge devices in natRouted/portForwarding mode. config = response.css('NetworkConfigSection/NetworkConfig/Configuration') fenceMode = config.css('/FenceMode').text natType = config.css('/Features/NatService/NatType').text raise InvalidStateError, "Invalid request because FenceMode must be set to natRouted." unless fenceMode == "natRouted" raise InvalidStateError, "Invalid request because NatType must be set to portForwarding." unless natType == "portForwarding" nat_rules = [] config.css('/Features/NatService/NatRule').each do |rule| # portforwarding rules information ruleId = rule.css('Id').text vmRule = rule.css('VmRule') nat_rules << { :nat_external_ip => vmRule.css('ExternalIpAddress').text, :nat_external_port => vmRule.css('ExternalPort').text, :vapp_scoped_local_id => vmRule.css('VAppScopedVmId').text, :vm_nic_id => vmRule.css('VmNicId').text, :nat_internal_port => vmRule.css('InternalPort').text, :nat_protocol => vmRule.css('Protocol').text } end nat_rules end |
#get_vapp_template(vAppId) ⇒ Object
Fetch details about a given vapp template:
-
name
-
description
-
Children VMs: – ID
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 824 def get_vapp_template(vAppId) params = { 'method' => :get, 'command' => "/vAppTemplate/vappTemplate-#{vAppId}" } response, headers = send_request(params) vapp_node = response.css('VAppTemplate').first if vapp_node name = vapp_node['name'] status = convert_vapp_status(vapp_node['status']) end description = response.css("Description").first description = description.text unless description.nil? ip = response.css('IpAddress').first ip = ip.text unless ip.nil? vms = response.css('Children Vm') vms_hash = {} vms.each do |vm| vms_hash[vm['name']] = { :id => vm['href'].gsub("#{@api_url}/vAppTemplate/vm-", '') } end # TODO: EXPAND INFO FROM RESPONSE { :name => name, :description => description, :vms_hash => vms_hash } end |
#get_vdc(vdcId) ⇒ Object
Fetch details about a given vdc:
-
description
-
vapps
-
networks
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 218 def get_vdc(vdcId) params = { 'method' => :get, 'command' => "/vdc/#{vdcId}" } response, headers = send_request(params) description = response.css("Description").first description = description.text unless description.nil? vapps = {} response.css("ResourceEntity[type='application/vnd.vmware.vcloud.vApp+xml']").each do |item| vapps[item['name']] = item['href'].gsub("#{@api_url}/vApp/vapp-", "") end networks = {} response.css("Network[type='application/vnd.vmware.vcloud.network+xml']").each do |item| networks[item['name']] = item['href'].gsub("#{@api_url}/network/", "") end { :description => description, :vapps => vapps, :networks => networks } end |
#get_vdc_by_name(organization, vdcName) ⇒ Object
Friendly helper method to fetch a Organization VDC by name
-
Organization object
-
Organization VDC Name
260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 260 def get_vdc_by_name(organization, vdcName) result = nil organization[:vdcs].each do |vdc| if vdc[0].downcase == vdcName.downcase result = get_vdc(vdc[1]) end end result end |
#get_vdc_id_by_name(organization, vdcName) ⇒ Object
Friendly helper method to fetch a Organization VDC Id by name
-
Organization object
-
Organization VDC Name
244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 244 def get_vdc_id_by_name(organization, vdcName) result = nil organization[:vdcs].each do |vdc| if vdc[0].downcase == vdcName.downcase result = vdc[1] end end result end |
#get_vm(vmId) ⇒ Object
Fetch details about a given VM
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 1626 def get_vm(vmId) params = { 'method' => :get, 'command' => "/vApp/vm-#{vmId}" } response, headers = send_request(params) os_desc = response.css('ovf|OperatingSystemSection ovf|Description').first.text networks = {} response.css('NetworkConnection').each do |network| ip = network.css('IpAddress').first ip = ip.text if ip networks[network['network']] = { :index => network.css('NetworkConnectionIndex').first.text, :ip => ip, :is_connected => network.css('IsConnected').first.text, :mac_address => network.css('MACAddress').first.text, :ip_allocation_mode => network.css('IpAddressAllocationMode').first.text } end admin_password = response.css('GuestCustomizationSection AdminPassword').first admin_password = admin_password.text if admin_password guest_customizations = { :enabled => response.css('GuestCustomizationSection Enabled').first.text, :admin_passwd_enabled => response.css('GuestCustomizationSection AdminPasswordEnabled').first.text, :admin_passwd_auto => response.css('GuestCustomizationSection AdminPasswordAuto').first.text, :admin_passwd => admin_password, :reset_passwd_required => response.css('GuestCustomizationSection ResetPasswordRequired').first.text, :computer_name => response.css('GuestCustomizationSection ComputerName').first.text } { :os_desc => os_desc, :networks => networks, :guest_customizations => guest_customizations } end |
#login ⇒ Object
Authenticate against the specified server
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 46 def login params = { 'method' => :post, 'command' => '/sessions' } response, headers = send_request(params) if !headers.has_key?("x-vcloud-authorization") raise "Unable to authenticate: missing x_vcloud_authorization header" end @auth_key = headers["x-vcloud-authorization"] end |
#logout ⇒ Object
Destroy the current session
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 63 def logout params = { 'method' => :delete, 'command' => '/session' } response, headers = send_request(params) # reset auth key to nil @auth_key = nil end |
#poweroff_vapp(vAppId) ⇒ Object
Shutdown a given vapp
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 397 def poweroff_vapp(vAppId) builder = Nokogiri::XML::Builder.new do |xml| xml.UndeployVAppParams( "xmlns" => "http://www.vmware.com/vcloud/v1.5") { xml.UndeployPowerAction 'powerOff' } end params = { 'method' => :post, 'command' => "/vApp/vapp-#{vAppId}/action/undeploy" } response, headers = send_request(params, builder.to_xml, "application/vnd.vmware.vcloud.undeployVAppParams+xml") task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#poweroff_vm(vmId) ⇒ Object
Shutdown a given VM Using undeploy as a REAL powerOff Only poweroff will put the VM into a partially powered off state.
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 492 def poweroff_vm(vmId) builder = Nokogiri::XML::Builder.new do |xml| xml.UndeployVAppParams( "xmlns" => "http://www.vmware.com/vcloud/v1.5") { xml.UndeployPowerAction 'powerOff' } end params = { 'method' => :post, 'command' => "/vApp/vm-#{vmId}/action/undeploy" } response, headers = send_request(params, builder.to_xml, "application/vnd.vmware.vcloud.undeployVAppParams+xml") task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#poweron_vapp(vAppId) ⇒ Object
Boot a given vapp
462 463 464 465 466 467 468 469 470 471 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 462 def poweron_vapp(vAppId) params = { 'method' => :post, 'command' => "/vApp/vapp-#{vAppId}/power/action/powerOn" } response, headers = send_request(params) task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#poweron_vm(vmId) ⇒ Object
Boot a given vm
565 566 567 568 569 570 571 572 573 574 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 565 def poweron_vm(vmId) params = { 'method' => :post, 'command' => "/vApp/vm-#{vmId}/power/action/powerOn" } response, headers = send_request(params) task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#reboot_vapp(vAppId) ⇒ Object
reboot a given vapp This will basically initial a guest OS reboot, and will only work if VMware-tools are installed on the underlying VMs. vShield Edge devices are not affected
434 435 436 437 438 439 440 441 442 443 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 434 def reboot_vapp(vAppId) params = { 'method' => :post, 'command' => "/vApp/vapp-#{vAppId}/power/action/reboot" } response, headers = send_request(params) task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#reboot_vm(vmId) ⇒ Object
reboot a given VM This will basically initial a guest OS reboot, and will only work if VMware-tools are installed on the underlying VMs. vShield Edge devices are not affected
537 538 539 540 541 542 543 544 545 546 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 537 def reboot_vm(vmId) params = { 'method' => :post, 'command' => "/vApp/vm-#{vmId}/power/action/reboot" } response, headers = send_request(params) task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#recompose_vapp_from_vm(vAppId, vm_list = {}, network_config = {}) ⇒ Object
Recompose an existing vapp using existing virtual machines
Params:
-
vdc: the associated VDC
-
vapp_name: name of the target vapp
-
vapp_description: description of the target vapp
-
vm_list: hash with IDs of the VMs to be used in the composing process
-
network_config: hash of the network configuration for the vapp
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 767 def recompose_vapp_from_vm(vAppId, vm_list={}, network_config={}) originalVApp = get_vapp(vAppId) builder = Nokogiri::XML::Builder.new do |xml| xml.RecomposeVAppParams( "xmlns" => "http://www.vmware.com/vcloud/v1.5", "xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1", "name" => originalVApp[:name]) { xml.Description originalVApp[:description] xml.InstantiationParams {} vm_list.each do |vm_name, vm_id| xml.SourcedItem { xml.Source("href" => "#{@api_url}/vAppTemplate/vm-#{vm_id}", "name" => vm_name) xml.InstantiationParams { xml.NetworkConnectionSection( "xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1", "type" => "application/vnd.vmware.vcloud.networkConnectionSection+xml", "href" => "#{@api_url}/vAppTemplate/vm-#{vm_id}/networkConnectionSection/") { xml['ovf'].Info "Network config for sourced item" xml.PrimaryNetworkConnectionIndex "0" xml.NetworkConnection("network" => network_config[:name]) { xml.NetworkConnectionIndex "0" xml.IsConnected "true" xml.IpAddressAllocationMode(network_config[:ip_allocation_mode] || "POOL") } } } xml.NetworkAssignment("containerNetwork" => network_config[:name], "innerNetwork" => network_config[:name]) } end xml.AllEULAsAccepted "true" } end params = { "method" => :post, "command" => "/vApp/vapp-#{vAppId}/action/recomposeVApp" } response, headers = send_request(params, builder.to_xml, "application/vnd.vmware.vcloud.recomposeVAppParams+xml") vapp_id = headers["Location"].gsub("#{@api_url}/vApp/vapp-", "") task = response.css("Task [operationName='vdcRecomposeVapp']").first task_id = task["href"].gsub("#{@api_url}/task/", "") { :vapp_id => vapp_id, :task_id => task_id } end |
#remove_edge_gateway_rules(edge_gateway_name, vdc_id, edge_gateway_ip, vAppId) ⇒ Object
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 1275 def remove_edge_gateway_rules(edge_gateway_name, vdc_id, edge_gateway_ip, vAppId) edge_vapp_ip = get_vapp_edge_public_ip(vAppId) edge_gateway_id = find_edge_gateway_id(edge_gateway_name, vdc_id) params = { 'method' => :get, 'command' => "/admin/edgeGateway/#{edge_gateway_id}" } response, headers = send_request(params) interesting = response.css("EdgeGateway Configuration EdgeGatewayServiceConfiguration") interesting.css("NatService NatRule").each do |node| if node.css("RuleType").text == "DNAT" && node.css("GatewayNatRule/OriginalIp").text == edge_gateway_ip && node.css("GatewayNatRule/TranslatedIp").text == edge_vapp_ip node.remove end if node.css("RuleType").text == "SNAT" && node.css("GatewayNatRule/OriginalIp").text == edge_vapp_ip && node.css("GatewayNatRule/TranslatedIp").text == edge_gateway_ip node.remove end end interesting.css("FirewallService FirewallRule").each do |node| if node.css("Port").text == "-1" && node.css("DestinationIp").text == edge_gateway_ip && node.css("DestinationPortRange").text == "Any" node.remove end end builder = Nokogiri::XML::Builder.new builder << interesting remove_edge_rules = Nokogiri::XML(builder.to_xml) xml1 = remove_edge_rules.at_css "EdgeGatewayServiceConfiguration" xml1["xmlns"] = "http://www.vmware.com/vcloud/v1.5" params = { 'method' => :post, 'command' => "/admin/edgeGateway/#{edge_gateway_id}/action/configureServices" } response, headers = send_request(params, remove_edge_rules.to_xml, "application/vnd.vmware.admin.edgeGatewayServiceConfiguration+xml") task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#reset_vapp(vAppId) ⇒ Object
reset a given vapp This will basically reset the VMs within the vApp vShield Edge devices are not affected.
449 450 451 452 453 454 455 456 457 458 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 449 def reset_vapp(vAppId) params = { 'method' => :post, 'command' => "/vApp/vapp-#{vAppId}/power/action/reset" } response, headers = send_request(params) task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#reset_vm(vmId) ⇒ Object
reset a given VM This will basically reset the VMs within the vApp vShield Edge devices are not affected.
552 553 554 555 556 557 558 559 560 561 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 552 def reset_vm(vmId) params = { 'method' => :post, 'command' => "/vApp/vm-#{vmId}/power/action/reset" } response, headers = send_request(params) task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#set_edge_gateway_rules(edge_gateway_name, vdc_id, edge_gateway_ip, vAppId) ⇒ Object
Set Org Edge port forwarding and firewall rules
-
vappid: id of the vapp to be modified
-
network_name: name of the vapp network to be modified
-
config: hash with network configuration specifications, must contain an array inside :nat_rules with the nat rules to be applied.
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 1113 def set_edge_gateway_rules(edge_gateway_name, vdc_id, edge_gateway_ip, vAppId) edge_vapp_ip = get_vapp_edge_public_ip(vAppId) edge_network_id = find_edge_gateway_network(edge_gateway_name, vdc_id, edge_gateway_ip) edge_gateway_id = find_edge_gateway_id(edge_gateway_name, vdc_id) params = { 'method' => :get, 'command' => "/admin/edgeGateway/#{edge_gateway_id}" } response, headers = send_request(params) interesting = response.css("EdgeGateway Configuration EdgeGatewayServiceConfiguration") natRule1 = Nokogiri::XML::Node.new 'NatRule', response ruleType = Nokogiri::XML::Node.new 'RuleType', response ruleType.content = "DNAT" natRule1.add_child ruleType isEnabled = Nokogiri::XML::Node.new 'IsEnabled', response isEnabled.content = "true" natRule1.add_child isEnabled gatewayNatRule = Nokogiri::XML::Node.new 'GatewayNatRule', response natRule1.add_child gatewayNatRule interface = Nokogiri::XML::Node.new 'Interface', response interface["href"] = edge_network_id gatewayNatRule.add_child interface originalIp = Nokogiri::XML::Node.new 'OriginalIp', response originalIp.content = edge_gateway_ip gatewayNatRule.add_child originalIp originalPort = Nokogiri::XML::Node.new 'OriginalPort', response originalPort.content = "any" gatewayNatRule.add_child originalPort translatedIp = Nokogiri::XML::Node.new 'TranslatedIp', response translatedIp.content = edge_vapp_ip gatewayNatRule.add_child translatedIp translatedPort = Nokogiri::XML::Node.new 'TranslatedPort', response translatedPort.content = "any" gatewayNatRule.add_child translatedPort protocol = Nokogiri::XML::Node.new 'Protocol', response protocol.content = "any" gatewayNatRule.add_child protocol # FIXME: frapposelli/tsugliani we should be able to remove this # FIXME: test this against a vCloud Director 5.1.x installation #icmpSubType = Nokogiri::XML::Node.new 'IcmpSubType', response #icmpSubType.content = "any" #gatewayNatRule.add_child icmpSubType natRule2 = Nokogiri::XML::Node.new 'NatRule', response ruleType = Nokogiri::XML::Node.new 'RuleType', response ruleType.content = "SNAT" natRule2.add_child ruleType isEnabled = Nokogiri::XML::Node.new 'IsEnabled', response isEnabled.content = "true" natRule2.add_child isEnabled gatewayNatRule = Nokogiri::XML::Node.new 'GatewayNatRule', response natRule2.add_child gatewayNatRule interface = Nokogiri::XML::Node.new 'Interface', response interface["href"] = edge_network_id gatewayNatRule.add_child interface originalIp = Nokogiri::XML::Node.new 'OriginalIp', response originalIp.content = edge_vapp_ip gatewayNatRule.add_child originalIp translatedIp = Nokogiri::XML::Node.new 'TranslatedIp', response translatedIp.content = edge_gateway_ip gatewayNatRule.add_child translatedIp protocol = Nokogiri::XML::Node.new 'Protocol', response protocol.content = "any" gatewayNatRule.add_child protocol firewallRule1 = Nokogiri::XML::Node.new 'FirewallRule', response isEnabled = Nokogiri::XML::Node.new 'IsEnabled', response isEnabled.content = "true" firewallRule1.add_child isEnabled description = Nokogiri::XML::Node.new 'Description', response description.content = "Allow Vagrant Comms" firewallRule1.add_child description policy = Nokogiri::XML::Node.new 'Policy', response policy.content = "allow" firewallRule1.add_child policy protocols = Nokogiri::XML::Node.new 'Protocols', response firewallRule1.add_child protocols any = Nokogiri::XML::Node.new 'Any', response any.content = "true" protocols.add_child any destinationPortRange = Nokogiri::XML::Node.new 'DestinationPortRange', response destinationPortRange.content = "Any" firewallRule1.add_child destinationPortRange destinationIp = Nokogiri::XML::Node.new 'DestinationIp', response destinationIp.content = edge_gateway_ip firewallRule1.add_child destinationIp sourcePortRange = Nokogiri::XML::Node.new 'SourcePortRange', response sourcePortRange.content = "Any" firewallRule1.add_child sourcePortRange sourceIp = Nokogiri::XML::Node.new 'SourceIp', response sourceIp.content = "Any" firewallRule1.add_child sourceIp enableLogging = Nokogiri::XML::Node.new 'EnableLogging', response enableLogging.content = "false" firewallRule1.add_child enableLogging builder = Nokogiri::XML::Builder.new builder << interesting set_edge_rules = Nokogiri::XML(builder.to_xml) do |config| config.default_xml.noblanks end nat_rules = set_edge_rules.at_css("NatService") nat_rules << natRule1 nat_rules << natRule2 fw_rules = set_edge_rules.at_css("FirewallService") fw_rules << firewallRule1 xml1 = set_edge_rules.at_css "EdgeGatewayServiceConfiguration" xml1["xmlns"] = "http://www.vmware.com/vcloud/v1.5" params = { 'method' => :post, 'command' => "/admin/edgeGateway/#{edge_gateway_id}/action/configureServices" } response, headers = send_request(params, set_edge_rules.to_xml(:indent => 2), "application/vnd.vmware.admin.edgeGatewayServiceConfiguration+xml") task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#set_vapp_network_config(vappid, network_name, config = {}) ⇒ Object
Set vApp Network Config
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 1542 def set_vapp_network_config(vappid, network_name, config={}) builder = Nokogiri::XML::Builder.new do |xml| xml.NetworkConfigSection( "xmlns" => "http://www.vmware.com/vcloud/v1.5", "xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1") { xml['ovf'].Info "Network configuration" xml.NetworkConfig("networkName" => network_name) { xml.Configuration { xml.FenceMode(config[:fence_mode] || 'isolated') xml.RetainNetInfoAcrossDeployments(config[:retain_net] || false) xml.ParentNetwork("href" => config[:parent_network]) } } } end params = { 'method' => :put, 'command' => "/vApp/vapp-#{vappid}/networkConfigSection" } response, headers = send_request(params, builder.to_xml, "application/vnd.vmware.vcloud.networkConfigSection+xml") task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#set_vapp_port_forwarding_rules(vappid, network_name, config = {}) ⇒ Object
Set vApp port forwarding rules
-
vappid: id of the vapp to be modified
-
network_name: name of the vapp network to be modified
-
config: hash with network configuration specifications, must contain an array inside :nat_rules with the nat rules to be applied.
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 863 def set_vapp_port_forwarding_rules(vappid, network_name, config={}) builder = Nokogiri::XML::Builder.new do |xml| xml.NetworkConfigSection( "xmlns" => "http://www.vmware.com/vcloud/v1.5", "xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1") { xml['ovf'].Info "Network configuration" xml.NetworkConfig("networkName" => network_name) { xml.Configuration { xml.ParentNetwork("href" => "#{@api_url}/network/#{config[:parent_network]}") xml.FenceMode(config[:fence_mode] || 'isolated') xml.Features { xml.NatService { xml.IsEnabled "true" xml.NatType "portForwarding" xml.Policy(config[:nat_policy_type] || "allowTraffic") config[:nat_rules].each do |nat_rule| xml.NatRule { xml.VmRule { xml.ExternalPort nat_rule[:nat_external_port] xml.VAppScopedVmId nat_rule[:vapp_scoped_local_id] xml.VmNicId(nat_rule[:nat_vmnic_id] || "0") xml.InternalPort nat_rule[:nat_internal_port] xml.Protocol(nat_rule[:nat_protocol] || "TCP") } } end } } } } } end params = { 'method' => :put, 'command' => "/vApp/vapp-#{vappid}/networkConfigSection" } response, headers = send_request(params, builder.to_xml, "application/vnd.vmware.vcloud.networkConfigSection+xml") task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#set_vm_guest_customization(vmid, computer_name, config = {}) ⇒ Object
Set VM Guest Customization Config
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 1601 def set_vm_guest_customization(vmid, computer_name, config={}) builder = Nokogiri::XML::Builder.new do |xml| xml.GuestCustomizationSection( "xmlns" => "http://www.vmware.com/vcloud/v1.5", "xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1") { xml['ovf'].Info "VM Guest Customization configuration" xml.Enabled config[:enabled] if config[:enabled] xml.AdminPasswordEnabled config[:admin_passwd_enabled] if config[:admin_passwd_enabled] xml.AdminPassword config[:admin_passwd] if config[:admin_passwd] xml.ComputerName computer_name } end params = { 'method' => :put, 'command' => "/vApp/vm-#{vmid}/guestCustomizationSection" } response, headers = send_request(params, builder.to_xml, "application/vnd.vmware.vcloud.guestCustomizationSection+xml") task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#set_vm_network_config(vmid, network_name, config = {}) ⇒ Object
Set VM Network Config
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 1571 def set_vm_network_config(vmid, network_name, config={}) builder = Nokogiri::XML::Builder.new do |xml| xml.NetworkConnectionSection( "xmlns" => "http://www.vmware.com/vcloud/v1.5", "xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1") { xml['ovf'].Info "VM Network configuration" xml.PrimaryNetworkConnectionIndex(config[:primary_index] || 0) xml.NetworkConnection("network" => network_name, "needsCustomization" => true) { xml.NetworkConnectionIndex(config[:network_index] || 0) xml.IpAddress config[:ip] if config[:ip] xml.IsConnected(config[:is_connected] || true) xml.IpAddressAllocationMode config[:ip_allocation_mode] if config[:ip_allocation_mode] } } end params = { 'method' => :put, 'command' => "/vApp/vm-#{vmid}/networkConnectionSection" } response, headers = send_request(params, builder.to_xml, "application/vnd.vmware.vcloud.networkConnectionSection+xml") task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#suspend_vapp(vAppId) ⇒ Object
Suspend a given vapp
418 419 420 421 422 423 424 425 426 427 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 418 def suspend_vapp(vAppId) params = { 'method' => :post, 'command' => "/vApp/vapp-#{vAppId}/power/action/suspend" } response, headers = send_request(params) task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#suspend_vm(vmId) ⇒ Object
Suspend a given VM
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 513 def suspend_vm(vmId) builder = Nokogiri::XML::Builder.new do |xml| xml.UndeployVAppParams( "xmlns" => "http://www.vmware.com/vcloud/v1.5") { xml.UndeployPowerAction 'suspend' } end params = { 'method' => :post, 'command' => "/vApp/vm-#{vmId}/action/undeploy" } response, headers = send_request(params, builder.to_xml, "application/vnd.vmware.vcloud.undeployVAppParams+xml") task_id = headers["Location"].gsub("#{@api_url}/task/", "") task_id end |
#upload_ovf(vdcId, vappName, vappDescription, ovfFile, catalogId, uploadOptions = {}) ⇒ Object
Upload an OVF package
-
vdcId
-
vappName
-
vappDescription
-
ovfFile
-
catalogId
-
uploadOptions {}
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 1368 def upload_ovf(vdcId, vappName, vappDescription, ovfFile, catalogId, uploadOptions={}) # if send_manifest is not set, setting it true if uploadOptions[:send_manifest].nil? || uploadOptions[:send_manifest] uploadManifest = "true" else uploadManifest = "false" end builder = Nokogiri::XML::Builder.new do |xml| xml.UploadVAppTemplateParams( "xmlns" => "http://www.vmware.com/vcloud/v1.5", "xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1", "manifestRequired" => uploadManifest, "name" => vappName) { xml.Description vappDescription } end params = { 'method' => :post, 'command' => "/vdc/#{vdcId}/action/uploadVAppTemplate" } @logger.debug("Sending uploadVAppTemplate request...") response, headers = send_request( params, builder.to_xml, "application/vnd.vmware.vcloud.uploadVAppTemplateParams+xml" ) # Get vAppTemplate Link from location vAppTemplate = headers["Location"].gsub("#{@api_url}/vAppTemplate/vappTemplate-", "") @logger.debug("Getting vAppTemplate ID: #{vAppTemplate}") descriptorUpload = response.css("Files Link [rel='upload:default']").first[:href].gsub("#{@host_url}/transfer/", "") transferGUID = descriptorUpload.gsub("/descriptor.ovf", "") ovfFileBasename = File.basename(ovfFile, ".ovf") ovfDir = File.dirname(ovfFile) # Send OVF Descriptor @logger.debug("Sending OVF Descriptor...") uploadURL = "/transfer/#{descriptorUpload}" uploadFile = "#{ovfDir}/#{ovfFileBasename}.ovf" upload_file(uploadURL, uploadFile, vAppTemplate, uploadOptions) # Begin the catch for upload interruption begin params = { 'method' => :get, 'command' => "/vAppTemplate/vappTemplate-#{vAppTemplate}" } response, headers = send_request(params) task = response.css("VAppTemplate Task[operationName='vdcUploadOvfContents']").first task_id = task["href"].gsub("#{@api_url}/task/", "") # Loop to wait for the upload links to show up in the vAppTemplate we just created @logger.debug("Waiting for the upload links to show up in the vAppTemplate we just created.") while true response, headers = send_request(params) @logger.debug("Request...") break unless response.css("Files Link [rel='upload:default']").count == 1 sleep 1 end if uploadManifest == "true" uploadURL = "/transfer/#{transferGUID}/descriptor.mf" uploadFile = "#{ovfDir}/#{ovfFileBasename}.mf" upload_file(uploadURL, uploadFile, vAppTemplate, uploadOptions) end # Start uploading OVF VMDK files params = { 'method' => :get, 'command' => "/vAppTemplate/vappTemplate-#{vAppTemplate}" } response, headers = send_request(params) response.css("Files File [bytesTransferred='0'] Link [rel='upload:default']").each do |file| fileName = file[:href].gsub("#{@host_url}/transfer/#{transferGUID}/","") uploadFile = "#{ovfDir}/#{fileName}" uploadURL = "/transfer/#{transferGUID}/#{fileName}" upload_file(uploadURL, uploadFile, vAppTemplate, uploadOptions) end # Add item to the catalog catalogId builder = Nokogiri::XML::Builder.new do |xml| xml.CatalogItem( "xmlns" => "http://www.vmware.com/vcloud/v1.5", "type" => "application/vnd.vmware.vcloud.catalogItem+xml", "name" => vappName) { xml.Description vappDescription xml.Entity( "href" => "#{@api_url}/vAppTemplate/vappTemplate-#{vAppTemplate}" ) } end params = { 'method' => :post, 'command' => "/catalog/#{catalogId}/catalogItems" } response, headers = send_request(params, builder.to_xml, "application/vnd.vmware.vcloud.catalogItem+xml") task_id ###### rescue Exception => e puts "Exception detected: #{e.}." puts "Aborting task..." # Get vAppTemplate Task params = { 'method' => :get, 'command' => "/vAppTemplate/vappTemplate-#{vAppTemplate}" } response, headers = send_request(params) # Cancel Task cancelHook = response.css("Tasks Task Link [rel='task:cancel']").first[:href].gsub("#{@api_url}","") params = { 'method' => :post, 'command' => cancelHook } response, headers = send_request(params) raise end end |
#wait_task_completion(taskid) ⇒ Object
Poll a given task until completion
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 |
# File 'lib/vagrant-vcloud/driver/version_5_1.rb', line 1521 def wait_task_completion(taskid) task, status, errormsg, start_time, end_time, response = nil loop do task = get_task(taskid) @logger.debug("Evaluating taskid: #{taskid}, current status #{task[:status]}") break if task[:status] != 'running' sleep 5 end if task[:status] == 'error' @logger.debug("Task Errored out") errormsg = task[:response].css("Error").first @logger.debug("Task Error Message #{errormsg['majorErrorCode']} - #{errormsg['message']}") errormsg = "Error code #{errormsg['majorErrorCode']} - #{errormsg['message']}" end { :status => task[:status], :errormsg => errormsg, :start_time => task[:start_time], :end_time => task[:end_time] } end |