def attach_port_group(self, vim_server_name, port_group_name): """Attach a portgroup to an external network. :param str vc_name: name of vc where portgroup is present. :param str pg_name: name of the portgroup to be attached to external network. return: object containing vmext:VMWExternalNetwork XML element that representing the external network. :rtype: lxml.objectify.ObjectifiedElement """ ext_net = self.get_resource() platform = Platform(self.client) if not vim_server_name or not port_group_name: raise InvalidParameterException( "Either vCenter Server name is none or portgroup name is none") vc_record = platform.get_vcenter(vim_server_name) vc_href = vc_record.get('href') pg_moref_types = \ platform.get_port_group_moref_types(vim_server_name, port_group_name) if hasattr(ext_net, '{' + NSMAP['vmext'] + '}VimPortGroupRef'): vim_port_group_refs = E_VMEXT.VimPortGroupRefs() vim_object_ref1 = self.__create_vimobj_ref(vc_href, pg_moref_types[0], pg_moref_types[1]) # Create a new VimObjectRef using vc href, portgroup moref and type # from existing VimPortGroupRef. Add the VimObjectRef to # VimPortGroupRefs and then delete VimPortGroupRef # from external network. vim_pg_ref = ext_net['{' + NSMAP['vmext'] + '}VimPortGroupRef'] vc2_href = vim_pg_ref.VimServerRef.get('href') vim_object_ref2 = self.__create_vimobj_ref( vc2_href, vim_pg_ref.MoRef.text, vim_pg_ref.VimObjectType.text) vim_port_group_refs.append(vim_object_ref1) vim_port_group_refs.append(vim_object_ref2) ext_net.remove(vim_pg_ref) ext_net.append(vim_port_group_refs) else: vim_port_group_refs = \ ext_net['{' + NSMAP['vmext'] + '}VimPortGroupRefs'] vim_object_ref1 = self.__create_vimobj_ref(vc_href, pg_moref_types[0], pg_moref_types[1]) vim_port_group_refs.append(vim_object_ref1) return self.client. \ put_linked_resource(ext_net, rel=RelationType.EDIT, media_type=EntityType. EXTERNAL_NETWORK.value, contents=ext_net)
def attach_port_group(self, vim_server_name, port_group_name): """Attach a portgroup to an external network. :param str vc_name: name of vc where portgroup is present. :param str pg_name: name of the portgroup to be attached to external network. return: object containing vmext:VMWExternalNetwork XML element that representing the external network. :rtype: lxml.objectify.ObjectifiedElement """ ext_net = self.get_resource() platform = Platform(self.client) if not vim_server_name or not port_group_name: raise InvalidParameterException( "Either vCenter Server name is none or portgroup name is none") vc_record = platform.get_vcenter(vim_server_name) vc_href = vc_record.get('href') pg_moref_types = \ platform.get_port_group_moref_types(vim_server_name, port_group_name) if hasattr(ext_net, '{' + NSMAP['vmext'] + '}VimPortGroupRef'): vim_port_group_refs = E_VMEXT.VimPortGroupRefs() vim_object_ref1 = self.__create_vimobj_ref( vc_href, pg_moref_types[0], pg_moref_types[1]) # Create a new VimObjectRef using vc href, portgroup moref and type # from existing VimPortGroupRef. Add the VimObjectRef to # VimPortGroupRefs and then delete VimPortGroupRef # from external network. vim_pg_ref = ext_net['{' + NSMAP['vmext'] + '}VimPortGroupRef'] vc2_href = vim_pg_ref.VimServerRef.get('href') vim_object_ref2 = self.__create_vimobj_ref( vc2_href, vim_pg_ref.MoRef.text, vim_pg_ref.VimObjectType.text) vim_port_group_refs.append(vim_object_ref1) vim_port_group_refs.append(vim_object_ref2) ext_net.remove(vim_pg_ref) ext_net.append(vim_port_group_refs) else: vim_port_group_refs = \ ext_net['{' + NSMAP['vmext'] + '}VimPortGroupRefs'] vim_object_ref1 = self.__create_vimobj_ref( vc_href, pg_moref_types[0], pg_moref_types[1]) vim_port_group_refs.append(vim_object_ref1) return self.client. \ put_linked_resource(ext_net, rel=RelationType.EDIT, media_type=EntityType. EXTERNAL_NETWORK.value, contents=ext_net)
def detach_port_group(self, vim_server_name, port_group_name): """Detach a portgroup from an external network. :param str vim_server_name: name of vim server where portgroup is present. :param str port_group_name: name of the portgroup to be detached from external network. return: object containing vmext:VMWExternalNetwork XML element that representing the external network. :rtype: lxml.objectify.ObjectifiedElement """ ext_net = self.get_resource() platform = Platform(self.client) if not vim_server_name or not port_group_name: raise InvalidParameterException( "Either vCenter Server name is none or portgroup name is none") vc_record = platform.get_vcenter(vim_server_name) vc_href = vc_record.get('href') if hasattr(ext_net, 'VimPortGroupRefs'): pg_moref_types = \ platform.get_port_group_moref_types(vim_server_name, port_group_name) else: raise \ InvalidParameterException("External network" " has only one port group") vim_port_group_refs = ext_net.VimPortGroupRefs vim_obj_refs = vim_port_group_refs.VimObjectRef for vim_obj_ref in vim_obj_refs: if vim_obj_ref.VimServerRef.get('href') == vc_href \ and vim_obj_ref.MoRef == pg_moref_types[0] \ and vim_obj_ref.VimObjectType == pg_moref_types[1]: vim_port_group_refs.remove(vim_obj_ref) return self.client. \ put_linked_resource(ext_net, rel=RelationType.EDIT, media_type=EntityType. EXTERNAL_NETWORK.value, contents=ext_net)
def detach_port_group(self, vim_server_name, port_group_name): """Detach a portgroup from an external network. :param str vim_server_name: name of vim server where portgroup is present. :param str port_group_name: name of the portgroup to be detached from external network. return: object containing vmext:VMWExternalNetwork XML element that representing the external network. :rtype: lxml.objectify.ObjectifiedElement """ ext_net = self.get_resource() platform = Platform(self.client) if not vim_server_name or not port_group_name: raise InvalidParameterException( "Either vCenter Server name is none or portgroup name is none") vc_record = platform.get_vcenter(vim_server_name) vc_href = vc_record.get('href') if hasattr(ext_net, 'VimPortGroupRefs'): pg_moref_types = \ platform.get_port_group_moref_types(vim_server_name, port_group_name) else: raise \ InvalidParameterException("External network" " has only one port group") vim_port_group_refs = ext_net.VimPortGroupRefs vim_obj_refs = vim_port_group_refs.VimObjectRef for vim_obj_ref in vim_obj_refs: if vim_obj_ref.VimServerRef.get('href') == vc_href \ and vim_obj_ref.MoRef == pg_moref_types[0] \ and vim_obj_ref.VimObjectType == pg_moref_types[1]: vim_port_group_refs.remove(vim_obj_ref) return self.client. \ put_linked_resource(ext_net, rel=RelationType.EDIT, media_type=EntityType. EXTERNAL_NETWORK.value, contents=ext_net)