- result = (response.content).replace("\n"," ")
-
- vapp_template_tree = XmlElementTree.fromstring(response.content)
- children_element = [child for child in vapp_template_tree if 'Children' in child.tag][0]
- vm_element = [child for child in children_element if 'Vm' in child.tag][0]
- vm_name = vm_element.get('name')
- vm_id = vm_element.get('id')
- vm_href = vm_element.get('href')
-
- cpus = re.search('<rasd:Description>Number of Virtual CPUs</.*?>(\d+)</rasd:VirtualQuantity>',result).group(1)
- memory_mb = re.search('<rasd:Description>Memory Size</.*?>(\d+)</rasd:VirtualQuantity>',result).group(1)
- cores = re.search('<vmw:CoresPerSocket ovf:required.*?>(\d+)</vmw:CoresPerSocket>',result).group(1)
-
- headers['Content-Type'] = 'application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml'
- vdc_id = vdc.get('id').split(':')[-1]
- instantiate_vapp_href = "{}/api/vdc/{}/action/instantiateVAppTemplate".format(self.url,
- vdc_id)
- data = """<?xml version="1.0" encoding="UTF-8"?>
- <InstantiateVAppTemplateParams
- xmlns="http://www.vmware.com/vcloud/v1.5"
- name="{}"
- deploy="false"
- powerOn="false"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1">
- <Description>Vapp instantiation</Description>
- <InstantiationParams>
- <NetworkConfigSection>
- <ovf:Info>Configuration parameters for logical networks</ovf:Info>
- <NetworkConfig networkName="{}">
- <Configuration>
- <ParentNetwork href="{}" />
- <FenceMode>bridged</FenceMode>
- </Configuration>
- </NetworkConfig>
- </NetworkConfigSection>
- <LeaseSettingsSection
- type="application/vnd.vmware.vcloud.leaseSettingsSection+xml">
- <ovf:Info>Lease Settings</ovf:Info>
- <StorageLeaseInSeconds>172800</StorageLeaseInSeconds>
- <StorageLeaseExpiration>2014-04-25T08:08:16.438-07:00</StorageLeaseExpiration>
- </LeaseSettingsSection>
- </InstantiationParams>
- <Source href="{}"/>
- <SourcedItem>
- <Source href="{}" id="{}" name="{}"
- type="application/vnd.vmware.vcloud.vm+xml"/>
- <VmGeneralParams>
- <NeedsCustomization>false</NeedsCustomization>
- </VmGeneralParams>
- <InstantiationParams>
- <NetworkConnectionSection>
- <ovf:Info>Specifies the available VM network connections</ovf:Info>
- <NetworkConnection network="{}">
- <NetworkConnectionIndex>0</NetworkConnectionIndex>
- <IsConnected>true</IsConnected>
- <IpAddressAllocationMode>DHCP</IpAddressAllocationMode>
- </NetworkConnection>
- </NetworkConnectionSection><ovf:VirtualHardwareSection>
- <ovf:Info>Virtual hardware requirements</ovf:Info>
- <ovf:Item xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData"
- xmlns:vmw="http://www.vmware.com/schema/ovf">
- <rasd:AllocationUnits>hertz * 10^6</rasd:AllocationUnits>
- <rasd:Description>Number of Virtual CPUs</rasd:Description>
- <rasd:ElementName xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str">{cpu} virtual CPU(s)</rasd:ElementName>
- <rasd:InstanceID>4</rasd:InstanceID>
- <rasd:Reservation>0</rasd:Reservation>
- <rasd:ResourceType>3</rasd:ResourceType>
- <rasd:VirtualQuantity xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="int">{cpu}</rasd:VirtualQuantity>
- <rasd:Weight>0</rasd:Weight>
- <vmw:CoresPerSocket ovf:required="false">{core}</vmw:CoresPerSocket>
- </ovf:Item><ovf:Item xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">
- <rasd:AllocationUnits>byte * 2^20</rasd:AllocationUnits>
- <rasd:Description>Memory Size</rasd:Description>
- <rasd:ElementName xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str">{memory} MB of memory</rasd:ElementName>
- <rasd:InstanceID>5</rasd:InstanceID>
- <rasd:Reservation>0</rasd:Reservation>
- <rasd:ResourceType>4</rasd:ResourceType>
- <rasd:VirtualQuantity xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="int">{memory}</rasd:VirtualQuantity>
- <rasd:Weight>0</rasd:Weight>
- </ovf:Item>
- </ovf:VirtualHardwareSection>
- </InstantiationParams>
- </SourcedItem>
- <AllEULAsAccepted>false</AllEULAsAccepted>
- </InstantiateVAppTemplateParams>""".format(vmname_andid,
- primary_netname,
- primary_net_href,
- vapp_tempalte_href,
- vm_href,
- vm_id,
- vm_name,
- primary_netname,
- cpu=cpus,
- core=cores,
- memory=memory_mb)
-
- response = self.perform_request(req_type='POST',
- url=instantiate_vapp_href,
- headers=headers,
- data=data)
+ result = (response.text).replace("\n", " ")
+
+ vapp_template_tree = XmlElementTree.fromstring(response.text)
+ children_element = [
+ child for child in vapp_template_tree if "Children" in child.tag
+ ][0]
+ vm_element = [child for child in children_element if "Vm" in child.tag][
+ 0
+ ]
+ vm_name = vm_element.get("name")
+ vm_id = vm_element.get("id")
+ vm_href = vm_element.get("href")
+
+ # cpus = re.search('<rasd:Description>Number of Virtual CPUs</.*?>(\d+)</rasd:VirtualQuantity>',
+ # result).group(1)
+ memory_mb = re.search(
+ "<rasd:Description>Memory Size</.*?>(\d+)</rasd:VirtualQuantity>",
+ result,
+ ).group(1)
+ # cores = re.search('<vmw:CoresPerSocket ovf:required.*?>(\d+)</vmw:CoresPerSocket>', result).group(1)
+
+ headers[
+ "Content-Type"
+ ] = "application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml"
+ vdc_id = vdc.get("id").split(":")[-1]
+ instantiate_vapp_href = (
+ "{}/api/vdc/{}/action/instantiateVAppTemplate".format(
+ self.url, vdc_id
+ )
+ )
+
+ with open(
+ os.path.join(
+ os.path.dirname(__file__), "InstantiateVAppTemplateParams.xml"
+ ),
+ "r",
+ ) as f:
+ template = f.read()
+
+ data = template.format(
+ vmname_andid,
+ primary_netname,
+ primary_net_href,
+ vapp_tempalte_href,
+ vm_href,
+ vm_id,
+ vm_name,
+ primary_netname,
+ cpu=vm_cpus,
+ core=1,
+ memory=vm_memory,
+ )
+
+ response = self.perform_request(
+ req_type="POST",
+ url=instantiate_vapp_href,
+ headers=headers,
+ data=data,
+ )