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