def delete(self): if self.vm: if self.vm.powered_on(): self.power_off() vm_name = self.vm.name() # capture the vm_name task_destroy = self.vm.vm.Destroy_Task() # delete the vm Task().wait_for_task(task_destroy) # wait until deletion is done #Datastore().folder_delete(vm_name) # todo remove this line once confirmed there are no side effects (not neeeded any more) delete folder created by VM return task_destroy
def delete_path(self, item_path): content = self.sdk.content() datacenter = self.sdk.datacenter() try: task = content.fileManager.DeleteDatastoreFile_Task(item_path, datacenter) Task().wait_for_task(task) if task.info.state == "success": return True except Exception as error: print(f"[Error][delete] {error.msg}") # todo: add global error handler and use it here return False
def create(self): folder = self.sdk.datacenter_folder() resource_pool = self.sdk.resource_pool() datastore_path = '[' + self.data_store + '] ' #+ self.vm_name # add "self.vm_name" here (as seen in samples) was creating an extra folder and was leaving a folder behind vmx_file = self.sdk.file_info(vm_path_name=datastore_path) # todo :refactor these values below config = pyVmomi.vim.vm.ConfigSpec(name=self.vm_name, memoryMB=self.memory, numCPUs=1, files=vmx_file, guestId=self.guest_id, version='vmx-07', deviceChange=self.devices) task = folder.CreateVM_Task(config=config, pool=resource_pool) Task().wait_for_task(task) self.vm = VM(task.info.result) return self.vm
def execute_search_task(self, search_function, search_spec): self.add_query_details_to_search_spec(search_spec) task = search_function("[%s]" % self.name, search_spec) Task().wait_for_task(task) return task.info.result
def save_screnshot_to_datastore(self): task = self.vm.vm.CreateScreenshot_Task() Task().wait_for_task(task) self.ds_screenshot = task.info.result return self.ds_screenshot
def power_off(self): task = self.vm.vm.PowerOffVM_Task() return Task().wait_for_task(task)
def power_on(self): if not self.vm.powered_on(): task = self.vm.vm.PowerOnVM_Task() # todo refactor into task API return Task().wait_for_task(task)