blob: a6c5049afb361e21ae756401bc01a5a5ebd901a6 [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
Ken'ichi Ohmichi0e836652015-01-08 04:38:56 +000015from tempest.common import service_client
Matthew Treinish684d8992014-01-30 16:27:40 +000016from tempest import config
Matthew Treinisha83a16e2012-12-07 13:44:02 -050017from tempest import exceptions
18
Matthew Treinish684d8992014-01-30 16:27:40 +000019CONF = config.CONF
20
chris fattarsi8ed39ac2012-04-30 14:11:27 -070021
Ken'ichi Ohmichi0e836652015-01-08 04:38:56 +000022class IdentityClientJSON(service_client.ServiceClient):
chris fattarsi8ed39ac2012-04-30 14:11:27 -070023
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000024 def __init__(self, auth_provider):
Ken'ichi Ohmichie9f50412015-01-05 04:57:26 +000025 super(IdentityClientJSON, self).__init__(
26 auth_provider,
27 CONF.identity.catalog_type,
28 CONF.identity.region,
29 endpoint_type='adminURL')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070030
31 def has_admin_extensions(self):
32 """
33 Returns True if the KSADM Admin Extensions are supported
34 False otherwise
35 """
36 if hasattr(self, '_has_admin_extensions'):
37 return self._has_admin_extensions
David Kranze9d2f422014-07-02 13:57:41 -040038 # Try something that requires admin
39 try:
40 self.list_roles()
41 self._has_admin_extensions = True
42 except Exception:
43 self._has_admin_extensions = False
chris fattarsi8ed39ac2012-04-30 14:11:27 -070044 return self._has_admin_extensions
45
46 def create_role(self, name):
Sean Daguef237ccb2013-01-04 15:19:14 -050047 """Create a role."""
chris fattarsi8ed39ac2012-04-30 14:11:27 -070048 post_body = {
49 'name': name,
50 }
51 post_body = json.dumps({'role': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +020052 resp, body = self.post('OS-KSADM/roles', post_body)
David Kranze9d2f422014-07-02 13:57:41 -040053 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000054 return service_client.ResponseBody(resp, self._parse_resp(body))
chris fattarsi8ed39ac2012-04-30 14:11:27 -070055
Gong Zhangcb6b8862014-02-20 15:14:05 +080056 def get_role(self, role_id):
57 """Get a role by its id."""
58 resp, body = self.get('OS-KSADM/roles/%s' % role_id)
David Kranze9d2f422014-07-02 13:57:41 -040059 self.expected_success(200, resp.status)
Gong Zhangcb6b8862014-02-20 15:14:05 +080060 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000061 return service_client.ResponseBody(resp, body['role'])
Gong Zhangcb6b8862014-02-20 15:14:05 +080062
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070063 def create_tenant(self, name, **kwargs):
64 """
65 Create a tenant
66 name (required): New tenant name
67 description: Description of new tenant (default is none)
68 enabled <true|false>: Initial tenant status (default is true)
69 """
70 post_body = {
71 'name': name,
72 'description': kwargs.get('description', ''),
Gordon Chungad873602013-02-18 19:26:27 -050073 'enabled': kwargs.get('enabled', True),
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070074 }
75 post_body = json.dumps({'tenant': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +020076 resp, body = self.post('tenants', post_body)
David Kranze9d2f422014-07-02 13:57:41 -040077 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000078 return service_client.ResponseBody(resp, self._parse_resp(body))
chris fattarsi9ba7b0e2012-05-07 13:55:51 -070079
chris fattarsi8ed39ac2012-04-30 14:11:27 -070080 def delete_role(self, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050081 """Delete a role."""
David Kranze9d2f422014-07-02 13:57:41 -040082 resp, body = self.delete('OS-KSADM/roles/%s' % str(role_id))
83 self.expected_success(204, resp.status)
84 return resp, body
chris fattarsi8ed39ac2012-04-30 14:11:27 -070085
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053086 def list_user_roles(self, tenant_id, user_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050087 """Returns a list of roles assigned to a user for a tenant."""
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053088 url = '/tenants/%s/users/%s/roles' % (tenant_id, user_id)
89 resp, body = self.get(url)
David Kranze9d2f422014-07-02 13:57:41 -040090 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000091 return service_client.ResponseBodyList(resp, self._parse_resp(body))
Rohit Karajgi69e80a02012-05-15 03:54:04 -070092
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053093 def assign_user_role(self, tenant_id, user_id, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050094 """Add roles to a user on a tenant."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +080095 resp, body = self.put('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
vponomaryov67b58fe2014-02-06 19:05:41 +020096 (tenant_id, user_id, role_id), "")
David Kranze9d2f422014-07-02 13:57:41 -040097 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000098 return service_client.ResponseBody(resp, self._parse_resp(body))
Rohit Karajgi69e80a02012-05-15 03:54:04 -070099
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530100 def remove_user_role(self, tenant_id, user_id, role_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500101 """Removes a role assignment for a user on a tenant."""
David Kranze9d2f422014-07-02 13:57:41 -0400102 resp, body = self.delete('/tenants/%s/users/%s/roles/OS-KSADM/%s' %
103 (tenant_id, user_id, role_id))
104 self.expected_success(204, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000105 return service_client.ResponseBody(resp, body)
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700106
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700107 def delete_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500108 """Delete a tenant."""
David Kranze9d2f422014-07-02 13:57:41 -0400109 resp, body = self.delete('tenants/%s' % str(tenant_id))
110 self.expected_success(204, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000111 return service_client.ResponseBody(resp, body)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700112
113 def get_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500114 """Get tenant details."""
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700115 resp, body = self.get('tenants/%s' % str(tenant_id))
David Kranze9d2f422014-07-02 13:57:41 -0400116 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000117 return service_client.ResponseBody(resp, self._parse_resp(body))
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700118
chris fattarsi8ed39ac2012-04-30 14:11:27 -0700119 def list_roles(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500120 """Returns roles."""
chris fattarsi8ed39ac2012-04-30 14:11:27 -0700121 resp, body = self.get('OS-KSADM/roles')
David Kranze9d2f422014-07-02 13:57:41 -0400122 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000123 return service_client.ResponseBodyList(resp, self._parse_resp(body))
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700124
125 def list_tenants(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500126 """Returns tenants."""
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700127 resp, body = self.get('tenants')
David Kranze9d2f422014-07-02 13:57:41 -0400128 self.expected_success(200, resp.status)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700129 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000130 return service_client.ResponseBodyList(resp, body['tenants'])
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700131
Dan Smithd6ff6b72012-08-23 10:29:41 -0700132 def get_tenant_by_name(self, tenant_name):
David Kranzb7afa922014-12-30 10:56:26 -0500133 tenants = self.list_tenants()
Dan Smithd6ff6b72012-08-23 10:29:41 -0700134 for tenant in tenants:
135 if tenant['name'] == tenant_name:
136 return tenant
137 raise exceptions.NotFound('No such tenant')
138
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700139 def update_tenant(self, tenant_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500140 """Updates a tenant."""
David Kranzb7afa922014-12-30 10:56:26 -0500141 body = self.get_tenant(tenant_id)
chris fattarsi9ba7b0e2012-05-07 13:55:51 -0700142 name = kwargs.get('name', body['name'])
143 desc = kwargs.get('description', body['description'])
144 en = kwargs.get('enabled', body['enabled'])
145 post_body = {
146 'id': tenant_id,
147 'name': name,
148 'description': desc,
149 'enabled': en,
150 }
151 post_body = json.dumps({'tenant': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200152 resp, body = self.post('tenants/%s' % tenant_id, post_body)
David Kranze9d2f422014-07-02 13:57:41 -0400153 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000154 return service_client.ResponseBody(resp, self._parse_resp(body))
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700155
huangtianhuafc8db4f2013-10-08 12:05:58 +0800156 def create_user(self, name, password, tenant_id, email, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500157 """Create a user."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700158 post_body = {
159 'name': name,
160 'password': password,
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700161 'email': email
162 }
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500163 if tenant_id is not None:
164 post_body['tenantId'] = tenant_id
huangtianhuafc8db4f2013-10-08 12:05:58 +0800165 if kwargs.get('enabled') is not None:
166 post_body['enabled'] = kwargs.get('enabled')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700167 post_body = json.dumps({'user': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200168 resp, body = self.post('users', post_body)
David Kranze9d2f422014-07-02 13:57:41 -0400169 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000170 return service_client.ResponseBody(resp, self._parse_resp(body))
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700171
Chang Bo Guob36b2f12013-09-13 04:52:00 -0700172 def update_user(self, user_id, **kwargs):
173 """Updates a user."""
174 put_body = json.dumps({'user': kwargs})
vponomaryov67b58fe2014-02-06 19:05:41 +0200175 resp, body = self.put('users/%s' % user_id, put_body)
David Kranze9d2f422014-07-02 13:57:41 -0400176 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000177 return service_client.ResponseBody(resp, self._parse_resp(body))
Chang Bo Guob36b2f12013-09-13 04:52:00 -0700178
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530179 def get_user(self, user_id):
180 """GET a user."""
181 resp, body = self.get("users/%s" % user_id)
David Kranze9d2f422014-07-02 13:57:41 -0400182 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000183 return service_client.ResponseBody(resp, self._parse_resp(body))
rajalakshmi-ganesan7312bb52013-01-29 20:03:42 +0530184
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700185 def delete_user(self, user_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500186 """Delete a user."""
David Kranze9d2f422014-07-02 13:57:41 -0400187 resp, body = self.delete("users/%s" % user_id)
188 self.expected_success(204, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000189 return service_client.ResponseBody(resp, body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700190
191 def get_users(self):
Sean Daguef237ccb2013-01-04 15:19:14 -0500192 """Get the list of users."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700193 resp, body = self.get("users")
David Kranze9d2f422014-07-02 13:57:41 -0400194 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000195 return service_client.ResponseBodyList(resp, self._parse_resp(body))
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700196
197 def enable_disable_user(self, user_id, enabled):
Sean Daguef237ccb2013-01-04 15:19:14 -0500198 """Enables or disables a user."""
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700199 put_body = {
Sean Dague14c68182013-04-14 15:34:30 -0400200 'enabled': enabled
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700201 }
202 put_body = json.dumps({'user': put_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200203 resp, body = self.put('users/%s/enabled' % user_id, put_body)
David Kranze9d2f422014-07-02 13:57:41 -0400204 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000205 return service_client.ResponseBody(resp, self._parse_resp(body))
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700206
Zhi Kun Liu30caeae2014-02-26 15:30:24 +0800207 def get_token(self, token_id):
208 """Get token details."""
209 resp, body = self.get("tokens/%s" % token_id)
David Kranze9d2f422014-07-02 13:57:41 -0400210 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000211 return service_client.ResponseBody(resp, self._parse_resp(body))
Zhi Kun Liu30caeae2014-02-26 15:30:24 +0800212
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700213 def delete_token(self, token_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500214 """Delete a token."""
David Kranze9d2f422014-07-02 13:57:41 -0400215 resp, body = self.delete("tokens/%s" % token_id)
216 self.expected_success(204, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000217 return service_client.ResponseBody(resp, body)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700218
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530219 def list_users_for_tenant(self, tenant_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500220 """List users for a Tenant."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530221 resp, body = self.get('/tenants/%s/users' % tenant_id)
David Kranze9d2f422014-07-02 13:57:41 -0400222 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000223 return service_client.ResponseBodyList(resp, self._parse_resp(body))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530224
Dan Smithd6ff6b72012-08-23 10:29:41 -0700225 def get_user_by_username(self, tenant_id, username):
David Kranzb7afa922014-12-30 10:56:26 -0500226 users = self.list_users_for_tenant(tenant_id)
Dan Smithd6ff6b72012-08-23 10:29:41 -0700227 for user in users:
228 if user['name'] == username:
229 return user
230 raise exceptions.NotFound('No such user')
231
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530232 def create_service(self, name, type, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500233 """Create a service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530234 post_body = {
Zhongyue Luoa1343de2013-01-04 16:21:35 +0800235 'name': name,
236 'type': type,
237 'description': kwargs.get('description')
238 }
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530239 post_body = json.dumps({'OS-KSADM:service': post_body})
vponomaryov67b58fe2014-02-06 19:05:41 +0200240 resp, body = self.post('/OS-KSADM/services', post_body)
David Kranze9d2f422014-07-02 13:57:41 -0400241 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000242 return service_client.ResponseBody(resp, self._parse_resp(body))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530243
244 def get_service(self, service_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500245 """Get Service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530246 url = '/OS-KSADM/services/%s' % service_id
247 resp, body = self.get(url)
David Kranze9d2f422014-07-02 13:57:41 -0400248 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000249 return service_client.ResponseBody(resp, self._parse_resp(body))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530250
umamohanb51ad002013-01-24 18:13:15 +0000251 def list_services(self):
252 """List Service - Returns Services."""
Tushar Kalraa76929c2014-03-31 12:23:07 -0700253 resp, body = self.get('/OS-KSADM/services')
David Kranze9d2f422014-07-02 13:57:41 -0400254 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000255 return service_client.ResponseBodyList(resp, self._parse_resp(body))
umamohanb51ad002013-01-24 18:13:15 +0000256
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530257 def delete_service(self, service_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500258 """Delete Service."""
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530259 url = '/OS-KSADM/services/%s' % service_id
David Kranze9d2f422014-07-02 13:57:41 -0400260 resp, body = self.delete(url)
261 self.expected_success(204, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000262 return service_client.ResponseBody(resp, body)
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530263
Abhijeet.Jainff5c3542014-05-06 16:07:30 +0530264 def update_user_password(self, user_id, new_pass):
265 """Update User Password."""
266 put_body = {
267 'password': new_pass,
268 'id': user_id
269 }
270 put_body = json.dumps({'user': put_body})
271 resp, body = self.put('users/%s/OS-KSADM/password' % user_id, put_body)
David Kranze9d2f422014-07-02 13:57:41 -0400272 self.expected_success(200, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000273 return service_client.ResponseBody(resp, self._parse_resp(body))
Abhijeet.Jainff5c3542014-05-06 16:07:30 +0530274
Abhijeet.Jain3f49b842014-05-20 12:06:20 +0530275 def list_extensions(self):
276 """List all the extensions."""
277 resp, body = self.get('/extensions')
David Kranze9d2f422014-07-02 13:57:41 -0400278 self.expected_success(200, resp.status)
Abhijeet.Jain3f49b842014-05-20 12:06:20 +0530279 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000280 return service_client.ResponseBodyList(resp,
281 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
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000313 return service_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
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +0000331 return service_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']