def test_update_project_get(self): project = self.tenants.first() quota = self.quotas.first() 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) self.mox.ReplayAll() url = reverse('horizon:syspanel:projects:update', args=[self.tenant.id]) res = self.client.get(url) self.assertTemplateUsed(res, 'syspanel/projects/update.html') workflow = res.context['workflow'] self.assertEqual(res.context['workflow'].name, UpdateProject.name) step = workflow.get_step("update_info") self.assertEqual(step.action.initial['ram'], quota.ram) self.assertEqual(step.action.initial['injected_files'], quota.injected_files) self.assertEqual(step.action.initial['name'], project.name) self.assertEqual(step.action.initial['description'], project.description) self.assertQuerysetEqual(workflow.steps, ['<UpdateProjectInfo: update_info>', '<UpdateProjectQuota: update_quotas>'])
def test_update_project_tenant_update_error(self): project = self.tenants.first() quota = self.quotas.first() default_role = self.roles.first() users = self.users.list() 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).AndRaise(self.exceptions.keystone) 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 test_update_project_get_error(self): api.tenant_get(IsA(http.HttpRequest), self.tenant.id, admin=True).AndRaise(self.exceptions.nova) self.mox.ReplayAll() url = reverse("horizon:admin:projects:update", args=[self.tenant.id]) res = self.client.get(url) self.assertRedirectsNoFollow(res, INDEX_URL)
def test_update_project_get_error(self): api.tenant_get(IsA(http.HttpRequest), self.tenant.id, admin=True) \ .AndRaise(self.exceptions.nova) self.mox.ReplayAll() url = reverse('horizon:admin:projects:update', args=[self.tenant.id]) res = self.client.get(url) self.assertRedirectsNoFollow(res, INDEX_URL)
def test_update_project_get_error(self): project = self.tenants.first() api.tenant_get(IsA(http.HttpRequest), self.tenant.id, admin=True) \ .AndReturn(project) api.tenant_quota_get(IsA(http.HttpRequest), self.tenant.id) \ .AndRaise(self.exceptions.nova) self.mox.ReplayAll() url = reverse('horizon:syspanel:projects:update', args=[self.tenant.id]) res = self.client.get(url) self.assertRedirectsNoFollow(res, INDEX_URL)
def get_context_data(self, **kwargs): context = super(CreateUserView, self).get_context_data(**kwargs) context['tenant_id'] = self.kwargs['tenant_id'] context['tenant_name'] = api.tenant_get(self.request, self.kwargs['tenant_id'], admin=True).name return context
def get_object(self, *args, **kwargs): tenant_id = kwargs["tenant_id"] try: return api.tenant_get(self.request, tenant_id) except Exception as e: LOG.exception('Error fetching tenant with id "%s"' % tenant_id) messages.error(self.request, _("Unable to update tenant: %s") % e.message) raise http.Http404("Tenant with ID %s not found." % tenant_id)
def get_object(self, *args, **kwargs): tenant_id = kwargs['tenant_id'] try: return api.tenant_get(self.request, tenant_id) except Exception as e: LOG.exception('Error fetching tenant with id "%s"' % tenant_id) messages.error(request, _('Unable to update tenant: %s') % e.message) raise http.Http404("Tenant with ID %s not found." % tenant_id)
def test_update_project_quota_update_error(self): project = self.tenants.first() quota = self.quotas.first() # first set of calls for 'get' because the url takes an arg api.tenant_get(IsA(http.HttpRequest), project.id, admin=True) \ .AndReturn(project) api.tenant_quota_get(IsA(http.HttpRequest), project.id) \ .AndReturn(quota) # 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) api.tenant_update(IsA(http.HttpRequest), **updated_project) \ .AndReturn(project) api.tenant_quota_update(IsA(http.HttpRequest), project.id, **updated_quota) \ .AndRaise(self.exceptions.nova) self.mox.ReplayAll() # submit form data workflow_data = {"name": updated_project["tenant_name"], "id": project.id, "description": updated_project["description"], "enabled": project.enabled} workflow_data.update(updated_quota) url = reverse('horizon:syspanel:projects:update', args=[self.tenant.id]) res = self.client.post(url, workflow_data) self.assertNoFormErrors(res) self.assertRedirectsNoFollow(res, INDEX_URL)
def test_update_project_get(self): project = self.tenants.first() quota = self.quotas.first() default_role = self.roles.first() users = self.users.list() roles = self.roles.list() 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) for user in users: api.roles_for_user(IsA(http.HttpRequest), user.id, self.tenant.id).AndReturn(roles) self.mox.ReplayAll() url = reverse('horizon:syspanel:projects:update', args=[self.tenant.id]) res = self.client.get(url) self.assertTemplateUsed(res, 'syspanel/projects/update.html') workflow = res.context['workflow'] self.assertEqual(res.context['workflow'].name, UpdateProject.name) step = workflow.get_step("update_info") self.assertEqual(step.action.initial['ram'], quota.ram) self.assertEqual(step.action.initial['injected_files'], quota.injected_files) self.assertEqual(step.action.initial['name'], project.name) self.assertEqual(step.action.initial['description'], project.description) self.assertQuerysetEqual(workflow.steps, ['<UpdateProjectInfo: update_info>', '<UpdateProjectMembers: update_members>', '<UpdateProjectQuota: update_quotas>'])
def test_update_project_get(self): project = self.tenants.first() quota = self.quotas.first() default_role = self.roles.first() users = self.users.list() roles = self.roles.list() 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) for user in users: api.roles_for_user(IsA(http.HttpRequest), user.id, self.tenant.id).AndReturn(roles) self.mox.ReplayAll() url = reverse("horizon:admin:projects:update", args=[self.tenant.id]) res = self.client.get(url) self.assertTemplateUsed(res, "admin/projects/update.html") workflow = res.context["workflow"] self.assertEqual(res.context["workflow"].name, UpdateProject.name) step = workflow.get_step("update_info") self.assertEqual(step.action.initial["ram"], quota.ram) self.assertEqual(step.action.initial["injected_files"], quota.injected_files) self.assertEqual(step.action.initial["name"], project.name) self.assertEqual(step.action.initial["description"], project.description) self.assertQuerysetEqual( workflow.steps, [ "<UpdateProjectInfo: update_info>", "<UpdateProjectMembers: update_members>", "<UpdateProjectQuota: update_quotas>", ], )
def update(request, tenant_id): form, handled = UpdateTenant.maybe_handle(request) if handled: return handled if request.method == 'GET': try: tenant = api.tenant_get(request, tenant_id) form = UpdateTenant(initial={'id': tenant.id, 'name': tenant.name, 'description': tenant.description, 'enabled': tenant.enabled}) except api_exceptions.ApiException, e: LOG.exception('Error fetching tenant with id "%s"' % tenant_id) messages.error(request, _('Unable to update tenant: %s') % e.message) return shortcuts.redirect('horizon:syspanel:tenants:index')
def get_initial(self): initial = super(UpdateProjectView, self).get_initial() project_id = self.kwargs['tenant_id'] initial['project_id'] = project_id try: # get initial project info project_info = api.tenant_get(self.request, project_id, admin=True) for field in PROJECT_INFO_FIELDS: initial[field] = getattr(project_info, field, None) # get initial project quota quota_data = api.tenant_quota_get(self.request, project_id) for field in QUOTA_FIELDS: initial[field] = getattr(quota_data, field, None) except: exceptions.handle(self.request, _('Unable to retrieve project details.'), redirect=reverse(INDEX_URL)) return initial
def get_object(self, tenant_id): return api.tenant_get(self.request, tenant_id)
def test_update_project_post(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: ulist = workflow_data["role_" + role.id] 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 ) for role in roles: if "role_" + role.id in workflow_data: ulist = workflow_data["role_" + role.id] for user in ulist: if not filter(lambda x: user == x.id, users): api.add_tenant_user_role( IsA(http.HttpRequest), tenant_id=self.tenant.id, user_id=user, role_id=role.id ) api.tenant_quota_update(IsA(http.HttpRequest), project.id, **updated_quota) 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 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)