blob: 0dcdacd766a1c0b1f545a129cda391c8e6859eaf [file] [log] [blame]
Daniel Mellado7aea5342016-02-09 09:10:12 +00001# 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
15from oslo_serialization import jsonutils as json
16from six.moves.urllib import parse as urllib
17
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080018from tempest.lib.common import rest_client
Daniel Mellado7aea5342016-02-09 09:10:12 +000019
20
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080021class UsersClient(rest_client.RestClient):
Daniel Mellado7aea5342016-02-09 09:10:12 +000022 api_version = "v3"
23
ghanshyam7f817db2016-08-01 18:37:13 +090024 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 Mellado7aea5342016-02-09 09:10:12 +000031 resp, body = self.post('users', post_body)
32 self.expected_success(201, resp.status)
33 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080034 return rest_client.ResponseBody(resp, body)
Daniel Mellado7aea5342016-02-09 09:10:12 +000035
ghanshyam7f817db2016-08-01 18:37:13 +090036 def update_user(self, user_id, **kwargs):
zhufl7daa31e2016-06-14 17:24:42 +080037 """Updates a user.
38
39 Available params: see http://developer.openstack.org/
40 api-ref-identity-v3.html#updateUser
41 """
ghanshyam7f817db2016-08-01 18:37:13 +090042 if 'id' not in kwargs:
43 kwargs['id'] = user_id
44 post_body = json.dumps({'user': kwargs})
Daniel Mellado7aea5342016-02-09 09:10:12 +000045 resp, body = self.patch('users/%s' % user_id, post_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 Mellado7aea5342016-02-09 09:10:12 +000049
50 def update_user_password(self, user_id, **kwargs):
51 """Update a user password
52
53 Available params: see http://developer.openstack.org/
54 api-ref-identity-v3.html#changeUserPassword
55 """
56 update_user = json.dumps({'user': kwargs})
57 resp, _ = self.post('users/%s/password' % user_id, update_user)
58 self.expected_success(204, resp.status)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080059 return rest_client.ResponseBody(resp)
Daniel Mellado7aea5342016-02-09 09:10:12 +000060
ghanshyam7f817db2016-08-01 18:37:13 +090061 def list_user_projects(self, user_id, **params):
62 """Lists the projects on which a user has roles assigned.
63
64 Available params: see http://developer.openstack.org/
65 api-ref/identity/v3/#list-projects-for-user
66 """
67 url = 'users/%s/projects' % user_id
68 if params:
69 url += '?%s' % urllib.urlencode(params)
70 resp, body = self.get(url)
Daniel Mellado7aea5342016-02-09 09:10:12 +000071 self.expected_success(200, resp.status)
72 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080073 return rest_client.ResponseBody(resp, body)
Daniel Mellado7aea5342016-02-09 09:10:12 +000074
ghanshyam7f817db2016-08-01 18:37:13 +090075 def list_users(self, **params):
76 """Get the list of users.
77
78 Available params: see http://developer.openstack.org/
79 api-ref/identity/v3/#list-users
80 """
Daniel Mellado7aea5342016-02-09 09:10:12 +000081 url = 'users'
82 if params:
83 url += '?%s' % urllib.urlencode(params)
84 resp, body = self.get(url)
85 self.expected_success(200, resp.status)
86 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080087 return rest_client.ResponseBody(resp, body)
Daniel Mellado7aea5342016-02-09 09:10:12 +000088
89 def show_user(self, user_id):
90 """GET a user."""
91 resp, body = self.get("users/%s" % user_id)
92 self.expected_success(200, resp.status)
93 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080094 return rest_client.ResponseBody(resp, body)
Daniel Mellado7aea5342016-02-09 09:10:12 +000095
96 def delete_user(self, user_id):
97 """Deletes a User."""
98 resp, body = self.delete("users/%s" % user_id)
99 self.expected_success(204, resp.status)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -0800100 return rest_client.ResponseBody(resp, body)
Daniel Mellado7aea5342016-02-09 09:10:12 +0000101
ghanshyam7f817db2016-08-01 18:37:13 +0900102 def list_user_groups(self, user_id, **params):
103 """Lists groups which a user belongs to.
104
105 Available params: see http://developer.openstack.org/
106 api-ref/identity/v3/#list-groups-to-which-a-user-belongs
107 """
108 url = 'users/%s/groups' % user_id
109 if params:
110 url += '?%s' % urllib.urlencode(params)
111 resp, body = self.get(url)
Daniel Mellado7aea5342016-02-09 09:10:12 +0000112 self.expected_success(200, resp.status)
113 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -0800114 return rest_client.ResponseBody(resp, body)