def tenant_vm_replace(args): """ Handle tenant vm replace command """ error_info = auth_api._tenant_vm_replace(args.name, args.vm_list) if error_info: return operation_fail(error_info.msg) else: print("vm-group vm replace succeeded")
def ReplaceVMs(self, tenant, vms): if len(vms) == 0: logging.error("Replace VMs: the VM list is empty") raise vmodl.fault.InvalidArgument("VM list is empty") logging.info("Replacing VMs for tenant: %s", tenant.name) logging.info("Existing VMs: %s", tenant.vms) logging.info("New VMs: %s", vms) error_info = auth_api._tenant_vm_replace(tenant.name, vms) if error_info: logging.error("Failed to replace VMs for tenant: %s", error_info.msg) if error_info.code == ErrorCode.TENANT_NOT_EXIST: raise vim.fault.NotFound(msg=error_info.msg) elif error_info.code == ErrorCode.VM_NOT_FOUND: raise vmodl.fault.InvalidArgument(invalidProperty="vms") else: raise vim.fault.VcsFault(msg=error_info.msg) logging.info("Succssfully replaced VMs for tenant: %s", tenant.name)
def test_tenant_vm(self): """ Test AdminCLI command for tenant vm management """ logging.debug("test_tenant_vm") # create tenant1 without adding any vms and privilege error_info, tenant = auth_api._tenant_create( name=self.tenant1_name, default_datastore=auth_data_const.VM_DS, description="Test tenant1", vm_list=[], privileges=[]) self.assertEqual(None, error_info) error_info, vms = auth_api._tenant_vm_ls(self.tenant1_name) self.assertEqual(None, error_info) headers = vmdkops_admin.tenant_vm_ls_headers() rows = vmdkops_admin.generate_tenant_vm_ls_rows(vms) self.assertEqual(len(headers), TENANT_VM_LS_EXPECTED_COLUMN_COUNT) expected_output = [] actual_output = rows self.assertEqual(expected_output, actual_output) # Trying to create tenant with duplicate vm names error_info, tenant_dup = auth_api._tenant_create( name="tenant_add_dup_vms", default_datastore=auth_data_const.VM_DS, description="Tenant with duplicate VMs", vm_list=[self.vm1_name, self.vm1_name], privileges=[]) self.assertEqual(error_code.ErrorCode.VM_DUPLICATE, error_info.code) # tenant vm add to add two VMs to the tenant error_info = auth_api._tenant_vm_add( name=self.tenant1_name, vm_list=[self.vm1_name, self.vm2_name]) self.assertEqual(None, error_info) error_info, vms = auth_api._tenant_vm_ls(self.tenant1_name) self.assertEqual(None, error_info) # create tenant2 with vm1 a part of it. Should fail as VM can be a part # of just one tenant error_info, tenant2 = auth_api._tenant_create( name="Test_tenant2", default_datastore=auth_data_const.VM_DS, description="Test_tenant2", vm_list=[self.vm1_name], privileges=[]) self.assertEqual(error_code.ErrorCode.VM_IN_ANOTHER_TENANT, error_info.code) # create tenant3 and then try to add vm1 to it which is a part of # another tenant. Should fail as VM can be a part of just one tenant error_info, tenant3 = auth_api._tenant_create( name="Test_tenant3", default_datastore=auth_data_const.VM_DS, description="Test_tenant3", vm_list=[], privileges=[]) self.assertEqual(None, error_info) error_info = auth_api._tenant_vm_add(name=tenant3.name, vm_list=[self.vm1_name]) self.assertEqual(error_code.ErrorCode.VM_IN_ANOTHER_TENANT, error_info.code) # Replace should fail since vm1 is already a part of tenant1 error_info = auth_api._tenant_vm_replace(name=tenant3.name, vm_list=[self.vm1_name]) self.assertEqual(error_code.ErrorCode.VM_IN_ANOTHER_TENANT, error_info.code) # remove the tenant3 error_info = test_utils.cleanup_tenant(name=tenant3.name) self.assertEqual(None, error_info) # There are 2 columns for each row, the name of the columns are # "Uuid", "Name" # Sample output of a row: # [u'564d2b7d-187c-eaaf-60bc-e015b5cdc3eb', 'test_vm1'] rows = vmdkops_admin.generate_tenant_vm_ls_rows(vms) # Two vms are associated with this tenant self.assertEqual(len(rows), 2) expected_output = [self.vm1_name, self.vm2_name] actual_output = [rows[0][1], rows[1][1]] self.assertEqual(expected_output, actual_output) # tenant vm rm to remove one VM from the tenant error_info = auth_api._tenant_vm_rm(name=self.tenant1_name, vm_list=[self.vm2_name]) self.assertEqual(None, error_info) error_info, vms = auth_api._tenant_vm_ls(self.tenant1_name) self.assertEqual(None, error_info) rows = vmdkops_admin.generate_tenant_vm_ls_rows(vms) # tenant should only have one VM now self.assertEqual(len(rows), 1) expected_output = [self.vm1_name] actual_output = [rows[0][1]] self.assertEqual(expected_output, actual_output)
def test_tenant_vm(self): """ Test AdminCLI command for tenant vm management """ logging.debug("test_tenant_vm") # create tenant1 without adding any vms and privilege error_info, tenant = auth_api._tenant_create( name=self.tenant1_name, default_datastore=auth_data_const.VM_DS, description="Test tenant1", vm_list=[], privileges=[]) self.assertEqual(None, error_info) error_info, vms = auth_api._tenant_vm_ls(self.tenant1_name) self.assertEqual(None, error_info) headers = vmdkops_admin.tenant_vm_ls_headers() rows = vmdkops_admin.generate_tenant_vm_ls_rows(vms) self.assertEqual(len(headers), TENANT_VM_LS_EXPECTED_COLUMN_COUNT) expected_output = [] actual_output = rows self.assertEqual(expected_output, actual_output) # Trying to create tenant with duplicate vm names error_info, tenant_dup = auth_api._tenant_create( name="tenant_add_dup_vms", default_datastore=auth_data_const.VM_DS, description="Tenant with duplicate VMs", vm_list=[self.vm1_name, self.vm1_name], privileges=[]) self.assertEqual(error_code.ErrorCode.VM_DUPLICATE, error_info.code) # tenant vm add to add two VMs to the tenant error_info = auth_api._tenant_vm_add( name=self.tenant1_name, vm_list=[self.vm1_name, self.vm2_name]) self.assertEqual(None, error_info) error_info, vms = auth_api._tenant_vm_ls(self.tenant1_name) self.assertEqual(None, error_info) # create tenant2 with vm1 a part of it. Should fail as VM can be a part # of just one tenant error_info, tenant2 = auth_api._tenant_create( name="Test_tenant2", default_datastore=auth_data_const.VM_DS, description="Test_tenant2", vm_list=[self.vm1_name], privileges=[]) self.assertEqual(error_code.ErrorCode.VM_IN_ANOTHER_TENANT, error_info.code) # create tenant3 and then try to add vm1 to it which is a part of # another tenant. Should fail as VM can be a part of just one tenant error_info, tenant3 = auth_api._tenant_create( name="Test_tenant3", default_datastore=auth_data_const.VM_DS, description="Test_tenant3", vm_list=[], privileges=[]) self.assertEqual(None, error_info) error_info = auth_api._tenant_vm_add( name=tenant3.name, vm_list=[self.vm1_name]) self.assertEqual(error_code.ErrorCode.VM_IN_ANOTHER_TENANT, error_info.code) # Replace should fail since vm1 is already a part of tenant1 error_info = auth_api._tenant_vm_replace( name=tenant3.name, vm_list=[self.vm1_name]) self.assertEqual(error_code.ErrorCode.VM_IN_ANOTHER_TENANT, error_info.code) # remove the tenant3 error_info = test_utils.cleanup_tenant(name=tenant3.name) self.assertEqual(None, error_info) # There are 2 columns for each row, the name of the columns are # "Uuid", "Name" # Sample output of a row: # [u'564d2b7d-187c-eaaf-60bc-e015b5cdc3eb', 'test_vm1'] rows = vmdkops_admin.generate_tenant_vm_ls_rows(vms) # Two vms are associated with this tenant self.assertEqual(len(rows), 2) expected_output = [self.vm1_name, self.vm2_name] actual_output = [rows[0][1], rows[1][1]] self.assertEqual(expected_output, actual_output) # tenant vm rm to remove one VM from the tenant error_info = auth_api._tenant_vm_rm( name=self.tenant1_name, vm_list=[self.vm2_name]) self.assertEqual(None, error_info) error_info, vms = auth_api._tenant_vm_ls(self.tenant1_name) self.assertEqual(None, error_info) rows = vmdkops_admin.generate_tenant_vm_ls_rows(vms) # tenant should only have one VM now self.assertEqual(len(rows), 1) expected_output = [self.vm1_name] actual_output = [rows[0][1]] self.assertEqual(expected_output, actual_output)