def add_resource_pools_to_provider_vdc(self, pvdc_name, resource_pool_names): """Add Resource Pools to a Provider Virtual Datacenter. :param str pvdc_name: name of the Provider Virtual Datacenter. :param list resource_pool_names: list or resource pool names. :return: an object containing EntityType.TASK XML data which represents the asynchronous task that is adding Resource Pools to the PVDC. :rtype: lxml.objectify.ObjectifiedElement """ provider_vdc = self.get_res_by_name(ResourceType.PROVIDER_VDC, pvdc_name) pvdc_resource = self.client.get_resource(provider_vdc.get('href')) pvdc_ext_href = get_admin_extension_href(pvdc_resource.get('href')) pvdc_ext_resource = self.client.get_resource(pvdc_ext_href) vc_name = pvdc_ext_resource.VimServer.get('name') vc_href = pvdc_ext_resource.VimServer.get('href') rp_morefs = self.get_resource_pool_morefs(vc_name, vc_href, resource_pool_names) payload = E_VMEXT.UpdateResourcePoolSetParams() for rp_moref in rp_morefs: add_item = E_VMEXT.AddItem() add_item.append( E_VMEXT.VimServerRef(type=EntityType.VIRTUAL_CENTER.value, href=vc_href)) add_item.append(E_VMEXT.MoRef(rp_moref)) add_item.append(E_VMEXT.VimObjectType('RESOURCE_POOL')) payload.append(add_item) return self.client.post_linked_resource( resource=pvdc_ext_resource, rel=RelationType.UPDATE_RESOURCE_POOLS, media_type=EntityType.RES_POOL_SET_UPDATE_PARAMS.value, contents=payload)
def __create_vimobj_ref(self, vc_href, pg_moref, pg_type): """Creates the VimObjectRef.""" vim_object_ref = E_VMEXT.VimObjectRef() vim_object_ref.append(E_VMEXT.VimServerRef(href=vc_href)) vim_object_ref.append(E_VMEXT.MoRef(pg_moref)) vim_object_ref.append(E_VMEXT.VimObjectType(pg_type)) return vim_object_ref
def attach_resource_pools_to_provider_vdc(self, pvdc_name, resource_pool_names): """Attach Resource Pools to a Provider Virtual Datacenter. This function attaches one or more resource pools (RPs) to a Provider Virtual Datacenter (PVDC). Caveat: The current implementation of this function takes a list of RP "basenames" as input. A basename is the last element of a full pathname. For example, given a pathname /a/b/c, the basename of that pathname is "c". Since RP names are only required to have unique pathnames but not unique basenames, this function may not work correctly if there are non-unique RP basenames. Therefore, in order to use this function, all RP basenames must be unique. It is therefore up to the user of this function to be aware of this limitation and name their RPs appropriately. This limitation will be fixed in a future version of this function. :param str pvdc_name: name of the Provider Virtual Datacenter. :param list resource_pool_names: list or resource pool names. :return: an object containing EntityType.TASK XML data which represents the asynchronous task that is adding Resource Pools to the PVDC. :rtype: lxml.objectify.ObjectifiedElement """ provider_vdc = self.get_res_by_name(ResourceType.PROVIDER_VDC, pvdc_name) pvdc_resource = self.client.get_resource(provider_vdc.get('href')) pvdc_ext_href = get_admin_extension_href(pvdc_resource.get('href')) pvdc_ext_resource = self.client.get_resource(pvdc_ext_href) vc_name = pvdc_ext_resource.VimServer.get('name') vc_href = pvdc_ext_resource.VimServer.get('href') rp_morefs = self.get_resource_pool_morefs(vc_name, vc_href, resource_pool_names) payload = E_VMEXT.UpdateResourcePoolSetParams() for rp_moref in rp_morefs: add_item = E_VMEXT.AddItem() add_item.append( E_VMEXT.VimServerRef(type=EntityType.VIRTUAL_CENTER.value, href=vc_href)) add_item.append(E_VMEXT.MoRef(rp_moref)) add_item.append(E_VMEXT.VimObjectType('RESOURCE_POOL')) payload.append(add_item) return self.client.post_linked_resource( resource=pvdc_ext_resource, rel=RelationType.UPDATE_RESOURCE_POOLS, media_type=EntityType.RES_POOL_SET_UPDATE_PARAMS.value, contents=payload)
def pvdc_migrate_vms(self, pvdc_name, vms_to_migrate, src_resource_pool, target_resource_pool=None): """Migrate VMs to (an optionally) specified ResourcePool. :param str pvdc_name: name of the Provider Virtual Datacenter. :param list(str) vms_to_migrate: list of VMs to migrate. :param str src_resource_pool: source resource pool name. :param str target_resource_pool: target resource pool name (optional). This function migrates the specified VMs to (an optionally) specified target resource pool. If no target resource pool is specified, the system will automatically choose a target resource pool and migrate the VMs to it. If any of the vms_to_migrate are not found on the source resource pool, an exception will be thrown. :return: an object containing EntityType.TASK XML data which represents the async task that is migrating VMs. :rtype: lxml.objectify.ObjectifiedElement :raises: EntityNotFoundException: if source or target resource pool cannot be found, or if any of the vms_to_migrate are not found on the source resource pool. """ provider_vdc = self.get_ref_by_name(ResourceType.PROVIDER_VDC, pvdc_name) pvdc_ext_href = get_admin_extension_href(provider_vdc.get('href')) pvdc_ext_resource = self.client.get_resource(pvdc_ext_href) vc_name = pvdc_ext_resource.VimServer.get('name') vc_href = pvdc_ext_resource.VimServer.get('href') # find the src_respool_resource to get href and link to migrate VMs query_filter = 'vcName==%s' % urllib.parse.quote(vc_name) query = self.client.get_typed_query( ResourceType.RESOURCE_POOL.value, query_result_format=QueryResultFormat.RECORDS, qfilter=query_filter) res_pools_in_use = {} for res_pool in list(query.execute()): res_pools_in_use[res_pool.get('name')] = res_pool.get('moref') if src_resource_pool not in res_pools_in_use.keys(): raise EntityNotFoundException( 'source resource pool: \'%s\' not found' % src_resource_pool) else: src_rp_moref = res_pools_in_use[src_resource_pool] if target_resource_pool is not None: if target_resource_pool not in res_pools_in_use.keys(): raise EntityNotFoundException( 'target resource pool: \'%s\' not found' % target_resource_pool) else: target_rp_moref = res_pools_in_use[target_resource_pool] res_pools_in_pvdc = self.client.get_linked_resource( resource=pvdc_ext_resource, rel=RelationType.DOWN, media_type=EntityType.VMW_PROVIDER_VDC_RESOURCE_POOL_SET.value) pvdc_res_pools = {} if hasattr(res_pools_in_pvdc, '{' + NSMAP['vmext'] + '}VMWProviderVdcResourcePool'): for res_pool in res_pools_in_pvdc.VMWProviderVdcResourcePool: pvdc_res_pools[res_pool.ResourcePoolVimObjectRef.MoRef] = \ res_pool src_respool_resource = pvdc_res_pools[src_rp_moref] # create map of VM names to VM hrefs (in source respool) vms_in_respool = self.client.get_linked_resource( resource=src_respool_resource, rel=RelationType.RESOURCE_POOL_VM_LIST, media_type=None) vm_hrefs_in_respool = {} if hasattr(vms_in_respool, 'ResourcePoolVMRecord'): for vm in vms_in_respool.ResourcePoolVMRecord: vm_hrefs_in_respool[vm.get('name')] = vm.get('href') # check that vms_to_migrate are contained in vms in respool # and build the vms to migrate list payload = E_VMEXT.MigrateParams() for vm_to_migrate in vms_to_migrate: if vm_to_migrate in vm_hrefs_in_respool.keys(): payload.append( E_VMEXT.VmRef(href=vm_hrefs_in_respool[vm_to_migrate])) else: raise EntityNotFoundException( 'virtual machine \'%s\' not Found' % vm_to_migrate) # if target respool specified, add target RP <vc, moref> to payload if target_resource_pool is not None: res_pool_ref = E_VMEXT.ResourcePoolRef() vc_ref = E_VMEXT.VimServerRef(href=vc_href) moref = E_VMEXT.MoRef(target_rp_moref) obj_type = E_VMEXT.VimObjectType('RESOURCE_POOL') res_pool_ref.append(vc_ref) res_pool_ref.append(moref) res_pool_ref.append(obj_type) payload.append(res_pool_ref) # do the migrate, return task return self.client.post_linked_resource(resource=src_respool_resource, rel=RelationType.MIGRATE_VMS, media_type=None, contents=payload)
def create_provider_vdc(self, vim_server_name, resource_pool_names, storage_profiles, pvdc_name, is_enabled=None, description=None, highest_hw_vers=None, vxlan_network_pool=None, nsxt_manager_name=None): """Create a Provider Virtual Datacenter. :param str vim_server_name: vim_server_name (VC name). :param list resource_pool_names: list of resource_pool_names. :param list storage_profiles: (list): list of storageProfile names. :param str pvdc_name: name of PVDC to be created. :param bool is_enabled: flag, True to enable and False to disable. :param str description: description of pvdc. :param str highest_hw_vers: highest supported hw version number. :param str vxlan_network_pool: name of vxlan_network_pool. :param str nsxt_manager_name: name of nsx-t manager. :return: an object containing vmext:VMWProviderVdc XML element that represents the new provider VDC. :rtype: lxml.objectify.ObjectifiedElement """ vc_record = self.get_vcenter(vim_server_name) vc_href = vc_record.get('href') rp_morefs = self.get_resource_pool_morefs(vc_href, resource_pool_names) vmw_prov_vdc_params = E_VMEXT.VMWProviderVdcParams(name=pvdc_name) if description is not None: vmw_prov_vdc_params.append(E.Description(description)) resource_pool_refs = E_VMEXT.ResourcePoolRefs() for rp_moref in rp_morefs: vim_object_ref = E_VMEXT.VimObjectRef() vim_object_ref.append(E_VMEXT.VimServerRef(href=vc_href)) vim_object_ref.append(E_VMEXT.MoRef(rp_moref)) vim_object_ref.append(E_VMEXT.VimObjectType('RESOURCE_POOL')) resource_pool_refs.append(vim_object_ref) vmw_prov_vdc_params.append(resource_pool_refs) vmw_prov_vdc_params.append(E_VMEXT.VimServer(href=vc_href)) if vxlan_network_pool is not None: network_pool_rec = self.get_ref_by_name(ResourceType.NETWORK_POOL, vxlan_network_pool) vx_href = network_pool_rec.get('href') vmw_prov_vdc_params.append(E_VMEXT.VxlanNetworkPool(href=vx_href)) if nsxt_manager_name is not None: nsxt_manager_rec = self.get_ref_by_name(ResourceType.NSXT_MANAGER, nsxt_manager_name) nsxt_href = nsxt_manager_rec.get('href') vmw_prov_vdc_params.append( E_VMEXT.NsxTManagerReference(href=nsxt_href)) if highest_hw_vers is not None: vmw_prov_vdc_params.append( E_VMEXT.HighestSupportedHardwareVersion(highest_hw_vers)) if is_enabled is not None: vmw_prov_vdc_params.append(E_VMEXT.IsEnabled(is_enabled)) for storage_profile in storage_profiles: vmw_prov_vdc_params.append(E_VMEXT.StorageProfile(storage_profile)) random_username_suffix = uuid.uuid4().hex default_user = '******' + random_username_suffix[:8] default_pwd = 'PWD' + random_username_suffix[:8] vmw_prov_vdc_params.append(E_VMEXT.DefaultPassword(default_pwd)) vmw_prov_vdc_params.append(E_VMEXT.DefaultUsername(default_user)) return self.client.post_linked_resource( self.extension.get_resource(), rel=RelationType.ADD, media_type=EntityType.PROVIDER_VDC_PARAMS.value, contents=vmw_prov_vdc_params)
def create_external_network(self, name, vim_server_name, port_group_names, gateway_ip, netmask, ip_ranges, description=None, primary_dns_ip=None, secondary_dns_ip=None, dns_suffix=None): """Create an external network. :param str name: name of external network to be created. :param str vim_server_name: VIM server_name (VC name). :param list port_group_names: list of port group names. :param str gateway_ip: IP address of the gateway of the new network. :param str netmask: Netmask of the gateway. :param list ip_ranges: list of IP ranges used for static pool allocation in the network. For example, [192.168.1.2-192.168.1.49, 192.168.1.100-192.168.1.149]. :param str description: description of external network. :param str primary_dns_ip: IP address of primary DNS server. :param str secondary_dns_ip: IP address of secondary DNS Server. :param str dns_suffix: DNS suffix. :return: an object containing vmext:VMWExternalNetwork XML element that represents the new external network. :rtype: lxml.objectify.ObjectifiedElement """ vc_record = self.get_vcenter(vim_server_name) vc_href = vc_record.get('href') pg_morefs = self.get_port_group_morefs(port_group_names) vmw_external_network = E_VMEXT.VMWExternalNetwork(name=name) if description is not None: vmw_external_network.append(E.Description(description)) config = E.Configuration() ip_scopes = E.IpScopes() ip_scope = E.IpScope() ip_scope.append(E.IsInherited(False)) ip_scope.append(E.Gateway(gateway_ip)) ip_scope.append(E.Netmask(netmask)) if primary_dns_ip is not None: ip_scope.append(E.Dns1(primary_dns_ip)) if secondary_dns_ip is not None: ip_scope.append(E.Dns2(secondary_dns_ip)) if dns_suffix is not None: ip_scope.append(E.DnsSuffix(dns_suffix)) e_ip_ranges = E.IpRanges() for ip_range in ip_ranges: e_ip_range = E.IpRange() ip_range_token = ip_range.split('-') e_ip_range.append(E.StartAddress(ip_range_token[0])) e_ip_range.append(E.EndAddress(ip_range_token[1])) e_ip_ranges.append(e_ip_range) ip_scope.append(e_ip_ranges) ip_scopes.append(ip_scope) config.append(ip_scopes) config.append(E.FenceMode(FenceMode.ISOLATED.value)) vmw_external_network.append(config) vim_port_group_refs = E_VMEXT.VimPortGroupRefs() for pg_moref in pg_morefs: vim_object_ref = E_VMEXT.VimObjectRef() vim_object_ref.append(E_VMEXT.VimServerRef(href=vc_href)) vim_object_ref.append(E_VMEXT.MoRef(pg_moref[0])) vim_object_ref.append(E_VMEXT.VimObjectType(pg_moref[1])) vim_port_group_refs.append(vim_object_ref) vmw_external_network.append(vim_port_group_refs) return self.client.post_linked_resource( self.extension.get_resource(), rel=RelationType.ADD, media_type=EntityType.EXTERNAL_NETWORK.value, contents=vmw_external_network)
def create_provider_vdc(self, vim_server_name, resource_pool_names, storage_profiles, pvdc_name, is_enabled=None, description=None, highest_hw_vers=None, vxlan_network_pool=None): """Create a Provider Virtual Datacenter. :param: vim_server_name: (str): vim_server_name (VC name). :param: resource_pool_names: (list): list of resource_pool_names. :param: storage_profiles: (list): list of storageProfile namespace. :param: pvdc_name: (str): name of PVDC to be created. :param: is_enabled: (boolean): enable flag. :param: description: (str): description of pvdc. :param: highest_hw_vers: (str): highest supported hw vers number. :param: vxlan_network_pool: (str): name of vxlan_network_pool. :return: A :class:lxml.objectify.StringElement object describing the : new provider VDC. """ vc_record = self.get_vcenter(vim_server_name) vc_href = vc_record.get('href') rp_morefs = self.get_resource_pool_morefs(vim_server_name, vc_href, resource_pool_names) vmw_prov_vdc_params = E_VMEXT.VMWProviderVdcParams(name=pvdc_name) if description is not None: vmw_prov_vdc_params.append(E.Description(description)) resource_pool_refs = E_VMEXT.ResourcePoolRefs() for rp_moref in rp_morefs: vim_object_ref = E_VMEXT.VimObjectRef() vim_object_ref.append(E_VMEXT.VimServerRef(href=vc_href)) vim_object_ref.append(E_VMEXT.MoRef(rp_moref)) vim_object_ref.append(E_VMEXT.VimObjectType('RESOURCE_POOL')) resource_pool_refs.append(vim_object_ref) vmw_prov_vdc_params.append(resource_pool_refs) vmw_prov_vdc_params.append(E_VMEXT.VimServer(href=vc_href)) if vxlan_network_pool is not None: vxlan_network_pool_record = self.get_vxlan_network_pool( vxlan_network_pool) vx_href = vxlan_network_pool_record.get('href') vmw_prov_vdc_params.append(E_VMEXT.VxlanNetworkPool( href=vx_href)) if highest_hw_vers is not None: vmw_prov_vdc_params.append( E_VMEXT.HighestSupportedHardwareVersion(highest_hw_vers)) if is_enabled is not None: vmw_prov_vdc_params.append(E_VMEXT.IsEnabled(is_enabled)) for storage_profile in storage_profiles: vmw_prov_vdc_params.append(E_VMEXT.StorageProfile( storage_profile)) random_username_suffix = uuid.uuid4().hex default_user = '******' + random_username_suffix[:8] default_pwd = 'PWD' + random_username_suffix[:8] vmw_prov_vdc_params.append(E_VMEXT.DefaultPassword(default_pwd)) vmw_prov_vdc_params.append(E_VMEXT.DefaultUsername(default_user)) return self.client.post_linked_resource(self.extension.get_resource(), rel=RelationType.ADD, media_type=EntityType. PROVIDER_VDC_PARAMS.value, contents=vmw_prov_vdc_params)