blob: de8f9cbe2c70c0415da22511c571f60107683e6f [file] [log] [blame]
Daniel Mellado6b16b922015-12-07 12:43:08 +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
15from tempest.common import service_client
16
17
18class RolesClient(service_client.ServiceClient):
19 api_version = "v2.0"
20
21 def create_role(self, name):
22 """Create a role."""
23 post_body = {
24 'name': name,
25 }
26 post_body = json.dumps({'role': post_body})
27 resp, body = self.post('OS-KSADM/roles', post_body)
28 self.expected_success(200, resp.status)
29 body = json.loads(body)
30 return service_client.ResponseBody(resp, body)
31
32 def show_role(self, role_id):
33 """Get a role by its id."""
34 resp, body = self.get('OS-KSADM/roles/%s' % role_id)
35 self.expected_success(200, resp.status)
36 body = json.loads(body)
37 return service_client.ResponseBody(resp, body)
38
39 def delete_role(self, role_id):
40 """Delete a role."""
41 resp, body = self.delete('OS-KSADM/roles/%s' % str(role_id))
42 self.expected_success(204, resp.status)
43 return resp, body
44
45 def list_user_roles(self, tenant_id, user_id):
46 """Returns a list of roles assigned to a user for a tenant."""
47 url = '/tenants/%s/users/%s/roles' % (tenant_id, user_id)
48 resp, body = self.get(url)
49 self.expected_success(200, resp.status)
50 body = json.loads(body)
51 return service_client.ResponseBody(resp, body)
52
53 def assign_user_role(self, tenant_id, user_id, role_id):
54 """Add roles to a user on a tenant."""
55 resp, body = self.put('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
56 (tenant_id, user_id, role_id), "")
57 self.expected_success(200, resp.status)
58 body = json.loads(body)
59 return service_client.ResponseBody(resp, body)
60
61 def delete_user_role(self, tenant_id, user_id, role_id):
62 """Removes a role assignment for a user on a tenant."""
63 resp, body = self.delete('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
64 (tenant_id, user_id, role_id))
65 self.expected_success(204, resp.status)
66 return service_client.ResponseBody(resp, body)
67
68 def list_roles(self):
69 """Returns roles."""
70 resp, body = self.get('OS-KSADM/roles')
71 self.expected_success(200, resp.status)
72 body = json.loads(body)
73 return service_client.ResponseBody(resp, body)