blob: 3495a244658a383a239f513df2fda7d163bd5de9 [file] [log] [blame]
Ken'ichi Ohmichi42662682015-01-05 05:00:04 +00001# (c) 2014 Deutsche Telekom AG
2# Copyright 2014 Red Hat, Inc.
3# Copyright 2014 NEC Corporation
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
Ken'ichi Ohmichi42662682015-01-05 05:00:04 +000018from tempest import config
Ken'ichi Ohmichi54030522016-03-02 11:01:34 -080019from tempest.lib.common import rest_client
Ken'ichi Ohmichi42662682015-01-05 05:00:04 +000020
21CONF = config.CONF
22
23
Ken'ichi Ohmichi54030522016-03-02 11:01:34 -080024class NegativeRestClient(rest_client.RestClient):
Ken'ichi Ohmichicb67d2d2015-11-19 08:23:22 +000025 """Version of RestClient that does not raise exceptions."""
Ken'ichi Ohmichi54030522016-03-02 11:01:34 -080026 def __init__(self, auth_provider, service, **kwargs):
David Kranz1e33e372015-03-20 09:42:56 -040027 region, endpoint_type = self._get_region_and_endpoint_type(service)
28 super(NegativeRestClient, self).__init__(
Ken'ichi Ohmichi54030522016-03-02 11:01:34 -080029 auth_provider, service, region, endpoint_type=endpoint_type,
30 **kwargs)
Ken'ichi Ohmichi42662682015-01-05 05:00:04 +000031
David Kranz1e33e372015-03-20 09:42:56 -040032 def _get_region_and_endpoint_type(self, service):
Ken'ichi Ohmichicb67d2d2015-11-19 08:23:22 +000033 """Returns the region for a specific service"""
Ken'ichi Ohmichi42662682015-01-05 05:00:04 +000034 service_region = None
David Kranz1e33e372015-03-20 09:42:56 -040035 service_endpoint_type = None
Ken'ichi Ohmichi42662682015-01-05 05:00:04 +000036 for cfgname in dir(CONF._config):
37 # Find all config.FOO.catalog_type and assume FOO is a service.
38 cfg = getattr(CONF, cfgname)
39 catalog_type = getattr(cfg, 'catalog_type', None)
40 if catalog_type == service:
41 service_region = getattr(cfg, 'region', None)
David Kranz1e33e372015-03-20 09:42:56 -040042 service_endpoint_type = getattr(cfg, 'endpoint_type', None)
Ken'ichi Ohmichi42662682015-01-05 05:00:04 +000043 if not service_region:
44 service_region = CONF.identity.region
David Kranz1e33e372015-03-20 09:42:56 -040045 return service_region, service_endpoint_type
Ken'ichi Ohmichi42662682015-01-05 05:00:04 +000046
47 def _error_checker(self, method, url,
48 headers, body, resp, resp_body):
49 pass
50
51 def send_request(self, method, url_template, resources, body=None):
52 url = url_template % tuple(resources)
53 if method == "GET":
54 resp, body = self.get(url)
55 elif method == "POST":
56 resp, body = self.post(url, body)
57 elif method == "PUT":
58 resp, body = self.put(url, body)
59 elif method == "PATCH":
60 resp, body = self.patch(url, body)
61 elif method == "HEAD":
62 resp, body = self.head(url)
63 elif method == "DELETE":
64 resp, body = self.delete(url)
65 elif method == "COPY":
66 resp, body = self.copy(url)
67 else:
68 assert False
69
70 return resp, body