def connect_to_network(self, network_name, network_href, fence_mode="bridged"): vApp_NetworkConfigSection = [ section for section in self.me.get_Section() if section.__class__.__name__ == "NetworkConfigSectionType" ][0] link = [ link for link in vApp_NetworkConfigSection.get_Link() if link.get_type() == "application/vnd.vmware.vcloud.networkConfigSection+xml" ][0] networkConfigSection = VAPP.create_networkConfigSection(network_name, network_href, fence_mode) for networkConfig in vApp_NetworkConfigSection.get_NetworkConfig(): if networkConfig.get_networkName() == network_name: task = TaskType() task.set_status("success") task.set_Progress("100") return task networkConfigSection.add_NetworkConfig(networkConfig) output = StringIO() networkConfigSection.export( output, 0, name_="NetworkConfigSection", namespacedef_='xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"', pretty_print=False, ) body = output.getvalue().replace('Info msgid=""', "ovf:Info").replace("/Info", "/ovf:Info").replace("vmw:", "") self.response = requests.put(link.get_href(), data=body, headers=self.headers, verify=self.verify) if self.response.status_code == requests.codes.accepted: return taskType.parseString(self.response.content, True)
def connect_to_network(self, network_name, network_href, fence_mode='bridged'): """ Connect the vApp to an existing virtual network in the VDC. :param network_name: (str): The name of the virtual network. :param network_href: (str): A uri that points to the network resource. :param fence_mode: (str, optional): :return: (TaskType) a :class:`pyvcloud.schema.vcd.v1_5.schemas.admin.vCloudEntities.TaskType` object that can be used to monitor the request. """ vApp_NetworkConfigSection = [section for section in self.me.get_Section() if section.__class__.__name__ == "NetworkConfigSectionType"][0] link = [link for link in vApp_NetworkConfigSection.get_Link() if link.get_type() == "application/vnd.vmware.vcloud.networkConfigSection+xml"][0] for networkConfig in vApp_NetworkConfigSection.get_NetworkConfig(): if networkConfig.get_networkName() == network_name: task = TaskType() task.set_status("success") task.set_Progress("100") return task networkConfigSection = VAPP.create_networkConfigSection(network_name, network_href, fence_mode, vApp_NetworkConfigSection) output = StringIO() networkConfigSection.export(output, 0, name_ = 'NetworkConfigSection', namespacedef_ = 'xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"', pretty_print = True) body = output.getvalue().\ replace('Info msgid=""', "ovf:Info").replace("Info", "ovf:Info").replace(":vmw", "").replace("vmw:","")\ .replace("RetainNetovf", "ovf").replace("ovf:InfoAcrossDeployments","RetainNetInfoAcrossDeployments") self.response = Http.put(link.get_href(), data=body, headers=self.headers, verify=self.verify, logger=self.logger) if self.response.status_code == requests.codes.accepted: return taskType.parseString(self.response.content, True)
def disconnect_vms(self, network_name=None): """ Disconnect the vm from the vapp network. :param network_name: (string): The name of the vApp network. If None, then disconnect from all the networks. :return: (bool): True if the user was vApp was successfully deployed, False otherwise. """ children = self.me.get_Children() if children: vms = children.get_Vm() for vm in vms: Log.debug(self.logger, "child VM name=%s" % vm.get_name()) networkConnectionSection = [section for section in vm.get_Section() if isinstance(section, NetworkConnectionSectionType)][0] found = -1 if network_name is None: networkConnectionSection.set_NetworkConnection([]) found = 1 else: for index, networkConnection in enumerate(networkConnectionSection.get_NetworkConnection()): if networkConnection.get_network() == network_name: found = index break if found != -1: networkConnectionSection.NetworkConnection.pop(found) if found != -1: output = StringIO() networkConnectionSection.export(output, 0, name_ = 'NetworkConnectionSection', namespacedef_ = 'xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:vmw="http://www.vmware.com/vcloud/v1.5" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"', pretty_print = True) body=output.getvalue().replace("vmw:Info", "ovf:Info") self.response = Http.put(vm.get_href() + "/networkConnectionSection/", data=body, headers=self.headers, verify=self.verify, logger=self.logger) if self.response.status_code == requests.codes.accepted: return taskType.parseString(self.response.content, True) task = TaskType() task.set_status("success") task.set_Progress("100") return task
def disconnect_vms(self, network_name=None): """ Disconnect the vm from the vapp network. :param network_name: (string): The name of the vApp network. If None, then disconnect from all the networks. :return: (bool): True if the user was vApp was successfully deployed, False otherwise. """ children = self.me.get_Children() if children: vms = children.get_Vm() for vm in vms: Log.debug(self.logger, "child VM name=%s" % vm.get_name()) networkConnectionSection = [ section for section in vm.get_Section() if isinstance(section, NetworkConnectionSectionType) ][0] found = -1 if network_name is None: networkConnectionSection.set_NetworkConnection([]) found = 1 else: for index, networkConnection in enumerate( networkConnectionSection.get_NetworkConnection()): if networkConnection.get_network() == network_name: found = index break if found != -1: networkConnectionSection.NetworkConnection.pop(found) if found != -1: output = StringIO() networkConnectionSection.export( output, 0, name_='NetworkConnectionSection', namespacedef_= 'xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:vmw="http://www.vmware.com/vcloud/v1.5" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"', pretty_print=True) body = output.getvalue().replace("vmw:Info", "ovf:Info") self.response = Http.put(vm.get_href() + "/networkConnectionSection/", data=body, headers=self.headers, verify=self.verify, logger=self.logger) if self.response.status_code == requests.codes.accepted: return taskType.parseString(self.response.content, True) task = TaskType() task.set_status("success") task.set_Progress("100") return task
def connect_to_network(self, network_name, network_href, fence_mode='bridged'): vApp_NetworkConfigSection = [ section for section in self.me.get_Section() if section.__class__.__name__ == "NetworkConfigSectionType" ][0] link = [ link for link in vApp_NetworkConfigSection.get_Link() if link.get_type() == "application/vnd.vmware.vcloud.networkConfigSection+xml" ][0] networkConfigSection = VAPP.create_networkConfigSection( network_name, network_href, fence_mode) for networkConfig in vApp_NetworkConfigSection.get_NetworkConfig(): if networkConfig.get_networkName() == network_name: task = TaskType() task.set_status("success") task.set_Progress("100") return task networkConfigSection.add_NetworkConfig(networkConfig) output = StringIO() networkConfigSection.export( output, 0, name_='NetworkConfigSection', namespacedef_= 'xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"', pretty_print=True) body = output.getvalue().\ replace('Info msgid=""', "ovf:Info").replace("Info", "ovf:Info").replace(":vmw", "").replace("vmw:","")\ .replace("RetainNetovf", "ovf").replace("ovf:InfoAcrossDeployments","RetainNetInfoAcrossDeployments") self.response = requests.put(link.get_href(), data=body, headers=self.headers, verify=self.verify) if self.response.status_code == requests.codes.accepted: return taskType.parseString(self.response.content, True)