blob: b0cab8e53e49fba58bb1bc304f57512c1af300b6 [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
14
vponomaryov67b58fe2014-02-06 19:05:41 +020015from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000016from tempest import config
Matthew Treinisha83a16e2012-12-07 13:44:02 -050017from tempest import exceptions
18
Matthew Treinish684d8992014-01-30 16:27:40 +000019CONF = config.CONF
20
chris fattarsi8ed39ac2012-04-30 14:11:27 -070021
vponomaryov67b58fe2014-02-06 19:05:41 +020022class IdentityClientJSON(rest_client.RestClient):
chris fattarsi8ed39ac2012-04-30 14:11:27 -070023
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000024 def __init__(self, auth_provider):
25 super(IdentityClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000026 self.service = CONF.identity.catalog_type
chris fattarsi8ed39ac2012-04-30 14:11:27 -070027 self.endpoint_url = 'adminURL'
28
vponomaryov67b58fe2014-02-06 19:05:41 +020029 # Needed for xml service client
Abhijeet.Jain3f49b842014-05-20 12:06:20 +053030 self.list_tags = ["roles", "tenants", "users", "services",
31 "extensions"]
vponomaryov67b58fe2014-02-06 19:05:41 +020032
chris fattarsi8ed39ac2012-04-30 14:11:27 -070033 def has_admin_extensions(self):
34 """
35 Returns True if the KSADM Admin Extensions are supported
36 False otherwise
37 """
38 if hasattr(self, '_has_admin_extensions'):
39 return self._has_admin_extensions
40 resp, body = self.list_roles()
41 self._has_admin_extensions = ('status' in resp and resp.status != 503)
42 return self._has_admin_extensions
43
44 def create_role(self, name):
Sean Daguef237ccb2013-01-04 15:19:14 -050045 """Create a role."""
chris fattarsi8ed39ac2012-04-30 14:11:27 -070046 post_body = {
47 'name': name,
48 }
49 post_body = json.dumps({'role': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +020050 resp, body = self.post('OS-KSADM/roles', post_body)
51 return resp, self._parse_resp(body)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070052
Gong Zhangcb6b8862014-02-20 15:14:05 +080053 def get_role(self, role_id):
54 """Get a role by its id."""
55 resp, body = self.get('OS-KSADM/roles/%s' % role_id)
56 body = json.loads(body)
57 return resp, body['role']
58
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070059 def create_tenant(self, name, **kwargs):
60 """
61 Create a tenant
62 name (required): New tenant name
63 description: Description of new tenant (default is none)
64 enabled <true|false>: Initial tenant status (default is true)
65 """
66 post_body = {
67 'name': name,
68 'description': kwargs.get('description', ''),
Gordon Chungad873602013-02-18 19:26:27 -050069 'enabled': kwargs.get('enabled', True),
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070070 }
71 post_body = json.dumps({'tenant': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +020072 resp, body = self.post('tenants', post_body)
73 return resp, self._parse_resp(body)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070074
chris fattarsi8ed39ac2012-04-30 14:11:27 -070075 def delete_role(self, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050076 """Delete a role."""
vponomaryov67b58fe2014-02-06 19:05:41 +020077 return self.delete('OS-KSADM/roles/%s' % str(role_id))
chris fattarsi8ed39ac2012-04-30 14:11:27 -070078
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053079 def list_user_roles(self, tenant_id, user_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050080 """Returns a list of roles assigned to a user for a tenant."""
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053081 url = '/tenants/%s/users/%s/roles' % (tenant_id, user_id)
82 resp, body = self.get(url)
vponomaryov67b58fe2014-02-06 19:05:41 +020083 return resp, self._parse_resp(body)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070084
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053085 def assign_user_role(self, tenant_id, user_id, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050086 """Add roles to a user on a tenant."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +080087 resp, body = self.put('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
vponomaryov67b58fe2014-02-06 19:05:41 +020088 (tenant_id, user_id, role_id), "")
89 return resp, self._parse_resp(body)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070090
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053091 def remove_user_role(self, tenant_id, user_id, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050092 """Removes a role assignment for a user on a tenant."""
Zhongyue Luo79d8d362012-09-25 13:49:27 +080093 return self.delete('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
94 (tenant_id, user_id, role_id))
Rohit Karajgi69e80a02012-05-15 03:54:04 -070095
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070096 def delete_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050097 """Delete a tenant."""
vponomaryov67b58fe2014-02-06 19:05:41 +020098 return self.delete('tenants/%s' % str(tenant_id))
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070099
100 def get_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500101 """Get tenant details."""
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700102 resp, body = self.get('tenants/%s' % str(tenant_id))
vponomaryov67b58fe2014-02-06 19:05:41 +0200103 return resp, self._parse_resp(body)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700104
chris fattarsi8ed39ac2012-04-30 14:11:27 -0700105 def list_roles(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500106 """Returns roles."""
chris fattarsi8ed39ac2012-04-30 14:11:27 -0700107 resp, body = self.get('OS-KSADM/roles')
vponomaryov67b58fe2014-02-06 19:05:41 +0200108 return resp, self._parse_resp(body)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700109
110 def list_tenants(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500111 """Returns tenants."""
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700112 resp, body = self.get('tenants')
113 body = json.loads(body)
114 return resp, body['tenants']
115
Dan Smithd6ff6b72012-08-23 10:29:41 -0700116 def get_tenant_by_name(self, tenant_name):
117 resp, tenants = self.list_tenants()
118 for tenant in tenants:
119 if tenant['name'] == tenant_name:
120 return tenant
121 raise exceptions.NotFound('No such tenant')
122
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700123 def update_tenant(self, tenant_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500124 """Updates a tenant."""
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700125 resp, body = self.get_tenant(tenant_id)
126 name = kwargs.get('name', body['name'])
127 desc = kwargs.get('description', body['description'])
128 en = kwargs.get('enabled', body['enabled'])
129 post_body = {
130 'id': tenant_id,
131 'name': name,
132 'description': desc,
133 'enabled': en,
134 }
135 post_body = json.dumps({'tenant': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200136 resp, body = self.post('tenants/%s' % tenant_id, post_body)
137 return resp, self._parse_resp(body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700138
huangtianhuafc8db4f2013-10-08 12:05:58 +0800139 def create_user(self, name, password, tenant_id, email, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500140 """Create a user."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700141 post_body = {
142 'name': name,
143 'password': password,
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700144 'email': email
145 }
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500146 if tenant_id is not None:
147 post_body['tenantId'] = tenant_id
huangtianhuafc8db4f2013-10-08 12:05:58 +0800148 if kwargs.get('enabled') is not None:
149 post_body['enabled'] = kwargs.get('enabled')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700150 post_body = json.dumps({'user': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200151 resp, body = self.post('users', post_body)
152 return resp, self._parse_resp(body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700153
Chang Bo Guob36b2f12013-09-13 04:52:00 -0700154 def update_user(self, user_id, **kwargs):
155 """Updates a user."""
156 put_body = json.dumps({'user': kwargs})
vponomaryov67b58fe2014-02-06 19:05:41 +0200157 resp, body = self.put('users/%s' % user_id, put_body)
158 return resp, self._parse_resp(body)
Chang Bo Guob36b2f12013-09-13 04:52:00 -0700159
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530160 def get_user(self, user_id):
161 """GET a user."""
162 resp, body = self.get("users/%s" % user_id)
vponomaryov67b58fe2014-02-06 19:05:41 +0200163 return resp, self._parse_resp(body)
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530164
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700165 def delete_user(self, user_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500166 """Delete a user."""
vponomaryov67b58fe2014-02-06 19:05:41 +0200167 return self.delete("users/%s" % user_id)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700168
169 def get_users(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500170 """Get the list of users."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700171 resp, body = self.get("users")
vponomaryov67b58fe2014-02-06 19:05:41 +0200172 return resp, self._parse_resp(body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700173
174 def enable_disable_user(self, user_id, enabled):
Sean Daguef237ccb2013-01-04 15:19:14 -0500175 """Enables or disables a user."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700176 put_body = {
Sean Dague14c68182013-04-14 15:34:30 -0400177 'enabled': enabled
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700178 }
179 put_body = json.dumps({'user': put_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200180 resp, body = self.put('users/%s/enabled' % user_id, put_body)
181 return resp, self._parse_resp(body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700182
Zhi Kun Liu30caeae2014-02-26 15:30:24 +0800183 def get_token(self, token_id):
184 """Get token details."""
185 resp, body = self.get("tokens/%s" % token_id)
186 return resp, self._parse_resp(body)
187
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700188 def delete_token(self, token_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500189 """Delete a token."""
vponomaryov67b58fe2014-02-06 19:05:41 +0200190 return self.delete("tokens/%s" % token_id)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700191
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530192 def list_users_for_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500193 """List users for a Tenant."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530194 resp, body = self.get('/tenants/%s/users' % tenant_id)
vponomaryov67b58fe2014-02-06 19:05:41 +0200195 return resp, self._parse_resp(body)
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530196
Dan Smithd6ff6b72012-08-23 10:29:41 -0700197 def get_user_by_username(self, tenant_id, username):
198 resp, users = self.list_users_for_tenant(tenant_id)
199 for user in users:
200 if user['name'] == username:
201 return user
202 raise exceptions.NotFound('No such user')
203
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530204 def create_service(self, name, type, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500205 """Create a service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530206 post_body = {
Zhongyue Luoa1343de2013-01-04 16:21:35 +0800207 'name': name,
208 'type': type,
209 'description': kwargs.get('description')
210 }
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530211 post_body = json.dumps({'OS-KSADM:service': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200212 resp, body = self.post('/OS-KSADM/services', post_body)
213 return resp, self._parse_resp(body)
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530214
215 def get_service(self, service_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500216 """Get Service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530217 url = '/OS-KSADM/services/%s' % service_id
218 resp, body = self.get(url)
vponomaryov67b58fe2014-02-06 19:05:41 +0200219 return resp, self._parse_resp(body)
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530220
umamohanb51ad002013-01-24 18:13:15 +0000221 def list_services(self):
222 """List Service - Returns Services."""
Tushar Kalraa76929c2014-03-31 12:23:07 -0700223 resp, body = self.get('/OS-KSADM/services')
vponomaryov67b58fe2014-02-06 19:05:41 +0200224 return resp, self._parse_resp(body)
umamohanb51ad002013-01-24 18:13:15 +0000225
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530226 def delete_service(self, service_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500227 """Delete Service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530228 url = '/OS-KSADM/services/%s' % service_id
229 return self.delete(url)
230
Abhijeet.Jainff5c3542014-05-06 16:07:30 +0530231 def update_user_password(self, user_id, new_pass):
232 """Update User Password."""
233 put_body = {
234 'password': new_pass,
235 'id': user_id
236 }
237 put_body = json.dumps({'user': put_body})
238 resp, body = self.put('users/%s/OS-KSADM/password' % user_id, put_body)
239 return resp, self._parse_resp(body)
240
Abhijeet.Jain3f49b842014-05-20 12:06:20 +0530241 def list_extensions(self):
242 """List all the extensions."""
243 resp, body = self.get('/extensions')
244 body = json.loads(body)
245 return resp, body['extensions']['values']
246
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700247
vponomaryov67b58fe2014-02-06 19:05:41 +0200248class TokenClientJSON(IdentityClientJSON):
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700249
Matthew Treinish684d8992014-01-30 16:27:40 +0000250 def __init__(self):
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000251 super(TokenClientJSON, self).__init__(None)
Matthew Treinish684d8992014-01-30 16:27:40 +0000252 auth_url = CONF.identity.uri
Jay Pipes7c88eb22013-01-16 21:32:43 -0500253
Jay Pipes7c88eb22013-01-16 21:32:43 -0500254 # Normalize URI to ensure /tokens is in it.
255 if 'tokens' not in auth_url:
256 auth_url = auth_url.rstrip('/') + '/tokens'
257
258 self.auth_url = auth_url
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700259
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500260 def auth(self, user, password, tenant=None):
Zhongyue Luo30a563f2012-09-30 23:43:50 +0900261 creds = {
262 'auth': {
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700263 'passwordCredentials': {
264 'username': user,
265 'password': password,
266 },
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700267 }
268 }
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500269
270 if tenant:
271 creds['auth']['tenantName'] = tenant
272
273 body = json.dumps(creds)
274 resp, body = self.post(self.auth_url, body=body)
275
276 return resp, body['access']
277
278 def auth_token(self, token_id, tenant=None):
279 creds = {
280 'auth': {
281 'token': {
282 'id': token_id,
283 },
284 }
285 }
286
287 if tenant:
288 creds['auth']['tenantName'] = tenant
289
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700290 body = json.dumps(creds)
vponomaryov67b58fe2014-02-06 19:05:41 +0200291 resp, body = self.post(self.auth_url, body=body)
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000292
293 return resp, body['access']
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700294
Sergey Murashov4fccd322014-03-22 09:58:52 +0400295 def request(self, method, url, extra_headers=False, headers=None,
296 body=None):
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700297 """A simple HTTP request interface."""
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800298 if headers is None:
vponomaryov67b58fe2014-02-06 19:05:41 +0200299 # Always accept 'json', for TokenClientXML too.
300 # Because XML response is not easily
301 # converted to the corresponding JSON one
302 headers = self.get_headers(accept_type="json")
Sergey Murashov4fccd322014-03-22 09:58:52 +0400303 elif extra_headers:
304 try:
305 headers.update(self.get_headers(accept_type="json"))
306 except (ValueError, TypeError):
307 headers = self.get_headers(accept_type="json")
308
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700309 resp, resp_body = self.http_obj.request(url, method,
310 headers=headers, body=body)
Sean Dague89a85912014-03-19 16:37:29 -0400311 self._log_request(method, url, resp)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700312
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000313 if resp.status in [401, 403]:
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700314 resp_body = json.loads(resp_body)
315 raise exceptions.Unauthorized(resp_body['error']['message'])
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000316 elif resp.status not in [200, 201]:
317 raise exceptions.IdentityError(
318 'Unexpected status code {0}'.format(resp.status))
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700319
vponomaryov67b58fe2014-02-06 19:05:41 +0200320 if isinstance(resp_body, str):
321 resp_body = json.loads(resp_body)
322 return resp, resp_body
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700323
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000324 def get_token(self, user, password, tenant, auth_data=False):
325 """
326 Returns (token id, token data) for supplied credentials
327 """
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700328 resp, body = self.auth(user, password, tenant)
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000329
330 if auth_data:
331 return body['token']['id'], body
332 else:
333 return body['token']['id']