def get_resource_pool_by_name(self, datacenter_name, resourcepool_name, cluster_name=None, host_name=None): """ Returns the identifier of a resource pool with the mentioned names. """ datacenter = self.get_datacenter_by_name(datacenter_name) if not datacenter: return None clusters = None if cluster_name: clusters = self.get_cluster_by_name(datacenter_name, cluster_name) if clusters: clusters = set([clusters]) hosts = None if host_name: hosts = self.get_host_by_name(datacenter_name, host_name) if hosts: hosts = set([hosts]) names = set([resourcepool_name]) if resourcepool_name else None filter_spec = ResourcePool.FilterSpec(datacenters=set([datacenter]), names=names, clusters=clusters) resource_pool_summaries = self.api_client.vcenter.ResourcePool.list( filter_spec) resource_pool = resource_pool_summaries[0].resource_pool if len( resource_pool_summaries) > 0 else None return resource_pool
def get_resource_pool(stub_config, datacenter_name): """ Returns the identifier of the first resourcepool in the datacenter """ datacenter = datacenter_helper.get_datacenter(stub_config, datacenter_name) if not datacenter: print("Datacenter '{}' not found".format(datacenter_name)) return None filter_spec = ResourcePool.FilterSpec(datacenters=set([datacenter])) resource_pool_svc = ResourcePool(stub_config) resource_pool_summaries = resource_pool_svc.list(filter_spec) if len(resource_pool_summaries) > 0: resource_pool = resource_pool_summaries[0].resource_pool print("Selecting ResourcePool '{}'".format(resource_pool)) return resource_pool else: print("ResourcePool not found in Datacenter '{}'".format( datacenter_name)) return None
def get_resource_pool_by_name(self, datacenter_name, resourcepool_name): """ Returns the identifier of a resource pool with the mentioned names. """ datacenter = self.get_datacenter_by_name(datacenter_name) if not datacenter: return None names = set([resourcepool_name]) if resourcepool_name else None filter_spec = ResourcePool.FilterSpec(datacenters=set([datacenter]), names=names) resource_pool_summaries = self.api_client.vcenter.ResourcePool.list( filter_spec) resource_pool = (resource_pool_summaries[0].resource_pool if len(resource_pool_summaries) > 0 else None) return resource_pool
def get_resource_pool(client, datacenter_name, resource_pool_name=None): """ Returns the identifier of the resource pool with the given name or the first resource pool in the datacenter if the name is not provided. """ datacenter = datacenter_helper.get_datacenter(client, datacenter_name) if not datacenter: print("Datacenter '{}' not found".format(datacenter_name)) return None names = set([resource_pool_name]) if resource_pool_name else None filter_spec = ResourcePool.FilterSpec(datacenters=set([datacenter]), names=names) resource_pool_summaries = client.vcenter.ResourcePool.list(filter_spec) if len(resource_pool_summaries) > 0: resource_pool = resource_pool_summaries[0].resource_pool print("Selecting ResourcePool '{}'".format(resource_pool)) return resource_pool else: print("ResourcePool not found in Datacenter '{}'".format( datacenter_name)) return None
def get_placement_spec_for_resource_pool(context): # Select a ResourcePool meeting our requirements datacenter_name = context.testbed.config['DATACENTER2_NAME'] datacenter = context.testbed.entities['DATACENTER_IDS'][datacenter_name] filter = ResourcePool.FilterSpec(datacenters=set([datacenter])) resource_pool_summaries = context.client.vcenter.ResourcePool.list( filter=filter) if len(resource_pool_summaries) > 0: resource_pool = resource_pool_summaries[0].resource_pool print('Selecting ResourcePool ({})'.format(resource_pool)) else: print("ResourcePool not found in Datacenter '{}'".format( datacenter_name)) return None # Select a Folder meeting our requirements. # # Must be in the same Datacenter as the ResourcePool that was chosen. datacenter_name = context.testbed.config['DATACENTER2_NAME'] datacenter = context.testbed.entities['DATACENTER_IDS'][datacenter_name] folder_name = context.testbed.config['VM_FOLDER2_NAME'] filter = Folder.FilterSpec(names=set([folder_name]), datacenters=set([datacenter])) folder_summaries = context.client.vcenter.Folder.list(filter=filter) if len(folder_summaries) > 0: folder = folder_summaries[0].folder print("Selecting Folder '{}' ({})".format(folder_name, folder)) else: print("Folder '{}' not found".format(folder_name)) return None # Select a Datastore meeting our requirements. # # Must be in the same Datacenter as the ResourcePool that was chosen. datacenter_name = context.testbed.config['DATACENTER2_NAME'] datacenter = context.testbed.entities['DATACENTER_IDS'][datacenter_name] # TODO Parameterize based on NFS or VMFS datastore_name = context.testbed.config['NFS_DATASTORE_NAME'] filter = Datastore.FilterSpec(names=set([datastore_name]), datacenters=set([datacenter])) datastore_summaries = context.client.vcenter.Datastore.list(filter=filter) if len(datastore_summaries) > 0: datastore = datastore_summaries[0].datastore print("Selecting Datastore '{}' ({})".format(datastore_name, datastore)) else: print("Datastore '{}' not found".format(datastore_name)) return None placement_spec = VM.PlacementSpec(folder=folder, resource_pool=resource_pool, datastore=datastore) print("get_placement_spec_for_resourcepool: Result is '{}'".format( placement_spec)) return placement_spec
def deploy_ovf_template(self): # Build the deployment target with resource pool ID and folder ID rp_filter_spec = ResourcePool.FilterSpec(names=set([self.resourcepoolname])) resource_pool_summaries = self.client.vcenter.ResourcePool.list(rp_filter_spec) if not resource_pool_summaries: raise ValueError("Resource pool with name '{}' not found". format(self.resourcepoolname)) resource_pool_id = resource_pool_summaries[0].resource_pool print('Resource pool ID: {}'.format(resource_pool_id)) folder_filter_spec = Folder.FilterSpec(names=set([self.foldername])) folder_summaries = self.client.vcenter.Folder.list(folder_filter_spec) if not folder_summaries: raise ValueError("Folder with name '{}' not found". format(self.foldername)) folder_id = folder_summaries[0].folder print('Folder ID: {}'.format(folder_id)) deployment_target = LibraryItem.DeploymentTarget( resource_pool_id=resource_pool_id, folder_id=folder_id ) # Find the library item find_spec = Item.FindSpec(name=self.lib_item_name) lib_item_ids = self.client.content.library.Item.find(find_spec) if not lib_item_ids: raise ValueError("Library item with name '{}' not found". format(self.lib_item_name)) lib_item_id = lib_item_ids[0] print('Library item ID: {}'.format(lib_item_id)) ovf_summary = self.client.vcenter.ovf.LibraryItem.filter( ovf_library_item_id=lib_item_id, target=deployment_target) print('Found an OVF template: {} to deploy.'.format(ovf_summary.name)) # Build the deployment spec deployment_spec = LibraryItem.ResourcePoolDeploymentSpec( name=self.vm_name, annotation=ovf_summary.annotation, accept_all_eula=True, network_mappings=None, storage_mappings=None, storage_provisioning=None, storage_profile_id=None, locale=None, flags=None, additional_parameters=None, default_datastore_id=None) # Deploy the ovf template result = self.client.vcenter.ovf.LibraryItem.deploy( lib_item_id, deployment_target, deployment_spec, client_token=generate_random_uuid()) # The type and ID of the target deployment is available in the deployment result. if result.succeeded: print('Deployment successful. VM Name: "{}", ID: "{}"' .format(self.vm_name, result.resource_id.id)) self.vm_id = result.resource_id.id error = result.error if error is not None: for warning in error.warnings: print('OVF warning: {}'.format(warning.message)) else: print('Deployment failed.') for error in result.error.errors: print('OVF error: {}'.format(error.message)) # Add an opaque network portgroup to the deployed VM if self.opaquenetworkname: filter = Network.FilterSpec( names=set([self.opaquenetworkname]), types=set([Network.Type.OPAQUE_NETWORK])) network_summaries = self.client.vcenter.Network.list(filter=filter) if not network_summaries: raise ValueError("Opaque network {} can not find".format( self.opaquenetworkname)) network = network_summaries[0].network nic_create_spec = Ethernet.CreateSpec( start_connected=True, mac_type=Ethernet.MacAddressType.GENERATED, backing=Ethernet.BackingSpec( type=Ethernet.BackingType.OPAQUE_NETWORK, network=network)) print('vm.hardware.Ethernet.create({}, {}) -> {}'.format( self.vm_id, nic_create_spec, network)) nic = self.client.vcenter.vm.hardware.Ethernet.create( self.vm_id, nic_create_spec) nic_info = self.client.vcenter.vm.hardware.Ethernet.get(self.vm_id, nic) print('vm.hardware.Ethernet.get({}, {}) -> {}'.format( self.vm_id, nic, pp(nic_info)))
def deploy_ovf_template(self): # Build the deployment target with resource pool ID filter_spec = ResourcePool.FilterSpec( names=set([self.resourcepoolname])) resource_pool_summaries = self.client.vcenter.ResourcePool.list( filter_spec) if not resource_pool_summaries: raise ValueError("Resource pool with name '{}' not found".format( self.resourcepoolname)) resource_pool_id = resource_pool_summaries[0].resource_pool print('Resource pool ID: {}'.format(resource_pool_id)) deployment_target = LibraryItem.DeploymentTarget( resource_pool_id=resource_pool_id) # Find the library item find_spec = Item.FindSpec(name=self.lib_item_name) lib_item_ids = self.client.content.library.Item.find(find_spec) if not lib_item_ids: raise ValueError("Library item with name '{}' not found".format( self.lib_item_name)) lib_item_id = lib_item_ids[0] print('Library item ID: {}'.format(lib_item_id)) ovf_summary = self.client.vcenter.ovf.LibraryItem.filter( ovf_library_item_id=lib_item_id, target=deployment_target) print('Found an OVF template: {} to deploy.'.format(ovf_summary.name)) # Build the deployment spec deployment_spec = LibraryItem.ResourcePoolDeploymentSpec( name=self.vm_name, annotation=ovf_summary.annotation, accept_all_eula=True, network_mappings=None, storage_mappings=None, storage_provisioning=None, storage_profile_id=None, locale=None, flags=None, additional_parameters=None, default_datastore_id=None) # Deploy the ovf template result = self.client.vcenter.ovf.LibraryItem.deploy( lib_item_id, deployment_target, deployment_spec, client_token=generate_random_uuid()) # The type and ID of the target deployment is available in the deployment result. if result.succeeded: print('Deployment successful. Result resource: {}, ID: {}'.format( result.resource_id.type, result.resource_id.id)) self.vm_id = result.resource_id.id error = result.error if error is not None: for warning in error.warnings: print('OVF warning: {}'.format(warning.message)) else: print('Deployment failed.') for error in result.error.errors: print('OVF error: {}'.format(error.message))
def _get_vm_filter_spec(self): vm_filter_spec = VM.FilterSpec() datacenters = self.get_option('datacenters') if datacenters: temp_dcs = [] for datacenter_name in datacenters: dc_filter_spec = Datacenter.FilterSpec( names=set([datacenter_name])) datacenter_summaries = self.pyv.rest_content.vcenter.Datacenter.list( dc_filter_spec) if len(datacenter_summaries) > 0: temp_dcs.append(datacenter_summaries[0].datacenter) else: self._handle_error(message="Unable to find datacenter %s" % datacenter_name) vm_filter_spec.datacenters = set(temp_dcs) clusters = self.get_option('clusters') if clusters: temp_clusters = [] for cluster_name in clusters: ccr_filter_spec = Cluster.FilterSpec(names=set([cluster_name])) cluster_summaries = self.pyv.rest_content.vcenter.Cluster.list( ccr_filter_spec) if len(cluster_summaries) > 0: temp_clusters.append(cluster_summaries[0].cluster) else: self._handle_error(message="Unable to find cluster %s" % cluster_name) vm_filter_spec.clusters = set(temp_clusters) folders = self.get_option('folders') if folders: temp_folders = [] for folder_name in folders: folder_filter_spec = Folder.FilterSpec( names=set([folder_name])) folder_summaries = self.pyv.rest_content.vcenter.Folder.list( folder_filter_spec) if len(folder_summaries) > 0: temp_folders.append(folder_summaries[0].folder) else: self._handle_error(message="Unable to find folder %s" % folder_name) vm_filter_spec.folders = set(temp_folders) esxi_hosts = self.get_option('esxi_hostsystems') if esxi_hosts: temp_hosts = [] for esxi_name in esxi_hosts: esxi_filter_spec = Host.FilterSpec(names=set([esxi_name])) esxi_summaries = self.pyv.rest_content.vcenter.Host.list( esxi_filter_spec) if len(esxi_summaries) > 0: temp_hosts.append(esxi_summaries[0].host) else: self._handle_error( message="Unable to find esxi hostsystem %s" % esxi_name) vm_filter_spec.folders = set(temp_hosts) resource_pools = self.get_option('resource_pools') if resource_pools: temp_rps = [] for rp_name in resource_pools: rp_filter_spec = ResourcePool.FilterSpec(names=set([rp_name])) rp_summaries = self.pyv.rest_content.vcenter.ResourcePool.list( rp_filter_spec) if len(rp_summaries) > 0: temp_rps.append(rp_summaries[0].resourcepool) else: self._handle_error( message="Unable to find resource pool %s" % rp_name) vm_filter_spec.folders = set(temp_rps) return vm_filter_spec