ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 1 | # Copyright 2015 Red Hat, Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | from oslo_serialization import jsonutils as json |
| 16 | from six.moves.urllib import parse as urllib |
| 17 | |
| 18 | from tempest.lib.common import rest_client |
| 19 | |
| 20 | |
| 21 | class TenantsClient(rest_client.RestClient): |
| 22 | api_version = "v2.0" |
| 23 | |
| 24 | def create_tenant(self, **kwargs): |
| 25 | """Create a tenant |
| 26 | |
OTSUKA, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 27 | For a full list of available parameters, please refer to the official |
| 28 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 29 | https://docs.openstack.org/api-ref/identity/v2-admin/index.html#create-tenant |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 30 | """ |
| 31 | post_body = json.dumps({'tenant': kwargs}) |
| 32 | resp, body = self.post('tenants', post_body) |
| 33 | self.expected_success(200, resp.status) |
| 34 | body = json.loads(body) |
| 35 | return rest_client.ResponseBody(resp, body) |
| 36 | |
| 37 | def delete_tenant(self, tenant_id): |
| 38 | """Delete a tenant. |
| 39 | |
OTSUKA, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 40 | For a full list of available parameters, please refer to the official |
| 41 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 42 | https://docs.openstack.org/api-ref/identity/v2-admin/index.html#delete-tenant |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 43 | """ |
| 44 | resp, body = self.delete('tenants/%s' % str(tenant_id)) |
| 45 | self.expected_success(204, resp.status) |
| 46 | return rest_client.ResponseBody(resp, body) |
| 47 | |
| 48 | def show_tenant(self, tenant_id): |
| 49 | """Get tenant details. |
| 50 | |
OTSUKA, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 51 | For a full list of available parameters, please refer to the official |
| 52 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 53 | https://docs.openstack.org/api-ref/identity/v2-admin/index.html#show-tenant-details-by-id |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 54 | """ |
| 55 | resp, body = self.get('tenants/%s' % str(tenant_id)) |
| 56 | self.expected_success(200, resp.status) |
| 57 | body = json.loads(body) |
| 58 | return rest_client.ResponseBody(resp, body) |
| 59 | |
| 60 | def list_tenants(self, **params): |
| 61 | """Returns tenants. |
| 62 | |
OTSUKA, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 63 | For a full list of available parameters, please refer to the official |
| 64 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 65 | https://docs.openstack.org/api-ref/identity/v2-admin/index.html#list-tenants-admin-endpoint |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 66 | """ |
| 67 | url = 'tenants' |
| 68 | if params: |
| 69 | url += '?%s' % urllib.urlencode(params) |
| 70 | resp, body = self.get(url) |
| 71 | self.expected_success(200, resp.status) |
| 72 | body = json.loads(body) |
| 73 | return rest_client.ResponseBody(resp, body) |
| 74 | |
| 75 | def update_tenant(self, tenant_id, **kwargs): |
| 76 | """Updates 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: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 80 | https://docs.openstack.org/api-ref/identity/v2-admin/index.html#update-tenant |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 81 | """ |
| 82 | if 'id' not in kwargs: |
| 83 | kwargs['id'] = tenant_id |
| 84 | post_body = json.dumps({'tenant': kwargs}) |
| 85 | resp, body = self.post('tenants/%s' % tenant_id, post_body) |
| 86 | self.expected_success(200, resp.status) |
| 87 | body = json.loads(body) |
| 88 | return rest_client.ResponseBody(resp, body) |
| 89 | |
| 90 | def list_tenant_users(self, tenant_id, **params): |
| 91 | """List users for a Tenant. |
| 92 | |
OTSUKA, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 93 | For a full list of available parameters, please refer to the official |
| 94 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 95 | https://docs.openstack.org/api-ref/identity/v2-admin/index.html#list-users-on-a-tenant |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 96 | """ |
| 97 | url = '/tenants/%s/users' % tenant_id |
| 98 | if params: |
| 99 | url += '?%s' % urllib.urlencode(params) |
| 100 | resp, body = self.get(url) |
| 101 | self.expected_success(200, resp.status) |
| 102 | body = json.loads(body) |
| 103 | return rest_client.ResponseBody(resp, body) |