ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | # not use this file except in compliance with the License. You may obtain |
| 3 | # a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | # License for the specific language governing permissions and limitations |
| 11 | # under the License. |
| 12 | |
songwenping | 99d6e00 | 2021-01-05 03:07:46 +0000 | [diff] [blame] | 13 | from urllib import parse as urllib |
| 14 | |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 15 | from oslo_serialization import jsonutils as json |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 16 | |
| 17 | from tempest.lib.common import rest_client |
| 18 | |
| 19 | |
| 20 | class UsersClient(rest_client.RestClient): |
| 21 | api_version = "v2.0" |
| 22 | |
| 23 | def create_user(self, **kwargs): |
| 24 | """Create a user. |
| 25 | |
OTSUKA, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 26 | For a full list of available parameters, please refer to the official |
| 27 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 28 | https://docs.openstack.org/api-ref/identity/v2-admin/index.html#create-user-admin-endpoint |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 29 | """ |
| 30 | post_body = json.dumps({'user': kwargs}) |
| 31 | resp, body = self.post('users', post_body) |
| 32 | self.expected_success(200, resp.status) |
| 33 | body = json.loads(body) |
| 34 | return rest_client.ResponseBody(resp, body) |
| 35 | |
| 36 | def update_user(self, user_id, **kwargs): |
| 37 | """Updates a user. |
| 38 | |
OTSUKA, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 39 | For a full list of available parameters, please refer to the official |
| 40 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 41 | https://docs.openstack.org/api-ref/identity/v2-admin/index.html#update-user-admin-endpoint |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 42 | """ |
| 43 | put_body = json.dumps({'user': kwargs}) |
| 44 | resp, body = self.put('users/%s' % user_id, put_body) |
| 45 | self.expected_success(200, resp.status) |
| 46 | body = json.loads(body) |
| 47 | return rest_client.ResponseBody(resp, body) |
| 48 | |
| 49 | def show_user(self, user_id): |
| 50 | """GET a user. |
| 51 | |
OTSUKA, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 52 | For a full list of available parameters, please refer to the official |
| 53 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 54 | https://docs.openstack.org/api-ref/identity/v2-admin/index.html#show-user-details-admin-endpoint |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 55 | """ |
| 56 | resp, body = self.get("users/%s" % user_id) |
| 57 | self.expected_success(200, resp.status) |
| 58 | body = json.loads(body) |
| 59 | return rest_client.ResponseBody(resp, body) |
| 60 | |
| 61 | def delete_user(self, user_id): |
| 62 | """Delete a user. |
| 63 | |
OTSUKA, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 64 | For a full list of available parameters, please refer to the official |
| 65 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 66 | https://docs.openstack.org/api-ref/identity/v2-admin/index.html#delete-user-admin-endpoint |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 67 | """ |
| 68 | resp, body = self.delete("users/%s" % user_id) |
| 69 | self.expected_success(204, resp.status) |
| 70 | return rest_client.ResponseBody(resp, body) |
| 71 | |
| 72 | def list_users(self, **params): |
| 73 | """Get the list of users. |
| 74 | |
OTSUKA, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 75 | For a full list of available parameters, please refer to the official |
| 76 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 77 | https://docs.openstack.org/api-ref/identity/v2-admin/index.html#list-users-admin-endpoint |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 78 | """ |
| 79 | url = "users" |
| 80 | if params: |
| 81 | url += '?%s' % urllib.urlencode(params) |
| 82 | resp, body = self.get(url) |
| 83 | self.expected_success(200, resp.status) |
| 84 | body = json.loads(body) |
| 85 | return rest_client.ResponseBody(resp, body) |
| 86 | |
| 87 | def update_user_enabled(self, user_id, **kwargs): |
| 88 | """Enables or disables a user. |
| 89 | |
OTSUKA, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 90 | For a full list of available parameters, please refer to the official |
| 91 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 92 | https://docs.openstack.org/api-ref/identity/v2-ext/index.html#enable-disable-user |
ghanshyam | 1719306 | 2016-06-24 10:36:54 +0900 | [diff] [blame] | 93 | """ |
| 94 | # NOTE: The URL (users/<id>/enabled) is different from the api-site |
| 95 | # one (users/<id>/OS-KSADM/enabled) , but they are the same API |
| 96 | # because of the fact that in keystone/contrib/admin_crud/core.py |
| 97 | # both api use same action='set_user_enabled' |
| 98 | put_body = json.dumps({'user': kwargs}) |
| 99 | resp, body = self.put('users/%s/enabled' % user_id, put_body) |
| 100 | self.expected_success(200, resp.status) |
| 101 | body = json.loads(body) |
| 102 | return rest_client.ResponseBody(resp, body) |
| 103 | |
| 104 | def update_user_password(self, user_id, **kwargs): |
| 105 | """Update User Password.""" |
| 106 | # TODO(piyush): Current api-site doesn't contain this API description. |
| 107 | # After fixing the api-site, we need to fix here also for putting the |
| 108 | # link to api-site. |
| 109 | # LP: https://bugs.launchpad.net/openstack-api-site/+bug/1524147 |
| 110 | put_body = json.dumps({'user': kwargs}) |
| 111 | resp, body = self.put('users/%s/OS-KSADM/password' % user_id, put_body) |
| 112 | self.expected_success(200, resp.status) |
| 113 | body = json.loads(body) |
| 114 | return rest_client.ResponseBody(resp, body) |
| 115 | |
| 116 | def update_user_own_password(self, user_id, **kwargs): |
| 117 | """User updates own password""" |
| 118 | # TODO(piyush): Current api-site doesn't contain this API description. |
| 119 | # After fixing the api-site, we need to fix here also for putting the |
| 120 | # link to api-site. |
| 121 | # LP: https://bugs.launchpad.net/openstack-api-site/+bug/1524153 |
| 122 | # NOTE: This API is used for updating user password by itself. |
| 123 | # Ref: http://lists.openstack.org/pipermail/openstack-dev/2015-December |
| 124 | # /081803.html |
| 125 | patch_body = json.dumps({'user': kwargs}) |
| 126 | resp, body = self.patch('OS-KSCRUD/users/%s' % user_id, patch_body) |
| 127 | self.expected_success(200, resp.status) |
| 128 | body = json.loads(body) |
| 129 | return rest_client.ResponseBody(resp, body) |
| 130 | |
| 131 | def create_user_ec2_credential(self, user_id, **kwargs): |
| 132 | # TODO(piyush): Current api-site doesn't contain this API description. |
| 133 | # After fixing the api-site, we need to fix here also for putting the |
| 134 | # link to api-site. |
| 135 | post_body = json.dumps(kwargs) |
| 136 | resp, body = self.post('/users/%s/credentials/OS-EC2' % user_id, |
| 137 | post_body) |
| 138 | self.expected_success(200, resp.status) |
| 139 | body = json.loads(body) |
| 140 | return rest_client.ResponseBody(resp, body) |
| 141 | |
| 142 | def delete_user_ec2_credential(self, user_id, access): |
| 143 | resp, body = self.delete('/users/%s/credentials/OS-EC2/%s' % |
| 144 | (user_id, access)) |
| 145 | self.expected_success(204, resp.status) |
| 146 | return rest_client.ResponseBody(resp, body) |
| 147 | |
| 148 | def list_user_ec2_credentials(self, user_id): |
| 149 | resp, body = self.get('/users/%s/credentials/OS-EC2' % user_id) |
| 150 | self.expected_success(200, resp.status) |
| 151 | body = json.loads(body) |
| 152 | return rest_client.ResponseBody(resp, body) |
| 153 | |
| 154 | def show_user_ec2_credential(self, user_id, access): |
| 155 | resp, body = self.get('/users/%s/credentials/OS-EC2/%s' % |
| 156 | (user_id, access)) |
| 157 | self.expected_success(200, resp.status) |
| 158 | body = json.loads(body) |
| 159 | return rest_client.ResponseBody(resp, body) |