def cleanup_tenant(name): error_info, vms = auth_api._tenant_vm_ls(name) if error_info: return error_info; # remove associated vms, if any if vms: vm_names = [vm_name for (_, vm_name) in vms] auth_api._tenant_vm_rm(name=name, vm_list=vm_names) # remove the tenant return auth_api._tenant_rm(name=name, remove_volumes=True)
def test_tenant_vm(self): """ Test AdminCLI command for tenant vm management """ # create tenant1 without adding any vms and privilege error_info, tenant = auth_api._tenant_create( name=self.tenant1_name, 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) # 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) # 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 tenant_vm_rm(args): """ Handle tenant vm rm command """ error_info = auth_api._tenant_vm_rm(args.name, args.vm_list) if error_info: return operation_fail(error_info.msg) else: print("vm-group vm rm succeeded")
def RemoveVMs(self, tenant, vms): if len(vms) == 0: logging.error("Remove VMs: the VM list is empty") raise vmodl.fault.InvalidArgument("VM list is empty") logging.info("Removing VMs: %s from tenant: %s", vms, tenant.name) error_info = auth_api._tenant_vm_rm(tenant.name, vms) if error_info: logging.error("Failed to remove VMs from 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 or error_info.code == ErrorCode.VM_NOT_IN_TENANT: raise vmodl.fault.InvalidArgument(invalidProperty="vms") else: raise vim.fault.VcsFault(msg=error_info.msg) logging.info("Succssfully removed VMs: %s from tenant: %s", vms, 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(self): """ Test AdminCLI command for tenant management """ # create tenant1 vm_list = [self.vm1_name] # create tenant and set "default_datastore" to an invalide datastore_name # should fail since the "default_datastore" is not valid error_info, tenant = auth_api._tenant_create( name=self.tenant1_name, default_datastore=self.bad_datastore_name, description="Test tenant1", vm_list=vm_list, privileges=[]) self.assertNotEqual(None, error_info) # try to create the tenant and set "default_datastore" to "_ALL_DS" # should fail since "_ALL_DS" cannot be set as "default_datastore" error_info, tenant = auth_api._tenant_create( name=self.tenant1_name, default_datastore=self.bad_datastore_name, description="Test tenant1", vm_list=vm_list, privileges=[]) self.assertNotEqual(None, error_info) # try to create the tenant and set "default_datastore" to "_VM_DS" # should succeed error_info, tenant = auth_api._tenant_create( name=self.tenant1_name, default_datastore=auth_data_const.VM_DS, description="Test tenant1", vm_list=vm_list, privileges=[]) self.assertEqual(None, error_info) error_info, tenant_list = auth_api._tenant_ls() self.assertEqual(None, error_info) header = vmdkops_admin.tenant_ls_headers() _, rows = vmdkops_admin.generate_tenant_ls_rows(tenant_list) self.assertEqual(len(header), TENANT_LS_EXPECTED_COLUMN_COUNT) # Two tenants in the list, "_DEFAULT" and "test_tenant1" # rows[0] is for "_DEFAULT" tenant, and rows[1] is for "test_tenant1" self.assertEqual(len(rows), 2) # There are 5 columns for each row, the name of the columns are # "Uuid", "Name", "Description", "Default_datastore", "VM_list" # Sample output of rows[1]: # [u'9e1be0ce-3d58-40f6-a335-d6e267e34baa', u'test_tenant1', u'Test tenant1', '', 'test_vm1'] expected_output = [ self.tenant1_name, "Test tenant1", auth_data_const.VM_DS, generate_vm_name_str(vm_list) ] actual_output = [ convert_to_str(rows[1][1]), convert_to_str(rows[1][2]), convert_to_str(rows[1][3]), convert_to_str(rows[1][4]) ] self.assertEqual(expected_output, actual_output) # tenant update to update description and default_datastore # update default_datastore to self.datastore_name, which should succeed error_info = auth_api._tenant_update( name=self.tenant1_name, description="This is test tenant1", default_datastore=self.datastore_name) self.assertEqual(None, error_info) # update default_datastore to "_ALL_DS", which should fail error_info = auth_api._tenant_update( name=self.tenant1_name, default_datastore=auth_data_const.ALL_DS) self.assertNotEqual(None, error_info) # list the access privilege for tenant1 # now should have two access privileges # to datastore "_VM_DS" and self.datastore error_info, privileges = auth_api._tenant_access_ls(self.tenant1_name) self.assertEqual(None, error_info) _, rows = vmdkops_admin.generate_tenant_access_ls_rows( privileges, self.tenant1_name) self.assertEqual(len(rows), 2) if rows[0][0] == self.datastore_name: expected_output = [[self.datastore_name, 'True', 'Unset', 'Unset'], [ auth_data_const.VM_DS, 'True', 'Unset', 'Unset' ]] else: expected_output = [[ auth_data_const.VM_DS, 'True', 'Unset', 'Unset' ], [self.datastore_name, 'True', 'Unset', 'Unset']] actual_output = [[rows[0][0], rows[0][1], rows[0][2], rows[0][3]], [rows[1][0], rows[1][1], rows[1][2], rows[1][3]]] self.assertEqual(expected_output, actual_output) # remove access privilege to "_VM_DS" error_info = auth_api._tenant_access_rm( name=self.tenant1_name, datastore=auth_data_const.VM_DS) self.assertEqual(None, error_info) # now, should only have one access privilege error_info, privileges = auth_api._tenant_access_ls(self.tenant1_name) self.assertEqual(None, error_info) _, rows = vmdkops_admin.generate_tenant_access_ls_rows( privileges, self.tenant1_name) self.assertEqual(len(rows), 1) expected_output = [self.datastore_name, 'True', 'Unset', 'Unset'] actual_output = [rows[0][0], rows[0][1], rows[0][2], rows[0][3]] self.assertEqual(expected_output, actual_output) # update default vmgroup description error_info = auth_api._tenant_update( name=auth_data_const.DEFAULT_TENANT, description="This is the default vmgroup") self.assertEqual(None, error_info) error_info, tenant_list = auth_api._tenant_ls() self.assertEqual(None, error_info) header = vmdkops_admin.tenant_ls_headers() _, rows = vmdkops_admin.generate_tenant_ls_rows(tenant_list) expected_output = [ self.tenant1_name, "This is test tenant1", self.datastore_name, generate_vm_name_str(vm_list) ] actual_output = [ convert_to_str(rows[1][1]), convert_to_str(rows[1][2]), convert_to_str(rows[1][3]), convert_to_str(rows[1][4]) ] self.assertEqual(expected_output, actual_output) # tenant update to rename the tenant error_info = auth_api._tenant_update(name=self.tenant1_name, new_name=self.tenant1_new_name) self.assertEqual(None, error_info) # verify default vmgroup can't be renamed error_info = auth_api._tenant_update( name=auth_data_const.DEFAULT_TENANT, new_name=self.tenant1_new_name) self.assertNotEqual(None, error_info) error_info, tenant_list = auth_api._tenant_ls() self.assertEqual(None, error_info) header = vmdkops_admin.tenant_ls_headers() _, rows = vmdkops_admin.generate_tenant_ls_rows(tenant_list) expected_output = [ self.tenant1_new_name, "This is test tenant1", self.datastore_name, generate_vm_name_str(vm_list) ] actual_output = [ convert_to_str(rows[1][1]), convert_to_str(rows[1][2]), convert_to_str(rows[1][3]), convert_to_str(rows[1][4]) ] # tenant rm to remove the tenant error_info = auth_api._tenant_vm_rm(name=self.tenant1_new_name, vm_list=vm_list) self.assertEqual(None, error_info) error_info = test_utils.cleanup_tenant(self.tenant1_new_name) self.assertEqual(None, error_info) error_info, tenant_list = auth_api._tenant_ls() self.assertEqual(None, error_info) header = vmdkops_admin.tenant_ls_headers() _, rows = vmdkops_admin.generate_tenant_ls_rows(tenant_list) # right now, should only have 1 tenant, which is "_DEFAULT" tenant self.assertEqual(len(rows), 1)
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(self): """ Test AdminCLI command for tenant management """ # create tenant1 vm_list = [self.vm1_name] # create tenant and set "default_datastore" to an invalide datastore_name # should fail since the "default_datastore" is not valid error_info, tenant = auth_api._tenant_create( name=self.tenant1_name, default_datastore=self.bad_datastore_name, description="Test tenant1", vm_list=vm_list, privileges=[]) self.assertNotEqual(None, error_info) # try to create the tenant and set "default_datastore" to "_ALL_DS" # should fail since "_ALL_DS" cannot be set as "default_datastore" error_info, tenant = auth_api._tenant_create( name=self.tenant1_name, default_datastore=self.bad_datastore_name, description="Test tenant1", vm_list=vm_list, privileges=[]) self.assertNotEqual(None, error_info) # try to create the tenant and set "default_datastore" to "_VM_DS" # should succeed error_info, tenant = auth_api._tenant_create( name=self.tenant1_name, default_datastore=auth_data_const.VM_DS, description="Test tenant1", vm_list=vm_list, privileges=[]) self.assertEqual(None, error_info) error_info, tenant_list = auth_api._tenant_ls() self.assertEqual(None, error_info) header = vmdkops_admin.tenant_ls_headers() _, rows = vmdkops_admin.generate_tenant_ls_rows(tenant_list) self.assertEqual(len(header), TENANT_LS_EXPECTED_COLUMN_COUNT) # Two tenants in the list, "_DEFAULT" and "test_tenant1" # rows[0] is for "_DEFAULT" tenant, and rows[1] is for "test_tenant1" self.assertEqual(len(rows), 2) # There are 5 columns for each row, the name of the columns are # "Uuid", "Name", "Description", "Default_datastore", "VM_list" # Sample output of rows[1]: # [u'9e1be0ce-3d58-40f6-a335-d6e267e34baa', u'test_tenant1', u'Test tenant1', '', 'test_vm1'] expected_output = [self.tenant1_name, "Test tenant1", auth_data_const.VM_DS, generate_vm_name_str(vm_list)] actual_output = [convert_to_str(rows[1][1]), convert_to_str(rows[1][2]), convert_to_str(rows[1][3]), convert_to_str(rows[1][4]) ] self.assertEqual(expected_output, actual_output) # tenant update to update description and default_datastore # update default_datastore to self.datastore_name, which should succeed error_info = auth_api._tenant_update( name=self.tenant1_name, description="This is test tenant1", default_datastore=self.datastore_name) self.assertEqual(None, error_info) # update default_datastore to "_ALL_DS", which should fail error_info = auth_api._tenant_update( name=self.tenant1_name, default_datastore=auth_data_const.ALL_DS) self.assertNotEqual(None, error_info) # list the access privilege for tenant1 # now should have two access privileges # to datastore "_VM_DS" and self.datastore error_info, privileges = auth_api._tenant_access_ls(self.tenant1_name) self.assertEqual(None, error_info) _, rows = vmdkops_admin.generate_tenant_access_ls_rows(privileges, self.tenant1_name) self.assertEqual(len(rows), 2) if rows[0][0] == self.datastore_name: expected_output = [ [self.datastore_name, 'True', 'Unset', 'Unset'], [auth_data_const.VM_DS, 'True', 'Unset', 'Unset'] ] else: expected_output = [ [auth_data_const.VM_DS, 'True', 'Unset', 'Unset'], [self.datastore_name, 'True', 'Unset', 'Unset'] ] actual_output = [ [rows[0][0], rows[0][1], rows[0][2], rows[0][3]], [rows[1][0], rows[1][1], rows[1][2], rows[1][3]] ] self.assertEqual(expected_output, actual_output) # remove access privilege to "_VM_DS" error_info = auth_api._tenant_access_rm(name=self.tenant1_name, datastore=auth_data_const.VM_DS) self.assertEqual(None, error_info) # now, should only have one access privilege error_info, privileges = auth_api._tenant_access_ls(self.tenant1_name) self.assertEqual(None, error_info) _, rows = vmdkops_admin.generate_tenant_access_ls_rows(privileges, self.tenant1_name) self.assertEqual(len(rows), 1) expected_output = [self.datastore_name, 'True', 'Unset', 'Unset'] actual_output = [rows[0][0], rows[0][1], rows[0][2], rows[0][3]] self.assertEqual(expected_output, actual_output) # update default vmgroup description error_info = auth_api._tenant_update(name=auth_data_const.DEFAULT_TENANT, description="This is the default vmgroup") self.assertEqual(None, error_info) error_info, tenant_list = auth_api._tenant_ls() self.assertEqual(None, error_info) header = vmdkops_admin.tenant_ls_headers() _, rows = vmdkops_admin.generate_tenant_ls_rows(tenant_list) expected_output = [self.tenant1_name, "This is test tenant1", self.datastore_name, generate_vm_name_str(vm_list)] actual_output = [convert_to_str(rows[1][1]), convert_to_str(rows[1][2]), convert_to_str(rows[1][3]), convert_to_str(rows[1][4]) ] self.assertEqual(expected_output, actual_output) # tenant update to rename the tenant error_info = auth_api._tenant_update( name=self.tenant1_name, new_name=self.tenant1_new_name) self.assertEqual(None, error_info) # verify default vmgroup can't be renamed error_info = auth_api._tenant_update(name=auth_data_const.DEFAULT_TENANT, new_name=self.tenant1_new_name) self.assertNotEqual(None, error_info) error_info, tenant_list = auth_api._tenant_ls() self.assertEqual(None, error_info) header = vmdkops_admin.tenant_ls_headers() _, rows = vmdkops_admin.generate_tenant_ls_rows(tenant_list) expected_output = [self.tenant1_new_name, "This is test tenant1", self.datastore_name, generate_vm_name_str(vm_list)] actual_output = [convert_to_str(rows[1][1]), convert_to_str(rows[1][2]), convert_to_str(rows[1][3]), convert_to_str(rows[1][4]) ] # tenant rm to remove the tenant error_info = auth_api._tenant_vm_rm(name=self.tenant1_new_name, vm_list=vm_list) self.assertEqual(None, error_info) error_info = test_utils.cleanup_tenant(self.tenant1_new_name) self.assertEqual(None, error_info) error_info, tenant_list = auth_api._tenant_ls() self.assertEqual(None, error_info) header = vmdkops_admin.tenant_ls_headers() _, rows = vmdkops_admin.generate_tenant_ls_rows(tenant_list) # right now, should only have 1 tenant, which is "_DEFAULT" tenant self.assertEqual(len(rows), 1)