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 | |
| 27 | Available params: see http://developer.openstack.org/ |
zhufl | a902142 | 2016-09-01 16:52:55 +0800 | [diff] [blame] | 28 | api-ref/identity/v2-admin/index.html# |
| 29 | 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 | |
| 40 | Available params: see http://developer.openstack.org/ |
| 41 | api-ref-identity-v2-ext.html#deleteTenant |
| 42 | """ |
| 43 | resp, body = self.delete('tenants/%s' % str(tenant_id)) |
| 44 | self.expected_success(204, resp.status) |
| 45 | return rest_client.ResponseBody(resp, body) |
| 46 | |
| 47 | def show_tenant(self, tenant_id): |
| 48 | """Get tenant details. |
| 49 | |
| 50 | Available params: see |
| 51 | http://developer.openstack.org/ |
| 52 | api-ref-identity-v2-ext.html#admin-showTenantById |
| 53 | """ |
| 54 | resp, body = self.get('tenants/%s' % str(tenant_id)) |
| 55 | self.expected_success(200, resp.status) |
| 56 | body = json.loads(body) |
| 57 | return rest_client.ResponseBody(resp, body) |
| 58 | |
| 59 | def list_tenants(self, **params): |
| 60 | """Returns tenants. |
| 61 | |
| 62 | Available params: see http://developer.openstack.org/ |
zhufl | a902142 | 2016-09-01 16:52:55 +0800 | [diff] [blame] | 63 | api-ref/identity/v2-admin/index.html# |
| 64 | list-tenants-admin-endpoint |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 65 | """ |
| 66 | url = 'tenants' |
| 67 | if params: |
| 68 | url += '?%s' % urllib.urlencode(params) |
| 69 | resp, body = self.get(url) |
| 70 | self.expected_success(200, resp.status) |
| 71 | body = json.loads(body) |
| 72 | return rest_client.ResponseBody(resp, body) |
| 73 | |
| 74 | def update_tenant(self, tenant_id, **kwargs): |
| 75 | """Updates a tenant. |
| 76 | |
| 77 | Available params: see http://developer.openstack.org/ |
zhufl | a902142 | 2016-09-01 16:52:55 +0800 | [diff] [blame] | 78 | api-ref/identity/v2-admin/index.html# |
| 79 | update-tenant |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 80 | """ |
| 81 | if 'id' not in kwargs: |
| 82 | kwargs['id'] = tenant_id |
| 83 | post_body = json.dumps({'tenant': kwargs}) |
| 84 | resp, body = self.post('tenants/%s' % tenant_id, post_body) |
| 85 | self.expected_success(200, resp.status) |
| 86 | body = json.loads(body) |
| 87 | return rest_client.ResponseBody(resp, body) |
| 88 | |
| 89 | def list_tenant_users(self, tenant_id, **params): |
| 90 | """List users for a Tenant. |
| 91 | |
| 92 | Available params: see http://developer.openstack.org/ |
zhufl | a902142 | 2016-09-01 16:52:55 +0800 | [diff] [blame] | 93 | api-ref/identity/v2-admin/index.html# |
| 94 | list-users-on-a-tenant |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 95 | """ |
| 96 | url = '/tenants/%s/users' % tenant_id |
| 97 | if params: |
| 98 | url += '?%s' % urllib.urlencode(params) |
| 99 | resp, body = self.get(url) |
| 100 | self.expected_success(200, resp.status) |
| 101 | body = json.loads(body) |
| 102 | return rest_client.ResponseBody(resp, body) |