rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 1 | # Copyright 2013 OpenStack Foundation |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 16 | import json |
nayna-patel | 153e9dd | 2014-05-16 09:00:05 +0000 | [diff] [blame^] | 17 | import urllib |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 18 | |
| 19 | from lxml import etree |
| 20 | |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 21 | from tempest.common import rest_client |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 22 | from tempest.common import xml_utils as common |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 23 | from tempest import config |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 24 | from tempest import exceptions |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 25 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 26 | CONF = config.CONF |
| 27 | |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 28 | XMLNS = "http://docs.openstack.org/identity/api/v3" |
| 29 | |
| 30 | |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 31 | class IdentityV3ClientXML(rest_client.RestClient): |
| 32 | TYPE = "xml" |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 33 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 34 | def __init__(self, auth_provider): |
| 35 | super(IdentityV3ClientXML, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 36 | self.service = CONF.identity.catalog_type |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 37 | self.endpoint_url = 'adminURL' |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 38 | self.api_version = "v3" |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 39 | |
| 40 | def _parse_projects(self, node): |
| 41 | array = [] |
| 42 | for child in node.getchildren(): |
| 43 | tag_list = child.tag.split('}', 1) |
| 44 | if tag_list[1] == "project": |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 45 | array.append(common.xml_to_json(child)) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 46 | return array |
| 47 | |
nayna-patel | 4df72dc | 2013-05-29 10:27:24 +0000 | [diff] [blame] | 48 | def _parse_domains(self, node): |
| 49 | array = [] |
| 50 | for child in node.getchildren(): |
| 51 | tag_list = child.tag.split('}', 1) |
| 52 | if tag_list[1] == "domain": |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 53 | array.append(common.xml_to_json(child)) |
nayna-patel | 4df72dc | 2013-05-29 10:27:24 +0000 | [diff] [blame] | 54 | return array |
| 55 | |
wanglianmin | 29b0f4c | 2014-03-06 19:09:16 +0800 | [diff] [blame] | 56 | def _parse_groups(self, node): |
| 57 | array = [] |
| 58 | for child in node.getchildren(): |
| 59 | tag_list = child.tag.split('}', 1) |
| 60 | if tag_list[1] == "group": |
| 61 | array.append(common.xml_to_json(child)) |
| 62 | return array |
| 63 | |
Zhi Kun Liu | e8136f0 | 2014-01-07 18:56:28 +0800 | [diff] [blame] | 64 | def _parse_group_users(self, node): |
| 65 | array = [] |
| 66 | for child in node.getchildren(): |
| 67 | tag_list = child.tag.split('}', 1) |
| 68 | if tag_list[1] == "user": |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 69 | array.append(common.xml_to_json(child)) |
Zhi Kun Liu | e8136f0 | 2014-01-07 18:56:28 +0800 | [diff] [blame] | 70 | return array |
| 71 | |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 72 | def _parse_roles(self, node): |
| 73 | array = [] |
| 74 | for child in node.getchildren(): |
| 75 | tag_list = child.tag.split('}', 1) |
| 76 | if tag_list[1] == "role": |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 77 | array.append(common.xml_to_json(child)) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 78 | return array |
| 79 | |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 80 | def _parse_array(self, node): |
| 81 | array = [] |
| 82 | for child in node.getchildren(): |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 83 | array.append(common.xml_to_json(child)) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 84 | return array |
| 85 | |
| 86 | def _parse_body(self, body): |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 87 | _json = common.xml_to_json(body) |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 88 | return _json |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 89 | |
| 90 | def create_user(self, user_name, **kwargs): |
| 91 | """Creates a user.""" |
| 92 | password = kwargs.get('password', None) |
| 93 | email = kwargs.get('email', None) |
| 94 | en = kwargs.get('enabled', 'true') |
| 95 | project_id = kwargs.get('project_id', None) |
| 96 | description = kwargs.get('description', None) |
| 97 | domain_id = kwargs.get('domain_id', 'default') |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 98 | post_body = common.Element("user", |
| 99 | xmlns=XMLNS, |
| 100 | name=user_name, |
| 101 | password=password, |
| 102 | description=description, |
| 103 | email=email, |
| 104 | enabled=str(en).lower(), |
| 105 | project_id=project_id, |
| 106 | domain_id=domain_id) |
| 107 | resp, body = self.post('users', str(common.Document(post_body))) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 108 | self.expected_success(201, resp.status) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 109 | body = self._parse_body(etree.fromstring(body)) |
| 110 | return resp, body |
| 111 | |
| 112 | def update_user(self, user_id, name, **kwargs): |
| 113 | """Updates a user.""" |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 114 | _, body = self.get_user(user_id) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 115 | email = kwargs.get('email', body['email']) |
| 116 | en = kwargs.get('enabled', body['enabled']) |
| 117 | project_id = kwargs.get('project_id', body['project_id']) |
| 118 | description = kwargs.get('description', body['description']) |
| 119 | domain_id = kwargs.get('domain_id', body['domain_id']) |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 120 | update_user = common.Element("user", |
| 121 | xmlns=XMLNS, |
| 122 | name=name, |
| 123 | email=email, |
| 124 | project_id=project_id, |
| 125 | domain_id=domain_id, |
| 126 | description=description, |
| 127 | enabled=str(en).lower()) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 128 | resp, body = self.patch('users/%s' % user_id, |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 129 | str(common.Document(update_user))) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 130 | self.expected_success(200, resp.status) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 131 | body = self._parse_body(etree.fromstring(body)) |
| 132 | return resp, body |
| 133 | |
| 134 | def list_user_projects(self, user_id): |
| 135 | """Lists the projects on which a user has roles assigned.""" |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 136 | resp, body = self.get('users/%s/projects' % user_id) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 137 | self.expected_success(200, resp.status) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 138 | body = self._parse_projects(etree.fromstring(body)) |
| 139 | return resp, body |
| 140 | |
| 141 | def get_users(self): |
| 142 | """Get the list of users.""" |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 143 | resp, body = self.get("users") |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 144 | self.expected_success(200, resp.status) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 145 | body = self._parse_array(etree.fromstring(body)) |
| 146 | return resp, body |
| 147 | |
| 148 | def get_user(self, user_id): |
| 149 | """GET a user.""" |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 150 | resp, body = self.get("users/%s" % user_id) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 151 | self.expected_success(200, resp.status) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 152 | body = self._parse_body(etree.fromstring(body)) |
| 153 | return resp, body |
| 154 | |
| 155 | def delete_user(self, user_id): |
| 156 | """Deletes a User.""" |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 157 | resp, body = self.delete("users/%s" % user_id) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 158 | self.expected_success(204, resp.status) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 159 | return resp, body |
| 160 | |
| 161 | def create_project(self, name, **kwargs): |
| 162 | """Creates a project.""" |
| 163 | description = kwargs.get('description', None) |
| 164 | en = kwargs.get('enabled', 'true') |
| 165 | domain_id = kwargs.get('domain_id', 'default') |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 166 | post_body = common.Element("project", |
| 167 | xmlns=XMLNS, |
| 168 | description=description, |
| 169 | domain_id=domain_id, |
| 170 | enabled=str(en).lower(), |
| 171 | name=name) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 172 | resp, body = self.post('projects', |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 173 | str(common.Document(post_body))) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 174 | self.expected_success(201, resp.status) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 175 | body = self._parse_body(etree.fromstring(body)) |
| 176 | return resp, body |
| 177 | |
nayna-patel | 153e9dd | 2014-05-16 09:00:05 +0000 | [diff] [blame^] | 178 | def list_projects(self, params=None): |
Nayna Patel | e633136 | 2013-08-12 06:59:48 +0000 | [diff] [blame] | 179 | """Get the list of projects.""" |
nayna-patel | 153e9dd | 2014-05-16 09:00:05 +0000 | [diff] [blame^] | 180 | url = 'projects' |
| 181 | if params: |
| 182 | url += '?%s' % urllib.urlencode(params) |
| 183 | resp, body = self.get(url) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 184 | self.expected_success(200, resp.status) |
Nayna Patel | e633136 | 2013-08-12 06:59:48 +0000 | [diff] [blame] | 185 | body = self._parse_projects(etree.fromstring(body)) |
| 186 | return resp, body |
| 187 | |
| 188 | def update_project(self, project_id, **kwargs): |
| 189 | """Updates a Project.""" |
| 190 | resp, body = self.get_project(project_id) |
| 191 | name = kwargs.get('name', body['name']) |
| 192 | desc = kwargs.get('description', body['description']) |
| 193 | en = kwargs.get('enabled', body['enabled']) |
| 194 | domain_id = kwargs.get('domain_id', body['domain_id']) |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 195 | post_body = common.Element("project", |
| 196 | xmlns=XMLNS, |
| 197 | name=name, |
| 198 | description=desc, |
| 199 | enabled=str(en).lower(), |
| 200 | domain_id=domain_id) |
Nayna Patel | e633136 | 2013-08-12 06:59:48 +0000 | [diff] [blame] | 201 | resp, body = self.patch('projects/%s' % project_id, |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 202 | str(common.Document(post_body))) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 203 | self.expected_success(200, resp.status) |
Nayna Patel | e633136 | 2013-08-12 06:59:48 +0000 | [diff] [blame] | 204 | body = self._parse_body(etree.fromstring(body)) |
| 205 | return resp, body |
| 206 | |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 207 | def get_project(self, project_id): |
| 208 | """GET a Project.""" |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 209 | resp, body = self.get("projects/%s" % project_id) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 210 | self.expected_success(200, resp.status) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 211 | body = self._parse_body(etree.fromstring(body)) |
| 212 | return resp, body |
| 213 | |
| 214 | def delete_project(self, project_id): |
| 215 | """Delete a project.""" |
| 216 | resp, body = self.delete('projects/%s' % str(project_id)) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 217 | self.expected_success(204, resp.status) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 218 | return resp, body |
| 219 | |
| 220 | def create_role(self, name): |
| 221 | """Create a Role.""" |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 222 | post_body = common.Element("role", |
| 223 | xmlns=XMLNS, |
| 224 | name=name) |
| 225 | resp, body = self.post('roles', str(common.Document(post_body))) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 226 | self.expected_success(201, resp.status) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 227 | body = self._parse_body(etree.fromstring(body)) |
| 228 | return resp, body |
| 229 | |
| 230 | def get_role(self, role_id): |
| 231 | """GET a Role.""" |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 232 | resp, body = self.get('roles/%s' % str(role_id)) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 233 | self.expected_success(200, resp.status) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 234 | body = self._parse_body(etree.fromstring(body)) |
| 235 | return resp, body |
| 236 | |
wanglianmin | a3e84ea | 2014-03-26 17:30:33 +0800 | [diff] [blame] | 237 | def list_roles(self): |
| 238 | """Get the list of Roles.""" |
| 239 | resp, body = self.get("roles") |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 240 | self.expected_success(200, resp.status) |
wanglianmin | a3e84ea | 2014-03-26 17:30:33 +0800 | [diff] [blame] | 241 | body = self._parse_roles(etree.fromstring(body)) |
| 242 | return resp, body |
| 243 | |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 244 | def update_role(self, name, role_id): |
| 245 | """Updates a Role.""" |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 246 | post_body = common.Element("role", |
| 247 | xmlns=XMLNS, |
| 248 | name=name) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 249 | resp, body = self.patch('roles/%s' % str(role_id), |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 250 | str(common.Document(post_body))) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 251 | self.expected_success(200, resp.status) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 252 | body = self._parse_body(etree.fromstring(body)) |
| 253 | return resp, body |
| 254 | |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 255 | def delete_role(self, role_id): |
| 256 | """Delete a role.""" |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 257 | resp, body = self.delete('roles/%s' % str(role_id)) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 258 | self.expected_success(204, resp.status) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 259 | return resp, body |
| 260 | |
| 261 | def assign_user_role(self, project_id, user_id, role_id): |
| 262 | """Add roles to a user on a tenant.""" |
| 263 | resp, body = self.put('projects/%s/users/%s/roles/%s' % |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 264 | (project_id, user_id, role_id), '') |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 265 | self.expected_success(204, resp.status) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 266 | return resp, body |
nayna-patel | 4df72dc | 2013-05-29 10:27:24 +0000 | [diff] [blame] | 267 | |
| 268 | def create_domain(self, name, **kwargs): |
| 269 | """Creates a domain.""" |
| 270 | description = kwargs.get('description', None) |
| 271 | en = kwargs.get('enabled', True) |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 272 | post_body = common.Element("domain", |
| 273 | xmlns=XMLNS, |
| 274 | name=name, |
| 275 | description=description, |
| 276 | enabled=str(en).lower()) |
| 277 | resp, body = self.post('domains', str(common.Document(post_body))) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 278 | self.expected_success(201, resp.status) |
nayna-patel | 4df72dc | 2013-05-29 10:27:24 +0000 | [diff] [blame] | 279 | body = self._parse_body(etree.fromstring(body)) |
| 280 | return resp, body |
| 281 | |
| 282 | def list_domains(self): |
| 283 | """Get the list of domains.""" |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 284 | resp, body = self.get("domains") |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 285 | self.expected_success(200, resp.status) |
nayna-patel | 4df72dc | 2013-05-29 10:27:24 +0000 | [diff] [blame] | 286 | body = self._parse_domains(etree.fromstring(body)) |
| 287 | return resp, body |
| 288 | |
| 289 | def delete_domain(self, domain_id): |
| 290 | """Delete a domain.""" |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 291 | resp, body = self.delete('domains/%s' % domain_id) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 292 | self.expected_success(204, resp.status) |
nayna-patel | 4df72dc | 2013-05-29 10:27:24 +0000 | [diff] [blame] | 293 | return resp, body |
| 294 | |
| 295 | def update_domain(self, domain_id, **kwargs): |
| 296 | """Updates a domain.""" |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 297 | _, body = self.get_domain(domain_id) |
nayna-patel | 4df72dc | 2013-05-29 10:27:24 +0000 | [diff] [blame] | 298 | description = kwargs.get('description', body['description']) |
| 299 | en = kwargs.get('enabled', body['enabled']) |
| 300 | name = kwargs.get('name', body['name']) |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 301 | post_body = common.Element("domain", |
| 302 | xmlns=XMLNS, |
| 303 | name=name, |
| 304 | description=description, |
| 305 | enabled=str(en).lower()) |
nayna-patel | 4df72dc | 2013-05-29 10:27:24 +0000 | [diff] [blame] | 306 | resp, body = self.patch('domains/%s' % domain_id, |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 307 | str(common.Document(post_body))) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 308 | self.expected_success(200, resp.status) |
nayna-patel | 4df72dc | 2013-05-29 10:27:24 +0000 | [diff] [blame] | 309 | body = self._parse_body(etree.fromstring(body)) |
| 310 | return resp, body |
| 311 | |
| 312 | def get_domain(self, domain_id): |
| 313 | """Get Domain details.""" |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 314 | resp, body = self.get('domains/%s' % domain_id) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 315 | self.expected_success(200, resp.status) |
nayna-patel | 4df72dc | 2013-05-29 10:27:24 +0000 | [diff] [blame] | 316 | body = self._parse_body(etree.fromstring(body)) |
| 317 | return resp, body |
nayna-patel | b35f723 | 2013-06-28 07:08:44 +0000 | [diff] [blame] | 318 | |
| 319 | def get_token(self, resp_token): |
| 320 | """GET a Token Details.""" |
| 321 | headers = {'Content-Type': 'application/xml', |
| 322 | 'Accept': 'application/xml', |
| 323 | 'X-Subject-Token': resp_token} |
| 324 | resp, body = self.get("auth/tokens", headers=headers) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 325 | self.expected_success(200, resp.status) |
nayna-patel | b35f723 | 2013-06-28 07:08:44 +0000 | [diff] [blame] | 326 | body = self._parse_body(etree.fromstring(body)) |
| 327 | return resp, body |
| 328 | |
| 329 | def delete_token(self, resp_token): |
| 330 | """Delete a Given Token.""" |
| 331 | headers = {'X-Subject-Token': resp_token} |
| 332 | resp, body = self.delete("auth/tokens", headers=headers) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 333 | self.expected_success(204, resp.status) |
nayna-patel | b35f723 | 2013-06-28 07:08:44 +0000 | [diff] [blame] | 334 | return resp, body |
| 335 | |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 336 | def create_group(self, name, **kwargs): |
| 337 | """Creates a group.""" |
| 338 | description = kwargs.get('description', None) |
| 339 | domain_id = kwargs.get('domain_id', 'default') |
| 340 | project_id = kwargs.get('project_id', None) |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 341 | post_body = common.Element("group", |
| 342 | xmlns=XMLNS, |
| 343 | name=name, |
| 344 | description=description, |
| 345 | domain_id=domain_id, |
| 346 | project_id=project_id) |
| 347 | resp, body = self.post('groups', str(common.Document(post_body))) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 348 | self.expected_success(201, resp.status) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 349 | body = self._parse_body(etree.fromstring(body)) |
| 350 | return resp, body |
| 351 | |
Zhi Kun Liu | e8136f0 | 2014-01-07 18:56:28 +0800 | [diff] [blame] | 352 | def get_group(self, group_id): |
| 353 | """Get group details.""" |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 354 | resp, body = self.get('groups/%s' % group_id) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 355 | self.expected_success(200, resp.status) |
Zhi Kun Liu | e8136f0 | 2014-01-07 18:56:28 +0800 | [diff] [blame] | 356 | body = self._parse_body(etree.fromstring(body)) |
| 357 | return resp, body |
| 358 | |
| 359 | def update_group(self, group_id, **kwargs): |
| 360 | """Updates a group.""" |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 361 | _, body = self.get_group(group_id) |
Zhi Kun Liu | e8136f0 | 2014-01-07 18:56:28 +0800 | [diff] [blame] | 362 | name = kwargs.get('name', body['name']) |
| 363 | description = kwargs.get('description', body['description']) |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 364 | post_body = common.Element("group", |
| 365 | xmlns=XMLNS, |
| 366 | name=name, |
| 367 | description=description) |
Zhi Kun Liu | e8136f0 | 2014-01-07 18:56:28 +0800 | [diff] [blame] | 368 | resp, body = self.patch('groups/%s' % group_id, |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 369 | str(common.Document(post_body))) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 370 | self.expected_success(200, resp.status) |
Zhi Kun Liu | e8136f0 | 2014-01-07 18:56:28 +0800 | [diff] [blame] | 371 | body = self._parse_body(etree.fromstring(body)) |
| 372 | return resp, body |
| 373 | |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 374 | def delete_group(self, group_id): |
| 375 | """Delete a group.""" |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 376 | resp, body = self.delete('groups/%s' % group_id) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 377 | self.expected_success(204, resp.status) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 378 | return resp, body |
| 379 | |
Zhi Kun Liu | e8136f0 | 2014-01-07 18:56:28 +0800 | [diff] [blame] | 380 | def add_group_user(self, group_id, user_id): |
| 381 | """Add user into group.""" |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 382 | resp, body = self.put('groups/%s/users/%s' % (group_id, user_id), '') |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 383 | self.expected_success(204, resp.status) |
Zhi Kun Liu | e8136f0 | 2014-01-07 18:56:28 +0800 | [diff] [blame] | 384 | return resp, body |
| 385 | |
| 386 | def list_group_users(self, group_id): |
| 387 | """List users in group.""" |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 388 | resp, body = self.get('groups/%s/users' % group_id) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 389 | self.expected_success(200, resp.status) |
Zhi Kun Liu | e8136f0 | 2014-01-07 18:56:28 +0800 | [diff] [blame] | 390 | body = self._parse_group_users(etree.fromstring(body)) |
| 391 | return resp, body |
| 392 | |
wanglianmin | 29b0f4c | 2014-03-06 19:09:16 +0800 | [diff] [blame] | 393 | def list_user_groups(self, user_id): |
| 394 | """Lists the groups which a user belongs to.""" |
| 395 | resp, body = self.get('users/%s/groups' % user_id) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 396 | self.expected_success(200, resp.status) |
wanglianmin | 29b0f4c | 2014-03-06 19:09:16 +0800 | [diff] [blame] | 397 | body = self._parse_groups(etree.fromstring(body)) |
| 398 | return resp, body |
| 399 | |
Zhi Kun Liu | e8136f0 | 2014-01-07 18:56:28 +0800 | [diff] [blame] | 400 | def delete_group_user(self, group_id, user_id): |
| 401 | """Delete user in group.""" |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 402 | resp, body = self.delete('groups/%s/users/%s' % (group_id, user_id)) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 403 | self.expected_success(204, resp.status) |
Zhi Kun Liu | e8136f0 | 2014-01-07 18:56:28 +0800 | [diff] [blame] | 404 | return resp, body |
| 405 | |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 406 | def assign_user_role_on_project(self, project_id, user_id, role_id): |
| 407 | """Add roles to a user on a project.""" |
| 408 | resp, body = self.put('projects/%s/users/%s/roles/%s' % |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 409 | (project_id, user_id, role_id), '') |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 410 | self.expected_success(204, resp.status) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 411 | return resp, body |
| 412 | |
| 413 | def assign_user_role_on_domain(self, domain_id, user_id, role_id): |
| 414 | """Add roles to a user on a domain.""" |
| 415 | resp, body = self.put('domains/%s/users/%s/roles/%s' % |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 416 | (domain_id, user_id, role_id), '') |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 417 | self.expected_success(204, resp.status) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 418 | return resp, body |
| 419 | |
| 420 | def list_user_roles_on_project(self, project_id, user_id): |
| 421 | """list roles of a user on a project.""" |
| 422 | resp, body = self.get('projects/%s/users/%s/roles' % |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 423 | (project_id, user_id)) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 424 | self.expected_success(200, resp.status) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 425 | body = self._parse_roles(etree.fromstring(body)) |
| 426 | return resp, body |
| 427 | |
| 428 | def list_user_roles_on_domain(self, domain_id, user_id): |
| 429 | """list roles of a user on a domain.""" |
| 430 | resp, body = self.get('domains/%s/users/%s/roles' % |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 431 | (domain_id, user_id)) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 432 | self.expected_success(200, resp.status) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 433 | body = self._parse_roles(etree.fromstring(body)) |
| 434 | return resp, body |
| 435 | |
| 436 | def revoke_role_from_user_on_project(self, project_id, user_id, role_id): |
| 437 | """Delete role of a user on a project.""" |
| 438 | resp, body = self.delete('projects/%s/users/%s/roles/%s' % |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 439 | (project_id, user_id, role_id)) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 440 | self.expected_success(204, resp.status) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 441 | return resp, body |
| 442 | |
| 443 | def revoke_role_from_user_on_domain(self, domain_id, user_id, role_id): |
| 444 | """Delete role of a user on a domain.""" |
| 445 | resp, body = self.delete('domains/%s/users/%s/roles/%s' % |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 446 | (domain_id, user_id, role_id)) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 447 | self.expected_success(204, resp.status) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 448 | return resp, body |
| 449 | |
| 450 | def assign_group_role_on_project(self, project_id, group_id, role_id): |
| 451 | """Add roles to a user on a project.""" |
| 452 | resp, body = self.put('projects/%s/groups/%s/roles/%s' % |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 453 | (project_id, group_id, role_id), '') |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 454 | self.expected_success(204, resp.status) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 455 | return resp, body |
| 456 | |
| 457 | def assign_group_role_on_domain(self, domain_id, group_id, role_id): |
| 458 | """Add roles to a user on a domain.""" |
| 459 | resp, body = self.put('domains/%s/groups/%s/roles/%s' % |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 460 | (domain_id, group_id, role_id), '') |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 461 | self.expected_success(204, resp.status) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 462 | return resp, body |
| 463 | |
| 464 | def list_group_roles_on_project(self, project_id, group_id): |
| 465 | """list roles of a user on a project.""" |
| 466 | resp, body = self.get('projects/%s/groups/%s/roles' % |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 467 | (project_id, group_id)) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 468 | self.expected_success(200, resp.status) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 469 | body = self._parse_roles(etree.fromstring(body)) |
| 470 | return resp, body |
| 471 | |
| 472 | def list_group_roles_on_domain(self, domain_id, group_id): |
| 473 | """list roles of a user on a domain.""" |
| 474 | resp, body = self.get('domains/%s/groups/%s/roles' % |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 475 | (domain_id, group_id)) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 476 | self.expected_success(200, resp.status) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 477 | body = self._parse_roles(etree.fromstring(body)) |
| 478 | return resp, body |
| 479 | |
| 480 | def revoke_role_from_group_on_project(self, project_id, group_id, role_id): |
| 481 | """Delete role of a user on a project.""" |
| 482 | resp, body = self.delete('projects/%s/groups/%s/roles/%s' % |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 483 | (project_id, group_id, role_id)) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 484 | self.expected_success(204, resp.status) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 485 | return resp, body |
| 486 | |
| 487 | def revoke_role_from_group_on_domain(self, domain_id, group_id, role_id): |
| 488 | """Delete role of a user on a domain.""" |
| 489 | resp, body = self.delete('domains/%s/groups/%s/roles/%s' % |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 490 | (domain_id, group_id, role_id)) |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 491 | self.expected_success(204, resp.status) |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 492 | return resp, body |
| 493 | |
nayna-patel | b35f723 | 2013-06-28 07:08:44 +0000 | [diff] [blame] | 494 | |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 495 | class V3TokenClientXML(rest_client.RestClient): |
| 496 | TYPE = "xml" |
nayna-patel | b35f723 | 2013-06-28 07:08:44 +0000 | [diff] [blame] | 497 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 498 | def __init__(self): |
| 499 | super(V3TokenClientXML, self).__init__(None) |
| 500 | auth_url = CONF.identity.uri_v3 |
Matthew Treinish | db2c597 | 2014-01-31 22:18:59 +0000 | [diff] [blame] | 501 | if not auth_url and CONF.identity_feature_enabled.api_v3: |
| 502 | raise exceptions.InvalidConfiguration('you must specify a v3 uri ' |
| 503 | 'if using the v3 identity ' |
| 504 | 'api') |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 505 | if 'auth/tokens' not in auth_url: |
| 506 | auth_url = auth_url.rstrip('/') + '/auth/tokens' |
nayna-patel | b35f723 | 2013-06-28 07:08:44 +0000 | [diff] [blame] | 507 | |
| 508 | self.auth_url = auth_url |
nayna-patel | b35f723 | 2013-06-28 07:08:44 +0000 | [diff] [blame] | 509 | |
Brant Knudson | c555329 | 2014-03-15 11:06:05 -0500 | [diff] [blame] | 510 | def auth(self, user=None, password=None, tenant=None, user_type='id', |
| 511 | domain=None, token=None): |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 512 | """ |
| 513 | :param user: user id or name, as specified in user_type |
Brant Knudson | c555329 | 2014-03-15 11:06:05 -0500 | [diff] [blame] | 514 | :param domain: the user and tenant domain |
| 515 | :param token: a token to re-scope. |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 516 | |
| 517 | Accepts different combinations of credentials. Restrictions: |
| 518 | - tenant and domain are only name (no id) |
| 519 | - user domain and tenant domain are assumed identical |
Brant Knudson | c555329 | 2014-03-15 11:06:05 -0500 | [diff] [blame] | 520 | - domain scope is not supported here |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 521 | Sample sample valid combinations: |
Brant Knudson | c555329 | 2014-03-15 11:06:05 -0500 | [diff] [blame] | 522 | - token |
| 523 | - token, tenant, domain |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 524 | - user_id, password |
| 525 | - username, password, domain |
| 526 | - username, password, tenant, domain |
| 527 | Validation is left to the server side. |
| 528 | """ |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 529 | |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 530 | methods = common.Element('methods') |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 531 | identity = common.Element('identity') |
Brant Knudson | c555329 | 2014-03-15 11:06:05 -0500 | [diff] [blame] | 532 | |
| 533 | if token: |
| 534 | method = common.Element('method') |
| 535 | method.append(common.Text('token')) |
| 536 | methods.append(method) |
| 537 | |
| 538 | token = common.Element('token', id=token) |
| 539 | identity.append(token) |
| 540 | |
| 541 | if user and password: |
| 542 | if user_type == 'id': |
| 543 | _user = common.Element('user', id=user, password=password) |
| 544 | else: |
| 545 | _user = common.Element('user', name=user, password=password) |
| 546 | if domain is not None: |
| 547 | _domain = common.Element('domain', name=domain) |
| 548 | _user.append(_domain) |
| 549 | |
| 550 | password = common.Element('password') |
| 551 | password.append(_user) |
| 552 | method = common.Element('method') |
| 553 | method.append(common.Text('password')) |
| 554 | methods.append(method) |
| 555 | identity.append(password) |
| 556 | |
nayna-patel | b35f723 | 2013-06-28 07:08:44 +0000 | [diff] [blame] | 557 | identity.append(methods) |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 558 | |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 559 | auth = common.Element('auth') |
nayna-patel | b35f723 | 2013-06-28 07:08:44 +0000 | [diff] [blame] | 560 | auth.append(identity) |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 561 | |
| 562 | if tenant is not None: |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 563 | project = common.Element('project', name=tenant) |
Brant Knudson | c555329 | 2014-03-15 11:06:05 -0500 | [diff] [blame] | 564 | _domain = common.Element('domain', name=domain) |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 565 | project.append(_domain) |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 566 | scope = common.Element('scope') |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 567 | scope.append(project) |
| 568 | auth.append(scope) |
| 569 | |
Haiwei Xu | aad85db | 2014-03-05 05:17:39 +0900 | [diff] [blame] | 570 | resp, body = self.post(self.auth_url, body=str(common.Document(auth))) |
nayna-patel | b35f723 | 2013-06-28 07:08:44 +0000 | [diff] [blame] | 571 | return resp, body |
| 572 | |
Sergey Murashov | 4fccd32 | 2014-03-22 09:58:52 +0400 | [diff] [blame] | 573 | def request(self, method, url, extra_headers=False, headers=None, |
| 574 | body=None): |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 575 | """A simple HTTP request interface.""" |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 576 | if headers is None: |
| 577 | # Always accept 'json', for xml token client too. |
| 578 | # Because XML response is not easily |
| 579 | # converted to the corresponding JSON one |
| 580 | headers = self.get_headers(accept_type="json") |
Sergey Murashov | 4fccd32 | 2014-03-22 09:58:52 +0400 | [diff] [blame] | 581 | elif extra_headers: |
| 582 | try: |
| 583 | headers.update(self.get_headers(accept_type="json")) |
| 584 | except (ValueError, TypeError): |
| 585 | headers = self.get_headers(accept_type="json") |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 586 | resp, resp_body = self.http_obj.request(url, method, |
| 587 | headers=headers, body=body) |
Sean Dague | 89a8591 | 2014-03-19 16:37:29 -0400 | [diff] [blame] | 588 | self._log_request(method, url, resp) |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 589 | |
| 590 | if resp.status in [401, 403]: |
| 591 | resp_body = json.loads(resp_body) |
| 592 | raise exceptions.Unauthorized(resp_body['error']['message']) |
| 593 | elif resp.status not in [200, 201, 204]: |
| 594 | raise exceptions.IdentityError( |
| 595 | 'Unexpected status code {0}'.format(resp.status)) |
| 596 | |
| 597 | return resp, json.loads(resp_body) |
| 598 | |
| 599 | def get_token(self, user, password, tenant, domain='Default', |
| 600 | auth_data=False): |
| 601 | """ |
| 602 | :param user: username |
| 603 | Returns (token id, token data) for supplied credentials |
| 604 | """ |
| 605 | resp, body = self.auth(user, password, tenant, user_type='name', |
| 606 | domain=domain) |
| 607 | |
| 608 | token = resp.get('x-subject-token') |
| 609 | if auth_data: |
| 610 | return token, body['token'] |
| 611 | else: |
| 612 | return token |