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 |
| 17 | from urlparse import urlparse |
| 18 | |
| 19 | from tempest.common.rest_client import RestClient |
| 20 | |
| 21 | |
| 22 | class IdentityV3ClientJSON(RestClient): |
| 23 | |
| 24 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 25 | super(IdentityV3ClientJSON, self).__init__(config, username, password, |
| 26 | auth_url, tenant_name) |
| 27 | self.service = self.config.identity.catalog_type |
| 28 | self.endpoint_url = 'adminURL' |
| 29 | |
| 30 | def request(self, method, url, headers=None, body=None, wait=None): |
| 31 | """Overriding the existing HTTP request in super class rest_client.""" |
| 32 | self._set_auth() |
| 33 | self.base_url = self.base_url.replace(urlparse(self.base_url).path, |
| 34 | "/v3") |
| 35 | return super(IdentityV3ClientJSON, self).request(method, url, |
| 36 | headers=headers, |
| 37 | body=body) |
| 38 | |
| 39 | def create_user(self, user_name, **kwargs): |
| 40 | """Creates a user.""" |
| 41 | password = kwargs.get('password', None) |
| 42 | email = kwargs.get('email', None) |
| 43 | en = kwargs.get('enabled', True) |
| 44 | project_id = kwargs.get('project_id', None) |
| 45 | description = kwargs.get('description', None) |
| 46 | domain_id = kwargs.get('domain_id', 'default') |
| 47 | post_body = { |
| 48 | 'project_id': project_id, |
| 49 | 'description': description, |
| 50 | 'domain_id': domain_id, |
| 51 | 'email': email, |
| 52 | 'enabled': en, |
| 53 | 'name': user_name, |
| 54 | 'password': password |
| 55 | } |
| 56 | post_body = json.dumps({'user': post_body}) |
| 57 | resp, body = self.post('users', post_body, |
| 58 | self.headers) |
| 59 | body = json.loads(body) |
| 60 | return resp, body['user'] |
| 61 | |
| 62 | def update_user(self, user_id, name, **kwargs): |
| 63 | """Updates a user.""" |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 64 | resp, body = self.get_user(user_id) |
| 65 | email = kwargs.get('email', body['email']) |
| 66 | en = kwargs.get('enabled', body['enabled']) |
| 67 | project_id = kwargs.get('project_id', body['project_id']) |
| 68 | description = kwargs.get('description', body['description']) |
| 69 | domain_id = kwargs.get('domain_id', body['domain_id']) |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 70 | post_body = { |
| 71 | 'name': name, |
| 72 | 'email': email, |
| 73 | 'enabled': en, |
| 74 | 'project_id': project_id, |
| 75 | 'id': user_id, |
| 76 | 'domain_id': domain_id, |
| 77 | 'description': description |
| 78 | } |
| 79 | post_body = json.dumps({'user': post_body}) |
| 80 | resp, body = self.patch('users/%s' % user_id, post_body, |
| 81 | self.headers) |
| 82 | body = json.loads(body) |
| 83 | return resp, body['user'] |
| 84 | |
| 85 | def list_user_projects(self, user_id): |
| 86 | """Lists the projects on which a user has roles assigned.""" |
| 87 | resp, body = self.get('users/%s/projects' % user_id, self.headers) |
| 88 | body = json.loads(body) |
| 89 | return resp, body['projects'] |
| 90 | |
| 91 | def get_users(self): |
| 92 | """Get the list of users.""" |
| 93 | resp, body = self.get("users") |
| 94 | body = json.loads(body) |
| 95 | return resp, body['users'] |
| 96 | |
| 97 | def get_user(self, user_id): |
| 98 | """GET a user.""" |
| 99 | resp, body = self.get("users/%s" % user_id) |
| 100 | body = json.loads(body) |
| 101 | return resp, body['user'] |
| 102 | |
| 103 | def delete_user(self, user_id): |
| 104 | """Deletes a User.""" |
| 105 | resp, body = self.delete("users/%s" % user_id) |
| 106 | return resp, body |
| 107 | |
| 108 | def create_project(self, name, **kwargs): |
| 109 | """Creates a project.""" |
| 110 | description = kwargs.get('description', None) |
| 111 | en = kwargs.get('enabled', True) |
| 112 | domain_id = kwargs.get('domain_id', 'default') |
| 113 | post_body = { |
| 114 | 'description': description, |
| 115 | 'domain_id': domain_id, |
| 116 | 'enabled': en, |
| 117 | 'name': name |
| 118 | } |
| 119 | post_body = json.dumps({'project': post_body}) |
| 120 | resp, body = self.post('projects', post_body, self.headers) |
| 121 | body = json.loads(body) |
| 122 | return resp, body['project'] |
| 123 | |
Nayna Patel | e633136 | 2013-08-12 06:59:48 +0000 | [diff] [blame] | 124 | def list_projects(self): |
| 125 | resp, body = self.get("projects") |
| 126 | body = json.loads(body) |
| 127 | return resp, body['projects'] |
| 128 | |
| 129 | def update_project(self, project_id, **kwargs): |
| 130 | resp, body = self.get_project(project_id) |
| 131 | name = kwargs.get('name', body['name']) |
| 132 | desc = kwargs.get('description', body['description']) |
| 133 | en = kwargs.get('enabled', body['enabled']) |
| 134 | domain_id = kwargs.get('domain_id', body['domain_id']) |
| 135 | post_body = { |
| 136 | 'id': project_id, |
| 137 | 'name': name, |
| 138 | 'description': desc, |
| 139 | 'enabled': en, |
| 140 | 'domain_id': domain_id, |
| 141 | } |
| 142 | post_body = json.dumps({'project': post_body}) |
| 143 | resp, body = self.patch('projects/%s' % project_id, post_body, |
| 144 | self.headers) |
| 145 | body = json.loads(body) |
| 146 | return resp, body['project'] |
| 147 | |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 148 | def get_project(self, project_id): |
| 149 | """GET a Project.""" |
| 150 | resp, body = self.get("projects/%s" % project_id) |
| 151 | body = json.loads(body) |
| 152 | return resp, body['project'] |
| 153 | |
| 154 | def delete_project(self, project_id): |
| 155 | """Delete a project.""" |
| 156 | resp, body = self.delete('projects/%s' % str(project_id)) |
| 157 | return resp, body |
| 158 | |
| 159 | def create_role(self, name): |
| 160 | """Create a Role.""" |
| 161 | post_body = { |
| 162 | 'name': name |
| 163 | } |
| 164 | post_body = json.dumps({'role': post_body}) |
| 165 | resp, body = self.post('roles', post_body, self.headers) |
| 166 | body = json.loads(body) |
| 167 | return resp, body['role'] |
| 168 | |
| 169 | def get_role(self, role_id): |
| 170 | """GET a Role.""" |
| 171 | resp, body = self.get('roles/%s' % str(role_id)) |
| 172 | body = json.loads(body) |
| 173 | return resp, body['role'] |
| 174 | |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 175 | def update_role(self, name, role_id): |
| 176 | """Create a Role.""" |
| 177 | post_body = { |
| 178 | 'name': name |
| 179 | } |
| 180 | post_body = json.dumps({'role': post_body}) |
| 181 | resp, body = self.patch('roles/%s' % str(role_id), post_body, |
| 182 | self.headers) |
| 183 | body = json.loads(body) |
| 184 | return resp, body['role'] |
| 185 | |
rajalakshmi-ganesan | 7312bb5 | 2013-01-29 20:03:42 +0530 | [diff] [blame] | 186 | def delete_role(self, role_id): |
| 187 | """Delete a role.""" |
| 188 | resp, body = self.delete('roles/%s' % str(role_id)) |
| 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 project.""" |
| 193 | resp, body = self.put('projects/%s/users/%s/roles/%s' % |
| 194 | (project_id, user_id, role_id), None, |
| 195 | self.headers) |
| 196 | return resp, body |
nayna-patel | 4df72dc | 2013-05-29 10:27:24 +0000 | [diff] [blame] | 197 | |
| 198 | def create_domain(self, name, **kwargs): |
| 199 | """Creates a domain.""" |
| 200 | description = kwargs.get('description', None) |
| 201 | en = kwargs.get('enabled', True) |
| 202 | post_body = { |
| 203 | 'description': description, |
| 204 | 'enabled': en, |
| 205 | 'name': name |
| 206 | } |
| 207 | post_body = json.dumps({'domain': post_body}) |
| 208 | resp, body = self.post('domains', post_body, self.headers) |
| 209 | body = json.loads(body) |
| 210 | return resp, body['domain'] |
| 211 | |
| 212 | def delete_domain(self, domain_id): |
| 213 | """Delete a domain.""" |
| 214 | resp, body = self.delete('domains/%s' % str(domain_id)) |
| 215 | return resp, body |
| 216 | |
| 217 | def list_domains(self): |
| 218 | """List Domains.""" |
| 219 | resp, body = self.get('domains') |
| 220 | body = json.loads(body) |
| 221 | return resp, body['domains'] |
| 222 | |
| 223 | def update_domain(self, domain_id, **kwargs): |
| 224 | """Updates a domain.""" |
| 225 | resp, body = self.get_domain(domain_id) |
| 226 | description = kwargs.get('description', body['description']) |
| 227 | en = kwargs.get('enabled', body['enabled']) |
| 228 | name = kwargs.get('name', body['name']) |
| 229 | post_body = { |
| 230 | 'description': description, |
| 231 | 'enabled': en, |
| 232 | 'name': name |
| 233 | } |
| 234 | post_body = json.dumps({'domain': post_body}) |
| 235 | resp, body = self.patch('domains/%s' % domain_id, post_body, |
| 236 | self.headers) |
| 237 | body = json.loads(body) |
| 238 | return resp, body['domain'] |
| 239 | |
| 240 | def get_domain(self, domain_id): |
| 241 | """Get Domain details.""" |
| 242 | resp, body = self.get('domains/%s' % domain_id) |
| 243 | body = json.loads(body) |
| 244 | return resp, body['domain'] |
nayna-patel | b35f723 | 2013-06-28 07:08:44 +0000 | [diff] [blame] | 245 | |
| 246 | def get_token(self, resp_token): |
| 247 | """Get token details.""" |
| 248 | headers = {'X-Subject-Token': resp_token} |
| 249 | resp, body = self.get("auth/tokens", headers=headers) |
| 250 | body = json.loads(body) |
| 251 | return resp, body['token'] |
| 252 | |
| 253 | def delete_token(self, resp_token): |
| 254 | """Deletes token.""" |
| 255 | headers = {'X-Subject-Token': resp_token} |
| 256 | resp, body = self.delete("auth/tokens", headers=headers) |
| 257 | return resp, body |
| 258 | |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 259 | def create_group(self, name, **kwargs): |
| 260 | """Creates a group.""" |
| 261 | description = kwargs.get('description', None) |
| 262 | domain_id = kwargs.get('domain_id', 'default') |
| 263 | project_id = kwargs.get('project_id', None) |
| 264 | post_body = { |
| 265 | 'description': description, |
| 266 | 'domain_id': domain_id, |
| 267 | 'project_id': project_id, |
| 268 | 'name': name |
| 269 | } |
| 270 | post_body = json.dumps({'group': post_body}) |
| 271 | resp, body = self.post('groups', post_body, self.headers) |
| 272 | body = json.loads(body) |
| 273 | return resp, body['group'] |
| 274 | |
Zhi Kun Liu | e8136f0 | 2014-01-07 18:56:28 +0800 | [diff] [blame] | 275 | def get_group(self, group_id): |
| 276 | """Get group details.""" |
| 277 | resp, body = self.get('groups/%s' % group_id, self.headers) |
| 278 | body = json.loads(body) |
| 279 | return resp, body['group'] |
| 280 | |
| 281 | def update_group(self, group_id, **kwargs): |
| 282 | """Updates a group.""" |
| 283 | resp, body = self.get_group(group_id) |
| 284 | name = kwargs.get('name', body['name']) |
| 285 | description = kwargs.get('description', body['description']) |
| 286 | post_body = { |
| 287 | 'name': name, |
| 288 | 'description': description |
| 289 | } |
| 290 | post_body = json.dumps({'group': post_body}) |
| 291 | resp, body = self.patch('groups/%s' % group_id, post_body, |
| 292 | self.headers) |
| 293 | body = json.loads(body) |
| 294 | return resp, body['group'] |
| 295 | |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 296 | def delete_group(self, group_id): |
| 297 | """Delete a group.""" |
| 298 | resp, body = self.delete('groups/%s' % str(group_id)) |
| 299 | return resp, body |
| 300 | |
Zhi Kun Liu | e8136f0 | 2014-01-07 18:56:28 +0800 | [diff] [blame] | 301 | def add_group_user(self, group_id, user_id): |
| 302 | """Add user into group.""" |
| 303 | resp, body = self.put('groups/%s/users/%s' % (group_id, user_id), |
| 304 | None, self.headers) |
| 305 | return resp, body |
| 306 | |
| 307 | def list_group_users(self, group_id): |
| 308 | """List users in group.""" |
| 309 | resp, body = self.get('groups/%s/users' % group_id, self.headers) |
| 310 | body = json.loads(body) |
| 311 | return resp, body['users'] |
| 312 | |
| 313 | def delete_group_user(self, group_id, user_id): |
| 314 | """Delete user in group.""" |
| 315 | resp, body = self.delete('groups/%s/users/%s' % (group_id, user_id), |
| 316 | self.headers) |
| 317 | return resp, body |
| 318 | |
nayna-patel | 755d814 | 2013-07-16 06:45:34 +0000 | [diff] [blame] | 319 | def assign_user_role_on_project(self, project_id, user_id, role_id): |
| 320 | """Add roles to a user on a project.""" |
| 321 | resp, body = self.put('projects/%s/users/%s/roles/%s' % |
| 322 | (project_id, user_id, role_id), None, |
| 323 | self.headers) |
| 324 | return resp, body |
| 325 | |
| 326 | def assign_user_role_on_domain(self, domain_id, user_id, role_id): |
| 327 | """Add roles to a user on a domain.""" |
| 328 | resp, body = self.put('domains/%s/users/%s/roles/%s' % |
| 329 | (domain_id, user_id, role_id), None, |
| 330 | self.headers) |
| 331 | return resp, body |
| 332 | |
| 333 | def list_user_roles_on_project(self, project_id, user_id): |
| 334 | """list roles of a user on a project.""" |
| 335 | resp, body = self.get('projects/%s/users/%s/roles' % |
| 336 | (project_id, user_id)) |
| 337 | body = json.loads(body) |
| 338 | return resp, body['roles'] |
| 339 | |
| 340 | def list_user_roles_on_domain(self, domain_id, user_id): |
| 341 | """list roles of a user on a domain.""" |
| 342 | resp, body = self.get('domains/%s/users/%s/roles' % |
| 343 | (domain_id, user_id)) |
| 344 | body = json.loads(body) |
| 345 | return resp, body['roles'] |
| 346 | |
| 347 | def revoke_role_from_user_on_project(self, project_id, user_id, role_id): |
| 348 | """Delete role of a user on a project.""" |
| 349 | resp, body = self.delete('projects/%s/users/%s/roles/%s' % |
| 350 | (project_id, user_id, role_id)) |
| 351 | return resp, body |
| 352 | |
| 353 | def revoke_role_from_user_on_domain(self, domain_id, user_id, role_id): |
| 354 | """Delete role of a user on a domain.""" |
| 355 | resp, body = self.delete('domains/%s/users/%s/roles/%s' % |
| 356 | (domain_id, user_id, role_id)) |
| 357 | return resp, body |
| 358 | |
| 359 | def assign_group_role_on_project(self, project_id, group_id, role_id): |
| 360 | """Add roles to a user on a project.""" |
| 361 | resp, body = self.put('projects/%s/groups/%s/roles/%s' % |
| 362 | (project_id, group_id, role_id), None, |
| 363 | self.headers) |
| 364 | return resp, body |
| 365 | |
| 366 | def assign_group_role_on_domain(self, domain_id, group_id, role_id): |
| 367 | """Add roles to a user on a domain.""" |
| 368 | resp, body = self.put('domains/%s/groups/%s/roles/%s' % |
| 369 | (domain_id, group_id, role_id), None, |
| 370 | self.headers) |
| 371 | return resp, body |
| 372 | |
| 373 | def list_group_roles_on_project(self, project_id, group_id): |
| 374 | """list roles of a user on a project.""" |
| 375 | resp, body = self.get('projects/%s/groups/%s/roles' % |
| 376 | (project_id, group_id)) |
| 377 | body = json.loads(body) |
| 378 | return resp, body['roles'] |
| 379 | |
| 380 | def list_group_roles_on_domain(self, domain_id, group_id): |
| 381 | """list roles of a user on a domain.""" |
| 382 | resp, body = self.get('domains/%s/groups/%s/roles' % |
| 383 | (domain_id, group_id)) |
| 384 | body = json.loads(body) |
| 385 | return resp, body['roles'] |
| 386 | |
| 387 | def revoke_role_from_group_on_project(self, project_id, group_id, role_id): |
| 388 | """Delete role of a user on a project.""" |
| 389 | resp, body = self.delete('projects/%s/groups/%s/roles/%s' % |
| 390 | (project_id, group_id, role_id)) |
| 391 | return resp, body |
| 392 | |
| 393 | def revoke_role_from_group_on_domain(self, domain_id, group_id, role_id): |
| 394 | """Delete role of a user on a domain.""" |
| 395 | resp, body = self.delete('domains/%s/groups/%s/roles/%s' % |
| 396 | (domain_id, group_id, role_id)) |
| 397 | return resp, body |
| 398 | |
Steven Hardy | bf70c5c | 2013-10-30 21:55:16 +0000 | [diff] [blame] | 399 | def create_trust(self, trustor_user_id, trustee_user_id, project_id, |
| 400 | role_names, impersonation, expires_at): |
| 401 | """Creates a trust.""" |
| 402 | roles = [{'name': n} for n in role_names] |
| 403 | post_body = { |
| 404 | 'trustor_user_id': trustor_user_id, |
| 405 | 'trustee_user_id': trustee_user_id, |
| 406 | 'project_id': project_id, |
| 407 | 'impersonation': impersonation, |
| 408 | 'roles': roles, |
| 409 | 'expires_at': expires_at |
| 410 | } |
| 411 | post_body = json.dumps({'trust': post_body}) |
| 412 | resp, body = self.post('OS-TRUST/trusts', post_body, self.headers) |
| 413 | body = json.loads(body) |
| 414 | return resp, body['trust'] |
| 415 | |
| 416 | def delete_trust(self, trust_id): |
| 417 | """Deletes a trust.""" |
| 418 | resp, body = self.delete("OS-TRUST/trusts/%s" % trust_id) |
| 419 | return resp, body |
| 420 | |
| 421 | def get_trusts(self, trustor_user_id=None, trustee_user_id=None): |
| 422 | """GET trusts.""" |
| 423 | if trustor_user_id: |
| 424 | resp, body = self.get("OS-TRUST/trusts?trustor_user_id=%s" |
| 425 | % trustor_user_id) |
| 426 | elif trustee_user_id: |
| 427 | resp, body = self.get("OS-TRUST/trusts?trustee_user_id=%s" |
| 428 | % trustee_user_id) |
| 429 | else: |
| 430 | resp, body = self.get("OS-TRUST/trusts") |
| 431 | body = json.loads(body) |
| 432 | return resp, body['trusts'] |
| 433 | |
| 434 | def get_trust(self, trust_id): |
| 435 | """GET trust.""" |
| 436 | resp, body = self.get("OS-TRUST/trusts/%s" % trust_id) |
| 437 | body = json.loads(body) |
| 438 | return resp, body['trust'] |
| 439 | |
| 440 | def get_trust_roles(self, trust_id): |
| 441 | """GET roles delegated by a trust.""" |
| 442 | resp, body = self.get("OS-TRUST/trusts/%s/roles" % trust_id) |
| 443 | body = json.loads(body) |
| 444 | return resp, body['roles'] |
| 445 | |
| 446 | def get_trust_role(self, trust_id, role_id): |
| 447 | """GET role delegated by a trust.""" |
| 448 | resp, body = self.get("OS-TRUST/trusts/%s/roles/%s" |
| 449 | % (trust_id, role_id)) |
| 450 | body = json.loads(body) |
| 451 | return resp, body['role'] |
| 452 | |
| 453 | def check_trust_role(self, trust_id, role_id): |
| 454 | """HEAD Check if role is delegated by a trust.""" |
| 455 | resp, body = self.head("OS-TRUST/trusts/%s/roles/%s" |
| 456 | % (trust_id, role_id)) |
| 457 | return resp, body |
| 458 | |
nayna-patel | b35f723 | 2013-06-28 07:08:44 +0000 | [diff] [blame] | 459 | |
| 460 | class V3TokenClientJSON(RestClient): |
| 461 | |
| 462 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 463 | super(V3TokenClientJSON, self).__init__(config, username, password, |
| 464 | auth_url, tenant_name) |
| 465 | self.service = self.config.identity.catalog_type |
| 466 | self.endpoint_url = 'adminURL' |
| 467 | |
| 468 | auth_url = config.identity.uri |
| 469 | |
| 470 | if 'tokens' not in auth_url: |
| 471 | auth_url = auth_url.rstrip('/') + '/tokens' |
| 472 | |
| 473 | self.auth_url = auth_url |
| 474 | self.config = config |
| 475 | |
| 476 | def auth(self, user_id, password): |
| 477 | creds = { |
| 478 | 'auth': { |
| 479 | 'identity': { |
| 480 | 'methods': ['password'], |
| 481 | 'password': { |
| 482 | 'user': { |
| 483 | 'id': user_id, |
| 484 | 'password': password |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | headers = {'Content-Type': 'application/json'} |
| 491 | body = json.dumps(creds) |
| 492 | resp, body = self.post("auth/tokens", headers=headers, body=body) |
| 493 | return resp, body |
| 494 | |
| 495 | def request(self, method, url, headers=None, body=None, wait=None): |
| 496 | """Overriding the existing HTTP request in super class rest_client.""" |
| 497 | self._set_auth() |
| 498 | self.base_url = self.base_url.replace(urlparse(self.base_url).path, |
| 499 | "/v3") |
| 500 | return super(V3TokenClientJSON, self).request(method, url, |
| 501 | headers=headers, |
| 502 | body=body) |