blob: 1580c33672d3efa6165261c2fc5745a0e471e79c [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
songwenping99d6e002021-01-05 03:07:46 +000013from urllib import parse as urllib
14
ghanshyam17193062016-06-24 10:36:54 +090015from oslo_serialization import jsonutils as json
ghanshyam17193062016-06-24 10:36:54 +090016
17from tempest.lib.common import rest_client
18
19
20class RolesClient(rest_client.RestClient):
21 api_version = "v2.0"
22
23 def create_role(self, **kwargs):
24 """Create a role.
25
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090026 For a full list of available parameters, please refer to the official
27 API reference:
Andreas Jaegerbf30ae72019-07-22 19:22:57 +020028 https://docs.openstack.org/api-ref/identity/v2-ext/index.html#create-a-role
ghanshyam17193062016-06-24 10:36:54 +090029 """
30 post_body = json.dumps({'role': kwargs})
31 resp, body = self.post('OS-KSADM/roles', post_body)
32 self.expected_success(200, resp.status)
33 body = json.loads(body)
34 return rest_client.ResponseBody(resp, body)
35
36 def show_role(self, role_id_or_name):
37 """Get a role by its id or name.
38
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090039 For a full list of available parameters, please refer to the official
40 API reference:
Andreas Jaegerbf30ae72019-07-22 19:22:57 +020041 https://docs.openstack.org/api-ref/identity/v2-ext/index.html#show-a-role
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090042 OR
Andreas Jaegerbf30ae72019-07-22 19:22:57 +020043 https://docs.openstack.org/api-ref/identity/v2-ext/index.html#show-role-information-by-name
ghanshyam17193062016-06-24 10:36:54 +090044 """
45 resp, body = self.get('OS-KSADM/roles/%s' % role_id_or_name)
46 self.expected_success(200, resp.status)
47 body = json.loads(body)
48 return rest_client.ResponseBody(resp, body)
49
50 def list_roles(self, **params):
51 """Returns roles.
52
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090053 For a full list of available parameters, please refer to the official
54 API reference:
Andreas Jaegerbf30ae72019-07-22 19:22:57 +020055 https://docs.openstack.org/api-ref/identity/v2-ext/index.html#list-all-roles
ghanshyam17193062016-06-24 10:36:54 +090056 """
57 url = 'OS-KSADM/roles'
58 if params:
59 url += '?%s' % urllib.urlencode(params)
60 resp, body = self.get(url)
61 self.expected_success(200, resp.status)
62 body = json.loads(body)
63 return rest_client.ResponseBody(resp, body)
64
65 def delete_role(self, role_id):
66 """Delete a role.
67
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090068 For a full list of available parameters, please refer to the official
69 API reference:
Andreas Jaegerbf30ae72019-07-22 19:22:57 +020070 https://docs.openstack.org/api-ref/identity/v2-ext/index.html#delete-a-role
ghanshyam17193062016-06-24 10:36:54 +090071 """
guo yunxian6cdf0562016-08-17 16:21:52 +080072 resp, body = self.delete('OS-KSADM/roles/%s' % role_id)
ghanshyam17193062016-06-24 10:36:54 +090073 self.expected_success(204, resp.status)
74 return rest_client.ResponseBody(resp, body)
75
76 def create_user_role_on_project(self, tenant_id, user_id, role_id):
77 """Add roles to a user on a tenant.
78
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090079 For a full list of available parameters, please refer to the official
80 API reference:
Andreas Jaegerbf30ae72019-07-22 19:22:57 +020081 https://docs.openstack.org/api-ref/identity/v2-ext/index.html#grant-roles-to-user-on-tenant
ghanshyam17193062016-06-24 10:36:54 +090082 """
83 resp, body = self.put('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
84 (tenant_id, user_id, role_id), "")
85 self.expected_success(200, resp.status)
86 body = json.loads(body)
87 return rest_client.ResponseBody(resp, body)
88
89 def list_user_roles_on_project(self, tenant_id, user_id, **params):
90 """Returns a list of roles assigned to a user for a tenant."""
91 # TODO(gmann): Need to write API-ref link, Bug# 1592711
92 url = '/tenants/%s/users/%s/roles' % (tenant_id, user_id)
93 if params:
94 url += '?%s' % urllib.urlencode(params)
95 resp, body = self.get(url)
96 self.expected_success(200, resp.status)
97 body = json.loads(body)
98 return rest_client.ResponseBody(resp, body)
99
100 def delete_role_from_user_on_project(self, tenant_id, user_id, role_id):
101 """Removes a role assignment for a user on a tenant.
102
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +0900103 For a full list of available parameters, please refer to the official
104 API reference:
Andreas Jaegerbf30ae72019-07-22 19:22:57 +0200105 https://docs.openstack.org/api-ref/identity/v2-ext/index.html#revoke-role-from-user-on-tenant
ghanshyam17193062016-06-24 10:36:54 +0900106 """
107 resp, body = self.delete('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
108 (tenant_id, user_id, role_id))
109 self.expected_success(204, resp.status)
110 return rest_client.ResponseBody(resp, body)