Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 1 | # 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 Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 18 | import httplib2 |
| 19 | import json |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 20 | import logging |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 21 | |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 22 | from lxml import etree |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 23 | |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 24 | from tempest.common.rest_client import RestClient |
| 25 | from tempest.common.rest_client import RestClientXML |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 26 | from tempest import exceptions |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 27 | from tempest.services.compute.xml.common import Document |
| 28 | from tempest.services.compute.xml.common import Element |
| 29 | from tempest.services.compute.xml.common import Text |
| 30 | from tempest.services.compute.xml.common import xml_to_json |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 31 | |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 32 | |
| 33 | XMLNS = "http://docs.openstack.org/identity/api/v2.0" |
| 34 | |
| 35 | |
| 36 | class AdminClientXML(RestClientXML): |
| 37 | |
| 38 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 39 | super(AdminClientXML, self).__init__(config, username, password, |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 40 | auth_url, tenant_name) |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 41 | 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 Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 66 | """Create a role.""" |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 67 | create_role = Element("role", xmlns=XMLNS, name=name) |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 68 | resp, body = self.post('OS-KSADM/roles', str(Document(create_role)), |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 69 | self.headers) |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 70 | 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 Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 87 | self.headers) |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 88 | body = self._parse_body(etree.fromstring(body)) |
| 89 | return resp, body |
| 90 | |
| 91 | def delete_role(self, role_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 92 | """Delete a role.""" |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 93 | 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 Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 98 | """Returns a list of roles assigned to a user for a tenant.""" |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 99 | 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 Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 105 | """Add roles to a user on a tenant.""" |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 106 | resp, body = self.put('/tenants/%s/users/%s/roles/OS-KSADM/%s' % |
| 107 | (tenant_id, user_id, role_id), '', self.headers) |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 108 | 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 Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 112 | """Removes a role assignment for a user on a tenant.""" |
Zhongyue Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame] | 113 | return self.delete('/tenants/%s/users/%s/roles/OS-KSADM/%s' % |
| 114 | (tenant_id, user_id, role_id), self.headers) |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 115 | |
| 116 | def delete_tenant(self, tenant_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 117 | """Delete a tenant.""" |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 118 | resp, body = self.delete('tenants/%s' % str(tenant_id), self.headers) |
| 119 | return resp, body |
| 120 | |
| 121 | def get_tenant(self, tenant_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 122 | """Get tenant details.""" |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 123 | 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 Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 128 | """Returns roles.""" |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 129 | 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 Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 134 | """Returns tenants.""" |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 135 | 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 Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 140 | """Updates a tenant.""" |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 141 | 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 Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 159 | """Create a user.""" |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 160 | create_user = Element("user", |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 161 | xmlns=XMLNS, |
| 162 | name=name, |
| 163 | password=password, |
| 164 | tenantId=tenant_id, |
| 165 | email=email) |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 166 | 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 Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 172 | """Delete a user.""" |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 173 | resp, body = self.delete("users/%s" % user_id, self.headers) |
| 174 | return resp, body |
| 175 | |
| 176 | def get_users(self): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 177 | """Get the list of users.""" |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 178 | 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 Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 183 | """Enables or disables a user.""" |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 184 | enable_user = Element("user", enabled=str(enabled).lower()) |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 185 | resp, body = self.put('users/%s/enabled' % user_id, |
Zhongyue Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame] | 186 | str(Document(enable_user)), self.headers) |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 187 | body = self._parse_array(etree.fromstring(body)) |
| 188 | return resp, body |
| 189 | |
| 190 | def delete_token(self, token_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 191 | """Delete a token.""" |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 192 | 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 Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 196 | """List users for a Tenant.""" |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 197 | 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 Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 202 | """Create a service.""" |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 203 | OS_KSADM = "http://docs.openstack.org/identity/api/ext/OS-KSADM/v1.0" |
| 204 | create_service = Element("service", |
Zhongyue Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame] | 205 | xmlns=OS_KSADM, |
| 206 | name=name, |
| 207 | type=type, |
| 208 | description=kwargs.get('description')) |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 209 | resp, body = self.post('OS-KSADM/services', |
| 210 | str(Document(create_service)), |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 211 | self.headers) |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 212 | body = self._parse_body(etree.fromstring(body)) |
| 213 | return resp, body |
| 214 | |
| 215 | def get_service(self, service_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 216 | """Get Service.""" |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 217 | 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 Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 223 | """Delete Service.""" |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 224 | url = '/OS-KSADM/services/%s' % service_id |
| 225 | return self.delete(url, self.headers) |
| 226 | |
| 227 | |
| 228 | class 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 Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame] | 235 | username=user, |
| 236 | password=password) |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 237 | auth = Element("auth", tenantName=tenant) |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 238 | 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 Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame] | 247 | if headers is None: |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 248 | 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'] |