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 | |
Nayna Patel | e633136 | 2013-08-12 06:59:48 +0000 | [diff] [blame] | 166 | def list_projects(self): |
| 167 | """Get the list of projects.""" |
| 168 | resp, body = self.get("projects", self.headers) |
| 169 | body = self._parse_projects(etree.fromstring(body)) |
| 170 | return resp, body |
| 171 | |
| 172 | def update_project(self, project_id, **kwargs): |
| 173 | """Updates a Project.""" |
| 174 | resp, body = self.get_project(project_id) |
| 175 | name = kwargs.get('name', body['name']) |
| 176 | desc = kwargs.get('description', body['description']) |
| 177 | en = kwargs.get('enabled', body['enabled']) |
| 178 | domain_id = kwargs.get('domain_id', body['domain_id']) |
| 179 | post_body = Element("project", |
| 180 | xmlns=XMLNS, |
| 181 | name=name, |
| 182 | description=desc, |
| 183 | enabled=str(en).lower(), |
| 184 | domain_id=domain_id) |
| 185 | resp, body = self.patch('projects/%s' % project_id, |
| 186 | str(Document(post_body)), |
| 187 | self.headers) |
| 188 | body = self._parse_body(etree.fromstring(body)) |
| 189 | return resp, body |
| 190 | |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 191 | def get_project(self, project_id): |
| 192 | """GET a Project.""" |
| 193 | resp, body = self.get("projects/%s" % project_id, self.headers) |
| 194 | body = self._parse_body(etree.fromstring(body)) |
| 195 | return resp, body |
| 196 | |
| 197 | def delete_project(self, project_id): |
| 198 | """Delete a project.""" |
| 199 | resp, body = self.delete('projects/%s' % str(project_id)) |
| 200 | return resp, body |
| 201 | |
| 202 | def create_role(self, name): |
| 203 | """Create a Role.""" |
| 204 | post_body = Element("role", |
| 205 | xmlns=XMLNS, |
| 206 | name=name) |
| 207 | resp, body = self.post('roles', |
| 208 | str(Document(post_body)), |
| 209 | self.headers) |
| 210 | body = self._parse_body(etree.fromstring(body)) |
| 211 | return resp, body |
| 212 | |
| 213 | def get_role(self, role_id): |
| 214 | """GET a Role.""" |
| 215 | resp, body = self.get('roles/%s' % str(role_id), self.headers) |
| 216 | body = self._parse_body(etree.fromstring(body)) |
| 217 | return resp, body |
| 218 | |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 219 | def update_role(self, name, role_id): |
| 220 | """Updates a Role.""" |
| 221 | post_body = Element("role", |
| 222 | xmlns=XMLNS, |
| 223 | name=name) |
| 224 | resp, body = self.patch('roles/%s' % str(role_id), |
| 225 | str(Document(post_body)), |
| 226 | self.headers) |
| 227 | body = self._parse_body(etree.fromstring(body)) |
| 228 | return resp, body |
| 229 | |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 230 | def delete_role(self, role_id): |
| 231 | """Delete a role.""" |
| 232 | resp, body = self.delete('roles/%s' % str(role_id), |
| 233 | self.headers) |
| 234 | return resp, body |
| 235 | |
| 236 | def assign_user_role(self, project_id, user_id, role_id): |
| 237 | """Add roles to a user on a tenant.""" |
| 238 | resp, body = self.put('projects/%s/users/%s/roles/%s' % |
| 239 | (project_id, user_id, role_id), '', self.headers) |
| 240 | return resp, body |
nayna-patel | 4df72dc | 2013-05-29 10:27:24 +0000 | [diff] [blame] | 241 | |
| 242 | def create_domain(self, name, **kwargs): |
| 243 | """Creates a domain.""" |
| 244 | description = kwargs.get('description', None) |
| 245 | en = kwargs.get('enabled', True) |
| 246 | post_body = Element("domain", |
| 247 | xmlns=XMLNS, |
| 248 | name=name, |
| 249 | description=description, |
| 250 | enabled=str(en).lower()) |
| 251 | resp, body = self.post('domains', str(Document(post_body)), |
| 252 | self.headers) |
| 253 | body = self._parse_body(etree.fromstring(body)) |
| 254 | return resp, body |
| 255 | |
| 256 | def list_domains(self): |
| 257 | """Get the list of domains.""" |
| 258 | resp, body = self.get("domains", self.headers) |
| 259 | body = self._parse_domains(etree.fromstring(body)) |
| 260 | return resp, body |
| 261 | |
| 262 | def delete_domain(self, domain_id): |
| 263 | """Delete a domain.""" |
| 264 | resp, body = self.delete('domains/%s' % domain_id, self.headers) |
| 265 | return resp, body |
| 266 | |
| 267 | def update_domain(self, domain_id, **kwargs): |
| 268 | """Updates a domain.""" |
| 269 | resp, body = self.get_domain(domain_id) |
| 270 | description = kwargs.get('description', body['description']) |
| 271 | en = kwargs.get('enabled', body['enabled']) |
| 272 | name = kwargs.get('name', body['name']) |
| 273 | post_body = Element("domain", |
| 274 | xmlns=XMLNS, |
| 275 | name=name, |
| 276 | description=description, |
| 277 | enabled=str(en).lower()) |
| 278 | resp, body = self.patch('domains/%s' % domain_id, |
| 279 | str(Document(post_body)), |
| 280 | self.headers) |
| 281 | body = self._parse_body(etree.fromstring(body)) |
| 282 | return resp, body |
| 283 | |
| 284 | def get_domain(self, domain_id): |
| 285 | """Get Domain details.""" |
| 286 | resp, body = self.get('domains/%s' % domain_id, self.headers) |
| 287 | body = self._parse_body(etree.fromstring(body)) |
| 288 | return resp, body |
nayna-patel | b35f723 | 2013-06-28 07:08:44 +0000 | [diff] [blame] | 289 | |
| 290 | def get_token(self, resp_token): |
| 291 | """GET a Token Details.""" |
| 292 | headers = {'Content-Type': 'application/xml', |
| 293 | 'Accept': 'application/xml', |
| 294 | 'X-Subject-Token': resp_token} |
| 295 | resp, body = self.get("auth/tokens", headers=headers) |
| 296 | body = self._parse_body(etree.fromstring(body)) |
| 297 | return resp, body |
| 298 | |
| 299 | def delete_token(self, resp_token): |
| 300 | """Delete a Given Token.""" |
| 301 | headers = {'X-Subject-Token': resp_token} |
| 302 | resp, body = self.delete("auth/tokens", headers=headers) |
| 303 | return resp, body |
| 304 | |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 305 | def create_group(self, name, **kwargs): |
| 306 | """Creates a group.""" |
| 307 | description = kwargs.get('description', None) |
| 308 | domain_id = kwargs.get('domain_id', 'default') |
| 309 | project_id = kwargs.get('project_id', None) |
| 310 | post_body = Element("group", |
| 311 | xmlns=XMLNS, |
| 312 | name=name, |
| 313 | description=description, |
| 314 | domain_id=domain_id, |
| 315 | project_id=project_id) |
| 316 | resp, body = self.post('groups', str(Document(post_body)), |
| 317 | self.headers) |
| 318 | body = self._parse_body(etree.fromstring(body)) |
| 319 | return resp, body |
| 320 | |
| 321 | def delete_group(self, group_id): |
| 322 | """Delete a group.""" |
| 323 | resp, body = self.delete('groups/%s' % group_id, self.headers) |
| 324 | return resp, body |
| 325 | |
| 326 | def assign_user_role_on_project(self, project_id, user_id, role_id): |
| 327 | """Add roles to a user on a project.""" |
| 328 | resp, body = self.put('projects/%s/users/%s/roles/%s' % |
| 329 | (project_id, user_id, role_id), '', |
| 330 | self.headers) |
| 331 | return resp, body |
| 332 | |
| 333 | def assign_user_role_on_domain(self, domain_id, user_id, role_id): |
| 334 | """Add roles to a user on a domain.""" |
| 335 | resp, body = self.put('domains/%s/users/%s/roles/%s' % |
| 336 | (domain_id, user_id, role_id), '', |
| 337 | self.headers) |
| 338 | return resp, body |
| 339 | |
| 340 | def list_user_roles_on_project(self, project_id, user_id): |
| 341 | """list roles of a user on a project.""" |
| 342 | resp, body = self.get('projects/%s/users/%s/roles' % |
| 343 | (project_id, user_id), self.headers) |
| 344 | body = self._parse_roles(etree.fromstring(body)) |
| 345 | return resp, body |
| 346 | |
| 347 | def list_user_roles_on_domain(self, domain_id, user_id): |
| 348 | """list roles of a user on a domain.""" |
| 349 | resp, body = self.get('domains/%s/users/%s/roles' % |
| 350 | (domain_id, user_id), self.headers) |
| 351 | body = self._parse_roles(etree.fromstring(body)) |
| 352 | return resp, body |
| 353 | |
| 354 | def revoke_role_from_user_on_project(self, project_id, user_id, role_id): |
| 355 | """Delete role of a user on a project.""" |
| 356 | resp, body = self.delete('projects/%s/users/%s/roles/%s' % |
| 357 | (project_id, user_id, role_id), self.headers) |
| 358 | return resp, body |
| 359 | |
| 360 | def revoke_role_from_user_on_domain(self, domain_id, user_id, role_id): |
| 361 | """Delete role of a user on a domain.""" |
| 362 | resp, body = self.delete('domains/%s/users/%s/roles/%s' % |
| 363 | (domain_id, user_id, role_id), self.headers) |
| 364 | return resp, body |
| 365 | |
| 366 | def assign_group_role_on_project(self, project_id, group_id, role_id): |
| 367 | """Add roles to a user on a project.""" |
| 368 | resp, body = self.put('projects/%s/groups/%s/roles/%s' % |
| 369 | (project_id, group_id, role_id), '', |
| 370 | self.headers) |
| 371 | return resp, body |
| 372 | |
| 373 | def assign_group_role_on_domain(self, domain_id, group_id, role_id): |
| 374 | """Add roles to a user on a domain.""" |
| 375 | resp, body = self.put('domains/%s/groups/%s/roles/%s' % |
| 376 | (domain_id, group_id, role_id), '', |
| 377 | self.headers) |
| 378 | return resp, body |
| 379 | |
| 380 | def list_group_roles_on_project(self, project_id, group_id): |
| 381 | """list roles of a user on a project.""" |
| 382 | resp, body = self.get('projects/%s/groups/%s/roles' % |
| 383 | (project_id, group_id), self.headers) |
| 384 | body = self._parse_roles(etree.fromstring(body)) |
| 385 | return resp, body |
| 386 | |
| 387 | def list_group_roles_on_domain(self, domain_id, group_id): |
| 388 | """list roles of a user on a domain.""" |
| 389 | resp, body = self.get('domains/%s/groups/%s/roles' % |
| 390 | (domain_id, group_id), self.headers) |
| 391 | body = self._parse_roles(etree.fromstring(body)) |
| 392 | return resp, body |
| 393 | |
| 394 | def revoke_role_from_group_on_project(self, project_id, group_id, role_id): |
| 395 | """Delete role of a user on a project.""" |
| 396 | resp, body = self.delete('projects/%s/groups/%s/roles/%s' % |
| 397 | (project_id, group_id, role_id), self.headers) |
| 398 | return resp, body |
| 399 | |
| 400 | def revoke_role_from_group_on_domain(self, domain_id, group_id, role_id): |
| 401 | """Delete role of a user on a domain.""" |
| 402 | resp, body = self.delete('domains/%s/groups/%s/roles/%s' % |
| 403 | (domain_id, group_id, role_id), self.headers) |
| 404 | return resp, body |
| 405 | |
nayna-patel | b35f723 | 2013-06-28 07:08:44 +0000 | [diff] [blame] | 406 | |
| 407 | class V3TokenClientXML(RestClientXML): |
| 408 | |
| 409 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 410 | super(V3TokenClientXML, self).__init__(config, username, password, |
| 411 | auth_url, tenant_name) |
| 412 | self.service = self.config.identity.catalog_type |
| 413 | self.endpoint_url = 'adminURL' |
| 414 | |
| 415 | auth_url = config.identity.uri |
| 416 | |
| 417 | if 'tokens' not in auth_url: |
| 418 | auth_url = auth_url.rstrip('/') + '/tokens' |
| 419 | |
| 420 | self.auth_url = auth_url |
| 421 | self.config = config |
| 422 | |
| 423 | def auth(self, user_id, password): |
| 424 | user = Element('user', |
| 425 | id=user_id, |
| 426 | password=password) |
| 427 | password = Element('password') |
| 428 | password.append(user) |
| 429 | |
| 430 | method = Element('method') |
| 431 | method.append(Text('password')) |
| 432 | methods = Element('methods') |
| 433 | methods.append(method) |
| 434 | identity = Element('identity') |
| 435 | identity.append(methods) |
| 436 | identity.append(password) |
| 437 | auth = Element('auth') |
| 438 | auth.append(identity) |
| 439 | headers = {'Content-Type': 'application/xml'} |
| 440 | resp, body = self.post("auth/tokens", headers=headers, |
| 441 | body=str(Document(auth))) |
| 442 | return resp, body |
| 443 | |
| 444 | def request(self, method, url, headers=None, body=None, wait=None): |
| 445 | """Overriding the existing HTTP request in super class rest_client.""" |
| 446 | self._set_auth() |
| 447 | self.base_url = self.base_url.replace(urlparse(self.base_url).path, |
| 448 | "/v3") |
| 449 | return super(V3TokenClientXML, self).request(method, url, |
| 450 | headers=headers, |
| 451 | body=body) |