blob: dc634a1c3af8d0fd3eb35490669f671317b3475c [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)
62 except lib_exceptions.Unauthorized as ex:
63 raise exceptions.Unauthorized(ex)
64 except lib_exceptions.NotFound as ex:
65 raise exceptions.NotFound(ex)
66 except lib_exceptions.BadRequest as ex:
67 raise exceptions.BadRequest(ex)
68 except lib_exceptions.Conflict as ex:
69 raise exceptions.Conflict(ex)
70 except lib_exceptions.OverLimit as ex:
71 raise exceptions.OverLimit(ex)
Ken'ichi Ohmichi79aede12015-01-17 08:19:47 +000072 except lib_exceptions.InvalidContentType as ex:
73 raise exceptions.InvalidContentType(ex)
74 except lib_exceptions.UnprocessableEntity as ex:
75 raise exceptions.UnprocessableEntity(ex)
Ken'ichi Ohmichib8c2d7c2015-01-29 01:51:57 +000076 # TODO(oomichi): This is just a workaround for failing gate tests
77 # when separating Forbidden from Unauthorized in tempest-lib.
78 # We will need to remove this translation and replace negative tests
79 # with lib_exceptions.Forbidden in the future.
80 except lib_exceptions.Forbidden as ex:
81 raise exceptions.Unauthorized(ex)
Ken'ichi Ohmichi79aede12015-01-17 08:19:47 +000082
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000083
84class ResponseBody(dict):
85 """Class that wraps an http response and dict body into a single value.
86
87 Callers that receive this object will normally use it as a dict but
88 can extract the response if needed.
89 """
90
91 def __init__(self, response, body=None):
92 body_data = body or {}
93 self.update(body_data)
94 self.response = response
95
96 def __str__(self):
97 body = super.__str__(self)
98 return "response: %s\nBody: %s" % (self.response, body)
99
100
101class 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)