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 |
| 25 | from tempest.services.compute.xml.common import xml_to_json |
| 26 | |
| 27 | |
| 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 | |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 55 | def _parse_array(self, node): |
| 56 | array = [] |
| 57 | for child in node.getchildren(): |
| 58 | array.append(xml_to_json(child)) |
| 59 | return array |
| 60 | |
| 61 | def _parse_body(self, body): |
| 62 | json = xml_to_json(body) |
| 63 | return json |
| 64 | |
| 65 | def request(self, method, url, headers=None, body=None, wait=None): |
| 66 | """Overriding the existing HTTP request in super class RestClient.""" |
| 67 | self._set_auth() |
| 68 | self.base_url = self.base_url.replace(urlparse(self.base_url).path, |
| 69 | "/v3") |
| 70 | return super(IdentityV3ClientXML, self).request(method, url, |
| 71 | headers=headers, |
| 72 | body=body) |
| 73 | |
| 74 | def create_user(self, user_name, **kwargs): |
| 75 | """Creates a user.""" |
| 76 | password = kwargs.get('password', None) |
| 77 | email = kwargs.get('email', None) |
| 78 | en = kwargs.get('enabled', 'true') |
| 79 | project_id = kwargs.get('project_id', None) |
| 80 | description = kwargs.get('description', None) |
| 81 | domain_id = kwargs.get('domain_id', 'default') |
| 82 | post_body = Element("user", |
| 83 | xmlns=XMLNS, |
| 84 | name=user_name, |
| 85 | password=password, |
| 86 | description=description, |
| 87 | email=email, |
| 88 | enabled=str(en).lower(), |
| 89 | project_id=project_id, |
| 90 | domain_id=domain_id) |
| 91 | resp, body = self.post('users', str(Document(post_body)), |
| 92 | self.headers) |
| 93 | body = self._parse_body(etree.fromstring(body)) |
| 94 | return resp, body |
| 95 | |
| 96 | def update_user(self, user_id, name, **kwargs): |
| 97 | """Updates a user.""" |
| 98 | email = kwargs.get('email', None) |
| 99 | en = kwargs.get('enabled', True) |
| 100 | project_id = kwargs.get('project_id', None) |
| 101 | domain_id = kwargs.get('domain_id', 'default') |
| 102 | description = kwargs.get('description', None) |
| 103 | update_user = Element("user", |
| 104 | xmlns=XMLNS, |
| 105 | name=name, |
| 106 | email=email, |
| 107 | project_id=project_id, |
| 108 | domain_id=domain_id, |
| 109 | description=description, |
| 110 | enabled=str(en).lower()) |
| 111 | resp, body = self.patch('users/%s' % user_id, |
| 112 | str(Document(update_user)), |
| 113 | self.headers) |
| 114 | body = self._parse_body(etree.fromstring(body)) |
| 115 | return resp, body |
| 116 | |
| 117 | def list_user_projects(self, user_id): |
| 118 | """Lists the projects on which a user has roles assigned.""" |
| 119 | resp, body = self.get('users/%s/projects' % user_id, self.headers) |
| 120 | body = self._parse_projects(etree.fromstring(body)) |
| 121 | return resp, body |
| 122 | |
| 123 | def get_users(self): |
| 124 | """Get the list of users.""" |
| 125 | resp, body = self.get("users", self.headers) |
| 126 | body = self._parse_array(etree.fromstring(body)) |
| 127 | return resp, body |
| 128 | |
| 129 | def get_user(self, user_id): |
| 130 | """GET a user.""" |
| 131 | resp, body = self.get("users/%s" % user_id, self.headers) |
| 132 | body = self._parse_body(etree.fromstring(body)) |
| 133 | return resp, body |
| 134 | |
| 135 | def delete_user(self, user_id): |
| 136 | """Deletes a User.""" |
| 137 | resp, body = self.delete("users/%s" % user_id, self.headers) |
| 138 | return resp, body |
| 139 | |
| 140 | def create_project(self, name, **kwargs): |
| 141 | """Creates a project.""" |
| 142 | description = kwargs.get('description', None) |
| 143 | en = kwargs.get('enabled', 'true') |
| 144 | domain_id = kwargs.get('domain_id', 'default') |
| 145 | post_body = Element("project", |
| 146 | xmlns=XMLNS, |
| 147 | description=description, |
| 148 | domain_id=domain_id, |
| 149 | enabled=str(en).lower(), |
| 150 | name=name) |
| 151 | resp, body = self.post('projects', |
| 152 | str(Document(post_body)), |
| 153 | self.headers) |
| 154 | body = self._parse_body(etree.fromstring(body)) |
| 155 | return resp, body |
| 156 | |
| 157 | def get_project(self, project_id): |
| 158 | """GET a Project.""" |
| 159 | resp, body = self.get("projects/%s" % project_id, self.headers) |
| 160 | body = self._parse_body(etree.fromstring(body)) |
| 161 | return resp, body |
| 162 | |
| 163 | def delete_project(self, project_id): |
| 164 | """Delete a project.""" |
| 165 | resp, body = self.delete('projects/%s' % str(project_id)) |
| 166 | return resp, body |
| 167 | |
| 168 | def create_role(self, name): |
| 169 | """Create a Role.""" |
| 170 | post_body = Element("role", |
| 171 | xmlns=XMLNS, |
| 172 | name=name) |
| 173 | resp, body = self.post('roles', |
| 174 | str(Document(post_body)), |
| 175 | self.headers) |
| 176 | body = self._parse_body(etree.fromstring(body)) |
| 177 | return resp, body |
| 178 | |
| 179 | def get_role(self, role_id): |
| 180 | """GET a Role.""" |
| 181 | resp, body = self.get('roles/%s' % str(role_id), self.headers) |
| 182 | body = self._parse_body(etree.fromstring(body)) |
| 183 | return resp, body |
| 184 | |
| 185 | def delete_role(self, role_id): |
| 186 | """Delete a role.""" |
| 187 | resp, body = self.delete('roles/%s' % str(role_id), |
| 188 | self.headers) |
| 189 | return resp, body |
| 190 | |
| 191 | def assign_user_role(self, project_id, user_id, role_id): |
| 192 | """Add roles to a user on a tenant.""" |
| 193 | resp, body = self.put('projects/%s/users/%s/roles/%s' % |
| 194 | (project_id, user_id, role_id), '', self.headers) |
| 195 | return resp, body |
nayna-patel | 4df72dc | 2013-05-29 10:27:24 +0000 | [diff] [blame] | 196 | |
| 197 | def create_domain(self, name, **kwargs): |
| 198 | """Creates a domain.""" |
| 199 | description = kwargs.get('description', None) |
| 200 | en = kwargs.get('enabled', True) |
| 201 | post_body = Element("domain", |
| 202 | xmlns=XMLNS, |
| 203 | name=name, |
| 204 | description=description, |
| 205 | enabled=str(en).lower()) |
| 206 | resp, body = self.post('domains', str(Document(post_body)), |
| 207 | self.headers) |
| 208 | body = self._parse_body(etree.fromstring(body)) |
| 209 | return resp, body |
| 210 | |
| 211 | def list_domains(self): |
| 212 | """Get the list of domains.""" |
| 213 | resp, body = self.get("domains", self.headers) |
| 214 | body = self._parse_domains(etree.fromstring(body)) |
| 215 | return resp, body |
| 216 | |
| 217 | def delete_domain(self, domain_id): |
| 218 | """Delete a domain.""" |
| 219 | resp, body = self.delete('domains/%s' % domain_id, self.headers) |
| 220 | return resp, body |
| 221 | |
| 222 | def update_domain(self, domain_id, **kwargs): |
| 223 | """Updates a domain.""" |
| 224 | resp, body = self.get_domain(domain_id) |
| 225 | description = kwargs.get('description', body['description']) |
| 226 | en = kwargs.get('enabled', body['enabled']) |
| 227 | name = kwargs.get('name', body['name']) |
| 228 | post_body = Element("domain", |
| 229 | xmlns=XMLNS, |
| 230 | name=name, |
| 231 | description=description, |
| 232 | enabled=str(en).lower()) |
| 233 | resp, body = self.patch('domains/%s' % domain_id, |
| 234 | str(Document(post_body)), |
| 235 | self.headers) |
| 236 | body = self._parse_body(etree.fromstring(body)) |
| 237 | return resp, body |
| 238 | |
| 239 | def get_domain(self, domain_id): |
| 240 | """Get Domain details.""" |
| 241 | resp, body = self.get('domains/%s' % domain_id, self.headers) |
| 242 | body = self._parse_body(etree.fromstring(body)) |
| 243 | return resp, body |