diff --git a/django-openstack/django_openstack/api.py b/django-openstack/django_openstack/api.py index c4a20e154..55b2f919b 100644 --- a/django-openstack/django_openstack/api.py +++ b/django-openstack/django_openstack/api.py @@ -236,6 +236,11 @@ class User(APIResourceWrapper): _attrs = ['email', 'enabled', 'id', 'tenantId', 'name'] +class Role(APIResourceWrapper): + """Wrapper around user role""" + _attrs = ['id', 'name', 'description', 'service_id'] + + class SecurityGroup(APIResourceWrapper): """Simple wrapper around openstackx.extras.security_groups.SecurityGroup""" _attrs = ['id', 'name', 'description', 'tenant_id', 'rules'] diff --git a/django-openstack/django_openstack/syspanel/views/users.py b/django-openstack/django_openstack/syspanel/views/users.py index c13703397..868110ed1 100644 --- a/django-openstack/django_openstack/syspanel/views/users.py +++ b/django-openstack/django_openstack/syspanel/views/users.py @@ -198,13 +198,19 @@ def create(request): user['password'], user['tenant_id'], True) - api.account_api(request).role_refs.add_for_tenant_user( - user['tenant_id'], user['id'], - settings.OPENSTACK_KEYSTONE_DEFAULT_ROLE) - messages.success(request, '%s was successfully created.' % user['id']) + try: + api.role_add_for_tenant_user( + request, user['tenant_id'], user['id'], + settings.OPENSTACK_KEYSTONE_DEFAULT_ROLE) + except api_exceptions.ApiException, e: + LOG.exception('ApiException while assigning\ + role to new user: %s' % user['id']) + messages.error(request, 'Error assigning role to user: %s' + % e.message) + return redirect('syspanel_users') except api_exceptions.ApiException, e: diff --git a/django-openstack/django_openstack/tests/api_tests.py b/django-openstack/django_openstack/tests/api_tests.py index ceb30b3fb..c0985a97c 100644 --- a/django-openstack/django_openstack/tests/api_tests.py +++ b/django-openstack/django_openstack/tests/api_tests.py @@ -543,6 +543,30 @@ class AccountApiTests(test.TestCase): self.mox.VerifyAll() + def test_role_add_for_tenant_user(self): + account_api = self.stub_account_api() + + role = api.Role(APIResource.get_instance()) + role.id = TEST_RETURN + role.name = TEST_RETURN + + account_api.role_refs = self.mox.CreateMockAnything() + account_api.role_refs.add_for_tenant_user(TEST_TENANT_ID, + TEST_USERNAME, + TEST_RETURN).AndReturn(role) + api._get_role = self.mox.CreateMockAnything() + api._get_role(IsA(http.HttpRequest), IsA(str)).AndReturn(role) + + self.mox.ReplayAll() + ret_val = api.role_add_for_tenant_user(self.request, + TEST_TENANT_ID, + TEST_USERNAME, + TEST_RETURN) + self.assertEqual(ret_val, None) + + self.mox.VerifyAll() + + class AdminApiTests(test.TestCase): def stub_admin_api(self, count=1): self.mox.StubOutWithMock(api, 'admin_api')