blob: a3ff92d7e8973e0a8808f67eae0e42e1b7614f04 [file] [log] [blame]
Sean Dague556add52013-07-19 14:28:44 -04001# 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
chris fattarsi8ed39ac2012-04-30 14:11:27 -070013import json
14
vponomaryov67b58fe2014-02-06 19:05:41 +020015from tempest.common import rest_client
Ken'ichi Ohmichi0e836652015-01-08 04:38:56 +000016from tempest.common import service_client
Matthew Treinish684d8992014-01-30 16:27:40 +000017from tempest import config
Matthew Treinisha83a16e2012-12-07 13:44:02 -050018from tempest import exceptions
19
Matthew Treinish684d8992014-01-30 16:27:40 +000020CONF = config.CONF
21
chris fattarsi8ed39ac2012-04-30 14:11:27 -070022
Ken'ichi Ohmichi0e836652015-01-08 04:38:56 +000023class IdentityClientJSON(service_client.ServiceClient):
chris fattarsi8ed39ac2012-04-30 14:11:27 -070024
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000025 def __init__(self, auth_provider):
Ken'ichi Ohmichie9f50412015-01-05 04:57:26 +000026 super(IdentityClientJSON, self).__init__(
27 auth_provider,
28 CONF.identity.catalog_type,
29 CONF.identity.region,
30 endpoint_type='adminURL')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070031
32 def has_admin_extensions(self):
33 """
34 Returns True if the KSADM Admin Extensions are supported
35 False otherwise
36 """
37 if hasattr(self, '_has_admin_extensions'):
38 return self._has_admin_extensions
David Kranze9d2f422014-07-02 13:57:41 -040039 # Try something that requires admin
40 try:
41 self.list_roles()
42 self._has_admin_extensions = True
43 except Exception:
44 self._has_admin_extensions = False
chris fattarsi8ed39ac2012-04-30 14:11:27 -070045 return self._has_admin_extensions
46
47 def create_role(self, name):
Sean Daguef237ccb2013-01-04 15:19:14 -050048 """Create a role."""
chris fattarsi8ed39ac2012-04-30 14:11:27 -070049 post_body = {
50 'name': name,
51 }
52 post_body = json.dumps({'role': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +020053 resp, body = self.post('OS-KSADM/roles', post_body)
David Kranze9d2f422014-07-02 13:57:41 -040054 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -050055 return rest_client.ResponseBody(resp, self._parse_resp(body))
chris fattarsi8ed39ac2012-04-30 14:11:27 -070056
Gong Zhangcb6b8862014-02-20 15:14:05 +080057 def get_role(self, role_id):
58 """Get a role by its id."""
59 resp, body = self.get('OS-KSADM/roles/%s' % role_id)
David Kranze9d2f422014-07-02 13:57:41 -040060 self.expected_success(200, resp.status)
Gong Zhangcb6b8862014-02-20 15:14:05 +080061 body = json.loads(body)
David Kranzb7afa922014-12-30 10:56:26 -050062 return rest_client.ResponseBody(resp, body['role'])
Gong Zhangcb6b8862014-02-20 15:14:05 +080063
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070064 def create_tenant(self, name, **kwargs):
65 """
66 Create a tenant
67 name (required): New tenant name
68 description: Description of new tenant (default is none)
69 enabled <true|false>: Initial tenant status (default is true)
70 """
71 post_body = {
72 'name': name,
73 'description': kwargs.get('description', ''),
Gordon Chungad873602013-02-18 19:26:27 -050074 'enabled': kwargs.get('enabled', True),
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070075 }
76 post_body = json.dumps({'tenant': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +020077 resp, body = self.post('tenants', post_body)
David Kranze9d2f422014-07-02 13:57:41 -040078 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -050079 return rest_client.ResponseBody(resp, self._parse_resp(body))
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070080
chris fattarsi8ed39ac2012-04-30 14:11:27 -070081 def delete_role(self, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050082 """Delete a role."""
David Kranze9d2f422014-07-02 13:57:41 -040083 resp, body = self.delete('OS-KSADM/roles/%s' % str(role_id))
84 self.expected_success(204, resp.status)
85 return resp, body
chris fattarsi8ed39ac2012-04-30 14:11:27 -070086
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053087 def list_user_roles(self, tenant_id, user_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050088 """Returns a list of roles assigned to a user for a tenant."""
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053089 url = '/tenants/%s/users/%s/roles' % (tenant_id, user_id)
90 resp, body = self.get(url)
David Kranze9d2f422014-07-02 13:57:41 -040091 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -050092 return rest_client.ResponseBodyList(resp, self._parse_resp(body))
Rohit Karajgi69e80a02012-05-15 03:54:04 -070093
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053094 def assign_user_role(self, tenant_id, user_id, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050095 """Add roles to a user on a tenant."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +080096 resp, body = self.put('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
vponomaryov67b58fe2014-02-06 19:05:41 +020097 (tenant_id, user_id, role_id), "")
David Kranze9d2f422014-07-02 13:57:41 -040098 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -050099 return rest_client.ResponseBody(resp, self._parse_resp(body))
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700100
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530101 def remove_user_role(self, tenant_id, user_id, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500102 """Removes a role assignment for a user on a tenant."""
David Kranze9d2f422014-07-02 13:57:41 -0400103 resp, body = self.delete('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
104 (tenant_id, user_id, role_id))
105 self.expected_success(204, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500106 return rest_client.ResponseBody(resp, body)
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700107
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700108 def delete_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500109 """Delete a tenant."""
David Kranze9d2f422014-07-02 13:57:41 -0400110 resp, body = self.delete('tenants/%s' % str(tenant_id))
111 self.expected_success(204, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500112 return rest_client.ResponseBody(resp, body)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700113
114 def get_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500115 """Get tenant details."""
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700116 resp, body = self.get('tenants/%s' % str(tenant_id))
David Kranze9d2f422014-07-02 13:57:41 -0400117 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500118 return rest_client.ResponseBody(resp, self._parse_resp(body))
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700119
chris fattarsi8ed39ac2012-04-30 14:11:27 -0700120 def list_roles(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500121 """Returns roles."""
chris fattarsi8ed39ac2012-04-30 14:11:27 -0700122 resp, body = self.get('OS-KSADM/roles')
David Kranze9d2f422014-07-02 13:57:41 -0400123 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500124 return rest_client.ResponseBodyList(resp, self._parse_resp(body))
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700125
126 def list_tenants(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500127 """Returns tenants."""
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700128 resp, body = self.get('tenants')
David Kranze9d2f422014-07-02 13:57:41 -0400129 self.expected_success(200, resp.status)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700130 body = json.loads(body)
David Kranzb7afa922014-12-30 10:56:26 -0500131 return rest_client.ResponseBodyList(resp, body['tenants'])
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700132
Dan Smithd6ff6b72012-08-23 10:29:41 -0700133 def get_tenant_by_name(self, tenant_name):
David Kranzb7afa922014-12-30 10:56:26 -0500134 tenants = self.list_tenants()
Dan Smithd6ff6b72012-08-23 10:29:41 -0700135 for tenant in tenants:
136 if tenant['name'] == tenant_name:
137 return tenant
138 raise exceptions.NotFound('No such tenant')
139
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700140 def update_tenant(self, tenant_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500141 """Updates a tenant."""
David Kranzb7afa922014-12-30 10:56:26 -0500142 body = self.get_tenant(tenant_id)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700143 name = kwargs.get('name', body['name'])
144 desc = kwargs.get('description', body['description'])
145 en = kwargs.get('enabled', body['enabled'])
146 post_body = {
147 'id': tenant_id,
148 'name': name,
149 'description': desc,
150 'enabled': en,
151 }
152 post_body = json.dumps({'tenant': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200153 resp, body = self.post('tenants/%s' % tenant_id, post_body)
David Kranze9d2f422014-07-02 13:57:41 -0400154 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500155 return rest_client.ResponseBody(resp, self._parse_resp(body))
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700156
huangtianhuafc8db4f2013-10-08 12:05:58 +0800157 def create_user(self, name, password, tenant_id, email, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500158 """Create a user."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700159 post_body = {
160 'name': name,
161 'password': password,
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700162 'email': email
163 }
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500164 if tenant_id is not None:
165 post_body['tenantId'] = tenant_id
huangtianhuafc8db4f2013-10-08 12:05:58 +0800166 if kwargs.get('enabled') is not None:
167 post_body['enabled'] = kwargs.get('enabled')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700168 post_body = json.dumps({'user': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200169 resp, body = self.post('users', post_body)
David Kranze9d2f422014-07-02 13:57:41 -0400170 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500171 return rest_client.ResponseBody(resp, self._parse_resp(body))
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700172
Chang Bo Guob36b2f12013-09-13 04:52:00 -0700173 def update_user(self, user_id, **kwargs):
174 """Updates a user."""
175 put_body = json.dumps({'user': kwargs})
vponomaryov67b58fe2014-02-06 19:05:41 +0200176 resp, body = self.put('users/%s' % user_id, put_body)
David Kranze9d2f422014-07-02 13:57:41 -0400177 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500178 return rest_client.ResponseBody(resp, self._parse_resp(body))
Chang Bo Guob36b2f12013-09-13 04:52:00 -0700179
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530180 def get_user(self, user_id):
181 """GET a user."""
182 resp, body = self.get("users/%s" % user_id)
David Kranze9d2f422014-07-02 13:57:41 -0400183 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500184 return rest_client.ResponseBody(resp, self._parse_resp(body))
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530185
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700186 def delete_user(self, user_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500187 """Delete a user."""
David Kranze9d2f422014-07-02 13:57:41 -0400188 resp, body = self.delete("users/%s" % user_id)
189 self.expected_success(204, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500190 return rest_client.ResponseBody(resp, body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700191
192 def get_users(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500193 """Get the list of users."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700194 resp, body = self.get("users")
David Kranze9d2f422014-07-02 13:57:41 -0400195 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500196 return rest_client.ResponseBodyList(resp, self._parse_resp(body))
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700197
198 def enable_disable_user(self, user_id, enabled):
Sean Daguef237ccb2013-01-04 15:19:14 -0500199 """Enables or disables a user."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700200 put_body = {
Sean Dague14c68182013-04-14 15:34:30 -0400201 'enabled': enabled
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700202 }
203 put_body = json.dumps({'user': put_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200204 resp, body = self.put('users/%s/enabled' % user_id, put_body)
David Kranze9d2f422014-07-02 13:57:41 -0400205 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500206 return rest_client.ResponseBody(resp, self._parse_resp(body))
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700207
Zhi Kun Liu30caeae2014-02-26 15:30:24 +0800208 def get_token(self, token_id):
209 """Get token details."""
210 resp, body = self.get("tokens/%s" % token_id)
David Kranze9d2f422014-07-02 13:57:41 -0400211 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500212 return rest_client.ResponseBody(resp, self._parse_resp(body))
Zhi Kun Liu30caeae2014-02-26 15:30:24 +0800213
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700214 def delete_token(self, token_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500215 """Delete a token."""
David Kranze9d2f422014-07-02 13:57:41 -0400216 resp, body = self.delete("tokens/%s" % token_id)
217 self.expected_success(204, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500218 return rest_client.ResponseBody(resp, body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700219
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530220 def list_users_for_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500221 """List users for a Tenant."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530222 resp, body = self.get('/tenants/%s/users' % tenant_id)
David Kranze9d2f422014-07-02 13:57:41 -0400223 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500224 return rest_client.ResponseBodyList(resp, self._parse_resp(body))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530225
Dan Smithd6ff6b72012-08-23 10:29:41 -0700226 def get_user_by_username(self, tenant_id, username):
David Kranzb7afa922014-12-30 10:56:26 -0500227 users = self.list_users_for_tenant(tenant_id)
Dan Smithd6ff6b72012-08-23 10:29:41 -0700228 for user in users:
229 if user['name'] == username:
230 return user
231 raise exceptions.NotFound('No such user')
232
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530233 def create_service(self, name, type, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500234 """Create a service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530235 post_body = {
Zhongyue Luoa1343de2013-01-04 16:21:35 +0800236 'name': name,
237 'type': type,
238 'description': kwargs.get('description')
239 }
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530240 post_body = json.dumps({'OS-KSADM:service': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200241 resp, body = self.post('/OS-KSADM/services', post_body)
David Kranze9d2f422014-07-02 13:57:41 -0400242 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500243 return rest_client.ResponseBody(resp, self._parse_resp(body))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530244
245 def get_service(self, service_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500246 """Get Service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530247 url = '/OS-KSADM/services/%s' % service_id
248 resp, body = self.get(url)
David Kranze9d2f422014-07-02 13:57:41 -0400249 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500250 return rest_client.ResponseBody(resp, self._parse_resp(body))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530251
umamohanb51ad002013-01-24 18:13:15 +0000252 def list_services(self):
253 """List Service - Returns Services."""
Tushar Kalraa76929c2014-03-31 12:23:07 -0700254 resp, body = self.get('/OS-KSADM/services')
David Kranze9d2f422014-07-02 13:57:41 -0400255 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500256 return rest_client.ResponseBodyList(resp, self._parse_resp(body))
umamohanb51ad002013-01-24 18:13:15 +0000257
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530258 def delete_service(self, service_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500259 """Delete Service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530260 url = '/OS-KSADM/services/%s' % service_id
David Kranze9d2f422014-07-02 13:57:41 -0400261 resp, body = self.delete(url)
262 self.expected_success(204, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500263 return rest_client.ResponseBody(resp, body)
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530264
Abhijeet.Jainff5c3542014-05-06 16:07:30 +0530265 def update_user_password(self, user_id, new_pass):
266 """Update User Password."""
267 put_body = {
268 'password': new_pass,
269 'id': user_id
270 }
271 put_body = json.dumps({'user': put_body})
272 resp, body = self.put('users/%s/OS-KSADM/password' % user_id, put_body)
David Kranze9d2f422014-07-02 13:57:41 -0400273 self.expected_success(200, resp.status)
David Kranzb7afa922014-12-30 10:56:26 -0500274 return rest_client.ResponseBody(resp, self._parse_resp(body))
Abhijeet.Jainff5c3542014-05-06 16:07:30 +0530275
Abhijeet.Jain3f49b842014-05-20 12:06:20 +0530276 def list_extensions(self):
277 """List all the extensions."""
278 resp, body = self.get('/extensions')
David Kranze9d2f422014-07-02 13:57:41 -0400279 self.expected_success(200, resp.status)
Abhijeet.Jain3f49b842014-05-20 12:06:20 +0530280 body = json.loads(body)
David Kranzb7afa922014-12-30 10:56:26 -0500281 return rest_client.ResponseBodyList(resp, body['extensions']['values'])
Abhijeet.Jain3f49b842014-05-20 12:06:20 +0530282
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700283
vponomaryov67b58fe2014-02-06 19:05:41 +0200284class TokenClientJSON(IdentityClientJSON):
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700285
Matthew Treinish684d8992014-01-30 16:27:40 +0000286 def __init__(self):
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000287 super(TokenClientJSON, self).__init__(None)
Matthew Treinish684d8992014-01-30 16:27:40 +0000288 auth_url = CONF.identity.uri
Jay Pipes7c88eb22013-01-16 21:32:43 -0500289
Jay Pipes7c88eb22013-01-16 21:32:43 -0500290 # Normalize URI to ensure /tokens is in it.
291 if 'tokens' not in auth_url:
292 auth_url = auth_url.rstrip('/') + '/tokens'
293
294 self.auth_url = auth_url
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700295
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500296 def auth(self, user, password, tenant=None):
Zhongyue Luo30a563f2012-09-30 23:43:50 +0900297 creds = {
298 'auth': {
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700299 'passwordCredentials': {
300 'username': user,
301 'password': password,
302 },
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700303 }
304 }
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500305
306 if tenant:
307 creds['auth']['tenantName'] = tenant
308
309 body = json.dumps(creds)
310 resp, body = self.post(self.auth_url, body=body)
David Kranzfb3efa72014-08-28 16:58:25 -0400311 self.expected_success(200, resp.status)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500312
David Kranzb7afa922014-12-30 10:56:26 -0500313 return rest_client.ResponseBody(resp, body['access'])
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500314
315 def auth_token(self, token_id, tenant=None):
316 creds = {
317 'auth': {
318 'token': {
319 'id': token_id,
320 },
321 }
322 }
323
324 if tenant:
325 creds['auth']['tenantName'] = tenant
326
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700327 body = json.dumps(creds)
vponomaryov67b58fe2014-02-06 19:05:41 +0200328 resp, body = self.post(self.auth_url, body=body)
David Kranzfb3efa72014-08-28 16:58:25 -0400329 self.expected_success(200, resp.status)
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000330
David Kranzb7afa922014-12-30 10:56:26 -0500331 return rest_client.ResponseBody(resp, body['access'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700332
Sergey Murashov4fccd322014-03-22 09:58:52 +0400333 def request(self, method, url, extra_headers=False, headers=None,
334 body=None):
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700335 """A simple HTTP request interface."""
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800336 if headers is None:
vponomaryov67b58fe2014-02-06 19:05:41 +0200337 headers = self.get_headers(accept_type="json")
Sergey Murashov4fccd322014-03-22 09:58:52 +0400338 elif extra_headers:
339 try:
340 headers.update(self.get_headers(accept_type="json"))
341 except (ValueError, TypeError):
342 headers = self.get_headers(accept_type="json")
343
Ken'ichi Ohmichi34f34722015-01-08 07:56:23 +0000344 resp, resp_body = self.raw_request(url, method,
345 headers=headers, body=body)
Sean Dague89a85912014-03-19 16:37:29 -0400346 self._log_request(method, url, resp)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700347
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000348 if resp.status in [401, 403]:
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700349 resp_body = json.loads(resp_body)
350 raise exceptions.Unauthorized(resp_body['error']['message'])
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000351 elif resp.status not in [200, 201]:
352 raise exceptions.IdentityError(
353 'Unexpected status code {0}'.format(resp.status))
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700354
vponomaryov67b58fe2014-02-06 19:05:41 +0200355 if isinstance(resp_body, str):
356 resp_body = json.loads(resp_body)
357 return resp, resp_body
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700358
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000359 def get_token(self, user, password, tenant, auth_data=False):
360 """
361 Returns (token id, token data) for supplied credentials
362 """
David Kranzb7afa922014-12-30 10:56:26 -0500363 body = self.auth(user, password, tenant)
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000364
365 if auth_data:
366 return body['token']['id'], body
367 else:
368 return body['token']['id']