def create_host_vapi(context, host_name, datacenter_name): """ Adds a single Host to the vCenter inventory under the named Datacenter using vAPI. """ user = context.testbed.config['ESX_USER'] pwd = context.testbed.config['ESX_PASS'] # Get the host folder for the Datacenter1 using the folder query datacenter = context.testbed.entities['DATACENTER_IDS'][datacenter_name] folder_summaries = context.client.vcenter.Folder.list( Folder.FilterSpec(type=Folder.Type.HOST, datacenters=set([datacenter]))) folder = folder_summaries[0].folder create_spec = Host.CreateSpec( hostname=host_name, user_name=user, password=pwd, folder=folder, thumbprint_verification=Host.CreateSpec.ThumbprintVerification.NONE) host = context.client.vcenter.Host.create(create_spec) print("Created Host '{}' ({})".format(host, host_name)) return host
def ensure_state(self): dc_status = self.check_dc_state() if not dc_status: folder_summaries = self.dc_service.Folder.list(Folder.FilterSpec(type=Folder.Type.DATACENTER)) folder = folder_summaries[0].folder datacenter1 = self.dc_service.Datacenter.create( Datacenter.CreateSpec(name=self.datacenter_name, folder=folder)) self.module.exit_json(changed=True, datacenter_status="%s is created" % datacenter1) self.module.exit_json(changed=False, datacenter_status="%s already exists" % self.datacenter_name)
def get_folder_by_name(self, datacenter_name, folder_name): """ Returns the identifier of a folder with the mentioned names. """ datacenter = self.get_datacenter_by_name(datacenter_name) if not datacenter: return None filter_spec = Folder.FilterSpec(type=Folder.Type.VIRTUAL_MACHINE, names=set([folder_name]), datacenters=set([datacenter])) folder_summaries = self.api_client.vcenter.Folder.list(filter_spec) folder = folder_summaries[0].folder if len(folder_summaries) > 0 else None return folder
def setup_cluster_vapi2(context): """Create a cluster from the Folder managed object""" cluster1_name = context.testbed.config['CLUSTER1_NAME'] # Get the host folder for the Datacenter2 using the folder query datacenter_name = context.testbed.config['DATACENTER2_NAME'] datacenter = context.testbed.entities['DATACENTER_IDS'][datacenter_name] folder_summaries = context.client.vcenter.Folder.list( Folder.FilterSpec(type=Folder.Type.HOST, datacenters=set([datacenter]))) folder = folder_summaries[0].folder # Create a managed object from the folder identifier folder_mo = vim.Folder(folder, context.soap_stub) cluster_mo = folder_mo.CreateClusterEx(cluster1_name, vim.cluster.ConfigSpecEx()) print("Created Cluster '{}' ({})".format(cluster_mo._moId, cluster1_name)) context.testbed.entities['CLUSTER_IDS'] = {cluster1_name: cluster_mo._moId}
def detect_vm_folder(context, datacenter_name, folder_name): """Find vm folder based on datacenter and folder name""" datacenter = datacenter_helper.get_datacenter(context.client, datacenter_name) if not datacenter: print("Datacenter '{}' not found".format(datacenter_name)) return None folder_summaries = context.client.vcenter.Folder.list( Folder.FilterSpec(type=Folder.Type.VIRTUAL_MACHINE, names=set([folder_name]), datacenters=set([datacenter]))) if len(folder_summaries) > 0: folder = folder_summaries[0].folder print("Detected VM Folder '{}' as {}".format(folder_name, folder)) context.testbed.entities['VM_FOLDER_IDS'][folder_name] = folder return True else: print("VM Folder '{}' missing in Datacenter {}". format(folder_name, datacenter_name)) return False
def get_folder(client, datacenter_name, folder_name): """ Returns the identifier of a folder Note: The method assumes that there is only one folder and datacenter with the mentioned names. """ datacenter = datacenter_helper.get_datacenter(client, datacenter_name) if not datacenter: print("Datacenter '{}' not found".format(datacenter_name)) return None filter_spec = Folder.FilterSpec(type=Folder.Type.VIRTUAL_MACHINE, names=set([folder_name]), datacenters=set([datacenter])) folder_summaries = client.vcenter.Folder.list(filter_spec) if len(folder_summaries) > 0: folder = folder_summaries[0].folder print("Detected folder '{}' as {}".format(folder_name, folder)) return folder else: print("Folder '{}' not found".format(folder_name)) return None
def get_placement_spec_for_cluster(context): """ Cluster names are not guaranteed to be unique within a vCenter instance, so we qualify our search using the Datacenter. If Folders are used, the search must be qualified using Folders since a Cluster name is not guaranteed to be unique across different Folders within a Datacenter. """ # Select a Cluster meeting our requirements datacenter_name = context.testbed.config['DATACENTER2_NAME'] datacenter = context.testbed.entities['DATACENTER_IDS'][datacenter_name] cluster_name = context.testbed.config['CLUSTER1_NAME'] filter = Cluster.FilterSpec(names=set([cluster_name]), datacenters=set([datacenter])) cluster_summaries = context.client.vcenter.Cluster.list(filter=filter) if len(cluster_summaries) > 0: cluster = cluster_summaries[0].cluster print("Selecting Cluster '{}' ({})".format(cluster_name, cluster)) else: print("Cluster '{}' not found".format(cluster_name)) return None # Select a Folder meeting our requirements. # # Must be in the same Datacenter as the Cluster 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 Cluster that was chosen. # TODO No way to validate that Cluster is connected to Datastore 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, cluster=cluster, datastore=datastore) print("get_placement_spec_for_cluster: Result is '{}'".format( placement_spec)) return placement_spec
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 folder_list_datacenter_folder(context): return context.client.vcenter.Folder.list( Folder.FilterSpec(type=Folder.Type.DATACENTER))
GiB = 1024 * 1024 * 1024 GiBMemory = 1024 def pp(value): """ Utility method used to print the data nicely. """ output = cStringIO() PrettyPrinter(stream=output).pprint(value) return output.getvalue() filter_spec = Datacenter.FilterSpec(names=set(['Datacenter'])) datacenter_summaries = client.vcenter.Datacenter.list(filter_spec) datacenter = datacenter_summaries[0].datacenter filter_spec = Folder.FilterSpec(names=set(['VmFolder']) #, datacenters=set(['Datacenter']) ) folder_summaries = client.vcenter.Folder.list(filter_spec) folder = folder_summaries[0].folder print(folder) filter_spec = Host.FilterSpec(names=set(['esxi-1.prod.vmware.haf'])) host_summaries = client.vcenter.Host.list(filter_spec) host = host_summaries[0] print(host) filter_spec = Datastore.FilterSpec(names=set(['Kingston120G'])) datastore_summaries = client.vcenter.Datastore.list(filter_spec) datastore = datastore_summaries[0] print(datastore)
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))
def folder_list_datacenter_folder(context): folder_svc = Folder(context.stub_config) return folder_svc.list(Folder.FilterSpec(type=Folder.Type.DATACENTER))
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