blob: 93936bcf3d21a1b2d82e547f5ed77fa71f16f1d7 [file] [log] [blame]
ghanshyamc0edda02015-02-06 15:51:40 +09001# Copyright 2015 NEC Corporation. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# 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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import json
16
17from tempest.common import service_client
18from tempest import config
19from tempest import exceptions
20
21CONF = config.CONF
22
23
24class TokenClientJSON(service_client.ServiceClient):
25
26 def __init__(self):
27 super(TokenClientJSON, self).__init__(None, None, None)
28 auth_url = CONF.identity.uri
29
30 # Normalize URI to ensure /tokens is in it.
31 if 'tokens' not in auth_url:
32 auth_url = auth_url.rstrip('/') + '/tokens'
33
34 self.auth_url = auth_url
35
36 def auth(self, user, password, tenant=None):
37 creds = {
38 'auth': {
39 'passwordCredentials': {
40 'username': user,
41 'password': password,
42 },
43 }
44 }
45
46 if tenant:
47 creds['auth']['tenantName'] = tenant
48
49 body = json.dumps(creds)
50 resp, body = self.post(self.auth_url, body=body)
51 self.expected_success(200, resp.status)
52
53 return service_client.ResponseBody(resp, body['access'])
54
55 def auth_token(self, token_id, tenant=None):
56 creds = {
57 'auth': {
58 'token': {
59 'id': token_id,
60 },
61 }
62 }
63
64 if tenant:
65 creds['auth']['tenantName'] = tenant
66
67 body = json.dumps(creds)
68 resp, body = self.post(self.auth_url, body=body)
69 self.expected_success(200, resp.status)
70
71 return service_client.ResponseBody(resp, body['access'])
72
73 def request(self, method, url, extra_headers=False, headers=None,
74 body=None):
75 """A simple HTTP request interface."""
76 if headers is None:
77 headers = self.get_headers(accept_type="json")
78 elif extra_headers:
79 try:
80 headers.update(self.get_headers(accept_type="json"))
81 except (ValueError, TypeError):
82 headers = self.get_headers(accept_type="json")
83
84 resp, resp_body = self.raw_request(url, method,
85 headers=headers, body=body)
86 self._log_request(method, url, resp)
87
88 if resp.status in [401, 403]:
89 resp_body = json.loads(resp_body)
90 raise exceptions.Unauthorized(resp_body['error']['message'])
91 elif resp.status not in [200, 201]:
92 raise exceptions.IdentityError(
93 'Unexpected status code {0}'.format(resp.status))
94
95 if isinstance(resp_body, str):
96 resp_body = json.loads(resp_body)
97 return resp, resp_body
98
99 def get_token(self, user, password, tenant, auth_data=False):
100 """
101 Returns (token id, token data) for supplied credentials
102 """
103 body = self.auth(user, password, tenant)
104
105 if auth_data:
106 return body['token']['id'], body
107 else:
108 return body['token']['id']