Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [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 |
ghanshyam | 7668fad | 2016-06-15 18:17:39 +0900 | [diff] [blame] | 16 | from six.moves.urllib import parse as urllib |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 17 | |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 18 | from tempest.lib.common import rest_client |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 19 | |
| 20 | |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 21 | class TenantsClient(rest_client.RestClient): |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 22 | api_version = "v2.0" |
| 23 | |
ghanshyam | 7668fad | 2016-06-15 18:17:39 +0900 | [diff] [blame] | 24 | def create_tenant(self, **kwargs): |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 25 | """Create a tenant |
| 26 | |
ghanshyam | 7668fad | 2016-06-15 18:17:39 +0900 | [diff] [blame] | 27 | Available params: see http://developer.openstack.org/ |
| 28 | api-ref-identity-v2-ext.html#createTenant |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 29 | """ |
ghanshyam | 7668fad | 2016-06-15 18:17:39 +0900 | [diff] [blame] | 30 | post_body = json.dumps({'tenant': kwargs}) |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 31 | resp, body = self.post('tenants', post_body) |
| 32 | self.expected_success(200, resp.status) |
| 33 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 34 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 35 | |
| 36 | def delete_tenant(self, tenant_id): |
ghanshyam | 7668fad | 2016-06-15 18:17:39 +0900 | [diff] [blame] | 37 | """Delete a tenant. |
| 38 | |
| 39 | Available params: see http://developer.openstack.org/ |
| 40 | api-ref-identity-v2-ext.html#deleteTenant |
| 41 | """ |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 42 | resp, body = self.delete('tenants/%s' % str(tenant_id)) |
| 43 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 44 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 45 | |
| 46 | def show_tenant(self, tenant_id): |
ghanshyam | 7668fad | 2016-06-15 18:17:39 +0900 | [diff] [blame] | 47 | """Get tenant details. |
| 48 | |
| 49 | Available params: see |
| 50 | http://developer.openstack.org/ |
| 51 | api-ref-identity-v2-ext.html#admin-showTenantById |
| 52 | """ |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 53 | resp, body = self.get('tenants/%s' % str(tenant_id)) |
| 54 | self.expected_success(200, resp.status) |
| 55 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 56 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 57 | |
ghanshyam | 7668fad | 2016-06-15 18:17:39 +0900 | [diff] [blame] | 58 | def list_tenants(self, **params): |
| 59 | """Returns tenants. |
| 60 | |
| 61 | Available params: see http://developer.openstack.org/ |
| 62 | api-ref-identity-v2-ext.html#admin-listTenants |
| 63 | """ |
| 64 | url = 'tenants' |
| 65 | if params: |
| 66 | url += '?%s' % urllib.urlencode(params) |
| 67 | resp, body = self.get(url) |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 68 | self.expected_success(200, resp.status) |
| 69 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 70 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 71 | |
| 72 | def update_tenant(self, tenant_id, **kwargs): |
zhufl | 7daa31e | 2016-06-14 17:24:42 +0800 | [diff] [blame] | 73 | """Updates a tenant. |
| 74 | |
| 75 | Available params: see http://developer.openstack.org/ |
| 76 | api-ref-identity-v2-ext.html#updateTenant |
| 77 | """ |
ghanshyam | 7668fad | 2016-06-15 18:17:39 +0900 | [diff] [blame] | 78 | if 'id' not in kwargs: |
| 79 | kwargs['id'] = tenant_id |
| 80 | post_body = json.dumps({'tenant': kwargs}) |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 81 | resp, body = self.post('tenants/%s' % tenant_id, post_body) |
| 82 | self.expected_success(200, resp.status) |
| 83 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 84 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 85 | |
ghanshyam | 7668fad | 2016-06-15 18:17:39 +0900 | [diff] [blame] | 86 | def list_tenant_users(self, tenant_id, **params): |
| 87 | """List users for a Tenant. |
| 88 | |
| 89 | Available params: see http://developer.openstack.org/ |
| 90 | api-ref-identity-v2-ext.html#listUsersForTenant |
| 91 | """ |
| 92 | url = '/tenants/%s/users' % tenant_id |
| 93 | if params: |
| 94 | url += '?%s' % urllib.urlencode(params) |
| 95 | resp, body = self.get(url) |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 96 | self.expected_success(200, resp.status) |
| 97 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 98 | return rest_client.ResponseBody(resp, body) |