blob: 77ddaa5d36b3de81fc8d6f84dccaf0e0cdf5b20a [file] [log] [blame]
Daniel Melladob04da902015-11-20 17:43:12 +01001# 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
15from oslo_serialization import jsonutils as json
ghanshyam7668fad2016-06-15 18:17:39 +090016from six.moves.urllib import parse as urllib
Daniel Melladob04da902015-11-20 17:43:12 +010017
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080018from tempest.lib.common import rest_client
Daniel Melladob04da902015-11-20 17:43:12 +010019
20
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080021class TenantsClient(rest_client.RestClient):
Daniel Melladob04da902015-11-20 17:43:12 +010022 api_version = "v2.0"
23
ghanshyam7668fad2016-06-15 18:17:39 +090024 def create_tenant(self, **kwargs):
Daniel Melladob04da902015-11-20 17:43:12 +010025 """Create a tenant
26
ghanshyam7668fad2016-06-15 18:17:39 +090027 Available params: see http://developer.openstack.org/
28 api-ref-identity-v2-ext.html#createTenant
Daniel Melladob04da902015-11-20 17:43:12 +010029 """
ghanshyam7668fad2016-06-15 18:17:39 +090030 post_body = json.dumps({'tenant': kwargs})
Daniel Melladob04da902015-11-20 17:43:12 +010031 resp, body = self.post('tenants', post_body)
32 self.expected_success(200, resp.status)
33 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080034 return rest_client.ResponseBody(resp, body)
Daniel Melladob04da902015-11-20 17:43:12 +010035
36 def delete_tenant(self, tenant_id):
ghanshyam7668fad2016-06-15 18:17:39 +090037 """Delete a tenant.
38
39 Available params: see http://developer.openstack.org/
40 api-ref-identity-v2-ext.html#deleteTenant
41 """
Daniel Melladob04da902015-11-20 17:43:12 +010042 resp, body = self.delete('tenants/%s' % str(tenant_id))
43 self.expected_success(204, resp.status)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080044 return rest_client.ResponseBody(resp, body)
Daniel Melladob04da902015-11-20 17:43:12 +010045
46 def show_tenant(self, tenant_id):
ghanshyam7668fad2016-06-15 18:17:39 +090047 """Get tenant details.
48
49 Available params: see
50 http://developer.openstack.org/
51 api-ref-identity-v2-ext.html#admin-showTenantById
52 """
Daniel Melladob04da902015-11-20 17:43:12 +010053 resp, body = self.get('tenants/%s' % str(tenant_id))
54 self.expected_success(200, resp.status)
55 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080056 return rest_client.ResponseBody(resp, body)
Daniel Melladob04da902015-11-20 17:43:12 +010057
ghanshyam7668fad2016-06-15 18:17:39 +090058 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 Melladob04da902015-11-20 17:43:12 +010068 self.expected_success(200, resp.status)
69 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080070 return rest_client.ResponseBody(resp, body)
Daniel Melladob04da902015-11-20 17:43:12 +010071
72 def update_tenant(self, tenant_id, **kwargs):
zhufl7daa31e2016-06-14 17:24:42 +080073 """Updates a tenant.
74
75 Available params: see http://developer.openstack.org/
76 api-ref-identity-v2-ext.html#updateTenant
77 """
ghanshyam7668fad2016-06-15 18:17:39 +090078 if 'id' not in kwargs:
79 kwargs['id'] = tenant_id
80 post_body = json.dumps({'tenant': kwargs})
Daniel Melladob04da902015-11-20 17:43:12 +010081 resp, body = self.post('tenants/%s' % tenant_id, post_body)
82 self.expected_success(200, resp.status)
83 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080084 return rest_client.ResponseBody(resp, body)
Daniel Melladob04da902015-11-20 17:43:12 +010085
ghanshyam7668fad2016-06-15 18:17:39 +090086 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 Melladob04da902015-11-20 17:43:12 +010096 self.expected_success(200, resp.status)
97 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080098 return rest_client.ResponseBody(resp, body)