blob: 635d0139b4addcce083fc17036e4b2a5429fb246 [file] [log] [blame]
ghanshyam17193062016-06-24 10:36:54 +09001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13from oslo_serialization import jsonutils as json
14from six.moves.urllib import parse as urllib
15
16from tempest.lib.common import rest_client
17
18
19class RolesClient(rest_client.RestClient):
20 api_version = "v2.0"
21
22 def create_role(self, **kwargs):
23 """Create a role.
24
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090025 For a full list of available parameters, please refer to the official
26 API reference:
27 http://developer.openstack.org/api-ref-identity-v2-ext.html#createRole
ghanshyam17193062016-06-24 10:36:54 +090028 """
29 post_body = json.dumps({'role': kwargs})
30 resp, body = self.post('OS-KSADM/roles', post_body)
31 self.expected_success(200, resp.status)
32 body = json.loads(body)
33 return rest_client.ResponseBody(resp, body)
34
35 def show_role(self, role_id_or_name):
36 """Get a role by its id or name.
37
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090038 For a full list of available parameters, please refer to the official
39 API reference:
40 http://developer.openstack.org/api-ref-identity-v2-ext.html#showRoleByID
41 OR
42 http://developer.openstack.org/api-ref-identity-v2-ext.html#showRoleByName
ghanshyam17193062016-06-24 10:36:54 +090043 """
44 resp, body = self.get('OS-KSADM/roles/%s' % role_id_or_name)
45 self.expected_success(200, resp.status)
46 body = json.loads(body)
47 return rest_client.ResponseBody(resp, body)
48
49 def list_roles(self, **params):
50 """Returns roles.
51
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090052 For a full list of available parameters, please refer to the official
53 API reference:
54 http://developer.openstack.org/api-ref-identity-v2-ext.html#listRoles
ghanshyam17193062016-06-24 10:36:54 +090055 """
56 url = 'OS-KSADM/roles'
57 if params:
58 url += '?%s' % urllib.urlencode(params)
59 resp, body = self.get(url)
60 self.expected_success(200, resp.status)
61 body = json.loads(body)
62 return rest_client.ResponseBody(resp, body)
63
64 def delete_role(self, role_id):
65 """Delete a role.
66
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090067 For a full list of available parameters, please refer to the official
68 API reference:
69 http://developer.openstack.org/api-ref-identity-v2-ext.html#deleteRole
ghanshyam17193062016-06-24 10:36:54 +090070 """
guo yunxian6cdf0562016-08-17 16:21:52 +080071 resp, body = self.delete('OS-KSADM/roles/%s' % role_id)
ghanshyam17193062016-06-24 10:36:54 +090072 self.expected_success(204, resp.status)
73 return rest_client.ResponseBody(resp, body)
74
75 def create_user_role_on_project(self, tenant_id, user_id, role_id):
76 """Add roles to a user on a tenant.
77
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090078 For a full list of available parameters, please refer to the official
79 API reference:
80 http://developer.openstack.org/api-ref-identity-v2-ext.html#grantRoleToUserOnTenant
ghanshyam17193062016-06-24 10:36:54 +090081 """
82 resp, body = self.put('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
83 (tenant_id, user_id, role_id), "")
84 self.expected_success(200, resp.status)
85 body = json.loads(body)
86 return rest_client.ResponseBody(resp, body)
87
88 def list_user_roles_on_project(self, tenant_id, user_id, **params):
89 """Returns a list of roles assigned to a user for a tenant."""
90 # TODO(gmann): Need to write API-ref link, Bug# 1592711
91 url = '/tenants/%s/users/%s/roles' % (tenant_id, user_id)
92 if params:
93 url += '?%s' % urllib.urlencode(params)
94 resp, body = self.get(url)
95 self.expected_success(200, resp.status)
96 body = json.loads(body)
97 return rest_client.ResponseBody(resp, body)
98
99 def delete_role_from_user_on_project(self, tenant_id, user_id, role_id):
100 """Removes a role assignment for a user on a tenant.
101
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +0900102 For a full list of available parameters, please refer to the official
103 API reference:
104 http://developer.openstack.org/api-ref-identity-v2-ext.html#revokeRoleFromUserOnTenant
ghanshyam17193062016-06-24 10:36:54 +0900105 """
106 resp, body = self.delete('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
107 (tenant_id, user_id, role_id))
108 self.expected_success(204, resp.status)
109 return rest_client.ResponseBody(resp, body)