def _execute(self): # List of visible content libraries visible_cls = self.client.local_library_service.list() if len(visible_cls) > 0: for visible_cl in visible_cls: get_visible_cl = self.client.local_library_service.get( visible_cl) print('Visible content library: {0} with id: {1}'.format( get_visible_cl.name, visible_cl)) # Find the datastore by the given datastore name using property collector self.datastore_id = get_datastore_id( service_manager=self.servicemanager, datastore_name=self.datastore_name) assert self.datastore_id is not None print('DataStore: {0} ID: {1}'.format(self.datastore_name, self.datastore_id)) # Build the storage backing for the library to be created storage_backings = [] storage_backing = StorageBacking(type=StorageBacking.Type.DATASTORE, datastore_id=self.datastore_id) storage_backings.append(storage_backing) # Build the specification for the library to be created create_spec = LibraryModel() create_spec.name = self.lib_name create_spec.description = "Local library backed by VC datastore" create_spec.type = create_spec.LibraryType.LOCAL create_spec.storage_backings = storage_backings # Create a local content library backed the VC datastore using vAPIs library_id = self.client.local_library_service.create( create_spec=create_spec, client_token=generate_random_uuid()) print('Local library created: ID: {0}'.format(library_id)) # Retrieve the local content library self.local_library = self.client.local_library_service.get(library_id) print('Retrieved library: ID: {0}'.format(self.local_library.id)) # Update the local content library update_spec = LibraryModel() update_spec.description = "new description" self.client.local_library_service.update(library_id, update_spec) print('Updated library description')
def create_storage_backings(self, service_manager, datastore_name): """ :param service_manager: :param datastore_name: name of the datastore providing storage :return: the storage backing array """ # Find the datastore by the given datastore name datastore_id = get_datastore_id(service_manager=service_manager, datastore_name=datastore_name) assert datastore_id is not None # If provided datastore is not of type vmfs, substitute the type # StorageBacking.Type.DATASTORE with StorageBacking.Type.OTHER # Build the specification for the library to be created storage_backings = [StorageBacking(type=StorageBacking.Type.DATASTORE, datastore_id=datastore_id)] return storage_backings
def _execute(self): # Get the identifiers of the resources used for deployment item_id = self.helper.get_item_id_by_name(self.item_name) assert item_id folder_id = get_folder(self.vsphere_client, self.datacenter_name, self.folder_name) assert folder_id resource_pool_id = get_resource_pool(self.vsphere_client, self.datacenter_name, self.resource_pool_name) assert resource_pool_id datastore_id = get_datastore_id(self.servicemanager, self.datastore_name) assert datastore_id # Build the deployment specification placement_spec = VmtxLibraryItem.DeployPlacementSpec( folder=folder_id, resource_pool=resource_pool_id) vm_home_storage_spec = VmtxLibraryItem.DeploySpecVmHomeStorage( datastore=datastore_id) disk_storage_spec = VmtxLibraryItem.DeploySpecDiskStorage( datastore=datastore_id) deploy_spec = VmtxLibraryItem.DeploySpec( name=self.vm_name, placement=placement_spec, vm_home_storage=vm_home_storage_spec, disk_storage=disk_storage_spec) # Deploy a virtual machine from the VM template item self.vm_id = self.client.vmtx_service.deploy(item_id, deploy_spec) self.vm = get_obj_by_moId(self.servicemanager.content, [vim.VirtualMachine], self.vm_id) print("Deployed VM '{0}' with ID: {1}".format(self.vm.name, self.vm_id)) # Print a summary of the deployed VM vm_summary = self.vm.summary.config print('Guest OS: {0}'.format(vm_summary.guestId)) print('{0} CPU(s)'.format(vm_summary.numCpu)) print('{0} MB memory'.format(vm_summary.memorySizeMB)) print('{0} disk(s)'.format(vm_summary.numVirtualDisks)) print('{0} network adapter(s)'.format(vm_summary.numEthernetCards))
def _execute(self): # Find the datastore by the given datastore name using property collector self.datastore_id = get_datastore_id(service_manager=self.servicemanager, datastore_name=self.datastore_name) assert self.datastore_id is not None print('DataStore: {0} ID: {1}'.format(self.datastore_name, self.datastore_id)) # Build the storage backing for the library to be created storage_backings = [] storage_backing = StorageBacking(type=StorageBacking.Type.DATASTORE, datastore_id=self.datastore_id) storage_backings.append(storage_backing) # Build the specification for the library to be created create_spec = LibraryModel() create_spec.name = self.lib_name create_spec.description = "Local library backed by VC datastore" create_spec.type = create_spec.LibraryType.LOCAL create_spec.storage_backings = storage_backings # Create a local content library backed the VC datastore using vAPIs library_id = self.client.local_library_service.create(create_spec=create_spec, client_token=generate_random_uuid()) print('Local library created: ID: {0}'.format(library_id)) self.local_library = self.client.local_library_service.get(library_id) # Create a new library item in the content library for uploading the files self.library_item_id = self.helper.create_library_item(library_id=self.local_library.id, item_name=self.lib_item_name, item_description='Sample simple VM template', item_type='ovf') assert self.library_item_id is not None assert self.client.library_item_service.get(self.library_item_id) is not None print('Library item created id: {0}'.format(self.library_item_id)) # Upload a VM template to the CL ovf_files_map = self.helper.get_ovf_files_map(ClsApiHelper.SIMPLE_OVF_RELATIVE_DIR) self.helper.upload_files(library_item_id=self.library_item_id, files_map=ovf_files_map) print('Uploaded ovf and vmdk files to library item {0}'.format(self.library_item_id)) # Download the library item from the CL temp_dir = tempfile.mkdtemp(prefix='simpleVmTemplate-') print('Downloading library item {0} to directory {1}'.format(self.library_item_id, temp_dir)) downloaded_files_map = self.helper.download_files(library_item_id=self.library_item_id, directory=temp_dir) assert len(downloaded_files_map) == len(ovf_files_map)
def _setup(self): self.datastore_name = self.args.datastorename self.cluster_name = self.args.clustername self.folder_name = self.args.foldername self.datacenter_name = self.args.datacentername self.servicemanager = self.get_service_manager() self.datastore_id = get_datastore_id( service_manager=self.servicemanager, datastore_name=self.datastore_name) self.client = ClsApiClient(self.servicemanager) self.helper = ClsApiHelper(self.client, self.skip_verification) session = get_unverified_session() if self.skip_verification else None self.vsphere_client = create_vsphere_client(server=self.server, username=self.username, password=self.password, session=session) self.folder_id = get_folder(self.vsphere_client, self.datacenter_name, self.folder_name) self.storage_backings = self.helper.create_storage_backings( self.servicemanager, self.datastore_name) cluster_obj = get_obj(self.servicemanager.content, [vim.ClusterComputeResource], self.cluster_name) self.resource_pool_id = cluster_obj.resourcePool._GetMoId()