Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 1 | # Copyright 2016 Red Hat, Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | from oslo_serialization import jsonutils as json |
| 16 | from six.moves.urllib import parse as urllib |
| 17 | |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 18 | from tempest.lib.common import rest_client |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 19 | |
| 20 | |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 21 | class UsersClient(rest_client.RestClient): |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 22 | api_version = "v3" |
| 23 | |
| 24 | def create_user(self, user_name, password=None, project_id=None, |
| 25 | email=None, domain_id='default', **kwargs): |
| 26 | """Creates a user.""" |
| 27 | en = kwargs.get('enabled', True) |
| 28 | description = kwargs.get('description', None) |
| 29 | default_project_id = kwargs.get('default_project_id') |
| 30 | post_body = { |
| 31 | 'project_id': project_id, |
| 32 | 'default_project_id': default_project_id, |
| 33 | 'description': description, |
| 34 | 'domain_id': domain_id, |
| 35 | 'email': email, |
| 36 | 'enabled': en, |
| 37 | 'name': user_name, |
| 38 | 'password': password |
| 39 | } |
| 40 | post_body = json.dumps({'user': post_body}) |
| 41 | resp, body = self.post('users', post_body) |
| 42 | self.expected_success(201, resp.status) |
| 43 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 44 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 45 | |
| 46 | def update_user(self, user_id, name, **kwargs): |
zhufl | 7daa31e | 2016-06-14 17:24:42 +0800 | [diff] [blame^] | 47 | """Updates a user. |
| 48 | |
| 49 | Available params: see http://developer.openstack.org/ |
| 50 | api-ref-identity-v3.html#updateUser |
| 51 | """ |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 52 | body = self.show_user(user_id)['user'] |
| 53 | email = kwargs.get('email', body['email']) |
| 54 | en = kwargs.get('enabled', body['enabled']) |
| 55 | project_id = kwargs.get('project_id', body['project_id']) |
| 56 | if 'default_project_id' in body.keys(): |
| 57 | default_project_id = kwargs.get('default_project_id', |
| 58 | body['default_project_id']) |
| 59 | else: |
| 60 | default_project_id = kwargs.get('default_project_id') |
| 61 | description = kwargs.get('description', body['description']) |
| 62 | domain_id = kwargs.get('domain_id', body['domain_id']) |
| 63 | post_body = { |
| 64 | 'name': name, |
| 65 | 'email': email, |
| 66 | 'enabled': en, |
| 67 | 'project_id': project_id, |
| 68 | 'default_project_id': default_project_id, |
| 69 | 'id': user_id, |
| 70 | 'domain_id': domain_id, |
| 71 | 'description': description |
| 72 | } |
| 73 | post_body = json.dumps({'user': post_body}) |
| 74 | resp, body = self.patch('users/%s' % user_id, post_body) |
| 75 | self.expected_success(200, resp.status) |
| 76 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 77 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 78 | |
| 79 | def update_user_password(self, user_id, **kwargs): |
| 80 | """Update a user password |
| 81 | |
| 82 | Available params: see http://developer.openstack.org/ |
| 83 | api-ref-identity-v3.html#changeUserPassword |
| 84 | """ |
| 85 | update_user = json.dumps({'user': kwargs}) |
| 86 | resp, _ = self.post('users/%s/password' % user_id, update_user) |
| 87 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 88 | return rest_client.ResponseBody(resp) |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 89 | |
| 90 | def list_user_projects(self, user_id): |
| 91 | """Lists the projects on which a user has roles assigned.""" |
| 92 | resp, body = self.get('users/%s/projects' % user_id) |
| 93 | self.expected_success(200, resp.status) |
| 94 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 95 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 96 | |
| 97 | def list_users(self, params=None): |
| 98 | """Get the list of users.""" |
| 99 | url = 'users' |
| 100 | if params: |
| 101 | url += '?%s' % urllib.urlencode(params) |
| 102 | resp, body = self.get(url) |
| 103 | self.expected_success(200, resp.status) |
| 104 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 105 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 106 | |
| 107 | def show_user(self, user_id): |
| 108 | """GET a user.""" |
| 109 | resp, body = self.get("users/%s" % user_id) |
| 110 | self.expected_success(200, resp.status) |
| 111 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 112 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 113 | |
| 114 | def delete_user(self, user_id): |
| 115 | """Deletes a User.""" |
| 116 | resp, body = self.delete("users/%s" % user_id) |
| 117 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 118 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 119 | |
| 120 | def list_user_groups(self, user_id): |
| 121 | """Lists groups which a user belongs to.""" |
| 122 | resp, body = self.get('users/%s/groups' % user_id) |
| 123 | self.expected_success(200, resp.status) |
| 124 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 125 | return rest_client.ResponseBody(resp, body) |