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