def handle(self, request, data): try: LOG.info('Creating user with name "%s"' % data['name']) new_user = api.user_create(request, data['name'], data['email'], data['password'], data['tenant_id'], True) messages.success(request, _('User "%s" was successfully created.') % data['name']) if data['role_id']: try: api.add_tenant_user_role(request, data['tenant_id'], new_user.id, data['role_id']) except: exceptions.handle(request, _('Unable to add user' 'to primary project.')) return new_user except: exceptions.handle(request, _('Unable to create user.'))
def handle(self, request, data): try: LOG.info('Creating user with name "%s"' % data['name']) new_user = api.user_create(request, data['name'], data['email'], data['password'], data['tenant_id'], True) messages.success(request, _('User "%s" was successfully created.') % data['name']) try: default_role = api.keystone.get_default_role(request) if default_role: api.add_tenant_user_role(request, data['tenant_id'], new_user.id, default_role.id) except: exceptions.handle(request, _('Unable to add user to primary project.')) return shortcuts.redirect('horizon:syspanel:users:index') except: exceptions.handle(request, _('Unable to create user.')) return shortcuts.redirect('horizon:syspanel:users:index')
def handle(self, request, data): try: api.add_tenant_user_role(request, data["tenant_id"], data["user_id"], data["role_id"]) messages.success(request, _("Successfully added user to project.")) except: exceptions.handle(request, _("Unable to add user to project.")) return shortcuts.redirect("horizon:syspanel:projects:users", tenant_id=data["tenant_id"])
def test_create(self): user = self.users.get(id="1") role = self.roles.first() api.tenant_list(IgnoreArg(), admin=True).AndReturn(self.tenants.list()) api.user_create(IgnoreArg(), user.name, user.email, user.password, self.tenant.id, True).AndReturn(user) api.keystone.role_list(IgnoreArg()).AndReturn(self.roles.list()) api.keystone.get_default_role(IgnoreArg()).AndReturn(role) api.add_tenant_user_role(IgnoreArg(), self.tenant.id, user.id, role.id) self.mox.ReplayAll() formData = { 'method': 'CreateUserForm', 'name': user.name, 'email': user.email, 'password': user.password, 'tenant_id': self.tenant.id, 'role_id': self.roles.first().id, 'confirm_password': user.password } res = self.client.post(USER_CREATE_URL, formData) self.assertNoFormErrors(res) self.assertMessageCount(success=1)
def test_create_user(self): user = self.users.get(id="1") role = self.roles.first() self.mox.StubOutWithMock(api, 'user_create') self.mox.StubOutWithMock(api, 'tenant_list') self.mox.StubOutWithMock(api.keystone, 'get_default_role') self.mox.StubOutWithMock(api, 'add_tenant_user_role') api.tenant_list(IgnoreArg(), admin=True).AndReturn(self.tenants.list()) api.user_create(IgnoreArg(), user.name, user.email, user.password, self.tenant.id, True).AndReturn(user) api.keystone.get_default_role(IgnoreArg()).AndReturn(role) api.add_tenant_user_role(IgnoreArg(), self.tenant.id, user.id, role.id) self.mox.ReplayAll() formData = {'method': 'CreateUserForm', 'name': user.name, 'email': user.email, 'password': user.password, 'tenant_id': self.tenant.id, 'confirm_password': user.password} res = self.client.post(USER_CREATE_URL, formData) self.assertNoFormErrors(res) self.assertMessageCount(success=1)
def test_create(self): user = self.users.get(id="1") role = self.roles.first() api.tenant_list(IgnoreArg(), admin=True).AndReturn(self.tenants.list()) api.user_create(IgnoreArg(), user.name, user.email, user.password, self.tenant.id, True).AndReturn(user) api.keystone.role_list(IgnoreArg()).AndReturn(self.roles.list()) api.keystone.get_default_role(IgnoreArg()).AndReturn(role) api.add_tenant_user_role(IgnoreArg(), self.tenant.id, user.id, role.id) self.mox.ReplayAll() formData = { "method": "CreateUserForm", "name": user.name, "email": user.email, "password": user.password, "tenant_id": self.tenant.id, "role_id": self.roles.first().id, "confirm_password": user.password, } res = self.client.post(USER_CREATE_URL, formData) self.assertNoFormErrors(res) self.assertMessageCount(success=1)
def handle(self, request, data): # create the project try: desc = data['description'] self.object = api.keystone.tenant_create(request, tenant_name=data['name'], description=desc, enabled=data['enabled']) except: exceptions.handle(request, ignore=True) return False project_id = self.object.id # update project members users_to_add = 0 try: available_roles = api.keystone.role_list(request) # count how many users are to be added for role in available_roles: role_list = data["role_" + role.id] users_to_add += len(role_list) # add new users to project for role in available_roles: role_list = data["role_" + role.id] users_added = 0 for user in role_list: api.add_tenant_user_role(request, tenant_id=project_id, user_id=user, role_id=role.id) users_added += 1 users_to_add -= users_added except: exceptions.handle( request, _('Failed to add %s project members ' 'and set project quotas.' % users_to_add)) # update the project quota ifcb = data['injected_file_content_bytes'] try: api.nova.tenant_quota_update(request, project_id, metadata_items=data['metadata_items'], injected_file_content_bytes=ifcb, volumes=data['volumes'], gigabytes=data['gigabytes'], ram=data['ram'], floating_ips=data['floating_ips'], instances=data['instances'], injected_files=data['injected_files'], cores=data['cores']) except: exceptions.handle(request, _('Unable to set project quotas.')) return True
def handle(self, request, data): try: api.add_tenant_user_role(request, data['tenant_id'], data['user_id'], data['role_id']) messages.success(request, _('Successfully added user to project.')) except: exceptions.handle(request, _('Unable to add user to project.')) return shortcuts.redirect('horizon:syspanel:projects:users', tenant_id=data['tenant_id'])
def handle(self, request, data): # create the project try: desc = data['description'] self.object = api.keystone.tenant_create(request, tenant_name=data['name'], description=desc, enabled=data['enabled']) except: exceptions.handle(request, ignore=True) return False project_id = self.object.id # update project members users_to_add = 0 try: available_roles = api.keystone.role_list(request) # count how many users are to be added for role in available_roles: role_list = data["role_" + role.id] users_to_add += len(role_list) # add new users to project for role in available_roles: role_list = data["role_" + role.id] users_added = 0 for user in role_list: api.add_tenant_user_role(request, tenant_id=project_id, user_id=user, role_id=role.id) users_added += 1 users_to_add -= users_added except: exceptions.handle(request, _('Failed to add %s project members ' 'and set project quotas.' % users_to_add)) # update the project quota ifcb = data['injected_file_content_bytes'] try: api.nova.tenant_quota_update(request, project_id, metadata_items=data['metadata_items'], injected_file_content_bytes=ifcb, volumes=data['volumes'], gigabytes=data['gigabytes'], ram=data['ram'], floating_ips=data['floating_ips'], instances=data['instances'], injected_files=data['injected_files'], cores=data['cores']) except: exceptions.handle(request, _('Unable to set project quotas.')) return True
def handle(self, request, data): try: api.add_tenant_user_role(request, data['tenant_id'], data['user_id'], data['role_id']) messages.success(request, _('Successfully added user to project.')) return True except: exceptions.handle(request, _('Unable to add user to project.'))
def handle(self, request, data): try: api.add_tenant_user_role(request, data['tenant_id'], data['user_id'], data['role_id']) messages.success(request, _('Successfully added user to tenant.')) except: exceptions.handle(request, _('Unable to add user to tenant.')) return shortcuts.redirect('horizon:syspanel:projects:users', tenant_id=data['tenant_id'])
def test_add_project_user_update_error(self): project = self.tenants.first() quota = self.quotas.first() default_role = self.roles.first() users = self.users.list() roles = self.roles.list() # init api.tenant_quota_defaults(IsA(http.HttpRequest), self.tenant.id) \ .AndReturn(quota) api.get_default_role(IsA(http.HttpRequest)).AndReturn(default_role) api.keystone.user_list(IsA(http.HttpRequest)).AndReturn(users) api.keystone.role_list(IsA(http.HttpRequest)).AndReturn(roles) # contribute api.keystone.role_list(IsA(http.HttpRequest)).AndReturn(roles) # handle project_details = self._get_project_info(project) quota_data = self._get_quota_info(quota) api.keystone.tenant_create(IsA(http.HttpRequest), **project_details) \ .AndReturn(project) api.keystone.role_list(IsA(http.HttpRequest)).AndReturn(roles) workflow_data = {} for role in roles: if "role_" + role.id in workflow_data: ulist = workflow_data["role_" + role.id] for user in ulist: api.add_tenant_user_role(IsA(http.HttpRequest), tenant_id=self.tenant.id, user_id=user, role_id=role.id) \ .AndRaise(self.exceptions.keystone) break break api.nova.tenant_quota_update(IsA(http.HttpRequest), project.id, **quota_data) self.mox.ReplayAll() workflow_data.update(self._get_workflow_data(project, quota)) url = reverse('horizon:syspanel:projects:create') res = self.client.post(url, workflow_data) self.assertNoFormErrors(res) self.assertRedirectsNoFollow(res, INDEX_URL)
def handle(self, request, data): try: LOG.info('Creating user with name "%s"' % data['name']) new_user = api.user_create(request, data['name'], data['email'], data['password'], data['tenant_id'], True) messages.success( request, _('User "%s" was successfully created.') % data['name']) try: api.add_tenant_user_role(request, data['tenant_id'], new_user.id, data['role_id']) except: exceptions.handle(request, _('Unable to add user to primary project.')) return shortcuts.redirect('horizon:syspanel:users:index') except: exceptions.handle(request, _('Unable to create user.')) return shortcuts.redirect('horizon:syspanel:users:index')
def handle(self, request, data): try: LOG.info('Creating user with name "%s"' % data['name']) new_user = api.user_create(request, data['name'], data['email'], data['password'], data['tenant_id'], True) messages.success( request, _('User "%s" was successfully created.') % data['name']) if data['role_id']: try: api.add_tenant_user_role(request, data['tenant_id'], new_user.id, data['role_id']) except: exceptions.handle( request, _('Unable to add user' 'to primary project.')) return new_user except: exceptions.handle(request, _('Unable to create user.'))
def handle(self, request, data): # create the project try: desc = data['description'] self.object = api.keystone.tenant_create(request, tenant_name=data['name'], description=desc, enabled=data['enabled']) except: exceptions.handle(request, ignore=True) return False project_id = self.object.id # update project members users_to_add = 0 try: available_roles = api.keystone.role_list(request) # count how many users are to be added for role in available_roles: role_list = data["role_" + role.id] users_to_add += len(role_list) # add new users to project for role in available_roles: role_list = data["role_" + role.id] users_added = 0 for user in role_list: api.add_tenant_user_role(request, tenant_id=project_id, user_id=user, role_id=role.id) users_added += 1 users_to_add -= users_added # jt # Make sure admin is added to the project as a ResellerAdmin users = api.keystone.user_list(request) admin_id = [user.id for user in users if user.name == 'admin'][0] reseller_admin_role_id = [role.id for role in available_roles if role.name == 'ResellerAdmin'][0] api.add_tenant_user_role(request, tenant_id=project_id, user_id=admin_id, role_id=reseller_admin_role_id) except: exceptions.handle(request, _('Failed to add %s project members ' 'and set project quotas.' % users_to_add)) # update the project quota ifcb = data['injected_file_content_bytes'] try: api.nova.tenant_quota_update(request, project_id, metadata_items=data['metadata_items'], injected_file_content_bytes=ifcb, volumes=data['volumes'], gigabytes=data['gigabytes'], ram=data['ram'], floating_ips=data['floating_ips'], instances=data['instances'], injected_files=data['injected_files'], cores=data['cores']) # jt # Update the custom quotas if data['images'] != 5: api.set_image_quota(project_id, data['images']) if data['expiration'] != 'Information not available.': api.set_expiration_date(project_id, data['expiration']) if data['object_mb'] != 204800: api.set_object_mb_quota(project_id, data['object_mb']) except: exceptions.handle(request, _('Unable to set project quotas.')) return True
def handle(self, request, data): project_id = data['project_id'] # update project info try: api.tenant_update(request, tenant_id=project_id, tenant_name=data['name'], description=data['description'], enabled=data['enabled']) except: exceptions.handle(request, ignore=True) return False # update project members users_to_modify = 0 try: available_roles = api.keystone.role_list(request) project_members = api.keystone.user_list(request, tenant_id=project_id) users_to_modify = len(project_members) for user in project_members: current_roles = api.roles_for_user(self.request, user.id, project_id) for role in available_roles: role_list = data["role_" + role.id] if user.id in role_list: if role not in current_roles: # user role has changed api.add_tenant_user_role(request, tenant_id=project_id, user_id=user.id, role_id=role.id) else: # user role is unchanged current_roles.pop(current_roles.index(role)) # delete user's removed roles for to_delete in current_roles: api.remove_tenant_user_role(request, tenant_id=project_id, user_id=user.id, role_id=to_delete.id) users_to_modify -= 1 # add new roles to project for role in available_roles: # count how many users may be added for exception handling role_list = data["role_" + role.id] users_to_modify += len(role_list) for role in available_roles: role_list = data["role_" + role.id] users_added = 0 for user in role_list: if not filter(lambda x: user == x.id, project_members): api.add_tenant_user_role(request, tenant_id=project_id, user_id=user, role_id=role.id) users_added += 1 users_to_modify -= users_added except: exceptions.handle( request, _('Failed to modify %s project members ' 'and update project quotas.' % users_to_modify)) return True # update the project quota ifcb = data['injected_file_content_bytes'] try: api.tenant_quota_update(request, project_id, metadata_items=data['metadata_items'], injected_file_content_bytes=ifcb, volumes=data['volumes'], gigabytes=data['gigabytes'], ram=data['ram'], floating_ips=data['floating_ips'], instances=data['instances'], injected_files=data['injected_files'], cores=data['cores']) return True except: exceptions.handle( request, _('Modified project information and ' 'members, but unable to modify ' 'project quotas.')) return True
def test_update_project_member_update_error(self): project = self.tenants.first() quota = self.quotas.first() default_role = self.roles.first() users = self.users.list() roles = self.roles.list() current_roles = self.roles.list() # get/init api.tenant_get(IsA(http.HttpRequest), self.tenant.id, admin=True).AndReturn(project) api.tenant_quota_get(IsA(http.HttpRequest), self.tenant.id).AndReturn(quota) api.get_default_role(IsA(http.HttpRequest)).AndReturn(default_role) api.keystone.user_list(IsA(http.HttpRequest)).AndReturn(users) api.keystone.role_list(IsA(http.HttpRequest)).AndReturn(roles) workflow_data = {} for user in users: api.roles_for_user(IsA(http.HttpRequest), user.id, self.tenant.id).AndReturn(roles) role_ids = [role.id for role in roles] if role_ids: workflow_data.setdefault("role_" + role_ids[0], []).append(user.id) # update some fields project._info["name"] = "updated name" project._info["description"] = "updated description" quota.metadata_items = 444 quota.volumes = 444 updated_project = { "tenant_name": project._info["name"], "tenant_id": project.id, "description": project._info["description"], "enabled": project.enabled, } updated_quota = self._get_quota_info(quota) # contribute api.keystone.role_list(IsA(http.HttpRequest)).AndReturn(roles) # handle api.tenant_update(IsA(http.HttpRequest), **updated_project).AndReturn(project) api.keystone.role_list(IsA(http.HttpRequest)).AndReturn(roles) api.keystone.user_list(IsA(http.HttpRequest), tenant_id=self.tenant.id).AndReturn(users) for user in users: api.roles_for_user(IsA(http.HttpRequest), user.id, self.tenant.id).AndReturn(current_roles) for role in roles: if "role_" + role.id in workflow_data: if role not in current_roles: api.add_tenant_user_role( IsA(http.HttpRequest), tenant_id=self.tenant.id, user_id=user, role_id=role.id ) else: current_roles.pop(current_roles.index(role)) for to_delete in current_roles: api.remove_tenant_user_role( IsA(http.HttpRequest), tenant_id=self.tenant.id, user_id=user.id, role_id=to_delete.id ).AndRaise(self.exceptions.nova) break break self.mox.ReplayAll() # submit form data project_data = { "name": project._info["name"], "id": project.id, "description": project._info["description"], "enabled": project.enabled, } workflow_data.update(project_data) workflow_data.update(updated_quota) url = reverse("horizon:admin:projects:update", args=[self.tenant.id]) res = self.client.post(url, workflow_data) self.assertNoFormErrors(res) self.assertRedirectsNoFollow(res, INDEX_URL)
def handle(self, request, data): project_id = data['project_id'] # update project info try: api.tenant_update(request, tenant_id=project_id, tenant_name=data['name'], description=data['description'], enabled=data['enabled']) except: exceptions.handle(request, ignore=True) return False # update project members users_to_modify = 0 try: available_roles = api.keystone.role_list(request) project_members = api.keystone.user_list(request, tenant_id=project_id) users_to_modify = len(project_members) for user in project_members: current_roles = api.roles_for_user(self.request, user.id, project_id) for role in available_roles: role_list = data["role_" + role.id] if user.id in role_list: if role not in current_roles: # user role has changed api.add_tenant_user_role(request, tenant_id=project_id, user_id=user.id, role_id=role.id) else: # user role is unchanged current_roles.pop(current_roles.index(role)) # delete user's removed roles for to_delete in current_roles: api.remove_tenant_user_role(request, tenant_id=project_id, user_id=user.id, role_id=to_delete.id) users_to_modify -= 1 # add new roles to project for role in available_roles: # count how many users may be added for exception handling role_list = data["role_" + role.id] users_to_modify += len(role_list) for role in available_roles: role_list = data["role_" + role.id] users_added = 0 for user in role_list: if not filter(lambda x: user == x.id, project_members): api.add_tenant_user_role(request, tenant_id=project_id, user_id=user, role_id=role.id) users_added += 1 users_to_modify -= users_added except: exceptions.handle(request, _('Failed to modify %s project members ' 'and update project quotas.' % users_to_modify)) return True # update the project quota ifcb = data['injected_file_content_bytes'] try: api.tenant_quota_update(request, project_id, metadata_items=data['metadata_items'], injected_file_content_bytes=ifcb, volumes=data['volumes'], gigabytes=data['gigabytes'], ram=data['ram'], floating_ips=data['floating_ips'], instances=data['instances'], injected_files=data['injected_files'], cores=data['cores']) return True except: exceptions.handle(request, _('Modified project information and ' 'members, but unable to modify ' 'project quotas.')) return True
def test_update_project_member_update_error(self): project = self.tenants.first() quota = self.quotas.first() default_role = self.roles.first() users = self.users.list() roles = self.roles.list() current_roles = self.roles.list() # get/init api.tenant_get(IsA(http.HttpRequest), self.tenant.id, admin=True) \ .AndReturn(project) api.tenant_quota_get(IsA(http.HttpRequest), self.tenant.id) \ .AndReturn(quota) api.get_default_role(IsA(http.HttpRequest)).AndReturn(default_role) api.keystone.user_list(IsA(http.HttpRequest)).AndReturn(users) api.keystone.role_list(IsA(http.HttpRequest)).AndReturn(roles) workflow_data = {} for user in users: api.roles_for_user(IsA(http.HttpRequest), user.id, self.tenant.id).AndReturn(roles) role_ids = [role.id for role in roles] if role_ids: workflow_data.setdefault("role_" + role_ids[0], []) \ .append(user.id) # update some fields project._info["name"] = "updated name" project._info["description"] = "updated description" quota.metadata_items = 444 quota.volumes = 444 updated_project = { "tenant_name": project._info["name"], "tenant_id": project.id, "description": project._info["description"], "enabled": project.enabled } updated_quota = self._get_quota_info(quota) # contribute api.keystone.role_list(IsA(http.HttpRequest)).AndReturn(roles) # handle api.tenant_update(IsA(http.HttpRequest), **updated_project) \ .AndReturn(project) api.keystone.role_list(IsA(http.HttpRequest)).AndReturn(roles) api.keystone.user_list(IsA(http.HttpRequest), tenant_id=self.tenant.id).AndReturn(users) for user in users: api.roles_for_user(IsA(http.HttpRequest), user.id, self.tenant.id) \ .AndReturn(current_roles) for role in roles: if "role_" + role.id in workflow_data: if role not in current_roles: api.add_tenant_user_role(IsA(http.HttpRequest), tenant_id=self.tenant.id, user_id=user, role_id=role.id) else: current_roles.pop(current_roles.index(role)) for to_delete in current_roles: api.remove_tenant_user_role(IsA(http.HttpRequest), tenant_id=self.tenant.id, user_id=user.id, role_id=to_delete.id) \ .AndRaise(self.exceptions.nova) break break self.mox.ReplayAll() # submit form data project_data = { "name": project._info["name"], "id": project.id, "description": project._info["description"], "enabled": project.enabled } workflow_data.update(project_data) workflow_data.update(updated_quota) url = reverse('horizon:admin:projects:update', args=[self.tenant.id]) res = self.client.post(url, workflow_data) self.assertNoFormErrors(res) self.assertRedirectsNoFollow(res, INDEX_URL)