zhufl | 0273652 | 2017-06-19 13:57:28 +0800 | [diff] [blame] | 1 | # Copyright 2016 Andrew Kerr |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
zhufl | e75f8ca | 2020-09-08 11:11:31 +0800 | [diff] [blame] | 16 | from tempest.lib.common import api_version_request |
zhufl | 0273652 | 2017-06-19 13:57:28 +0800 | [diff] [blame] | 17 | from tempest.lib.common import api_version_utils |
| 18 | from tempest.lib.common import rest_client |
zhufl | e75f8ca | 2020-09-08 11:11:31 +0800 | [diff] [blame] | 19 | from tempest.lib import exceptions |
zhufl | 0273652 | 2017-06-19 13:57:28 +0800 | [diff] [blame] | 20 | |
| 21 | VOLUME_MICROVERSION = None |
| 22 | |
| 23 | |
| 24 | class BaseClient(rest_client.RestClient): |
| 25 | """Base volume service clients class to support microversion.""" |
| 26 | api_microversion_header_name = 'Openstack-Api-Version' |
| 27 | |
| 28 | def get_headers(self, accept_type=None, send_type=None): |
| 29 | headers = super(BaseClient, self).get_headers( |
| 30 | accept_type=accept_type, send_type=send_type) |
| 31 | if VOLUME_MICROVERSION: |
| 32 | headers[self.api_microversion_header_name] = ('volume %s' % |
| 33 | VOLUME_MICROVERSION) |
| 34 | return headers |
| 35 | |
| 36 | def request(self, method, url, extra_headers=False, headers=None, |
| 37 | body=None, chunked=False): |
| 38 | |
| 39 | resp, resp_body = super(BaseClient, self).request( |
| 40 | method, url, extra_headers, headers, body, chunked) |
| 41 | if (VOLUME_MICROVERSION and |
| 42 | VOLUME_MICROVERSION != api_version_utils.LATEST_MICROVERSION): |
| 43 | api_version_utils.assert_version_header_matches_request( |
| 44 | self.api_microversion_header_name, |
| 45 | 'volume %s' % VOLUME_MICROVERSION, |
| 46 | resp) |
| 47 | return resp, resp_body |
zhufl | e75f8ca | 2020-09-08 11:11:31 +0800 | [diff] [blame] | 48 | |
| 49 | def get_schema(self, schema_versions_info): |
| 50 | """Get JSON schema |
| 51 | |
| 52 | This method provides the matching schema for requested |
| 53 | microversion. |
| 54 | |
| 55 | :param schema_versions_info: List of dict which provides schema |
| 56 | information with range of valid versions. |
| 57 | |
| 58 | Example:: |
| 59 | |
| 60 | schema_versions_info = [ |
| 61 | {'min': None, 'max': '2.1', 'schema': schemav21}, |
| 62 | {'min': '2.2', 'max': '2.9', 'schema': schemav22}, |
| 63 | {'min': '2.10', 'max': None, 'schema': schemav210}] |
| 64 | """ |
| 65 | schema = None |
| 66 | version = api_version_request.APIVersionRequest(VOLUME_MICROVERSION) |
| 67 | for items in schema_versions_info: |
| 68 | min_version = api_version_request.APIVersionRequest(items['min']) |
| 69 | max_version = api_version_request.APIVersionRequest(items['max']) |
| 70 | # This is case where COMPUTE_MICROVERSION is None, which means |
| 71 | # request without microversion So select base v2.1 schema. |
| 72 | if version.is_null() and items['min'] is None: |
| 73 | schema = items['schema'] |
| 74 | break |
| 75 | # else select appropriate schema as per COMPUTE_MICROVERSION |
| 76 | elif version.matches(min_version, max_version): |
| 77 | schema = items['schema'] |
| 78 | break |
| 79 | if schema is None: |
| 80 | raise exceptions.JSONSchemaNotFound( |
| 81 | version=version.get_string(), |
| 82 | schema_versions_info=schema_versions_info) |
| 83 | return schema |