ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 1 | # 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 | |
songwenping | 99d6e00 | 2021-01-05 03:07:46 +0000 | [diff] [blame] | 13 | from urllib import parse as urllib |
| 14 | |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 15 | from oslo_serialization import jsonutils as json |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 16 | |
| 17 | from tempest.lib.common import rest_client |
| 18 | |
| 19 | |
| 20 | class RolesClient(rest_client.RestClient): |
| 21 | api_version = "v2.0" |
| 22 | |
| 23 | def create_role(self, **kwargs): |
| 24 | """Create a role. |
| 25 | |
OTSUKA, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 26 | For a full list of available parameters, please refer to the official |
| 27 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 28 | https://docs.openstack.org/api-ref/identity/v2-ext/index.html#create-a-role |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 29 | """ |
| 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, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 39 | For a full list of available parameters, please refer to the official |
| 40 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 41 | https://docs.openstack.org/api-ref/identity/v2-ext/index.html#show-a-role |
OTSUKA, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 42 | OR |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 43 | https://docs.openstack.org/api-ref/identity/v2-ext/index.html#show-role-information-by-name |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 44 | """ |
| 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, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 53 | For a full list of available parameters, please refer to the official |
| 54 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 55 | https://docs.openstack.org/api-ref/identity/v2-ext/index.html#list-all-roles |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 56 | """ |
| 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, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 68 | For a full list of available parameters, please refer to the official |
| 69 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 70 | https://docs.openstack.org/api-ref/identity/v2-ext/index.html#delete-a-role |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 71 | """ |
guo yunxian | 6cdf056 | 2016-08-17 16:21:52 +0800 | [diff] [blame] | 72 | resp, body = self.delete('OS-KSADM/roles/%s' % role_id) |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 73 | 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, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 79 | For a full list of available parameters, please refer to the official |
| 80 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 81 | https://docs.openstack.org/api-ref/identity/v2-ext/index.html#grant-roles-to-user-on-tenant |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 82 | """ |
| 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, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 103 | For a full list of available parameters, please refer to the official |
| 104 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 105 | https://docs.openstack.org/api-ref/identity/v2-ext/index.html#revoke-role-from-user-on-tenant |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 106 | """ |
| 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) |