blob: 5398621998871cb61c08f3e7837efe45e176e7dd [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/
zhuflba3edec2016-09-07 17:07:32 +080040 api-ref/identity/v3/#update-user
zhufl7daa31e2016-06-14 17:24:42 +080041 """
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/
zhuflba3edec2016-09-07 17:07:32 +080054 api-ref/identity/v3/index.html#
55 change-password-for-user
Daniel Mellado7aea5342016-02-09 09:10:12 +000056 """
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 Ohmichid35a1332016-03-02 10:38:07 -080060 return rest_client.ResponseBody(resp)
Daniel Mellado7aea5342016-02-09 09:10:12 +000061
ghanshyam7f817db2016-08-01 18:37:13 +090062 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 Mellado7aea5342016-02-09 09:10:12 +000072 self.expected_success(200, resp.status)
73 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080074 return rest_client.ResponseBody(resp, body)
Daniel Mellado7aea5342016-02-09 09:10:12 +000075
ghanshyam7f817db2016-08-01 18:37:13 +090076 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 Mellado7aea5342016-02-09 09:10:12 +000082 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 Ohmichid35a1332016-03-02 10:38:07 -080088 return rest_client.ResponseBody(resp, body)
Daniel Mellado7aea5342016-02-09 09:10:12 +000089
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 Ohmichid35a1332016-03-02 10:38:07 -080095 return rest_client.ResponseBody(resp, body)
Daniel Mellado7aea5342016-02-09 09:10:12 +000096
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 Ohmichid35a1332016-03-02 10:38:07 -0800101 return rest_client.ResponseBody(resp, body)
Daniel Mellado7aea5342016-02-09 09:10:12 +0000102
ghanshyam7f817db2016-08-01 18:37:13 +0900103 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 Mellado7aea5342016-02-09 09:10:12 +0000113 self.expected_success(200, resp.status)
114 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -0800115 return rest_client.ResponseBody(resp, body)