blob: 6573b6eba5ef90806da51412e4b8b0ea11f49c5b [file] [log] [blame]
Daniel Mellado82c83a52015-12-09 15:16:49 +00001# 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
13from oslo_serialization import jsonutils as json
14
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080015from tempest.lib.common import rest_client
Daniel Mellado82c83a52015-12-09 15:16:49 +000016
17
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080018class UsersClient(rest_client.RestClient):
Daniel Mellado82c83a52015-12-09 15:16:49 +000019 api_version = "v2.0"
20
21 def create_user(self, name, password, tenant_id, email, **kwargs):
22 """Create a user."""
23 post_body = {
24 'name': name,
25 'password': password,
26 'email': email
27 }
28 if tenant_id is not None:
29 post_body['tenantId'] = tenant_id
30 if kwargs.get('enabled') is not None:
31 post_body['enabled'] = kwargs.get('enabled')
32 post_body = json.dumps({'user': post_body})
33 resp, body = self.post('users', post_body)
34 self.expected_success(200, resp.status)
35 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080036 return rest_client.ResponseBody(resp, body)
Daniel Mellado82c83a52015-12-09 15:16:49 +000037
38 def update_user(self, user_id, **kwargs):
zhufl7daa31e2016-06-14 17:24:42 +080039 """Updates a user.
40
41 Available params: see http://developer.openstack.org/
42 api-ref-identity-admin-v2.html#admin-updateUser
43 """
Daniel Mellado82c83a52015-12-09 15:16:49 +000044 put_body = json.dumps({'user': kwargs})
45 resp, body = self.put('users/%s' % user_id, put_body)
46 self.expected_success(200, resp.status)
47 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080048 return rest_client.ResponseBody(resp, body)
Daniel Mellado82c83a52015-12-09 15:16:49 +000049
50 def show_user(self, user_id):
51 """GET a user."""
52 resp, body = self.get("users/%s" % user_id)
53 self.expected_success(200, resp.status)
54 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080055 return rest_client.ResponseBody(resp, body)
Daniel Mellado82c83a52015-12-09 15:16:49 +000056
57 def delete_user(self, user_id):
58 """Delete a user."""
59 resp, body = self.delete("users/%s" % user_id)
60 self.expected_success(204, resp.status)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080061 return rest_client.ResponseBody(resp, body)
Daniel Mellado82c83a52015-12-09 15:16:49 +000062
63 def list_users(self):
64 """Get the list of users."""
65 resp, body = self.get("users")
66 self.expected_success(200, resp.status)
67 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080068 return rest_client.ResponseBody(resp, body)
Daniel Mellado82c83a52015-12-09 15:16:49 +000069
70 def enable_disable_user(self, user_id, **kwargs):
71 """Enables or disables a user.
72
73 Available params: see http://developer.openstack.org/
74 api-ref-identity-v2-ext.html#enableUser
75 """
76 # NOTE: The URL (users/<id>/enabled) is different from the api-site
77 # one (users/<id>/OS-KSADM/enabled) , but they are the same API
78 # because of the fact that in keystone/contrib/admin_crud/core.py
79 # both api use same action='set_user_enabled'
80 put_body = json.dumps({'user': kwargs})
81 resp, body = self.put('users/%s/enabled' % user_id, put_body)
82 self.expected_success(200, resp.status)
83 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080084 return rest_client.ResponseBody(resp, body)
Daniel Mellado82c83a52015-12-09 15:16:49 +000085
86 def update_user_password(self, user_id, **kwargs):
87 """Update User Password."""
88 # TODO(piyush): Current api-site doesn't contain this API description.
89 # After fixing the api-site, we need to fix here also for putting the
90 # link to api-site.
91 # LP: https://bugs.launchpad.net/openstack-api-site/+bug/1524147
92 put_body = json.dumps({'user': kwargs})
93 resp, body = self.put('users/%s/OS-KSADM/password' % user_id, put_body)
94 self.expected_success(200, resp.status)
95 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080096 return rest_client.ResponseBody(resp, body)
Daniel Mellado82c83a52015-12-09 15:16:49 +000097
98 def update_user_own_password(self, user_id, **kwargs):
99 """User updates own password"""
100 # TODO(piyush): Current api-site doesn't contain this API description.
101 # After fixing the api-site, we need to fix here also for putting the
102 # link to api-site.
103 # LP: https://bugs.launchpad.net/openstack-api-site/+bug/1524153
104 # NOTE: This API is used for updating user password by itself.
105 # Ref: http://lists.openstack.org/pipermail/openstack-dev/2015-December
106 # /081803.html
107 patch_body = json.dumps({'user': kwargs})
108 resp, body = self.patch('OS-KSCRUD/users/%s' % user_id, patch_body)
109 self.expected_success(200, resp.status)
110 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -0800111 return rest_client.ResponseBody(resp, body)
Daniel Mellado82c83a52015-12-09 15:16:49 +0000112
113 def create_user_ec2_credentials(self, user_id, **kwargs):
114 # TODO(piyush): Current api-site doesn't contain this API description.
115 # After fixing the api-site, we need to fix here also for putting the
116 # link to api-site.
117 post_body = json.dumps(kwargs)
118 resp, body = self.post('/users/%s/credentials/OS-EC2' % user_id,
119 post_body)
120 self.expected_success(200, resp.status)
121 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -0800122 return rest_client.ResponseBody(resp, body)
Daniel Mellado82c83a52015-12-09 15:16:49 +0000123
124 def delete_user_ec2_credentials(self, user_id, access):
125 resp, body = self.delete('/users/%s/credentials/OS-EC2/%s' %
126 (user_id, access))
127 self.expected_success(204, resp.status)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -0800128 return rest_client.ResponseBody(resp, body)
Daniel Mellado82c83a52015-12-09 15:16:49 +0000129
130 def list_user_ec2_credentials(self, user_id):
131 resp, body = self.get('/users/%s/credentials/OS-EC2' % user_id)
132 self.expected_success(200, resp.status)
133 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -0800134 return rest_client.ResponseBody(resp, body)
Daniel Mellado82c83a52015-12-09 15:16:49 +0000135
136 def show_user_ec2_credentials(self, user_id, access):
137 resp, body = self.get('/users/%s/credentials/OS-EC2/%s' %
138 (user_id, access))
139 self.expected_success(200, resp.status)
140 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -0800141 return rest_client.ResponseBody(resp, body)