blob: 6fbb3a9c897efd362c82b0f88feea2bc8ac066ef [file] [log] [blame]
Sean Dague556add52013-07-19 14:28:44 -04001# 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
chris fattarsi8ed39ac2012-04-30 14:11:27 -070013import json
Masayuki Igawabfa07602015-01-20 18:47:17 +090014from tempest_lib import exceptions as lib_exc
chris fattarsi8ed39ac2012-04-30 14:11:27 -070015
Ken'ichi Ohmichi0e836652015-01-08 04:38:56 +000016from tempest.common import service_client
Matthew Treinish684d8992014-01-30 16:27:40 +000017from tempest import config
Matthew Treinisha83a16e2012-12-07 13:44:02 -050018
Matthew Treinish684d8992014-01-30 16:27:40 +000019CONF = config.CONF
20
chris fattarsi8ed39ac2012-04-30 14:11:27 -070021
Ken'ichi Ohmichi0e836652015-01-08 04:38:56 +000022class IdentityClientJSON(service_client.ServiceClient):
chris fattarsi8ed39ac2012-04-30 14:11:27 -070023
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000024 def __init__(self, auth_provider):
Ken'ichi Ohmichie9f50412015-01-05 04:57:26 +000025 super(IdentityClientJSON, self).__init__(
26 auth_provider,
27 CONF.identity.catalog_type,
28 CONF.identity.region,
29 endpoint_type='adminURL')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070030
31 def has_admin_extensions(self):
32 """
33 Returns True if the KSADM Admin Extensions are supported
34 False otherwise
35 """
36 if hasattr(self, '_has_admin_extensions'):
37 return self._has_admin_extensions
David Kranze9d2f422014-07-02 13:57:41 -040038 # Try something that requires admin
39 try:
40 self.list_roles()
41 self._has_admin_extensions = True
42 except Exception:
43 self._has_admin_extensions = False
chris fattarsi8ed39ac2012-04-30 14:11:27 -070044 return self._has_admin_extensions
45
46 def create_role(self, name):
Sean Daguef237ccb2013-01-04 15:19:14 -050047 """Create a role."""
chris fattarsi8ed39ac2012-04-30 14:11:27 -070048 post_body = {
49 'name': name,
50 }
51 post_body = json.dumps({'role': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +020052 resp, body = self.post('OS-KSADM/roles', post_body)
David Kranze9d2f422014-07-02 13:57:41 -040053 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000054 return service_client.ResponseBody(resp, self._parse_resp(body))
chris fattarsi8ed39ac2012-04-30 14:11:27 -070055
Gong Zhangcb6b8862014-02-20 15:14:05 +080056 def get_role(self, role_id):
57 """Get a role by its id."""
58 resp, body = self.get('OS-KSADM/roles/%s' % role_id)
David Kranze9d2f422014-07-02 13:57:41 -040059 self.expected_success(200, resp.status)
Gong Zhangcb6b8862014-02-20 15:14:05 +080060 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000061 return service_client.ResponseBody(resp, body['role'])
Gong Zhangcb6b8862014-02-20 15:14:05 +080062
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070063 def create_tenant(self, name, **kwargs):
64 """
65 Create a tenant
66 name (required): New tenant name
67 description: Description of new tenant (default is none)
68 enabled <true|false>: Initial tenant status (default is true)
69 """
70 post_body = {
71 'name': name,
72 'description': kwargs.get('description', ''),
Gordon Chungad873602013-02-18 19:26:27 -050073 'enabled': kwargs.get('enabled', True),
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070074 }
75 post_body = json.dumps({'tenant': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +020076 resp, body = self.post('tenants', post_body)
David Kranze9d2f422014-07-02 13:57:41 -040077 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000078 return service_client.ResponseBody(resp, self._parse_resp(body))
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070079
chris fattarsi8ed39ac2012-04-30 14:11:27 -070080 def delete_role(self, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050081 """Delete a role."""
David Kranze9d2f422014-07-02 13:57:41 -040082 resp, body = self.delete('OS-KSADM/roles/%s' % str(role_id))
83 self.expected_success(204, resp.status)
84 return resp, body
chris fattarsi8ed39ac2012-04-30 14:11:27 -070085
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053086 def list_user_roles(self, tenant_id, user_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050087 """Returns a list of roles assigned to a user for a tenant."""
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053088 url = '/tenants/%s/users/%s/roles' % (tenant_id, user_id)
89 resp, body = self.get(url)
David Kranze9d2f422014-07-02 13:57:41 -040090 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000091 return service_client.ResponseBodyList(resp, self._parse_resp(body))
Rohit Karajgi69e80a02012-05-15 03:54:04 -070092
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053093 def assign_user_role(self, tenant_id, user_id, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050094 """Add roles to a user on a tenant."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +080095 resp, body = self.put('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
vponomaryov67b58fe2014-02-06 19:05:41 +020096 (tenant_id, user_id, role_id), "")
David Kranze9d2f422014-07-02 13:57:41 -040097 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000098 return service_client.ResponseBody(resp, self._parse_resp(body))
Rohit Karajgi69e80a02012-05-15 03:54:04 -070099
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530100 def remove_user_role(self, tenant_id, user_id, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500101 """Removes a role assignment for a user on a tenant."""
David Kranze9d2f422014-07-02 13:57:41 -0400102 resp, body = self.delete('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
103 (tenant_id, user_id, role_id))
104 self.expected_success(204, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000105 return service_client.ResponseBody(resp, body)
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700106
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700107 def delete_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500108 """Delete a tenant."""
David Kranze9d2f422014-07-02 13:57:41 -0400109 resp, body = self.delete('tenants/%s' % str(tenant_id))
110 self.expected_success(204, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000111 return service_client.ResponseBody(resp, body)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700112
113 def get_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500114 """Get tenant details."""
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700115 resp, body = self.get('tenants/%s' % str(tenant_id))
David Kranze9d2f422014-07-02 13:57:41 -0400116 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000117 return service_client.ResponseBody(resp, self._parse_resp(body))
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700118
chris fattarsi8ed39ac2012-04-30 14:11:27 -0700119 def list_roles(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500120 """Returns roles."""
chris fattarsi8ed39ac2012-04-30 14:11:27 -0700121 resp, body = self.get('OS-KSADM/roles')
David Kranze9d2f422014-07-02 13:57:41 -0400122 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000123 return service_client.ResponseBodyList(resp, self._parse_resp(body))
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700124
125 def list_tenants(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500126 """Returns tenants."""
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700127 resp, body = self.get('tenants')
David Kranze9d2f422014-07-02 13:57:41 -0400128 self.expected_success(200, resp.status)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700129 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000130 return service_client.ResponseBodyList(resp, body['tenants'])
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700131
Dan Smithd6ff6b72012-08-23 10:29:41 -0700132 def get_tenant_by_name(self, tenant_name):
David Kranzb7afa922014-12-30 10:56:26 -0500133 tenants = self.list_tenants()
Dan Smithd6ff6b72012-08-23 10:29:41 -0700134 for tenant in tenants:
135 if tenant['name'] == tenant_name:
136 return tenant
Masayuki Igawabfa07602015-01-20 18:47:17 +0900137 raise lib_exc.NotFound('No such tenant')
Dan Smithd6ff6b72012-08-23 10:29:41 -0700138
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700139 def update_tenant(self, tenant_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500140 """Updates a tenant."""
David Kranzb7afa922014-12-30 10:56:26 -0500141 body = self.get_tenant(tenant_id)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700142 name = kwargs.get('name', body['name'])
143 desc = kwargs.get('description', body['description'])
144 en = kwargs.get('enabled', body['enabled'])
145 post_body = {
146 'id': tenant_id,
147 'name': name,
148 'description': desc,
149 'enabled': en,
150 }
151 post_body = json.dumps({'tenant': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200152 resp, body = self.post('tenants/%s' % tenant_id, post_body)
David Kranze9d2f422014-07-02 13:57:41 -0400153 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000154 return service_client.ResponseBody(resp, self._parse_resp(body))
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700155
huangtianhuafc8db4f2013-10-08 12:05:58 +0800156 def create_user(self, name, password, tenant_id, email, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500157 """Create a user."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700158 post_body = {
159 'name': name,
160 'password': password,
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700161 'email': email
162 }
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500163 if tenant_id is not None:
164 post_body['tenantId'] = tenant_id
huangtianhuafc8db4f2013-10-08 12:05:58 +0800165 if kwargs.get('enabled') is not None:
166 post_body['enabled'] = kwargs.get('enabled')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700167 post_body = json.dumps({'user': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200168 resp, body = self.post('users', post_body)
David Kranze9d2f422014-07-02 13:57:41 -0400169 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000170 return service_client.ResponseBody(resp, self._parse_resp(body))
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700171
Chang Bo Guob36b2f12013-09-13 04:52:00 -0700172 def update_user(self, user_id, **kwargs):
173 """Updates a user."""
174 put_body = json.dumps({'user': kwargs})
vponomaryov67b58fe2014-02-06 19:05:41 +0200175 resp, body = self.put('users/%s' % user_id, put_body)
David Kranze9d2f422014-07-02 13:57:41 -0400176 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000177 return service_client.ResponseBody(resp, self._parse_resp(body))
Chang Bo Guob36b2f12013-09-13 04:52:00 -0700178
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530179 def get_user(self, user_id):
180 """GET a user."""
181 resp, body = self.get("users/%s" % user_id)
David Kranze9d2f422014-07-02 13:57:41 -0400182 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000183 return service_client.ResponseBody(resp, self._parse_resp(body))
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530184
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700185 def delete_user(self, user_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500186 """Delete a user."""
David Kranze9d2f422014-07-02 13:57:41 -0400187 resp, body = self.delete("users/%s" % user_id)
188 self.expected_success(204, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000189 return service_client.ResponseBody(resp, body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700190
191 def get_users(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500192 """Get the list of users."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700193 resp, body = self.get("users")
David Kranze9d2f422014-07-02 13:57:41 -0400194 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000195 return service_client.ResponseBodyList(resp, self._parse_resp(body))
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700196
197 def enable_disable_user(self, user_id, enabled):
Sean Daguef237ccb2013-01-04 15:19:14 -0500198 """Enables or disables a user."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700199 put_body = {
Sean Dague14c68182013-04-14 15:34:30 -0400200 'enabled': enabled
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700201 }
202 put_body = json.dumps({'user': put_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200203 resp, body = self.put('users/%s/enabled' % user_id, put_body)
David Kranze9d2f422014-07-02 13:57:41 -0400204 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000205 return service_client.ResponseBody(resp, self._parse_resp(body))
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700206
Zhi Kun Liu30caeae2014-02-26 15:30:24 +0800207 def get_token(self, token_id):
208 """Get token details."""
209 resp, body = self.get("tokens/%s" % token_id)
David Kranze9d2f422014-07-02 13:57:41 -0400210 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000211 return service_client.ResponseBody(resp, self._parse_resp(body))
Zhi Kun Liu30caeae2014-02-26 15:30:24 +0800212
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700213 def delete_token(self, token_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500214 """Delete a token."""
David Kranze9d2f422014-07-02 13:57:41 -0400215 resp, body = self.delete("tokens/%s" % token_id)
216 self.expected_success(204, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000217 return service_client.ResponseBody(resp, body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700218
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530219 def list_users_for_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500220 """List users for a Tenant."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530221 resp, body = self.get('/tenants/%s/users' % tenant_id)
David Kranze9d2f422014-07-02 13:57:41 -0400222 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000223 return service_client.ResponseBodyList(resp, self._parse_resp(body))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530224
Dan Smithd6ff6b72012-08-23 10:29:41 -0700225 def get_user_by_username(self, tenant_id, username):
David Kranzb7afa922014-12-30 10:56:26 -0500226 users = self.list_users_for_tenant(tenant_id)
Dan Smithd6ff6b72012-08-23 10:29:41 -0700227 for user in users:
228 if user['name'] == username:
229 return user
Masayuki Igawabfa07602015-01-20 18:47:17 +0900230 raise lib_exc.NotFound('No such user')
Dan Smithd6ff6b72012-08-23 10:29:41 -0700231
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530232 def create_service(self, name, type, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500233 """Create a service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530234 post_body = {
Zhongyue Luoa1343de2013-01-04 16:21:35 +0800235 'name': name,
236 'type': type,
237 'description': kwargs.get('description')
238 }
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530239 post_body = json.dumps({'OS-KSADM:service': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200240 resp, body = self.post('/OS-KSADM/services', post_body)
David Kranze9d2f422014-07-02 13:57:41 -0400241 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000242 return service_client.ResponseBody(resp, self._parse_resp(body))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530243
244 def get_service(self, service_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500245 """Get Service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530246 url = '/OS-KSADM/services/%s' % service_id
247 resp, body = self.get(url)
David Kranze9d2f422014-07-02 13:57:41 -0400248 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000249 return service_client.ResponseBody(resp, self._parse_resp(body))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530250
umamohanb51ad002013-01-24 18:13:15 +0000251 def list_services(self):
252 """List Service - Returns Services."""
Tushar Kalraa76929c2014-03-31 12:23:07 -0700253 resp, body = self.get('/OS-KSADM/services')
David Kranze9d2f422014-07-02 13:57:41 -0400254 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000255 return service_client.ResponseBodyList(resp, self._parse_resp(body))
umamohanb51ad002013-01-24 18:13:15 +0000256
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530257 def delete_service(self, service_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500258 """Delete Service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530259 url = '/OS-KSADM/services/%s' % service_id
David Kranze9d2f422014-07-02 13:57:41 -0400260 resp, body = self.delete(url)
261 self.expected_success(204, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000262 return service_client.ResponseBody(resp, body)
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530263
Abhijeet.Jainff5c3542014-05-06 16:07:30 +0530264 def update_user_password(self, user_id, new_pass):
265 """Update User Password."""
266 put_body = {
267 'password': new_pass,
268 'id': user_id
269 }
270 put_body = json.dumps({'user': put_body})
271 resp, body = self.put('users/%s/OS-KSADM/password' % user_id, put_body)
David Kranze9d2f422014-07-02 13:57:41 -0400272 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000273 return service_client.ResponseBody(resp, self._parse_resp(body))
Abhijeet.Jainff5c3542014-05-06 16:07:30 +0530274
Abhijeet.Jain3f49b842014-05-20 12:06:20 +0530275 def list_extensions(self):
276 """List all the extensions."""
277 resp, body = self.get('/extensions')
David Kranze9d2f422014-07-02 13:57:41 -0400278 self.expected_success(200, resp.status)
Abhijeet.Jain3f49b842014-05-20 12:06:20 +0530279 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000280 return service_client.ResponseBodyList(resp,
281 body['extensions']['values'])