ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 1 | # 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 | |
| 15 | import json |
Masayuki Igawa | 29d554f | 2015-01-20 12:36:42 +0900 | [diff] [blame] | 16 | from tempest_lib import exceptions as lib_exc |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 17 | |
| 18 | from tempest.common import service_client |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 19 | from tempest import exceptions |
| 20 | |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 21 | |
| 22 | class TokenClientJSON(service_client.ServiceClient): |
| 23 | |
ghanshyam | 5ff763f | 2015-02-18 16:15:58 +0900 | [diff] [blame^] | 24 | def __init__(self, auth_url): |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 25 | super(TokenClientJSON, self).__init__(None, None, None) |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 26 | |
| 27 | # Normalize URI to ensure /tokens is in it. |
| 28 | if 'tokens' not in auth_url: |
| 29 | auth_url = auth_url.rstrip('/') + '/tokens' |
| 30 | |
| 31 | self.auth_url = auth_url |
| 32 | |
| 33 | def auth(self, user, password, tenant=None): |
| 34 | creds = { |
| 35 | 'auth': { |
| 36 | 'passwordCredentials': { |
| 37 | 'username': user, |
| 38 | 'password': password, |
| 39 | }, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | if tenant: |
| 44 | creds['auth']['tenantName'] = tenant |
| 45 | |
| 46 | body = json.dumps(creds) |
| 47 | resp, body = self.post(self.auth_url, body=body) |
| 48 | self.expected_success(200, resp.status) |
| 49 | |
| 50 | return service_client.ResponseBody(resp, body['access']) |
| 51 | |
| 52 | def auth_token(self, token_id, tenant=None): |
| 53 | creds = { |
| 54 | 'auth': { |
| 55 | 'token': { |
| 56 | 'id': token_id, |
| 57 | }, |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if tenant: |
| 62 | creds['auth']['tenantName'] = tenant |
| 63 | |
| 64 | body = json.dumps(creds) |
| 65 | resp, body = self.post(self.auth_url, body=body) |
| 66 | self.expected_success(200, resp.status) |
| 67 | |
| 68 | return service_client.ResponseBody(resp, body['access']) |
| 69 | |
| 70 | def request(self, method, url, extra_headers=False, headers=None, |
| 71 | body=None): |
| 72 | """A simple HTTP request interface.""" |
| 73 | if headers is None: |
| 74 | headers = self.get_headers(accept_type="json") |
| 75 | elif extra_headers: |
| 76 | try: |
| 77 | headers.update(self.get_headers(accept_type="json")) |
| 78 | except (ValueError, TypeError): |
| 79 | headers = self.get_headers(accept_type="json") |
| 80 | |
| 81 | resp, resp_body = self.raw_request(url, method, |
| 82 | headers=headers, body=body) |
| 83 | self._log_request(method, url, resp) |
| 84 | |
| 85 | if resp.status in [401, 403]: |
| 86 | resp_body = json.loads(resp_body) |
Masayuki Igawa | 29d554f | 2015-01-20 12:36:42 +0900 | [diff] [blame] | 87 | raise lib_exc.Unauthorized(resp_body['error']['message']) |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 88 | elif resp.status not in [200, 201]: |
| 89 | raise exceptions.IdentityError( |
| 90 | 'Unexpected status code {0}'.format(resp.status)) |
| 91 | |
| 92 | if isinstance(resp_body, str): |
| 93 | resp_body = json.loads(resp_body) |
| 94 | return resp, resp_body |
| 95 | |
| 96 | def get_token(self, user, password, tenant, auth_data=False): |
| 97 | """ |
| 98 | Returns (token id, token data) for supplied credentials |
| 99 | """ |
| 100 | body = self.auth(user, password, tenant) |
| 101 | |
| 102 | if auth_data: |
| 103 | return body['token']['id'], body |
| 104 | else: |
| 105 | return body['token']['id'] |