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 | |
| 18 | import logging |
| 19 | from lxml import etree |
| 20 | from tempest.common.rest_client import RestClient |
| 21 | from tempest.common.rest_client import RestClientXML |
| 22 | from tempest.services.nova.xml.common import Document |
| 23 | from tempest.services.nova.xml.common import Element |
| 24 | from tempest.services.nova.xml.common import Text |
| 25 | from tempest.services.nova.xml.common import xml_to_json |
| 26 | from tempest import exceptions |
| 27 | import httplib2 |
| 28 | import json |
| 29 | |
| 30 | XMLNS = "http://docs.openstack.org/identity/api/v2.0" |
| 31 | |
| 32 | |
| 33 | class AdminClientXML(RestClientXML): |
| 34 | |
| 35 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 36 | super(AdminClientXML, self).__init__(config, username, password, |
| 37 | auth_url, tenant_name) |
| 38 | 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): |
| 48 | json = xml_to_json(body) |
| 49 | return json |
| 50 | |
| 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): |
| 63 | """Create a role""" |
| 64 | create_role = Element("role", |
| 65 | xmlns=XMLNS, |
| 66 | name=name) |
| 67 | resp, body = self.post('OS-KSADM/roles', str(Document(create_role)), |
| 68 | self.headers) |
| 69 | body = self._parse_body(etree.fromstring(body)) |
| 70 | return resp, body |
| 71 | |
| 72 | def create_tenant(self, name, **kwargs): |
| 73 | """ |
| 74 | Create a tenant |
| 75 | name (required): New tenant name |
| 76 | description: Description of new tenant (default is none) |
| 77 | enabled <true|false>: Initial tenant status (default is true) |
| 78 | """ |
| 79 | en = kwargs.get('enabled', 'true') |
| 80 | create_tenant = Element("tenant", |
| 81 | xmlns=XMLNS, |
| 82 | name=name, |
| 83 | description=kwargs.get('description', ''), |
| 84 | enabled=str(en).lower()) |
| 85 | resp, body = self.post('tenants', str(Document(create_tenant)), |
| 86 | self.headers) |
| 87 | body = self._parse_body(etree.fromstring(body)) |
| 88 | return resp, body |
| 89 | |
| 90 | def delete_role(self, role_id): |
| 91 | """Delete a role""" |
| 92 | resp, body = self.delete('OS-KSADM/roles/%s' % str(role_id), |
| 93 | self.headers) |
| 94 | return resp, body |
| 95 | |
| 96 | def list_user_roles(self, tenant_id, user_id): |
| 97 | """Returns a list of roles assigned to a user for a tenant""" |
| 98 | url = '/tenants/%s/users/%s/roles' % (tenant_id, user_id) |
| 99 | resp, body = self.get(url, self.headers) |
| 100 | body = self._parse_array(etree.fromstring(body)) |
| 101 | return resp, body |
| 102 | |
| 103 | def assign_user_role(self, tenant_id, user_id, role_id): |
| 104 | """Add roles to a user on a tenant""" |
| 105 | resp, body = self.put('/tenants/%s/users/%s/roles/OS-KSADM/%s' |
| 106 | % (tenant_id, user_id, role_id), |
| 107 | '', self.headers) |
| 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): |
| 112 | """Removes a role assignment for a user on a tenant""" |
| 113 | return self.delete('/tenants/%s/users/%s/roles/OS-KSADM/%s' |
| 114 | % (tenant_id, user_id, role_id), self.headers) |
| 115 | |
| 116 | def delete_tenant(self, tenant_id): |
| 117 | """Delete a tenant""" |
| 118 | resp, body = self.delete('tenants/%s' % str(tenant_id), self.headers) |
| 119 | return resp, body |
| 120 | |
| 121 | def get_tenant(self, tenant_id): |
| 122 | """Get tenant details""" |
| 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): |
| 128 | """Returns roles""" |
| 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): |
| 134 | """Returns tenants""" |
| 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): |
| 140 | """Updates a tenant""" |
| 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): |
| 159 | """Create a user""" |
| 160 | create_user = Element("user", |
| 161 | xmlns=XMLNS, |
| 162 | name=name, |
| 163 | password=password, |
| 164 | tenantId=tenant_id, |
| 165 | email=email) |
| 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): |
| 172 | """Delete a user""" |
| 173 | resp, body = self.delete("users/%s" % user_id, self.headers) |
| 174 | return resp, body |
| 175 | |
| 176 | def get_users(self): |
| 177 | """Get the list of users""" |
| 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): |
| 183 | """Enables or disables a user""" |
| 184 | enable_user = Element("user", |
| 185 | enabled=str(enabled).lower()) |
| 186 | resp, body = self.put('users/%s/enabled' % user_id, |
| 187 | str(Document(enable_user)), self.headers) |
| 188 | body = self._parse_array(etree.fromstring(body)) |
| 189 | return resp, body |
| 190 | |
| 191 | def delete_token(self, token_id): |
| 192 | """Delete a token""" |
| 193 | resp, body = self.delete("tokens/%s" % token_id, self.headers) |
| 194 | return resp, body |
| 195 | |
| 196 | def list_users_for_tenant(self, tenant_id): |
| 197 | """List users for a Tenant""" |
| 198 | resp, body = self.get('/tenants/%s/users' % tenant_id, self.headers) |
| 199 | body = self._parse_array(etree.fromstring(body)) |
| 200 | return resp, body |
| 201 | |
| 202 | def create_service(self, name, type, **kwargs): |
| 203 | """Create a service""" |
| 204 | OS_KSADM = "http://docs.openstack.org/identity/api/ext/OS-KSADM/v1.0" |
| 205 | create_service = Element("service", |
| 206 | xmlns=OS_KSADM, |
| 207 | name=name, |
| 208 | type=type, |
| 209 | description=kwargs.get('description')) |
| 210 | resp, body = self.post('OS-KSADM/services', |
| 211 | str(Document(create_service)), |
| 212 | self.headers) |
| 213 | body = self._parse_body(etree.fromstring(body)) |
| 214 | return resp, body |
| 215 | |
| 216 | def get_service(self, service_id): |
| 217 | """Get Service""" |
| 218 | url = '/OS-KSADM/services/%s' % service_id |
| 219 | resp, body = self.get(url, self.headers) |
| 220 | body = self._parse_body(etree.fromstring(body)) |
| 221 | return resp, body |
| 222 | |
| 223 | def delete_service(self, service_id): |
| 224 | """Delete Service""" |
| 225 | url = '/OS-KSADM/services/%s' % service_id |
| 226 | return self.delete(url, self.headers) |
| 227 | |
| 228 | |
| 229 | class TokenClientXML(RestClientXML): |
| 230 | |
| 231 | def __init__(self, config): |
| 232 | self.auth_url = config.identity.auth_url |
| 233 | |
| 234 | def auth(self, user, password, tenant): |
| 235 | passwordCreds = Element("passwordCredentials", |
| 236 | username=user, |
| 237 | password=password) |
| 238 | auth = Element("auth", |
| 239 | tenantName=tenant) |
| 240 | auth.append(passwordCreds) |
| 241 | headers = {'Content-Type': 'application/xml'} |
| 242 | resp, body = self.post(self.auth_url, headers=headers, |
| 243 | body=str(Document(auth))) |
| 244 | return resp, body |
| 245 | |
| 246 | def request(self, method, url, headers=None, body=None): |
| 247 | """A simple HTTP request interface.""" |
| 248 | self.http_obj = httplib2.Http() |
Zhongyue Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame^] | 249 | if headers is None: |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 250 | headers = {} |
| 251 | |
| 252 | resp, resp_body = self.http_obj.request(url, method, |
| 253 | headers=headers, body=body) |
| 254 | |
| 255 | if resp.status in (401, 403): |
| 256 | resp_body = json.loads(resp_body) |
| 257 | raise exceptions.Unauthorized(resp_body['error']['message']) |
| 258 | |
| 259 | return resp, resp_body |
| 260 | |
| 261 | def get_token(self, user, password, tenant): |
| 262 | resp, body = self.auth(user, password, tenant) |
| 263 | if resp['status'] != '202': |
| 264 | body = json.loads(body) |
| 265 | access = body['access'] |
| 266 | token = access['token'] |
| 267 | return token['id'] |