Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [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 | from oslo_log import log as logging |
| 16 | from oslo_serialization import jsonutils as json |
| 17 | |
| 18 | from tempest.lib.common import rest_client |
| 19 | from tempest.lib import exceptions |
| 20 | |
| 21 | |
| 22 | class TokenClient(rest_client.RestClient): |
| 23 | |
| 24 | def __init__(self, auth_url, disable_ssl_certificate_validation=None, |
zhufl | 071e94c | 2016-07-12 10:26:34 +0800 | [diff] [blame] | 25 | ca_certs=None, trace_requests=None, **kwargs): |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 26 | dscv = disable_ssl_certificate_validation |
| 27 | super(TokenClient, self).__init__( |
| 28 | None, None, None, disable_ssl_certificate_validation=dscv, |
zhufl | 071e94c | 2016-07-12 10:26:34 +0800 | [diff] [blame] | 29 | ca_certs=ca_certs, trace_requests=trace_requests, **kwargs) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 30 | |
| 31 | if auth_url is None: |
| 32 | raise exceptions.IdentityError("Couldn't determine auth_url") |
| 33 | |
| 34 | # Normalize URI to ensure /tokens is in it. |
| 35 | if 'tokens' not in auth_url: |
| 36 | auth_url = auth_url.rstrip('/') + '/tokens' |
| 37 | |
| 38 | self.auth_url = auth_url |
| 39 | |
| 40 | def auth(self, user, password, tenant=None): |
| 41 | creds = { |
| 42 | 'auth': { |
| 43 | 'passwordCredentials': { |
| 44 | 'username': user, |
| 45 | 'password': password, |
| 46 | }, |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if tenant: |
| 51 | creds['auth']['tenantName'] = tenant |
| 52 | |
| 53 | body = json.dumps(creds, sort_keys=True) |
| 54 | resp, body = self.post(self.auth_url, body=body) |
| 55 | self.expected_success(200, resp.status) |
| 56 | |
| 57 | return rest_client.ResponseBody(resp, body['access']) |
| 58 | |
| 59 | def auth_token(self, token_id, tenant=None): |
| 60 | creds = { |
| 61 | 'auth': { |
| 62 | 'token': { |
| 63 | 'id': token_id, |
| 64 | }, |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if tenant: |
| 69 | creds['auth']['tenantName'] = tenant |
| 70 | |
| 71 | body = json.dumps(creds) |
| 72 | resp, body = self.post(self.auth_url, body=body) |
| 73 | self.expected_success(200, resp.status) |
| 74 | |
| 75 | return rest_client.ResponseBody(resp, body['access']) |
| 76 | |
| 77 | def request(self, method, url, extra_headers=False, headers=None, |
Jordan Pittier | 4408c4a | 2016-04-29 15:05:09 +0200 | [diff] [blame] | 78 | body=None, chunked=False): |
| 79 | """A simple HTTP request interface. |
| 80 | |
| 81 | Note: this overloads the `request` method from the parent class and |
| 82 | thus must implement the same method signature. |
| 83 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 84 | if headers is None: |
| 85 | headers = self.get_headers(accept_type="json") |
| 86 | elif extra_headers: |
| 87 | try: |
| 88 | headers.update(self.get_headers(accept_type="json")) |
| 89 | except (ValueError, TypeError): |
| 90 | headers = self.get_headers(accept_type="json") |
| 91 | |
| 92 | resp, resp_body = self.raw_request(url, method, |
| 93 | headers=headers, body=body) |
| 94 | self._log_request(method, url, resp, req_headers=headers, |
| 95 | req_body='<omitted>', resp_body=resp_body) |
| 96 | |
| 97 | if resp.status in [401, 403]: |
| 98 | resp_body = json.loads(resp_body) |
| 99 | raise exceptions.Unauthorized(resp_body['error']['message']) |
| 100 | elif resp.status not in [200, 201]: |
| 101 | raise exceptions.IdentityError( |
| 102 | 'Unexpected status code {0}'.format(resp.status)) |
| 103 | |
| 104 | return resp, json.loads(resp_body) |
| 105 | |
| 106 | def get_token(self, user, password, tenant, auth_data=False): |
| 107 | """Returns (token id, token data) for supplied credentials.""" |
| 108 | body = self.auth(user, password, tenant) |
| 109 | |
| 110 | if auth_data: |
| 111 | return body['token']['id'], body |
| 112 | else: |
| 113 | return body['token']['id'] |
| 114 | |
| 115 | |
| 116 | class TokenClientJSON(TokenClient): |
| 117 | LOG = logging.getLogger(__name__) |
| 118 | |
| 119 | def _warn(self): |
| 120 | self.LOG.warning("%s class was deprecated and renamed to %s" % |
| 121 | (self.__class__.__name__, 'TokenClient')) |
| 122 | |
| 123 | def __init__(self, *args, **kwargs): |
| 124 | self._warn() |
| 125 | super(TokenClientJSON, self).__init__(*args, **kwargs) |