blob: aaa75f1a8040d65c5901e16cdd20228f74e325f5 [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
25 Available params: see http://developer.openstack.org/
26 api-ref-identity-v2-ext.html#createRole
27 """
28 post_body = json.dumps({'role': kwargs})
29 resp, body = self.post('OS-KSADM/roles', post_body)
30 self.expected_success(200, resp.status)
31 body = json.loads(body)
32 return rest_client.ResponseBody(resp, body)
33
34 def show_role(self, role_id_or_name):
35 """Get a role by its id or name.
36
37 Available params: see
38 http://developer.openstack.org/
39 api-ref-identity-v2-ext.html#showRoleByID
40 OR
41 http://developer.openstack.org/
42 api-ref-identity-v2-ext.html#showRoleByName
43 """
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
52 Available params: see http://developer.openstack.org/
53 api-ref-identity-v2-ext.html#listRoles
54 """
55 url = 'OS-KSADM/roles'
56 if params:
57 url += '?%s' % urllib.urlencode(params)
58 resp, body = self.get(url)
59 self.expected_success(200, resp.status)
60 body = json.loads(body)
61 return rest_client.ResponseBody(resp, body)
62
63 def delete_role(self, role_id):
64 """Delete a role.
65
66 Available params: see http://developer.openstack.org/
67 api-ref-identity-v2-ext.html#deleteRole
68 """
guo yunxian6cdf0562016-08-17 16:21:52 +080069 resp, body = self.delete('OS-KSADM/roles/%s' % role_id)
ghanshyam17193062016-06-24 10:36:54 +090070 self.expected_success(204, resp.status)
71 return rest_client.ResponseBody(resp, body)
72
73 def create_user_role_on_project(self, tenant_id, user_id, role_id):
74 """Add roles to a user on a tenant.
75
76 Available params: see
77 http://developer.openstack.org/
78 api-ref-identity-v2-ext.html#grantRoleToUserOnTenant
79 """
80 resp, body = self.put('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
81 (tenant_id, user_id, role_id), "")
82 self.expected_success(200, resp.status)
83 body = json.loads(body)
84 return rest_client.ResponseBody(resp, body)
85
86 def list_user_roles_on_project(self, tenant_id, user_id, **params):
87 """Returns a list of roles assigned to a user for a tenant."""
88 # TODO(gmann): Need to write API-ref link, Bug# 1592711
89 url = '/tenants/%s/users/%s/roles' % (tenant_id, user_id)
90 if params:
91 url += '?%s' % urllib.urlencode(params)
92 resp, body = self.get(url)
93 self.expected_success(200, resp.status)
94 body = json.loads(body)
95 return rest_client.ResponseBody(resp, body)
96
97 def delete_role_from_user_on_project(self, tenant_id, user_id, role_id):
98 """Removes a role assignment for a user on a tenant.
99
100 Available params: see
101 http://developer.openstack.org/
102 api-ref-identity-v2-ext.html#revokeRoleFromUserOnTenant
103 """
104 resp, body = self.delete('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
105 (tenant_id, user_id, role_id))
106 self.expected_success(204, resp.status)
107 return rest_client.ResponseBody(resp, body)