Ken'ichi Ohmichi | 0e83665 | 2015-01-08 04:38:56 +0000 | [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 | |
Ken'ichi Ohmichi | 79aede1 | 2015-01-17 08:19:47 +0000 | [diff] [blame] | 15 | from tempest_lib.common import rest_client |
| 16 | from tempest_lib import exceptions as lib_exceptions |
| 17 | |
Ken'ichi Ohmichi | 0e83665 | 2015-01-08 04:38:56 +0000 | [diff] [blame] | 18 | from tempest import config |
Ken'ichi Ohmichi | 79aede1 | 2015-01-17 08:19:47 +0000 | [diff] [blame] | 19 | from tempest import exceptions |
Ken'ichi Ohmichi | 0e83665 | 2015-01-08 04:38:56 +0000 | [diff] [blame] | 20 | |
| 21 | CONF = config.CONF |
| 22 | |
| 23 | |
| 24 | class ServiceClient(rest_client.RestClient): |
| 25 | |
| 26 | def __init__(self, auth_provider, service, region, |
Ken'ichi Ohmichi | c2b11ce | 2015-01-16 07:17:29 +0000 | [diff] [blame] | 27 | endpoint_type=None, build_interval=None, build_timeout=None, |
| 28 | disable_ssl_certificate_validation=None, ca_certs=None, |
| 29 | trace_requests=None): |
| 30 | |
| 31 | # TODO(oomichi): This params setting should be removed after all |
| 32 | # service clients pass these values, and we can make ServiceClient |
| 33 | # free from CONF values. |
| 34 | dscv = (disable_ssl_certificate_validation or |
| 35 | CONF.identity.disable_ssl_certificate_validation) |
Ken'ichi Ohmichi | 0e83665 | 2015-01-08 04:38:56 +0000 | [diff] [blame] | 36 | params = { |
Ken'ichi Ohmichi | c2b11ce | 2015-01-16 07:17:29 +0000 | [diff] [blame] | 37 | 'disable_ssl_certificate_validation': dscv, |
| 38 | 'ca_certs': ca_certs or CONF.identity.ca_certificates_file, |
| 39 | 'trace_requests': trace_requests or CONF.debug.trace_requests |
Ken'ichi Ohmichi | 0e83665 | 2015-01-08 04:38:56 +0000 | [diff] [blame] | 40 | } |
Ken'ichi Ohmichi | c2b11ce | 2015-01-16 07:17:29 +0000 | [diff] [blame] | 41 | |
Ken'ichi Ohmichi | 0e83665 | 2015-01-08 04:38:56 +0000 | [diff] [blame] | 42 | if endpoint_type is not None: |
| 43 | params.update({'endpoint_type': endpoint_type}) |
| 44 | if build_interval is not None: |
| 45 | params.update({'build_interval': build_interval}) |
| 46 | if build_timeout is not None: |
| 47 | params.update({'build_timeout': build_timeout}) |
| 48 | super(ServiceClient, self).__init__(auth_provider, service, region, |
| 49 | **params) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 50 | |
Ken'ichi Ohmichi | 79aede1 | 2015-01-17 08:19:47 +0000 | [diff] [blame] | 51 | def request(self, method, url, extra_headers=False, headers=None, |
| 52 | body=None): |
| 53 | # TODO(oomichi): This translation is just for avoiding a single |
| 54 | # huge patch to migrate rest_client module to tempest-lib. |
| 55 | # Ideally(in the future), we need to remove this translation and |
| 56 | # replace each API tests with tempest-lib's exceptions. |
| 57 | try: |
| 58 | return super(ServiceClient, self).request( |
| 59 | method, url, |
| 60 | extra_headers=extra_headers, |
| 61 | headers=headers, body=body) |
Ken'ichi Ohmichi | 79aede1 | 2015-01-17 08:19:47 +0000 | [diff] [blame] | 62 | except lib_exceptions.BadRequest as ex: |
| 63 | raise exceptions.BadRequest(ex) |
Ken'ichi Ohmichi | b8c2d7c | 2015-01-29 01:51:57 +0000 | [diff] [blame] | 64 | # TODO(oomichi): This is just a workaround for failing gate tests |
| 65 | # when separating Forbidden from Unauthorized in tempest-lib. |
| 66 | # We will need to remove this translation and replace negative tests |
| 67 | # with lib_exceptions.Forbidden in the future. |
| 68 | except lib_exceptions.Forbidden as ex: |
Masayuki Igawa | 29d554f | 2015-01-20 12:36:42 +0900 | [diff] [blame] | 69 | raise lib_exceptions.Unauthorized(ex) |
Ken'ichi Ohmichi | 79aede1 | 2015-01-17 08:19:47 +0000 | [diff] [blame] | 70 | |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 71 | |
| 72 | class ResponseBody(dict): |
| 73 | """Class that wraps an http response and dict body into a single value. |
| 74 | |
| 75 | Callers that receive this object will normally use it as a dict but |
| 76 | can extract the response if needed. |
| 77 | """ |
| 78 | |
| 79 | def __init__(self, response, body=None): |
| 80 | body_data = body or {} |
| 81 | self.update(body_data) |
| 82 | self.response = response |
| 83 | |
| 84 | def __str__(self): |
| 85 | body = super.__str__(self) |
| 86 | return "response: %s\nBody: %s" % (self.response, body) |
| 87 | |
| 88 | |
David Kranz | e4e3b41 | 2015-02-10 10:50:42 -0500 | [diff] [blame] | 89 | class ResponseBodyData(object): |
| 90 | """Class that wraps an http response and string data into a single value. |
| 91 | """ |
| 92 | |
| 93 | def __init__(self, response, data): |
| 94 | self.response = response |
| 95 | self.data = data |
| 96 | |
| 97 | def __str__(self): |
| 98 | return "response: %s\nBody: %s" % (self.response, self.data) |
| 99 | |
| 100 | |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 101 | class ResponseBodyList(list): |
| 102 | """Class that wraps an http response and list body into a single value. |
| 103 | |
| 104 | Callers that receive this object will normally use it as a list but |
| 105 | can extract the response if needed. |
| 106 | """ |
| 107 | |
| 108 | def __init__(self, response, body=None): |
| 109 | body_data = body or [] |
| 110 | self.extend(body_data) |
| 111 | self.response = response |
| 112 | |
| 113 | def __str__(self): |
| 114 | body = super.__str__(self) |
| 115 | return "response: %s\nBody: %s" % (self.response, body) |