Maru Newby | b096d9f | 2015-03-09 18:54:54 +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 | |
| 15 | from tempest_lib.common import rest_client |
| 16 | |
| 17 | from neutron.tests.tempest import config |
| 18 | |
| 19 | CONF = config.CONF |
| 20 | |
| 21 | |
| 22 | class ServiceClient(rest_client.RestClient): |
| 23 | |
| 24 | def __init__(self, auth_provider, service, region, |
| 25 | endpoint_type=None, build_interval=None, build_timeout=None, |
| 26 | disable_ssl_certificate_validation=None, ca_certs=None, |
| 27 | trace_requests=None): |
| 28 | |
| 29 | # TODO(oomichi): This params setting should be removed after all |
| 30 | # service clients pass these values, and we can make ServiceClient |
| 31 | # free from CONF values. |
| 32 | dscv = (disable_ssl_certificate_validation or |
| 33 | CONF.identity.disable_ssl_certificate_validation) |
| 34 | params = { |
| 35 | 'disable_ssl_certificate_validation': dscv, |
| 36 | 'ca_certs': ca_certs or CONF.identity.ca_certificates_file, |
| 37 | 'trace_requests': trace_requests or CONF.debug.trace_requests |
| 38 | } |
| 39 | |
| 40 | if endpoint_type is not None: |
| 41 | params.update({'endpoint_type': endpoint_type}) |
| 42 | if build_interval is not None: |
| 43 | params.update({'build_interval': build_interval}) |
| 44 | if build_timeout is not None: |
| 45 | params.update({'build_timeout': build_timeout}) |
| 46 | super(ServiceClient, self).__init__(auth_provider, service, region, |
| 47 | **params) |
| 48 | |
| 49 | |
| 50 | class ResponseBody(dict): |
| 51 | """Class that wraps an http response and dict body into a single value. |
| 52 | |
| 53 | Callers that receive this object will normally use it as a dict but |
| 54 | can extract the response if needed. |
| 55 | """ |
| 56 | |
| 57 | def __init__(self, response, body=None): |
| 58 | body_data = body or {} |
| 59 | self.update(body_data) |
| 60 | self.response = response |
| 61 | |
| 62 | def __str__(self): |
| 63 | body = super(ResponseBody, self).__str__() |
| 64 | return "response: %s\nBody: %s" % (self.response, body) |
| 65 | |
| 66 | |
| 67 | class ResponseBodyData(object): |
| 68 | """Class that wraps an http response and string data into a single value. |
| 69 | """ |
| 70 | |
| 71 | def __init__(self, response, data): |
| 72 | self.response = response |
| 73 | self.data = data |
| 74 | |
| 75 | def __str__(self): |
| 76 | return "response: %s\nBody: %s" % (self.response, self.data) |
| 77 | |
| 78 | |
| 79 | class ResponseBodyList(list): |
| 80 | """Class that wraps an http response and list body into a single value. |
| 81 | |
| 82 | Callers that receive this object will normally use it as a list but |
| 83 | can extract the response if needed. |
| 84 | """ |
| 85 | |
| 86 | def __init__(self, response, body=None): |
| 87 | body_data = body or [] |
| 88 | self.extend(body_data) |
| 89 | self.response = response |
| 90 | |
| 91 | def __str__(self): |
| 92 | body = super(ResponseBodyList, self).__str__() |
| 93 | return "response: %s\nBody: %s" % (self.response, body) |