blob: c95faaaa993f0445086ef17a1fb2d0cd040764aa [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
30 self.list_tags = ["roles", "tenants", "users", "services"]
31
chris fattarsi8ed39ac2012-04-30 14:11:27 -070032 def has_admin_extensions(self):
33 """
34 Returns True if the KSADM Admin Extensions are supported
35 False otherwise
36 """
37 if hasattr(self, '_has_admin_extensions'):
38 return self._has_admin_extensions
39 resp, body = self.list_roles()
40 self._has_admin_extensions = ('status' in resp and resp.status != 503)
41 return self._has_admin_extensions
42
43 def create_role(self, name):
Sean Daguef237ccb2013-01-04 15:19:14 -050044 """Create a role."""
chris fattarsi8ed39ac2012-04-30 14:11:27 -070045 post_body = {
46 'name': name,
47 }
48 post_body = json.dumps({'role': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +020049 resp, body = self.post('OS-KSADM/roles', post_body)
50 return resp, self._parse_resp(body)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070051
Gong Zhangcb6b8862014-02-20 15:14:05 +080052 def get_role(self, role_id):
53 """Get a role by its id."""
54 resp, body = self.get('OS-KSADM/roles/%s' % role_id)
55 body = json.loads(body)
56 return resp, body['role']
57
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070058 def create_tenant(self, name, **kwargs):
59 """
60 Create a tenant
61 name (required): New tenant name
62 description: Description of new tenant (default is none)
63 enabled <true|false>: Initial tenant status (default is true)
64 """
65 post_body = {
66 'name': name,
67 'description': kwargs.get('description', ''),
Gordon Chungad873602013-02-18 19:26:27 -050068 'enabled': kwargs.get('enabled', True),
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070069 }
70 post_body = json.dumps({'tenant': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +020071 resp, body = self.post('tenants', post_body)
72 return resp, self._parse_resp(body)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070073
chris fattarsi8ed39ac2012-04-30 14:11:27 -070074 def delete_role(self, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050075 """Delete a role."""
vponomaryov67b58fe2014-02-06 19:05:41 +020076 return self.delete('OS-KSADM/roles/%s' % str(role_id))
chris fattarsi8ed39ac2012-04-30 14:11:27 -070077
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053078 def list_user_roles(self, tenant_id, user_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050079 """Returns a list of roles assigned to a user for a tenant."""
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053080 url = '/tenants/%s/users/%s/roles' % (tenant_id, user_id)
81 resp, body = self.get(url)
vponomaryov67b58fe2014-02-06 19:05:41 +020082 return resp, self._parse_resp(body)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070083
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053084 def assign_user_role(self, tenant_id, user_id, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050085 """Add roles to a user on a tenant."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +080086 resp, body = self.put('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
vponomaryov67b58fe2014-02-06 19:05:41 +020087 (tenant_id, user_id, role_id), "")
88 return resp, self._parse_resp(body)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070089
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053090 def remove_user_role(self, tenant_id, user_id, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050091 """Removes a role assignment for a user on a tenant."""
Zhongyue Luo79d8d362012-09-25 13:49:27 +080092 return self.delete('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
93 (tenant_id, user_id, role_id))
Rohit Karajgi69e80a02012-05-15 03:54:04 -070094
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070095 def delete_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050096 """Delete a tenant."""
vponomaryov67b58fe2014-02-06 19:05:41 +020097 return self.delete('tenants/%s' % str(tenant_id))
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070098
99 def get_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500100 """Get tenant details."""
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700101 resp, body = self.get('tenants/%s' % str(tenant_id))
vponomaryov67b58fe2014-02-06 19:05:41 +0200102 return resp, self._parse_resp(body)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700103
chris fattarsi8ed39ac2012-04-30 14:11:27 -0700104 def list_roles(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500105 """Returns roles."""
chris fattarsi8ed39ac2012-04-30 14:11:27 -0700106 resp, body = self.get('OS-KSADM/roles')
vponomaryov67b58fe2014-02-06 19:05:41 +0200107 return resp, self._parse_resp(body)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700108
109 def list_tenants(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500110 """Returns tenants."""
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700111 resp, body = self.get('tenants')
112 body = json.loads(body)
113 return resp, body['tenants']
114
Dan Smithd6ff6b72012-08-23 10:29:41 -0700115 def get_tenant_by_name(self, tenant_name):
116 resp, tenants = self.list_tenants()
117 for tenant in tenants:
118 if tenant['name'] == tenant_name:
119 return tenant
120 raise exceptions.NotFound('No such tenant')
121
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700122 def update_tenant(self, tenant_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500123 """Updates a tenant."""
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700124 resp, body = self.get_tenant(tenant_id)
125 name = kwargs.get('name', body['name'])
126 desc = kwargs.get('description', body['description'])
127 en = kwargs.get('enabled', body['enabled'])
128 post_body = {
129 'id': tenant_id,
130 'name': name,
131 'description': desc,
132 'enabled': en,
133 }
134 post_body = json.dumps({'tenant': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200135 resp, body = self.post('tenants/%s' % tenant_id, post_body)
136 return resp, self._parse_resp(body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700137
huangtianhuafc8db4f2013-10-08 12:05:58 +0800138 def create_user(self, name, password, tenant_id, email, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500139 """Create a user."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700140 post_body = {
141 'name': name,
142 'password': password,
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700143 'email': email
144 }
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500145 if tenant_id is not None:
146 post_body['tenantId'] = tenant_id
huangtianhuafc8db4f2013-10-08 12:05:58 +0800147 if kwargs.get('enabled') is not None:
148 post_body['enabled'] = kwargs.get('enabled')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700149 post_body = json.dumps({'user': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200150 resp, body = self.post('users', post_body)
151 return resp, self._parse_resp(body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700152
Chang Bo Guob36b2f12013-09-13 04:52:00 -0700153 def update_user(self, user_id, **kwargs):
154 """Updates a user."""
155 put_body = json.dumps({'user': kwargs})
vponomaryov67b58fe2014-02-06 19:05:41 +0200156 resp, body = self.put('users/%s' % user_id, put_body)
157 return resp, self._parse_resp(body)
Chang Bo Guob36b2f12013-09-13 04:52:00 -0700158
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530159 def get_user(self, user_id):
160 """GET a user."""
161 resp, body = self.get("users/%s" % user_id)
vponomaryov67b58fe2014-02-06 19:05:41 +0200162 return resp, self._parse_resp(body)
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530163
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700164 def delete_user(self, user_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500165 """Delete a user."""
vponomaryov67b58fe2014-02-06 19:05:41 +0200166 return self.delete("users/%s" % user_id)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700167
168 def get_users(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500169 """Get the list of users."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700170 resp, body = self.get("users")
vponomaryov67b58fe2014-02-06 19:05:41 +0200171 return resp, self._parse_resp(body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700172
173 def enable_disable_user(self, user_id, enabled):
Sean Daguef237ccb2013-01-04 15:19:14 -0500174 """Enables or disables a user."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700175 put_body = {
Sean Dague14c68182013-04-14 15:34:30 -0400176 'enabled': enabled
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700177 }
178 put_body = json.dumps({'user': put_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200179 resp, body = self.put('users/%s/enabled' % user_id, put_body)
180 return resp, self._parse_resp(body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700181
Zhi Kun Liu30caeae2014-02-26 15:30:24 +0800182 def get_token(self, token_id):
183 """Get token details."""
184 resp, body = self.get("tokens/%s" % token_id)
185 return resp, self._parse_resp(body)
186
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700187 def delete_token(self, token_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500188 """Delete a token."""
vponomaryov67b58fe2014-02-06 19:05:41 +0200189 return self.delete("tokens/%s" % token_id)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700190
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530191 def list_users_for_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500192 """List users for a Tenant."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530193 resp, body = self.get('/tenants/%s/users' % tenant_id)
vponomaryov67b58fe2014-02-06 19:05:41 +0200194 return resp, self._parse_resp(body)
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530195
Dan Smithd6ff6b72012-08-23 10:29:41 -0700196 def get_user_by_username(self, tenant_id, username):
197 resp, users = self.list_users_for_tenant(tenant_id)
198 for user in users:
199 if user['name'] == username:
200 return user
201 raise exceptions.NotFound('No such user')
202
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530203 def create_service(self, name, type, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500204 """Create a service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530205 post_body = {
Zhongyue Luoa1343de2013-01-04 16:21:35 +0800206 'name': name,
207 'type': type,
208 'description': kwargs.get('description')
209 }
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530210 post_body = json.dumps({'OS-KSADM:service': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200211 resp, body = self.post('/OS-KSADM/services', post_body)
212 return resp, self._parse_resp(body)
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530213
214 def get_service(self, service_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500215 """Get Service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530216 url = '/OS-KSADM/services/%s' % service_id
217 resp, body = self.get(url)
vponomaryov67b58fe2014-02-06 19:05:41 +0200218 return resp, self._parse_resp(body)
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530219
umamohanb51ad002013-01-24 18:13:15 +0000220 def list_services(self):
221 """List Service - Returns Services."""
222 resp, body = self.get('/OS-KSADM/services/')
vponomaryov67b58fe2014-02-06 19:05:41 +0200223 return resp, self._parse_resp(body)
umamohanb51ad002013-01-24 18:13:15 +0000224
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530225 def delete_service(self, service_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500226 """Delete Service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530227 url = '/OS-KSADM/services/%s' % service_id
228 return self.delete(url)
229
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700230
vponomaryov67b58fe2014-02-06 19:05:41 +0200231class TokenClientJSON(IdentityClientJSON):
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700232
Matthew Treinish684d8992014-01-30 16:27:40 +0000233 def __init__(self):
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000234 super(TokenClientJSON, self).__init__(None)
Matthew Treinish684d8992014-01-30 16:27:40 +0000235 auth_url = CONF.identity.uri
Jay Pipes7c88eb22013-01-16 21:32:43 -0500236
Jay Pipes7c88eb22013-01-16 21:32:43 -0500237 # Normalize URI to ensure /tokens is in it.
238 if 'tokens' not in auth_url:
239 auth_url = auth_url.rstrip('/') + '/tokens'
240
241 self.auth_url = auth_url
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700242
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500243 def auth(self, user, password, tenant=None):
Zhongyue Luo30a563f2012-09-30 23:43:50 +0900244 creds = {
245 'auth': {
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700246 'passwordCredentials': {
247 'username': user,
248 'password': password,
249 },
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700250 }
251 }
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500252
253 if tenant:
254 creds['auth']['tenantName'] = tenant
255
256 body = json.dumps(creds)
257 resp, body = self.post(self.auth_url, body=body)
258
259 return resp, body['access']
260
261 def auth_token(self, token_id, tenant=None):
262 creds = {
263 'auth': {
264 'token': {
265 'id': token_id,
266 },
267 }
268 }
269
270 if tenant:
271 creds['auth']['tenantName'] = tenant
272
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700273 body = json.dumps(creds)
vponomaryov67b58fe2014-02-06 19:05:41 +0200274 resp, body = self.post(self.auth_url, body=body)
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000275
276 return resp, body['access']
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700277
278 def request(self, method, url, headers=None, body=None):
279 """A simple HTTP request interface."""
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800280 if headers is None:
vponomaryov67b58fe2014-02-06 19:05:41 +0200281 # Always accept 'json', for TokenClientXML too.
282 # Because XML response is not easily
283 # converted to the corresponding JSON one
284 headers = self.get_headers(accept_type="json")
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700285 resp, resp_body = self.http_obj.request(url, method,
286 headers=headers, body=body)
Sean Dague89a85912014-03-19 16:37:29 -0400287 self._log_request(method, url, resp)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700288
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000289 if resp.status in [401, 403]:
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700290 resp_body = json.loads(resp_body)
291 raise exceptions.Unauthorized(resp_body['error']['message'])
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000292 elif resp.status not in [200, 201]:
293 raise exceptions.IdentityError(
294 'Unexpected status code {0}'.format(resp.status))
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700295
vponomaryov67b58fe2014-02-06 19:05:41 +0200296 if isinstance(resp_body, str):
297 resp_body = json.loads(resp_body)
298 return resp, resp_body
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700299
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000300 def get_token(self, user, password, tenant, auth_data=False):
301 """
302 Returns (token id, token data) for supplied credentials
303 """
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700304 resp, body = self.auth(user, password, tenant)
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000305
306 if auth_data:
307 return body['token']['id'], body
308 else:
309 return body['token']['id']