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 | |
| 13 | from oslo_serialization import jsonutils as json |
| 14 | from six.moves.urllib import parse as urllib |
| 15 | |
| 16 | from tempest.lib.common import rest_client |
| 17 | |
| 18 | |
| 19 | class RolesClient(rest_client.RestClient): |
| 20 | api_version = "v2.0" |
| 21 | |
| 22 | def create_role(self, **kwargs): |
| 23 | """Create a role. |
| 24 | |
OTSUKA, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame^] | 25 | 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 |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 28 | """ |
| 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, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame^] | 38 | 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 |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 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 | |
OTSUKA, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame^] | 52 | 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 |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 55 | """ |
| 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, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame^] | 67 | 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 |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 70 | """ |
guo yunxian | 6cdf056 | 2016-08-17 16:21:52 +0800 | [diff] [blame] | 71 | resp, body = self.delete('OS-KSADM/roles/%s' % role_id) |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 72 | 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, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame^] | 78 | 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 |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 81 | """ |
| 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, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame^] | 102 | 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 |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 105 | """ |
| 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) |