blob: 5e68939dbe3b2f163427c336c85c4615c405c889 [file] [log] [blame]
Yaroslav Lobankov997a1452015-11-19 17:11:37 +03001# Copyright 2013 OpenStack Foundation
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Yaroslav Lobankov45025c02015-11-19 17:55:15 +030016"""
17http://developer.openstack.org/api-ref-identity-v3.html#groups-v3
18"""
19
Yaroslav Lobankov997a1452015-11-19 17:11:37 +030020from oslo_serialization import jsonutils as json
ghanshyam83e94d42016-08-01 16:48:06 +090021from six.moves.urllib import parse as urllib
Yaroslav Lobankov997a1452015-11-19 17:11:37 +030022
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080023from tempest.lib.common import rest_client
Yaroslav Lobankov997a1452015-11-19 17:11:37 +030024
25
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080026class GroupsClient(rest_client.RestClient):
Yaroslav Lobankov997a1452015-11-19 17:11:37 +030027 api_version = "v3"
28
Yaroslav Lobankov45025c02015-11-19 17:55:15 +030029 def create_group(self, **kwargs):
30 """Creates a group.
31
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090032 For a full list of available parameters, please refer to the official
33 API reference:
34 http://developer.openstack.org/api-ref/identity/v3/index.html#create-group
Yaroslav Lobankov45025c02015-11-19 17:55:15 +030035 """
36 post_body = json.dumps({'group': kwargs})
Yaroslav Lobankov997a1452015-11-19 17:11:37 +030037 resp, body = self.post('groups', post_body)
38 self.expected_success(201, resp.status)
39 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080040 return rest_client.ResponseBody(resp, body)
Yaroslav Lobankov997a1452015-11-19 17:11:37 +030041
Yaroslav Lobankov937438c2015-11-20 11:21:11 +030042 def show_group(self, group_id):
Yaroslav Lobankov997a1452015-11-19 17:11:37 +030043 """Get group details."""
44 resp, body = self.get('groups/%s' % group_id)
45 self.expected_success(200, resp.status)
46 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080047 return rest_client.ResponseBody(resp, body)
Yaroslav Lobankov997a1452015-11-19 17:11:37 +030048
ghanshyam83e94d42016-08-01 16:48:06 +090049 def list_groups(self, **params):
50 """Lists the groups.
51
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090052 For a full list of available parameters, please refer to the official
53 API reference:
54 http://developer.openstack.org/api-ref/identity/v3/#list-groups
ghanshyam83e94d42016-08-01 16:48:06 +090055 """
56 url = 'groups'
57 if params:
58 url += '?%s' % urllib.urlencode(params)
59 resp, body = self.get(url)
Yaroslav Lobankov997a1452015-11-19 17:11:37 +030060 self.expected_success(200, resp.status)
61 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080062 return rest_client.ResponseBody(resp, body)
Yaroslav Lobankov997a1452015-11-19 17:11:37 +030063
64 def update_group(self, group_id, **kwargs):
Yaroslav Lobankov45025c02015-11-19 17:55:15 +030065 """Updates a group.
66
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090067 For a full list of available parameters, please refer to the official
68 API reference:
69 http://developer.openstack.org/api-ref/identity/v3/index.html#update-group
Yaroslav Lobankov45025c02015-11-19 17:55:15 +030070 """
71 post_body = json.dumps({'group': kwargs})
Yaroslav Lobankov997a1452015-11-19 17:11:37 +030072 resp, body = self.patch('groups/%s' % group_id, post_body)
73 self.expected_success(200, resp.status)
74 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080075 return rest_client.ResponseBody(resp, body)
Yaroslav Lobankov997a1452015-11-19 17:11:37 +030076
77 def delete_group(self, group_id):
78 """Delete a group."""
guo yunxian6cdf0562016-08-17 16:21:52 +080079 resp, body = self.delete('groups/%s' % group_id)
Yaroslav Lobankov997a1452015-11-19 17:11:37 +030080 self.expected_success(204, resp.status)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080081 return rest_client.ResponseBody(resp, body)
Yaroslav Lobankov997a1452015-11-19 17:11:37 +030082
83 def add_group_user(self, group_id, user_id):
84 """Add user into group."""
85 resp, body = self.put('groups/%s/users/%s' % (group_id, user_id),
86 None)
87 self.expected_success(204, resp.status)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -080088 return rest_client.ResponseBody(resp, body)
Yaroslav Lobankov997a1452015-11-19 17:11:37 +030089
ghanshyam83e94d42016-08-01 16:48:06 +090090 def list_group_users(self, group_id, **params):
91 """List users in group.
92
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090093 For a full list of available parameters, please refer to the official
94 API reference:
95 http://developer.openstack.org/api-ref/identity/v3/#list-users-in-group
ghanshyam83e94d42016-08-01 16:48:06 +090096 """
97 url = 'groups/%s/users' % group_id
98 if params:
99 url += '?%s' % urllib.urlencode(params)
100 resp, body = self.get(url)
Yaroslav Lobankov997a1452015-11-19 17:11:37 +0300101 self.expected_success(200, resp.status)
102 body = json.loads(body)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -0800103 return rest_client.ResponseBody(resp, body)
Yaroslav Lobankov997a1452015-11-19 17:11:37 +0300104
105 def delete_group_user(self, group_id, user_id):
106 """Delete user in group."""
107 resp, body = self.delete('groups/%s/users/%s' % (group_id, user_id))
108 self.expected_success(204, resp.status)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -0800109 return rest_client.ResponseBody(resp, body)
Maho Koshiyab6fa2e42015-12-07 16:52:53 +0900110
111 def check_group_user_existence(self, group_id, user_id):
112 """Check user in group."""
113 resp, body = self.head('groups/%s/users/%s' % (group_id, user_id))
114 self.expected_success(204, resp.status)
Ken'ichi Ohmichid35a1332016-03-02 10:38:07 -0800115 return rest_client.ResponseBody(resp)