rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | # |
| 3 | # Copyright 2013 OpenStack Foundation |
| 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 | from urlparse import urlparse |
| 19 | |
| 20 | from lxml import etree |
| 21 | |
| 22 | from tempest.common.rest_client import RestClientXML |
| 23 | from tempest.services.compute.xml.common import Document |
| 24 | from tempest.services.compute.xml.common import Element |
nayna-patel | b35f723 | 2013-06-28 07:08:44 +0000 | [diff] [blame] | 25 | from tempest.services.compute.xml.common import Text |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 26 | from tempest.services.compute.xml.common import xml_to_json |
| 27 | |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 28 | XMLNS = "http://docs.openstack.org/identity/api/v3" |
| 29 | |
| 30 | |
| 31 | class IdentityV3ClientXML(RestClientXML): |
| 32 | |
| 33 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 34 | super(IdentityV3ClientXML, self).__init__(config, username, password, |
| 35 | auth_url, tenant_name) |
| 36 | self.service = self.config.identity.catalog_type |
| 37 | self.endpoint_url = 'adminURL' |
| 38 | |
| 39 | def _parse_projects(self, node): |
| 40 | array = [] |
| 41 | for child in node.getchildren(): |
| 42 | tag_list = child.tag.split('}', 1) |
| 43 | if tag_list[1] == "project": |
| 44 | array.append(xml_to_json(child)) |
| 45 | return array |
| 46 | |
nayna-patel | 4df72dc | 2013-05-29 10:27:24 +0000 | [diff] [blame] | 47 | def _parse_domains(self, node): |
| 48 | array = [] |
| 49 | for child in node.getchildren(): |
| 50 | tag_list = child.tag.split('}', 1) |
| 51 | if tag_list[1] == "domain": |
| 52 | array.append(xml_to_json(child)) |
| 53 | return array |
| 54 | |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 55 | def _parse_roles(self, node): |
| 56 | array = [] |
| 57 | for child in node.getchildren(): |
| 58 | tag_list = child.tag.split('}', 1) |
| 59 | if tag_list[1] == "role": |
| 60 | array.append(xml_to_json(child)) |
| 61 | return array |
| 62 | |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 63 | def _parse_array(self, node): |
| 64 | array = [] |
| 65 | for child in node.getchildren(): |
| 66 | array.append(xml_to_json(child)) |
| 67 | return array |
| 68 | |
| 69 | def _parse_body(self, body): |
| 70 | json = xml_to_json(body) |
| 71 | return json |
| 72 | |
| 73 | def request(self, method, url, headers=None, body=None, wait=None): |
| 74 | """Overriding the existing HTTP request in super class RestClient.""" |
| 75 | self._set_auth() |
| 76 | self.base_url = self.base_url.replace(urlparse(self.base_url).path, |
| 77 | "/v3") |
| 78 | return super(IdentityV3ClientXML, self).request(method, url, |
| 79 | headers=headers, |
| 80 | body=body) |
| 81 | |
| 82 | def create_user(self, user_name, **kwargs): |
| 83 | """Creates a user.""" |
| 84 | password = kwargs.get('password', None) |
| 85 | email = kwargs.get('email', None) |
| 86 | en = kwargs.get('enabled', 'true') |
| 87 | project_id = kwargs.get('project_id', None) |
| 88 | description = kwargs.get('description', None) |
| 89 | domain_id = kwargs.get('domain_id', 'default') |
| 90 | post_body = Element("user", |
| 91 | xmlns=XMLNS, |
| 92 | name=user_name, |
| 93 | password=password, |
| 94 | description=description, |
| 95 | email=email, |
| 96 | enabled=str(en).lower(), |
| 97 | project_id=project_id, |
| 98 | domain_id=domain_id) |
| 99 | resp, body = self.post('users', str(Document(post_body)), |
| 100 | self.headers) |
| 101 | body = self._parse_body(etree.fromstring(body)) |
| 102 | return resp, body |
| 103 | |
| 104 | def update_user(self, user_id, name, **kwargs): |
| 105 | """Updates a user.""" |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 106 | resp, body = self.get_user(user_id) |
| 107 | email = kwargs.get('email', body['email']) |
| 108 | en = kwargs.get('enabled', body['enabled']) |
| 109 | project_id = kwargs.get('project_id', body['project_id']) |
| 110 | description = kwargs.get('description', body['description']) |
| 111 | domain_id = kwargs.get('domain_id', body['domain_id']) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 112 | update_user = Element("user", |
| 113 | xmlns=XMLNS, |
| 114 | name=name, |
| 115 | email=email, |
| 116 | project_id=project_id, |
| 117 | domain_id=domain_id, |
| 118 | description=description, |
| 119 | enabled=str(en).lower()) |
| 120 | resp, body = self.patch('users/%s' % user_id, |
| 121 | str(Document(update_user)), |
| 122 | self.headers) |
| 123 | body = self._parse_body(etree.fromstring(body)) |
| 124 | return resp, body |
| 125 | |
| 126 | def list_user_projects(self, user_id): |
| 127 | """Lists the projects on which a user has roles assigned.""" |
| 128 | resp, body = self.get('users/%s/projects' % user_id, self.headers) |
| 129 | body = self._parse_projects(etree.fromstring(body)) |
| 130 | return resp, body |
| 131 | |
| 132 | def get_users(self): |
| 133 | """Get the list of users.""" |
| 134 | resp, body = self.get("users", self.headers) |
| 135 | body = self._parse_array(etree.fromstring(body)) |
| 136 | return resp, body |
| 137 | |
| 138 | def get_user(self, user_id): |
| 139 | """GET a user.""" |
| 140 | resp, body = self.get("users/%s" % user_id, self.headers) |
| 141 | body = self._parse_body(etree.fromstring(body)) |
| 142 | return resp, body |
| 143 | |
| 144 | def delete_user(self, user_id): |
| 145 | """Deletes a User.""" |
| 146 | resp, body = self.delete("users/%s" % user_id, self.headers) |
| 147 | return resp, body |
| 148 | |
| 149 | def create_project(self, name, **kwargs): |
| 150 | """Creates a project.""" |
| 151 | description = kwargs.get('description', None) |
| 152 | en = kwargs.get('enabled', 'true') |
| 153 | domain_id = kwargs.get('domain_id', 'default') |
| 154 | post_body = Element("project", |
| 155 | xmlns=XMLNS, |
| 156 | description=description, |
| 157 | domain_id=domain_id, |
| 158 | enabled=str(en).lower(), |
| 159 | name=name) |
| 160 | resp, body = self.post('projects', |
| 161 | str(Document(post_body)), |
| 162 | self.headers) |
| 163 | body = self._parse_body(etree.fromstring(body)) |
| 164 | return resp, body |
| 165 | |
| 166 | def get_project(self, project_id): |
| 167 | """GET a Project.""" |
| 168 | resp, body = self.get("projects/%s" % project_id, self.headers) |
| 169 | body = self._parse_body(etree.fromstring(body)) |
| 170 | return resp, body |
| 171 | |
| 172 | def delete_project(self, project_id): |
| 173 | """Delete a project.""" |
| 174 | resp, body = self.delete('projects/%s' % str(project_id)) |
| 175 | return resp, body |
| 176 | |
| 177 | def create_role(self, name): |
| 178 | """Create a Role.""" |
| 179 | post_body = Element("role", |
| 180 | xmlns=XMLNS, |
| 181 | name=name) |
| 182 | resp, body = self.post('roles', |
| 183 | str(Document(post_body)), |
| 184 | self.headers) |
| 185 | body = self._parse_body(etree.fromstring(body)) |
| 186 | return resp, body |
| 187 | |
| 188 | def get_role(self, role_id): |
| 189 | """GET a Role.""" |
| 190 | resp, body = self.get('roles/%s' % str(role_id), self.headers) |
| 191 | body = self._parse_body(etree.fromstring(body)) |
| 192 | return resp, body |
| 193 | |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 194 | def update_role(self, name, role_id): |
| 195 | """Updates a Role.""" |
| 196 | post_body = Element("role", |
| 197 | xmlns=XMLNS, |
| 198 | name=name) |
| 199 | resp, body = self.patch('roles/%s' % str(role_id), |
| 200 | str(Document(post_body)), |
| 201 | self.headers) |
| 202 | body = self._parse_body(etree.fromstring(body)) |
| 203 | return resp, body |
| 204 | |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 205 | def delete_role(self, role_id): |
| 206 | """Delete a role.""" |
| 207 | resp, body = self.delete('roles/%s' % str(role_id), |
| 208 | self.headers) |
| 209 | return resp, body |
| 210 | |
| 211 | def assign_user_role(self, project_id, user_id, role_id): |
| 212 | """Add roles to a user on a tenant.""" |
| 213 | resp, body = self.put('projects/%s/users/%s/roles/%s' % |
| 214 | (project_id, user_id, role_id), '', self.headers) |
| 215 | return resp, body |
nayna-patel | 4df72dc | 2013-05-29 10:27:24 +0000 | [diff] [blame] | 216 | |
| 217 | def create_domain(self, name, **kwargs): |
| 218 | """Creates a domain.""" |
| 219 | description = kwargs.get('description', None) |
| 220 | en = kwargs.get('enabled', True) |
| 221 | post_body = Element("domain", |
| 222 | xmlns=XMLNS, |
| 223 | name=name, |
| 224 | description=description, |
| 225 | enabled=str(en).lower()) |
| 226 | resp, body = self.post('domains', str(Document(post_body)), |
| 227 | self.headers) |
| 228 | body = self._parse_body(etree.fromstring(body)) |
| 229 | return resp, body |
| 230 | |
| 231 | def list_domains(self): |
| 232 | """Get the list of domains.""" |
| 233 | resp, body = self.get("domains", self.headers) |
| 234 | body = self._parse_domains(etree.fromstring(body)) |
| 235 | return resp, body |
| 236 | |
| 237 | def delete_domain(self, domain_id): |
| 238 | """Delete a domain.""" |
| 239 | resp, body = self.delete('domains/%s' % domain_id, self.headers) |
| 240 | return resp, body |
| 241 | |
| 242 | def update_domain(self, domain_id, **kwargs): |
| 243 | """Updates a domain.""" |
| 244 | resp, body = self.get_domain(domain_id) |
| 245 | description = kwargs.get('description', body['description']) |
| 246 | en = kwargs.get('enabled', body['enabled']) |
| 247 | name = kwargs.get('name', body['name']) |
| 248 | post_body = Element("domain", |
| 249 | xmlns=XMLNS, |
| 250 | name=name, |
| 251 | description=description, |
| 252 | enabled=str(en).lower()) |
| 253 | resp, body = self.patch('domains/%s' % domain_id, |
| 254 | str(Document(post_body)), |
| 255 | self.headers) |
| 256 | body = self._parse_body(etree.fromstring(body)) |
| 257 | return resp, body |
| 258 | |
| 259 | def get_domain(self, domain_id): |
| 260 | """Get Domain details.""" |
| 261 | resp, body = self.get('domains/%s' % domain_id, self.headers) |
| 262 | body = self._parse_body(etree.fromstring(body)) |
| 263 | return resp, body |
nayna-patel | b35f723 | 2013-06-28 07:08:44 +0000 | [diff] [blame] | 264 | |
| 265 | def get_token(self, resp_token): |
| 266 | """GET a Token Details.""" |
| 267 | headers = {'Content-Type': 'application/xml', |
| 268 | 'Accept': 'application/xml', |
| 269 | 'X-Subject-Token': resp_token} |
| 270 | resp, body = self.get("auth/tokens", headers=headers) |
| 271 | body = self._parse_body(etree.fromstring(body)) |
| 272 | return resp, body |
| 273 | |
| 274 | def delete_token(self, resp_token): |
| 275 | """Delete a Given Token.""" |
| 276 | headers = {'X-Subject-Token': resp_token} |
| 277 | resp, body = self.delete("auth/tokens", headers=headers) |
| 278 | return resp, body |
| 279 | |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 280 | def create_group(self, name, **kwargs): |
| 281 | """Creates a group.""" |
| 282 | description = kwargs.get('description', None) |
| 283 | domain_id = kwargs.get('domain_id', 'default') |
| 284 | project_id = kwargs.get('project_id', None) |
| 285 | post_body = Element("group", |
| 286 | xmlns=XMLNS, |
| 287 | name=name, |
| 288 | description=description, |
| 289 | domain_id=domain_id, |
| 290 | project_id=project_id) |
| 291 | resp, body = self.post('groups', str(Document(post_body)), |
| 292 | self.headers) |
| 293 | body = self._parse_body(etree.fromstring(body)) |
| 294 | return resp, body |
| 295 | |
| 296 | def delete_group(self, group_id): |
| 297 | """Delete a group.""" |
| 298 | resp, body = self.delete('groups/%s' % group_id, self.headers) |
| 299 | return resp, body |
| 300 | |
| 301 | def assign_user_role_on_project(self, project_id, user_id, role_id): |
| 302 | """Add roles to a user on a project.""" |
| 303 | resp, body = self.put('projects/%s/users/%s/roles/%s' % |
| 304 | (project_id, user_id, role_id), '', |
| 305 | self.headers) |
| 306 | return resp, body |
| 307 | |
| 308 | def assign_user_role_on_domain(self, domain_id, user_id, role_id): |
| 309 | """Add roles to a user on a domain.""" |
| 310 | resp, body = self.put('domains/%s/users/%s/roles/%s' % |
| 311 | (domain_id, user_id, role_id), '', |
| 312 | self.headers) |
| 313 | return resp, body |
| 314 | |
| 315 | def list_user_roles_on_project(self, project_id, user_id): |
| 316 | """list roles of a user on a project.""" |
| 317 | resp, body = self.get('projects/%s/users/%s/roles' % |
| 318 | (project_id, user_id), self.headers) |
| 319 | body = self._parse_roles(etree.fromstring(body)) |
| 320 | return resp, body |
| 321 | |
| 322 | def list_user_roles_on_domain(self, domain_id, user_id): |
| 323 | """list roles of a user on a domain.""" |
| 324 | resp, body = self.get('domains/%s/users/%s/roles' % |
| 325 | (domain_id, user_id), self.headers) |
| 326 | body = self._parse_roles(etree.fromstring(body)) |
| 327 | return resp, body |
| 328 | |
| 329 | def revoke_role_from_user_on_project(self, project_id, user_id, role_id): |
| 330 | """Delete role of a user on a project.""" |
| 331 | resp, body = self.delete('projects/%s/users/%s/roles/%s' % |
| 332 | (project_id, user_id, role_id), self.headers) |
| 333 | return resp, body |
| 334 | |
| 335 | def revoke_role_from_user_on_domain(self, domain_id, user_id, role_id): |
| 336 | """Delete role of a user on a domain.""" |
| 337 | resp, body = self.delete('domains/%s/users/%s/roles/%s' % |
| 338 | (domain_id, user_id, role_id), self.headers) |
| 339 | return resp, body |
| 340 | |
| 341 | def assign_group_role_on_project(self, project_id, group_id, role_id): |
| 342 | """Add roles to a user on a project.""" |
| 343 | resp, body = self.put('projects/%s/groups/%s/roles/%s' % |
| 344 | (project_id, group_id, role_id), '', |
| 345 | self.headers) |
| 346 | return resp, body |
| 347 | |
| 348 | def assign_group_role_on_domain(self, domain_id, group_id, role_id): |
| 349 | """Add roles to a user on a domain.""" |
| 350 | resp, body = self.put('domains/%s/groups/%s/roles/%s' % |
| 351 | (domain_id, group_id, role_id), '', |
| 352 | self.headers) |
| 353 | return resp, body |
| 354 | |
| 355 | def list_group_roles_on_project(self, project_id, group_id): |
| 356 | """list roles of a user on a project.""" |
| 357 | resp, body = self.get('projects/%s/groups/%s/roles' % |
| 358 | (project_id, group_id), self.headers) |
| 359 | body = self._parse_roles(etree.fromstring(body)) |
| 360 | return resp, body |
| 361 | |
| 362 | def list_group_roles_on_domain(self, domain_id, group_id): |
| 363 | """list roles of a user on a domain.""" |
| 364 | resp, body = self.get('domains/%s/groups/%s/roles' % |
| 365 | (domain_id, group_id), self.headers) |
| 366 | body = self._parse_roles(etree.fromstring(body)) |
| 367 | return resp, body |
| 368 | |
| 369 | def revoke_role_from_group_on_project(self, project_id, group_id, role_id): |
| 370 | """Delete role of a user on a project.""" |
| 371 | resp, body = self.delete('projects/%s/groups/%s/roles/%s' % |
| 372 | (project_id, group_id, role_id), self.headers) |
| 373 | return resp, body |
| 374 | |
| 375 | def revoke_role_from_group_on_domain(self, domain_id, group_id, role_id): |
| 376 | """Delete role of a user on a domain.""" |
| 377 | resp, body = self.delete('domains/%s/groups/%s/roles/%s' % |
| 378 | (domain_id, group_id, role_id), self.headers) |
| 379 | return resp, body |
| 380 | |
nayna-patel | b35f723 | 2013-06-28 07:08:44 +0000 | [diff] [blame] | 381 | |
| 382 | class V3TokenClientXML(RestClientXML): |
| 383 | |
| 384 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 385 | super(V3TokenClientXML, self).__init__(config, username, password, |
| 386 | auth_url, tenant_name) |
| 387 | self.service = self.config.identity.catalog_type |
| 388 | self.endpoint_url = 'adminURL' |
| 389 | |
| 390 | auth_url = config.identity.uri |
| 391 | |
| 392 | if 'tokens' not in auth_url: |
| 393 | auth_url = auth_url.rstrip('/') + '/tokens' |
| 394 | |
| 395 | self.auth_url = auth_url |
| 396 | self.config = config |
| 397 | |
| 398 | def auth(self, user_id, password): |
| 399 | user = Element('user', |
| 400 | id=user_id, |
| 401 | password=password) |
| 402 | password = Element('password') |
| 403 | password.append(user) |
| 404 | |
| 405 | method = Element('method') |
| 406 | method.append(Text('password')) |
| 407 | methods = Element('methods') |
| 408 | methods.append(method) |
| 409 | identity = Element('identity') |
| 410 | identity.append(methods) |
| 411 | identity.append(password) |
| 412 | auth = Element('auth') |
| 413 | auth.append(identity) |
| 414 | headers = {'Content-Type': 'application/xml'} |
| 415 | resp, body = self.post("auth/tokens", headers=headers, |
| 416 | body=str(Document(auth))) |
| 417 | return resp, body |
| 418 | |
| 419 | def request(self, method, url, headers=None, body=None, wait=None): |
| 420 | """Overriding the existing HTTP request in super class rest_client.""" |
| 421 | self._set_auth() |
| 422 | self.base_url = self.base_url.replace(urlparse(self.base_url).path, |
| 423 | "/v3") |
| 424 | return super(V3TokenClientXML, self).request(method, url, |
| 425 | headers=headers, |
| 426 | body=body) |