Ken'ichi Ohmichi | 4266268 | 2015-01-05 05:00:04 +0000 | [diff] [blame] | 1 | # (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 Ohmichi | 4266268 | 2015-01-05 05:00:04 +0000 | [diff] [blame] | 18 | from tempest import config |
Ken'ichi Ohmichi | 5403052 | 2016-03-02 11:01:34 -0800 | [diff] [blame] | 19 | from tempest.lib.common import rest_client |
Ken'ichi Ohmichi | 4266268 | 2015-01-05 05:00:04 +0000 | [diff] [blame] | 20 | |
| 21 | CONF = config.CONF |
| 22 | |
| 23 | |
Ken'ichi Ohmichi | 5403052 | 2016-03-02 11:01:34 -0800 | [diff] [blame] | 24 | class NegativeRestClient(rest_client.RestClient): |
Ken'ichi Ohmichi | cb67d2d | 2015-11-19 08:23:22 +0000 | [diff] [blame] | 25 | """Version of RestClient that does not raise exceptions.""" |
Ken'ichi Ohmichi | 5403052 | 2016-03-02 11:01:34 -0800 | [diff] [blame] | 26 | def __init__(self, auth_provider, service, **kwargs): |
David Kranz | 1e33e37 | 2015-03-20 09:42:56 -0400 | [diff] [blame] | 27 | region, endpoint_type = self._get_region_and_endpoint_type(service) |
| 28 | super(NegativeRestClient, self).__init__( |
Ken'ichi Ohmichi | 5403052 | 2016-03-02 11:01:34 -0800 | [diff] [blame] | 29 | auth_provider, service, region, endpoint_type=endpoint_type, |
| 30 | **kwargs) |
Ken'ichi Ohmichi | 4266268 | 2015-01-05 05:00:04 +0000 | [diff] [blame] | 31 | |
David Kranz | 1e33e37 | 2015-03-20 09:42:56 -0400 | [diff] [blame] | 32 | def _get_region_and_endpoint_type(self, service): |
Ken'ichi Ohmichi | cb67d2d | 2015-11-19 08:23:22 +0000 | [diff] [blame] | 33 | """Returns the region for a specific service""" |
Ken'ichi Ohmichi | 4266268 | 2015-01-05 05:00:04 +0000 | [diff] [blame] | 34 | service_region = None |
David Kranz | 1e33e37 | 2015-03-20 09:42:56 -0400 | [diff] [blame] | 35 | service_endpoint_type = None |
Ken'ichi Ohmichi | 4266268 | 2015-01-05 05:00:04 +0000 | [diff] [blame] | 36 | 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 Kranz | 1e33e37 | 2015-03-20 09:42:56 -0400 | [diff] [blame] | 42 | service_endpoint_type = getattr(cfg, 'endpoint_type', None) |
Ken'ichi Ohmichi | 4266268 | 2015-01-05 05:00:04 +0000 | [diff] [blame] | 43 | if not service_region: |
| 44 | service_region = CONF.identity.region |
David Kranz | 1e33e37 | 2015-03-20 09:42:56 -0400 | [diff] [blame] | 45 | return service_region, service_endpoint_type |
Ken'ichi Ohmichi | 4266268 | 2015-01-05 05:00:04 +0000 | [diff] [blame] | 46 | |
| 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 |