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 | |
ghanshyam | 7f817db | 2016-08-01 18:37:13 +0900 | [diff] [blame] | 24 | def create_user(self, **kwargs): |
| 25 | """Creates a user. |
| 26 | |
| 27 | Available params: see http://developer.openstack.org/ |
| 28 | api-ref/identity/v3/#create-user |
| 29 | """ |
| 30 | post_body = json.dumps({'user': kwargs}) |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 31 | resp, body = self.post('users', post_body) |
| 32 | self.expected_success(201, resp.status) |
| 33 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 34 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 35 | |
ghanshyam | 7f817db | 2016-08-01 18:37:13 +0900 | [diff] [blame] | 36 | def update_user(self, user_id, **kwargs): |
zhufl | 7daa31e | 2016-06-14 17:24:42 +0800 | [diff] [blame] | 37 | """Updates a user. |
| 38 | |
| 39 | Available params: see http://developer.openstack.org/ |
zhufl | ba3edec | 2016-09-07 17:07:32 +0800 | [diff] [blame] | 40 | api-ref/identity/v3/#update-user |
zhufl | 7daa31e | 2016-06-14 17:24:42 +0800 | [diff] [blame] | 41 | """ |
ghanshyam | 7f817db | 2016-08-01 18:37:13 +0900 | [diff] [blame] | 42 | if 'id' not in kwargs: |
| 43 | kwargs['id'] = user_id |
| 44 | post_body = json.dumps({'user': kwargs}) |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 45 | resp, body = self.patch('users/%s' % user_id, post_body) |
| 46 | self.expected_success(200, resp.status) |
| 47 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 48 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 49 | |
| 50 | def update_user_password(self, user_id, **kwargs): |
| 51 | """Update a user password |
| 52 | |
| 53 | Available params: see http://developer.openstack.org/ |
zhufl | ba3edec | 2016-09-07 17:07:32 +0800 | [diff] [blame] | 54 | api-ref/identity/v3/index.html# |
| 55 | change-password-for-user |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 56 | """ |
| 57 | update_user = json.dumps({'user': kwargs}) |
| 58 | resp, _ = self.post('users/%s/password' % user_id, update_user) |
| 59 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 60 | return rest_client.ResponseBody(resp) |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 61 | |
ghanshyam | 7f817db | 2016-08-01 18:37:13 +0900 | [diff] [blame] | 62 | def list_user_projects(self, user_id, **params): |
| 63 | """Lists the projects on which a user has roles assigned. |
| 64 | |
| 65 | Available params: see http://developer.openstack.org/ |
| 66 | api-ref/identity/v3/#list-projects-for-user |
| 67 | """ |
| 68 | url = 'users/%s/projects' % user_id |
| 69 | if params: |
| 70 | url += '?%s' % urllib.urlencode(params) |
| 71 | resp, body = self.get(url) |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 72 | self.expected_success(200, resp.status) |
| 73 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 74 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 75 | |
ghanshyam | 7f817db | 2016-08-01 18:37:13 +0900 | [diff] [blame] | 76 | def list_users(self, **params): |
| 77 | """Get the list of users. |
| 78 | |
| 79 | Available params: see http://developer.openstack.org/ |
| 80 | api-ref/identity/v3/#list-users |
| 81 | """ |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 82 | url = 'users' |
| 83 | if params: |
| 84 | url += '?%s' % urllib.urlencode(params) |
| 85 | resp, body = self.get(url) |
| 86 | self.expected_success(200, resp.status) |
| 87 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 88 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 89 | |
| 90 | def show_user(self, user_id): |
| 91 | """GET a user.""" |
| 92 | resp, body = self.get("users/%s" % 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 delete_user(self, user_id): |
| 98 | """Deletes a User.""" |
| 99 | resp, body = self.delete("users/%s" % user_id) |
| 100 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 101 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 102 | |
ghanshyam | 7f817db | 2016-08-01 18:37:13 +0900 | [diff] [blame] | 103 | def list_user_groups(self, user_id, **params): |
| 104 | """Lists groups which a user belongs to. |
| 105 | |
| 106 | Available params: see http://developer.openstack.org/ |
| 107 | api-ref/identity/v3/#list-groups-to-which-a-user-belongs |
| 108 | """ |
| 109 | url = 'users/%s/groups' % user_id |
| 110 | if params: |
| 111 | url += '?%s' % urllib.urlencode(params) |
| 112 | resp, body = self.get(url) |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 113 | self.expected_success(200, resp.status) |
| 114 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 115 | return rest_client.ResponseBody(resp, body) |