blob: 9a31540bb515f08aad1c66eb16b79a2937d97207 [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
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070052 def create_tenant(self, name, **kwargs):
53 """
54 Create a tenant
55 name (required): New tenant name
56 description: Description of new tenant (default is none)
57 enabled <true|false>: Initial tenant status (default is true)
58 """
59 post_body = {
60 'name': name,
61 'description': kwargs.get('description', ''),
Gordon Chungad873602013-02-18 19:26:27 -050062 'enabled': kwargs.get('enabled', True),
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070063 }
64 post_body = json.dumps({'tenant': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +020065 resp, body = self.post('tenants', post_body)
66 return resp, self._parse_resp(body)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070067
chris fattarsi8ed39ac2012-04-30 14:11:27 -070068 def delete_role(self, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050069 """Delete a role."""
vponomaryov67b58fe2014-02-06 19:05:41 +020070 return self.delete('OS-KSADM/roles/%s' % str(role_id))
chris fattarsi8ed39ac2012-04-30 14:11:27 -070071
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053072 def list_user_roles(self, tenant_id, user_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050073 """Returns a list of roles assigned to a user for a tenant."""
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053074 url = '/tenants/%s/users/%s/roles' % (tenant_id, user_id)
75 resp, body = self.get(url)
vponomaryov67b58fe2014-02-06 19:05:41 +020076 return resp, self._parse_resp(body)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070077
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053078 def assign_user_role(self, tenant_id, user_id, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050079 """Add roles to a user on a tenant."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +080080 resp, body = self.put('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
vponomaryov67b58fe2014-02-06 19:05:41 +020081 (tenant_id, user_id, role_id), "")
82 return resp, self._parse_resp(body)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070083
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053084 def remove_user_role(self, tenant_id, user_id, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050085 """Removes a role assignment for a user on a tenant."""
Zhongyue Luo79d8d362012-09-25 13:49:27 +080086 return self.delete('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
87 (tenant_id, user_id, role_id))
Rohit Karajgi69e80a02012-05-15 03:54:04 -070088
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070089 def delete_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050090 """Delete a tenant."""
vponomaryov67b58fe2014-02-06 19:05:41 +020091 return self.delete('tenants/%s' % str(tenant_id))
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070092
93 def get_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050094 """Get tenant details."""
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070095 resp, body = self.get('tenants/%s' % str(tenant_id))
vponomaryov67b58fe2014-02-06 19:05:41 +020096 return resp, self._parse_resp(body)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070097
chris fattarsi8ed39ac2012-04-30 14:11:27 -070098 def list_roles(self):
Sean Daguef237ccb2013-01-04 15:19:14 -050099 """Returns roles."""
chris fattarsi8ed39ac2012-04-30 14:11:27 -0700100 resp, body = self.get('OS-KSADM/roles')
vponomaryov67b58fe2014-02-06 19:05:41 +0200101 return resp, self._parse_resp(body)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700102
103 def list_tenants(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500104 """Returns tenants."""
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700105 resp, body = self.get('tenants')
106 body = json.loads(body)
107 return resp, body['tenants']
108
Dan Smithd6ff6b72012-08-23 10:29:41 -0700109 def get_tenant_by_name(self, tenant_name):
110 resp, tenants = self.list_tenants()
111 for tenant in tenants:
112 if tenant['name'] == tenant_name:
113 return tenant
114 raise exceptions.NotFound('No such tenant')
115
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700116 def update_tenant(self, tenant_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500117 """Updates a tenant."""
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700118 resp, body = self.get_tenant(tenant_id)
119 name = kwargs.get('name', body['name'])
120 desc = kwargs.get('description', body['description'])
121 en = kwargs.get('enabled', body['enabled'])
122 post_body = {
123 'id': tenant_id,
124 'name': name,
125 'description': desc,
126 'enabled': en,
127 }
128 post_body = json.dumps({'tenant': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200129 resp, body = self.post('tenants/%s' % tenant_id, post_body)
130 return resp, self._parse_resp(body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700131
huangtianhuafc8db4f2013-10-08 12:05:58 +0800132 def create_user(self, name, password, tenant_id, email, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500133 """Create a user."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700134 post_body = {
135 'name': name,
136 'password': password,
137 'tenantId': tenant_id,
138 'email': email
139 }
huangtianhuafc8db4f2013-10-08 12:05:58 +0800140 if kwargs.get('enabled') is not None:
141 post_body['enabled'] = kwargs.get('enabled')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700142 post_body = json.dumps({'user': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200143 resp, body = self.post('users', post_body)
144 return resp, self._parse_resp(body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700145
Chang Bo Guob36b2f12013-09-13 04:52:00 -0700146 def update_user(self, user_id, **kwargs):
147 """Updates a user."""
148 put_body = json.dumps({'user': kwargs})
vponomaryov67b58fe2014-02-06 19:05:41 +0200149 resp, body = self.put('users/%s' % user_id, put_body)
150 return resp, self._parse_resp(body)
Chang Bo Guob36b2f12013-09-13 04:52:00 -0700151
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530152 def get_user(self, user_id):
153 """GET a user."""
154 resp, body = self.get("users/%s" % user_id)
vponomaryov67b58fe2014-02-06 19:05:41 +0200155 return resp, self._parse_resp(body)
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530156
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700157 def delete_user(self, user_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500158 """Delete a user."""
vponomaryov67b58fe2014-02-06 19:05:41 +0200159 return self.delete("users/%s" % user_id)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700160
161 def get_users(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500162 """Get the list of users."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700163 resp, body = self.get("users")
vponomaryov67b58fe2014-02-06 19:05:41 +0200164 return resp, self._parse_resp(body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700165
166 def enable_disable_user(self, user_id, enabled):
Sean Daguef237ccb2013-01-04 15:19:14 -0500167 """Enables or disables a user."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700168 put_body = {
Sean Dague14c68182013-04-14 15:34:30 -0400169 'enabled': enabled
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700170 }
171 put_body = json.dumps({'user': put_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200172 resp, body = self.put('users/%s/enabled' % user_id, put_body)
173 return resp, self._parse_resp(body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700174
Zhi Kun Liu30caeae2014-02-26 15:30:24 +0800175 def get_token(self, token_id):
176 """Get token details."""
177 resp, body = self.get("tokens/%s" % token_id)
178 return resp, self._parse_resp(body)
179
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700180 def delete_token(self, token_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500181 """Delete a token."""
vponomaryov67b58fe2014-02-06 19:05:41 +0200182 return self.delete("tokens/%s" % token_id)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700183
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530184 def list_users_for_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500185 """List users for a Tenant."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530186 resp, body = self.get('/tenants/%s/users' % tenant_id)
vponomaryov67b58fe2014-02-06 19:05:41 +0200187 return resp, self._parse_resp(body)
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530188
Dan Smithd6ff6b72012-08-23 10:29:41 -0700189 def get_user_by_username(self, tenant_id, username):
190 resp, users = self.list_users_for_tenant(tenant_id)
191 for user in users:
192 if user['name'] == username:
193 return user
194 raise exceptions.NotFound('No such user')
195
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530196 def create_service(self, name, type, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500197 """Create a service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530198 post_body = {
Zhongyue Luoa1343de2013-01-04 16:21:35 +0800199 'name': name,
200 'type': type,
201 'description': kwargs.get('description')
202 }
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530203 post_body = json.dumps({'OS-KSADM:service': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200204 resp, body = self.post('/OS-KSADM/services', post_body)
205 return resp, self._parse_resp(body)
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530206
207 def get_service(self, service_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500208 """Get Service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530209 url = '/OS-KSADM/services/%s' % service_id
210 resp, body = self.get(url)
vponomaryov67b58fe2014-02-06 19:05:41 +0200211 return resp, self._parse_resp(body)
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530212
umamohanb51ad002013-01-24 18:13:15 +0000213 def list_services(self):
214 """List Service - Returns Services."""
215 resp, body = self.get('/OS-KSADM/services/')
vponomaryov67b58fe2014-02-06 19:05:41 +0200216 return resp, self._parse_resp(body)
umamohanb51ad002013-01-24 18:13:15 +0000217
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530218 def delete_service(self, service_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500219 """Delete Service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530220 url = '/OS-KSADM/services/%s' % service_id
221 return self.delete(url)
222
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700223
vponomaryov67b58fe2014-02-06 19:05:41 +0200224class TokenClientJSON(IdentityClientJSON):
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700225
Matthew Treinish684d8992014-01-30 16:27:40 +0000226 def __init__(self):
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000227 super(TokenClientJSON, self).__init__(None)
Matthew Treinish684d8992014-01-30 16:27:40 +0000228 auth_url = CONF.identity.uri
Jay Pipes7c88eb22013-01-16 21:32:43 -0500229
Jay Pipes7c88eb22013-01-16 21:32:43 -0500230 # Normalize URI to ensure /tokens is in it.
231 if 'tokens' not in auth_url:
232 auth_url = auth_url.rstrip('/') + '/tokens'
233
234 self.auth_url = auth_url
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700235
236 def auth(self, user, password, tenant):
Zhongyue Luo30a563f2012-09-30 23:43:50 +0900237 creds = {
238 'auth': {
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700239 'passwordCredentials': {
240 'username': user,
241 'password': password,
242 },
Zhongyue Luo30a563f2012-09-30 23:43:50 +0900243 'tenantName': tenant,
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700244 }
245 }
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700246 body = json.dumps(creds)
vponomaryov67b58fe2014-02-06 19:05:41 +0200247 resp, body = self.post(self.auth_url, body=body)
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000248
249 return resp, body['access']
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700250
251 def request(self, method, url, headers=None, body=None):
252 """A simple HTTP request interface."""
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800253 if headers is None:
vponomaryov67b58fe2014-02-06 19:05:41 +0200254 # Always accept 'json', for TokenClientXML too.
255 # Because XML response is not easily
256 # converted to the corresponding JSON one
257 headers = self.get_headers(accept_type="json")
Attila Fazekas7b487be2013-02-12 11:14:41 +0100258 self._log_request(method, url, headers, body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700259 resp, resp_body = self.http_obj.request(url, method,
260 headers=headers, body=body)
Attila Fazekas7b487be2013-02-12 11:14:41 +0100261 self._log_response(resp, resp_body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700262
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000263 if resp.status in [401, 403]:
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700264 resp_body = json.loads(resp_body)
265 raise exceptions.Unauthorized(resp_body['error']['message'])
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000266 elif resp.status not in [200, 201]:
267 raise exceptions.IdentityError(
268 'Unexpected status code {0}'.format(resp.status))
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700269
vponomaryov67b58fe2014-02-06 19:05:41 +0200270 if isinstance(resp_body, str):
271 resp_body = json.loads(resp_body)
272 return resp, resp_body
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700273
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000274 def get_token(self, user, password, tenant, auth_data=False):
275 """
276 Returns (token id, token data) for supplied credentials
277 """
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700278 resp, body = self.auth(user, password, tenant)
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000279
280 if auth_data:
281 return body['token']['id'], body
282 else:
283 return body['token']['id']