+
+ def reserve_memory_for_all_vms(self, vapp, memory_mb):
+ """
+ Method to reserve memory for all VMs
+ Args :
+ vapp - VApp
+ memory_mb - Memory in MB
+ Returns:
+ None
+ """
+
+ self.logger.info("Reserve memory for all VMs")
+ for vms in vapp.get_all_vms():
+ vm_id = vms.get('id').split(':')[-1]
+
+ url_rest_call = "{}/api/vApp/vm-{}/virtualHardwareSection/memory".format(self.url, vm_id)
+
+ headers = {'Accept':'application/*+xml;version=' + API_VERSION,
+ 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
+ headers['Content-Type'] = 'application/vnd.vmware.vcloud.rasdItem+xml'
+ response = self.perform_request(req_type='GET',
+ url=url_rest_call,
+ headers=headers)
+
+ if response.status_code == 403:
+ response = self.retry_rest('GET', url_rest_call)
+
+ if response.status_code != 200:
+ self.logger.error("REST call {} failed reason : {}"\
+ "status code : {}".format(url_rest_call,
+ response.content,
+ response.status_code))
+ raise vimconn.vimconnException("reserve_memory_for_all_vms : Failed to get "\
+ "memory")
+
+ bytexml = bytes(bytearray(response.content, encoding='utf-8'))
+ contentelem = lxmlElementTree.XML(bytexml)
+ namespaces = {prefix:uri for prefix,uri in contentelem.nsmap.iteritems() if prefix}
+ namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
+
+ # Find the reservation element in the response
+ memelem_list = contentelem.findall(".//rasd:Reservation", namespaces)
+ for memelem in memelem_list:
+ memelem.text = str(memory_mb)
+
+ newdata = lxmlElementTree.tostring(contentelem, pretty_print=True)
+
+ response = self.perform_request(req_type='PUT',
+ url=url_rest_call,
+ headers=headers,
+ data=newdata)
+
+ if response.status_code == 403:
+ add_headers = {'Content-Type': headers['Content-Type']}
+ response = self.retry_rest('PUT', url_rest_call, add_headers, newdata)
+
+ if response.status_code != 202:
+ self.logger.error("REST call {} failed reason : {}"\
+ "status code : {} ".format(url_rest_call,
+ response.content,
+ response.status_code))
+ raise vimconn.vimconnException("reserve_memory_for_all_vms : Failed to update "\
+ "virtual hardware memory section")
+ else:
+ mem_task = self.get_task_from_response(response.content)
+ result = self.client.get_task_monitor().wait_for_success(task=mem_task)
+ if result.get('status') == 'success':
+ self.logger.info("reserve_memory_for_all_vms(): VM {} succeeded "\
+ .format(vm_id))
+ else:
+ self.logger.error("reserve_memory_for_all_vms(): VM {} failed "\
+ .format(vm_id))
+
+ def connect_vapp_to_org_vdc_network(self, vapp_id, net_name):
+ """
+ Configure VApp network config with org vdc network
+ Args :
+ vapp - VApp
+ Returns:
+ None
+ """
+
+ self.logger.info("Connecting vapp {} to org vdc network {}".
+ format(vapp_id, net_name))
+
+ url_rest_call = "{}/api/vApp/vapp-{}/networkConfigSection/".format(self.url, vapp_id)
+
+ headers = {'Accept':'application/*+xml;version=' + API_VERSION,
+ 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
+ response = self.perform_request(req_type='GET',
+ url=url_rest_call,
+ headers=headers)
+
+ if response.status_code == 403:
+ response = self.retry_rest('GET', url_rest_call)
+
+ if response.status_code != 200:
+ self.logger.error("REST call {} failed reason : {}"\
+ "status code : {}".format(url_rest_call,
+ response.content,
+ response.status_code))
+ raise vimconn.vimconnException("connect_vapp_to_org_vdc_network : Failed to get "\
+ "network config section")
+
+ data = response.content
+ headers['Content-Type'] = 'application/vnd.vmware.vcloud.networkConfigSection+xml'
+ net_id = self.get_network_id_by_name(net_name)
+ if not net_id:
+ raise vimconn.vimconnException("connect_vapp_to_org_vdc_network : Failed to find "\
+ "existing network")
+
+ bytexml = bytes(bytearray(data, encoding='utf-8'))
+ newelem = lxmlElementTree.XML(bytexml)
+ namespaces = {prefix: uri for prefix, uri in newelem.nsmap.iteritems() if prefix}
+ namespaces["xmlns"] = "http://www.vmware.com/vcloud/v1.5"
+ nwcfglist = newelem.findall(".//xmlns:NetworkConfig", namespaces)
+
+ newstr = """<NetworkConfig networkName="{}">
+ <Configuration>
+ <ParentNetwork href="{}/api/network/{}"/>
+ <FenceMode>bridged</FenceMode>
+ </Configuration>
+ </NetworkConfig>
+ """.format(net_name, self.url, net_id)
+ newcfgelem = lxmlElementTree.fromstring(newstr)
+ if nwcfglist:
+ nwcfglist[0].addnext(newcfgelem)
+
+ newdata = lxmlElementTree.tostring(newelem, pretty_print=True)
+
+ response = self.perform_request(req_type='PUT',
+ url=url_rest_call,
+ headers=headers,
+ data=newdata)
+
+ if response.status_code == 403:
+ add_headers = {'Content-Type': headers['Content-Type']}
+ response = self.retry_rest('PUT', url_rest_call, add_headers, newdata)
+
+ if response.status_code != 202:
+ self.logger.error("REST call {} failed reason : {}"\
+ "status code : {} ".format(url_rest_call,
+ response.content,
+ response.status_code))
+ raise vimconn.vimconnException("connect_vapp_to_org_vdc_network : Failed to update "\
+ "network config section")
+ else:
+ vapp_task = self.get_task_from_response(response.content)
+ result = self.client.get_task_monitor().wait_for_success(task=vapp_task)
+ if result.get('status') == 'success':
+ self.logger.info("connect_vapp_to_org_vdc_network(): Vapp {} connected to "\
+ "network {}".format(vapp_id, net_name))
+ else:
+ self.logger.error("connect_vapp_to_org_vdc_network(): Vapp {} failed to "\
+ "connect to network {}".format(vapp_id, net_name))
+
+ def remove_primary_network_adapter_from_all_vms(self, vapp):
+ """
+ Method to remove network adapter type to vm
+ Args :
+ vapp - VApp
+ Returns:
+ None
+ """
+
+ self.logger.info("Removing network adapter from all VMs")
+ for vms in vapp.get_all_vms():
+ vm_id = vms.get('id').split(':')[-1]
+
+ url_rest_call = "{}/api/vApp/vm-{}/networkConnectionSection/".format(self.url, vm_id)
+
+ headers = {'Accept':'application/*+xml;version=' + API_VERSION,
+ 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
+ response = self.perform_request(req_type='GET',
+ url=url_rest_call,
+ headers=headers)
+
+ if response.status_code == 403:
+ response = self.retry_rest('GET', url_rest_call)
+
+ if response.status_code != 200:
+ self.logger.error("REST call {} failed reason : {}"\
+ "status code : {}".format(url_rest_call,
+ response.content,
+ response.status_code))
+ raise vimconn.vimconnException("remove_primary_network_adapter : Failed to get "\
+ "network connection section")
+
+ data = response.content
+ data = data.split('<Link rel="edit"')[0]
+
+ headers['Content-Type'] = 'application/vnd.vmware.vcloud.networkConnectionSection+xml'
+
+ newdata = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+ <NetworkConnectionSection xmlns="http://www.vmware.com/vcloud/v1.5"
+ xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"
+ xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData"
+ xmlns:common="http://schemas.dmtf.org/wbem/wscim/1/common"
+ xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData"
+ xmlns:vmw="http://www.vmware.com/schema/ovf"
+ xmlns:ovfenv="http://schemas.dmtf.org/ovf/environment/1"
+ xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5"
+ xmlns:ns9="http://www.vmware.com/vcloud/versions"
+ href="{url}" type="application/vnd.vmware.vcloud.networkConnectionSection+xml" ovf:required="false">
+ <ovf:Info>Specifies the available VM network connections</ovf:Info>
+ <PrimaryNetworkConnectionIndex>0</PrimaryNetworkConnectionIndex>
+ <Link rel="edit" href="{url}" type="application/vnd.vmware.vcloud.networkConnectionSection+xml"/>
+ </NetworkConnectionSection>""".format(url=url_rest_call)
+ response = self.perform_request(req_type='PUT',
+ url=url_rest_call,
+ headers=headers,
+ data=newdata)
+
+ if response.status_code == 403:
+ add_headers = {'Content-Type': headers['Content-Type']}
+ response = self.retry_rest('PUT', url_rest_call, add_headers, newdata)
+
+ if response.status_code != 202:
+ self.logger.error("REST call {} failed reason : {}"\
+ "status code : {} ".format(url_rest_call,
+ response.content,
+ response.status_code))
+ raise vimconn.vimconnException("remove_primary_network_adapter : Failed to update "\
+ "network connection section")
+ else:
+ nic_task = self.get_task_from_response(response.content)
+ result = self.client.get_task_monitor().wait_for_success(task=nic_task)
+ if result.get('status') == 'success':
+ self.logger.info("remove_primary_network_adapter(): VM {} conneced to "\
+ "default NIC type".format(vm_id))
+ else:
+ self.logger.error("remove_primary_network_adapter(): VM {} failed to "\
+ "connect NIC type".format(vm_id))
+