blob: 9d44826711eb5cd1a1289e7e283181407ee24fca [file] [log] [blame]
Vincent Hou6b8a7b72012-08-25 01:24:33 +08001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2#
Kurt Taylor6a6f5be2013-04-02 18:53:47 -04003# Copyright 2012 IBM Corp.
Vincent Hou6b8a7b72012-08-25 01:24:33 +08004# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
Matthew Treinisha83a16e2012-12-07 13:44:02 -050018import json
Matthew Treinisha83a16e2012-12-07 13:44:02 -050019
Vincent Hou6b8a7b72012-08-25 01:24:33 +080020from lxml import etree
Matthew Treinisha83a16e2012-12-07 13:44:02 -050021
Mate Lakat23a58a32013-08-23 02:06:22 +010022from tempest.common import http
Vincent Hou6b8a7b72012-08-25 01:24:33 +080023from tempest.common.rest_client import RestClientXML
Matthew Treinisha83a16e2012-12-07 13:44:02 -050024from tempest import exceptions
dwallecke62b9f02012-10-10 23:34:42 -050025from tempest.services.compute.xml.common import Document
26from tempest.services.compute.xml.common import Element
dwallecke62b9f02012-10-10 23:34:42 -050027from tempest.services.compute.xml.common import xml_to_json
Matthew Treinisha83a16e2012-12-07 13:44:02 -050028
Vincent Hou6b8a7b72012-08-25 01:24:33 +080029
30XMLNS = "http://docs.openstack.org/identity/api/v2.0"
31
32
Attila Fazekas407b6db2013-01-19 12:48:36 +010033class IdentityClientXML(RestClientXML):
Vincent Hou6b8a7b72012-08-25 01:24:33 +080034
35 def __init__(self, config, username, password, auth_url, tenant_name=None):
Attila Fazekas407b6db2013-01-19 12:48:36 +010036 super(IdentityClientXML, self).__init__(config, username, password,
37 auth_url, tenant_name)
Vincent Hou6b8a7b72012-08-25 01:24:33 +080038 self.service = self.config.identity.catalog_type
39 self.endpoint_url = 'adminURL'
40
41 def _parse_array(self, node):
42 array = []
43 for child in node.getchildren():
44 array.append(xml_to_json(child))
45 return array
46
47 def _parse_body(self, body):
Attila Fazekas7b487be2013-02-12 11:14:41 +010048 data = xml_to_json(body)
49 return data
Vincent Hou6b8a7b72012-08-25 01:24:33 +080050
51 def has_admin_extensions(self):
52 """
53 Returns True if the KSADM Admin Extensions are supported
54 False otherwise
55 """
56 if hasattr(self, '_has_admin_extensions'):
57 return self._has_admin_extensions
58 resp, body = self.list_roles()
59 self._has_admin_extensions = ('status' in resp and resp.status != 503)
60 return self._has_admin_extensions
61
62 def create_role(self, name):
Sean Daguef237ccb2013-01-04 15:19:14 -050063 """Create a role."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +080064 create_role = Element("role", xmlns=XMLNS, name=name)
Vincent Hou6b8a7b72012-08-25 01:24:33 +080065 resp, body = self.post('OS-KSADM/roles', str(Document(create_role)),
Zhongyue Luoe0884a32012-09-25 17:24:17 +080066 self.headers)
Vincent Hou6b8a7b72012-08-25 01:24:33 +080067 body = self._parse_body(etree.fromstring(body))
68 return resp, body
69
70 def create_tenant(self, name, **kwargs):
71 """
72 Create a tenant
73 name (required): New tenant name
74 description: Description of new tenant (default is none)
75 enabled <true|false>: Initial tenant status (default is true)
76 """
77 en = kwargs.get('enabled', 'true')
78 create_tenant = Element("tenant",
79 xmlns=XMLNS,
80 name=name,
81 description=kwargs.get('description', ''),
82 enabled=str(en).lower())
83 resp, body = self.post('tenants', str(Document(create_tenant)),
Zhongyue Luoe0884a32012-09-25 17:24:17 +080084 self.headers)
Vincent Hou6b8a7b72012-08-25 01:24:33 +080085 body = self._parse_body(etree.fromstring(body))
86 return resp, body
87
88 def delete_role(self, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050089 """Delete a role."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +080090 resp, body = self.delete('OS-KSADM/roles/%s' % str(role_id),
91 self.headers)
92 return resp, body
93
94 def list_user_roles(self, tenant_id, user_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050095 """Returns a list of roles assigned to a user for a tenant."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +080096 url = '/tenants/%s/users/%s/roles' % (tenant_id, user_id)
97 resp, body = self.get(url, self.headers)
98 body = self._parse_array(etree.fromstring(body))
99 return resp, body
100
101 def assign_user_role(self, tenant_id, user_id, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500102 """Add roles to a user on a tenant."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800103 resp, body = self.put('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
104 (tenant_id, user_id, role_id), '', self.headers)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800105 body = self._parse_body(etree.fromstring(body))
106 return resp, body
107
108 def remove_user_role(self, tenant_id, user_id, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500109 """Removes a role assignment for a user on a tenant."""
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800110 return self.delete('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
111 (tenant_id, user_id, role_id), self.headers)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800112
113 def delete_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500114 """Delete a tenant."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800115 resp, body = self.delete('tenants/%s' % str(tenant_id), self.headers)
116 return resp, body
117
118 def get_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500119 """Get tenant details."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800120 resp, body = self.get('tenants/%s' % str(tenant_id), self.headers)
121 body = self._parse_body(etree.fromstring(body))
122 return resp, body
123
124 def list_roles(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500125 """Returns roles."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800126 resp, body = self.get('OS-KSADM/roles', self.headers)
127 body = self._parse_array(etree.fromstring(body))
128 return resp, body
129
130 def list_tenants(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500131 """Returns tenants."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800132 resp, body = self.get('tenants', self.headers)
133 body = self._parse_array(etree.fromstring(body))
134 return resp, body
135
Matthew Treinishbf41e102013-01-08 15:56:28 -0500136 def get_tenant_by_name(self, tenant_name):
137 resp, tenants = self.list_tenants()
138 for tenant in tenants:
139 if tenant['name'] == tenant_name:
140 return tenant
141 raise exceptions.NotFound('No such tenant')
142
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800143 def update_tenant(self, tenant_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500144 """Updates a tenant."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800145 resp, body = self.get_tenant(tenant_id)
146 name = kwargs.get('name', body['name'])
147 desc = kwargs.get('description', body['description'])
148 en = kwargs.get('enabled', body['enabled'])
149 update_tenant = Element("tenant",
150 xmlns=XMLNS,
151 id=tenant_id,
152 name=name,
153 description=desc,
154 enabled=str(en).lower())
155
156 resp, body = self.post('tenants/%s' % tenant_id,
157 str(Document(update_tenant)),
158 self.headers)
159 body = self._parse_body(etree.fromstring(body))
160 return resp, body
161
162 def create_user(self, name, password, tenant_id, email):
Sean Daguef237ccb2013-01-04 15:19:14 -0500163 """Create a user."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800164 create_user = Element("user",
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800165 xmlns=XMLNS,
166 name=name,
167 password=password,
168 tenantId=tenant_id,
169 email=email)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800170 resp, body = self.post('users', str(Document(create_user)),
171 self.headers)
172 body = self._parse_body(etree.fromstring(body))
173 return resp, body
174
Chang Bo Guob36b2f12013-09-13 04:52:00 -0700175 def update_user(self, user_id, **kwargs):
176 """Updates a user."""
177 if 'enabled' in kwargs:
178 kwargs['enabled'] = str(kwargs['enabled']).lower()
179 update_user = Element("user", xmlns=XMLNS, **kwargs)
180
181 resp, body = self.put('users/%s' % user_id,
182 str(Document(update_user)),
183 self.headers)
184 body = self._parse_body(etree.fromstring(body))
185 return resp, body
186
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530187 def get_user(self, user_id):
188 """GET a user."""
189 resp, body = self.get("users/%s" % user_id, self.headers)
190 body = self._parse_body(etree.fromstring(body))
191 return resp, body
192
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800193 def delete_user(self, user_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500194 """Delete a user."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800195 resp, body = self.delete("users/%s" % user_id, self.headers)
196 return resp, body
197
198 def get_users(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500199 """Get the list of users."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800200 resp, body = self.get("users", self.headers)
201 body = self._parse_array(etree.fromstring(body))
202 return resp, body
203
204 def enable_disable_user(self, user_id, enabled):
Sean Daguef237ccb2013-01-04 15:19:14 -0500205 """Enables or disables a user."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800206 enable_user = Element("user", enabled=str(enabled).lower())
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800207 resp, body = self.put('users/%s/enabled' % user_id,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800208 str(Document(enable_user)), self.headers)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800209 body = self._parse_array(etree.fromstring(body))
210 return resp, body
211
212 def delete_token(self, token_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500213 """Delete a token."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800214 resp, body = self.delete("tokens/%s" % token_id, self.headers)
215 return resp, body
216
217 def list_users_for_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500218 """List users for a Tenant."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800219 resp, body = self.get('/tenants/%s/users' % tenant_id, self.headers)
220 body = self._parse_array(etree.fromstring(body))
221 return resp, body
222
Matthew Treinishbf41e102013-01-08 15:56:28 -0500223 def get_user_by_username(self, tenant_id, username):
224 resp, users = self.list_users_for_tenant(tenant_id)
225 for user in users:
226 if user['name'] == username:
227 return user
228 raise exceptions.NotFound('No such user')
229
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800230 def create_service(self, name, type, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500231 """Create a service."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800232 OS_KSADM = "http://docs.openstack.org/identity/api/ext/OS-KSADM/v1.0"
233 create_service = Element("service",
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800234 xmlns=OS_KSADM,
235 name=name,
236 type=type,
237 description=kwargs.get('description'))
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800238 resp, body = self.post('OS-KSADM/services',
239 str(Document(create_service)),
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800240 self.headers)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800241 body = self._parse_body(etree.fromstring(body))
242 return resp, body
243
umamohanb51ad002013-01-24 18:13:15 +0000244 def list_services(self):
245 """Returns services."""
246 resp, body = self.get('OS-KSADM/services', self.headers)
247 body = self._parse_array(etree.fromstring(body))
248 return resp, body
249
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800250 def get_service(self, service_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500251 """Get Service."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800252 url = '/OS-KSADM/services/%s' % service_id
253 resp, body = self.get(url, self.headers)
254 body = self._parse_body(etree.fromstring(body))
255 return resp, body
256
257 def delete_service(self, service_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500258 """Delete Service."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800259 url = '/OS-KSADM/services/%s' % service_id
260 return self.delete(url, self.headers)
261
262
263class TokenClientXML(RestClientXML):
264
265 def __init__(self, config):
Jay Pipes7c88eb22013-01-16 21:32:43 -0500266 auth_url = config.identity.uri
267
268 # TODO(jaypipes) Why is this all repeated code in here?
269 # Normalize URI to ensure /tokens is in it.
270 if 'tokens' not in auth_url:
271 auth_url = auth_url.rstrip('/') + '/tokens'
272
273 self.auth_url = auth_url
Jay Pipescd8eaec2013-01-16 21:03:48 -0500274 self.config = config
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800275
276 def auth(self, user, password, tenant):
277 passwordCreds = Element("passwordCredentials",
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800278 username=user,
279 password=password)
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800280 auth = Element("auth", tenantName=tenant)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800281 auth.append(passwordCreds)
282 headers = {'Content-Type': 'application/xml'}
283 resp, body = self.post(self.auth_url, headers=headers,
284 body=str(Document(auth)))
285 return resp, body
286
287 def request(self, method, url, headers=None, body=None):
288 """A simple HTTP request interface."""
Jay Pipescd8eaec2013-01-16 21:03:48 -0500289 dscv = self.config.identity.disable_ssl_certificate_validation
Mate Lakat23a58a32013-08-23 02:06:22 +0100290 self.http_obj = http.ClosingHttp(
291 disable_ssl_certificate_validation=dscv)
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800292 if headers is None:
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800293 headers = {}
Attila Fazekas7b487be2013-02-12 11:14:41 +0100294 self._log_request(method, url, headers, body)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800295 resp, resp_body = self.http_obj.request(url, method,
296 headers=headers, body=body)
Attila Fazekas7b487be2013-02-12 11:14:41 +0100297 self._log_response(resp, resp_body)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800298
299 if resp.status in (401, 403):
300 resp_body = json.loads(resp_body)
301 raise exceptions.Unauthorized(resp_body['error']['message'])
302
303 return resp, resp_body
304
305 def get_token(self, user, password, tenant):
306 resp, body = self.auth(user, password, tenant)
307 if resp['status'] != '202':
308 body = json.loads(body)
309 access = body['access']
310 token = access['token']
311 return token['id']