blob: 0812b9948b83e997bf821acd87ffff9d73917fb6 [file] [log] [blame]
Ken'ichi Ohmichi0e836652015-01-08 04:38:56 +00001# 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 Ohmichi79aede12015-01-17 08:19:47 +000015from tempest_lib.common import rest_client
16from tempest_lib import exceptions as lib_exceptions
17
Ken'ichi Ohmichi0e836652015-01-08 04:38:56 +000018from tempest import config
Ken'ichi Ohmichi79aede12015-01-17 08:19:47 +000019from tempest import exceptions
Ken'ichi Ohmichi0e836652015-01-08 04:38:56 +000020
21CONF = config.CONF
22
23
24class ServiceClient(rest_client.RestClient):
25
26 def __init__(self, auth_provider, service, region,
Ken'ichi Ohmichic2b11ce2015-01-16 07:17:29 +000027 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 Ohmichi0e836652015-01-08 04:38:56 +000036 params = {
Ken'ichi Ohmichic2b11ce2015-01-16 07:17:29 +000037 '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 Ohmichi0e836652015-01-08 04:38:56 +000040 }
Ken'ichi Ohmichic2b11ce2015-01-16 07:17:29 +000041
Ken'ichi Ohmichi0e836652015-01-08 04:38:56 +000042 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 Ohmichia6ac2422015-01-13 01:09:39 +000050
Ken'ichi Ohmichi79aede12015-01-17 08:19:47 +000051 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 Ohmichi79aede12015-01-17 08:19:47 +000062 except lib_exceptions.BadRequest as ex:
63 raise exceptions.BadRequest(ex)
Ken'ichi Ohmichib8c2d7c2015-01-29 01:51:57 +000064 # 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 Igawa29d554f2015-01-20 12:36:42 +090069 raise lib_exceptions.Unauthorized(ex)
Ken'ichi Ohmichi79aede12015-01-17 08:19:47 +000070
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000071
72class 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 Kranze4e3b412015-02-10 10:50:42 -050089class 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 Ohmichia6ac2422015-01-13 01:09:39 +0000101class 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)