def create(self, **kwargs): """ Create the virtual machine and update the vm object """ conn = connection() if not self._vm_object: data_store_name = kwargs['vcdriver_data_store'] data_store = get_vcenter_object_by_name(conn, vim.Datastore, data_store_name) capacity = float(data_store.summary.capacity) free_space = float(data_store.summary.freeSpace) free_percentage = 100 * free_space / capacity threshold = kwargs['vcdriver_data_store_threshold'] if free_percentage < float(threshold): raise NotEnoughDiskSpace(data_store_name, threshold, free_percentage) self._vm_object = wait_for_vcenter_task( get_vcenter_object_by_name( conn, vim.VirtualMachine, self.template).CloneVM_Task( folder=get_vcenter_object_by_name( conn, vim.Folder, kwargs['vcdriver_folder']), name=self.name, spec=vim.vm.CloneSpec(location=vim.vm.RelocateSpec( datastore=data_store, pool=get_vcenter_object_by_name( conn, vim.ResourcePool, kwargs['vcdriver_resource_pool'])), powerOn=True, template=False)), 'Create virtual machine "{}" from template "{}"'.format( self.name, self.template), self.timeout)
def refresh(self): """ Close session and create a new session """ if self._vm_object: close() # Refresh object with updated data (connection id changed) self._vm_object = get_vcenter_object_by_name( connection(), vim.VirtualMachine, self.name)
def test_get_vcenter_object_by_name(): apple = mock.MagicMock() orange_1 = mock.MagicMock() orange_2 = mock.MagicMock() apple.name = 'apple' orange_1.name = 'orange' orange_2.name = 'orange' view_mock = mock.MagicMock() view_mock.view = [apple, orange_1, orange_2] content_mock = mock.MagicMock() content_mock.viewManager.CreateContainerView = mock.MagicMock( return_value=view_mock) connection_mock = mock.MagicMock() connection_mock.RetrieveContent = mock.MagicMock(return_value=content_mock) assert get_vcenter_object_by_name(connection_mock, mock.MagicMock, 'apple') == apple with pytest.raises(NoObjectFound): get_vcenter_object_by_name(connection_mock, mock.MagicMock, 'grapes'), with pytest.raises(TooManyObjectsFound): get_vcenter_object_by_name(connection_mock, mock.MagicMock, 'orange')
def destroy_virtual_machines(folder_name, timeout=600): """ Destroy all the virtual machines in the folder with the given name :param folder_name: The folder name :param timeout: The timeout for vcenter tasks in seconds :return: A list with the destroyed vms """ folder = get_vcenter_object_by_name(connection(), vim.Folder, folder_name) destroyed_vms = [] for entity in folder.childEntity: if isinstance(entity, vim.VirtualMachine): vm = VirtualMachine(name=entity.summary.config.name, timeout=timeout) vm.__setattr__('_vm_object', entity) vm.destroy() destroyed_vms.append(vm) return destroyed_vms
def find(self): """ Find and update the vm object based on the name """ if not self._vm_object: self._vm_object = get_vcenter_object_by_name( connection(), vim.VirtualMachine, self.name)
def test_get_vcenter_object_by_name(): apple = mock.MagicMock() orange_1 = mock.MagicMock() orange_2 = mock.MagicMock() banana = mock.MagicMock() mango = object() # does not have name attr apple.name = 'apple' orange_1.name = 'orange' orange_2.name = 'orange' type(banana).name = mock.PropertyMock( side_effect=vmodl.fault.ManagedObjectNotFound) view_mock = mock.MagicMock() view_mock.view = [apple, orange_1, orange_2, banana, mango] content_mock = mock.MagicMock() content_mock.viewManager.CreateContainerView = mock.MagicMock( return_value=view_mock) connection_mock = mock.MagicMock() connection_mock.RetrieveContent = mock.MagicMock(return_value=content_mock) assert get_vcenter_object_by_name(connection_mock, mock.MagicMock, 'apple') == apple with pytest.raises(NoObjectFound): get_vcenter_object_by_name(connection_mock, mock.MagicMock, 'mango') with pytest.raises(NoObjectFound): get_vcenter_object_by_name(connection_mock, mock.MagicMock, 'grapes') with pytest.raises(NoObjectFound): get_vcenter_object_by_name(connection_mock, mock.MagicMock, 'banana') with pytest.raises(TooManyObjectsFound): get_vcenter_object_by_name(connection_mock, mock.MagicMock, 'orange') with pytest.raises(TooManyObjectsFound): get_vcenter_object_by_name(connection_mock, mock.MagicMock, 'orange')