blob: 8448ae0b3f42c149e08b6faacc9fd2cc5558cd84 [file] [log] [blame]
Vincent Hou6b8a7b72012-08-25 01:24:33 +08001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2#
3# Copyright 2012 IBM
4# 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 httplib2
19import json
Vincent Hou6b8a7b72012-08-25 01:24:33 +080020import logging
Matthew Treinisha83a16e2012-12-07 13:44:02 -050021
Vincent Hou6b8a7b72012-08-25 01:24:33 +080022from lxml import etree
Matthew Treinisha83a16e2012-12-07 13:44:02 -050023
Vincent Hou6b8a7b72012-08-25 01:24:33 +080024from tempest.common.rest_client import RestClient
25from tempest.common.rest_client import RestClientXML
Matthew Treinisha83a16e2012-12-07 13:44:02 -050026from tempest import exceptions
dwallecke62b9f02012-10-10 23:34:42 -050027from tempest.services.compute.xml.common import Document
28from tempest.services.compute.xml.common import Element
29from tempest.services.compute.xml.common import Text
30from tempest.services.compute.xml.common import xml_to_json
Matthew Treinisha83a16e2012-12-07 13:44:02 -050031
Vincent Hou6b8a7b72012-08-25 01:24:33 +080032
33XMLNS = "http://docs.openstack.org/identity/api/v2.0"
34
35
36class AdminClientXML(RestClientXML):
37
38 def __init__(self, config, username, password, auth_url, tenant_name=None):
39 super(AdminClientXML, self).__init__(config, username, password,
Zhongyue Luoe0884a32012-09-25 17:24:17 +080040 auth_url, tenant_name)
Vincent Hou6b8a7b72012-08-25 01:24:33 +080041 self.service = self.config.identity.catalog_type
42 self.endpoint_url = 'adminURL'
43
44 def _parse_array(self, node):
45 array = []
46 for child in node.getchildren():
47 array.append(xml_to_json(child))
48 return array
49
50 def _parse_body(self, body):
51 json = xml_to_json(body)
52 return json
53
54 def has_admin_extensions(self):
55 """
56 Returns True if the KSADM Admin Extensions are supported
57 False otherwise
58 """
59 if hasattr(self, '_has_admin_extensions'):
60 return self._has_admin_extensions
61 resp, body = self.list_roles()
62 self._has_admin_extensions = ('status' in resp and resp.status != 503)
63 return self._has_admin_extensions
64
65 def create_role(self, name):
Sean Daguef237ccb2013-01-04 15:19:14 -050066 """Create a role."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +080067 create_role = Element("role", xmlns=XMLNS, name=name)
Vincent Hou6b8a7b72012-08-25 01:24:33 +080068 resp, body = self.post('OS-KSADM/roles', str(Document(create_role)),
Zhongyue Luoe0884a32012-09-25 17:24:17 +080069 self.headers)
Vincent Hou6b8a7b72012-08-25 01:24:33 +080070 body = self._parse_body(etree.fromstring(body))
71 return resp, body
72
73 def create_tenant(self, name, **kwargs):
74 """
75 Create a tenant
76 name (required): New tenant name
77 description: Description of new tenant (default is none)
78 enabled <true|false>: Initial tenant status (default is true)
79 """
80 en = kwargs.get('enabled', 'true')
81 create_tenant = Element("tenant",
82 xmlns=XMLNS,
83 name=name,
84 description=kwargs.get('description', ''),
85 enabled=str(en).lower())
86 resp, body = self.post('tenants', str(Document(create_tenant)),
Zhongyue Luoe0884a32012-09-25 17:24:17 +080087 self.headers)
Vincent Hou6b8a7b72012-08-25 01:24:33 +080088 body = self._parse_body(etree.fromstring(body))
89 return resp, body
90
91 def delete_role(self, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050092 """Delete a role."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +080093 resp, body = self.delete('OS-KSADM/roles/%s' % str(role_id),
94 self.headers)
95 return resp, body
96
97 def list_user_roles(self, tenant_id, user_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050098 """Returns a list of roles assigned to a user for a tenant."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +080099 url = '/tenants/%s/users/%s/roles' % (tenant_id, user_id)
100 resp, body = self.get(url, self.headers)
101 body = self._parse_array(etree.fromstring(body))
102 return resp, body
103
104 def assign_user_role(self, tenant_id, user_id, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500105 """Add roles to a user on a tenant."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800106 resp, body = self.put('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
107 (tenant_id, user_id, role_id), '', self.headers)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800108 body = self._parse_body(etree.fromstring(body))
109 return resp, body
110
111 def remove_user_role(self, tenant_id, user_id, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500112 """Removes a role assignment for a user on a tenant."""
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800113 return self.delete('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
114 (tenant_id, user_id, role_id), self.headers)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800115
116 def delete_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500117 """Delete a tenant."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800118 resp, body = self.delete('tenants/%s' % str(tenant_id), self.headers)
119 return resp, body
120
121 def get_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500122 """Get tenant details."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800123 resp, body = self.get('tenants/%s' % str(tenant_id), self.headers)
124 body = self._parse_body(etree.fromstring(body))
125 return resp, body
126
127 def list_roles(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500128 """Returns roles."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800129 resp, body = self.get('OS-KSADM/roles', self.headers)
130 body = self._parse_array(etree.fromstring(body))
131 return resp, body
132
133 def list_tenants(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500134 """Returns tenants."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800135 resp, body = self.get('tenants', self.headers)
136 body = self._parse_array(etree.fromstring(body))
137 return resp, body
138
139 def update_tenant(self, tenant_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500140 """Updates a tenant."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800141 resp, body = self.get_tenant(tenant_id)
142 name = kwargs.get('name', body['name'])
143 desc = kwargs.get('description', body['description'])
144 en = kwargs.get('enabled', body['enabled'])
145 update_tenant = Element("tenant",
146 xmlns=XMLNS,
147 id=tenant_id,
148 name=name,
149 description=desc,
150 enabled=str(en).lower())
151
152 resp, body = self.post('tenants/%s' % tenant_id,
153 str(Document(update_tenant)),
154 self.headers)
155 body = self._parse_body(etree.fromstring(body))
156 return resp, body
157
158 def create_user(self, name, password, tenant_id, email):
Sean Daguef237ccb2013-01-04 15:19:14 -0500159 """Create a user."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800160 create_user = Element("user",
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800161 xmlns=XMLNS,
162 name=name,
163 password=password,
164 tenantId=tenant_id,
165 email=email)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800166 resp, body = self.post('users', str(Document(create_user)),
167 self.headers)
168 body = self._parse_body(etree.fromstring(body))
169 return resp, body
170
171 def delete_user(self, user_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500172 """Delete a user."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800173 resp, body = self.delete("users/%s" % user_id, self.headers)
174 return resp, body
175
176 def get_users(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500177 """Get the list of users."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800178 resp, body = self.get("users", self.headers)
179 body = self._parse_array(etree.fromstring(body))
180 return resp, body
181
182 def enable_disable_user(self, user_id, enabled):
Sean Daguef237ccb2013-01-04 15:19:14 -0500183 """Enables or disables a user."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800184 enable_user = Element("user", enabled=str(enabled).lower())
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800185 resp, body = self.put('users/%s/enabled' % user_id,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800186 str(Document(enable_user)), self.headers)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800187 body = self._parse_array(etree.fromstring(body))
188 return resp, body
189
190 def delete_token(self, token_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500191 """Delete a token."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800192 resp, body = self.delete("tokens/%s" % token_id, self.headers)
193 return resp, body
194
195 def list_users_for_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500196 """List users for a Tenant."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800197 resp, body = self.get('/tenants/%s/users' % tenant_id, self.headers)
198 body = self._parse_array(etree.fromstring(body))
199 return resp, body
200
201 def create_service(self, name, type, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500202 """Create a service."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800203 OS_KSADM = "http://docs.openstack.org/identity/api/ext/OS-KSADM/v1.0"
204 create_service = Element("service",
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800205 xmlns=OS_KSADM,
206 name=name,
207 type=type,
208 description=kwargs.get('description'))
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800209 resp, body = self.post('OS-KSADM/services',
210 str(Document(create_service)),
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800211 self.headers)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800212 body = self._parse_body(etree.fromstring(body))
213 return resp, body
214
215 def get_service(self, service_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500216 """Get Service."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800217 url = '/OS-KSADM/services/%s' % service_id
218 resp, body = self.get(url, self.headers)
219 body = self._parse_body(etree.fromstring(body))
220 return resp, body
221
222 def delete_service(self, service_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500223 """Delete Service."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800224 url = '/OS-KSADM/services/%s' % service_id
225 return self.delete(url, self.headers)
226
227
228class TokenClientXML(RestClientXML):
229
230 def __init__(self, config):
231 self.auth_url = config.identity.auth_url
232
233 def auth(self, user, password, tenant):
234 passwordCreds = Element("passwordCredentials",
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800235 username=user,
236 password=password)
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800237 auth = Element("auth", tenantName=tenant)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800238 auth.append(passwordCreds)
239 headers = {'Content-Type': 'application/xml'}
240 resp, body = self.post(self.auth_url, headers=headers,
241 body=str(Document(auth)))
242 return resp, body
243
244 def request(self, method, url, headers=None, body=None):
245 """A simple HTTP request interface."""
246 self.http_obj = httplib2.Http()
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800247 if headers is None:
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800248 headers = {}
249
250 resp, resp_body = self.http_obj.request(url, method,
251 headers=headers, body=body)
252
253 if resp.status in (401, 403):
254 resp_body = json.loads(resp_body)
255 raise exceptions.Unauthorized(resp_body['error']['message'])
256
257 return resp, resp_body
258
259 def get_token(self, user, password, tenant):
260 resp, body = self.auth(user, password, tenant)
261 if resp['status'] != '202':
262 body = json.loads(body)
263 access = body['access']
264 token = access['token']
265 return token['id']