Ghanshyam | eac8324 | 2015-12-04 17:59:01 +0900 | [diff] [blame] | 1 | # 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 | |
Ghanshyam | 99018e0 | 2015-12-17 17:23:33 +0900 | [diff] [blame] | 15 | from tempest_lib.common import rest_client |
Ghanshyam | eac8324 | 2015-12-04 17:59:01 +0900 | [diff] [blame] | 16 | |
Ghanshyam | 77f3f90 | 2015-12-17 17:42:08 +0900 | [diff] [blame^] | 17 | from tempest.common import api_version_request |
| 18 | from tempest import exceptions |
| 19 | |
Ghanshyam | eac8324 | 2015-12-04 17:59:01 +0900 | [diff] [blame] | 20 | |
Ghanshyam | 99018e0 | 2015-12-17 17:23:33 +0900 | [diff] [blame] | 21 | class BaseComputeClient(rest_client.RestClient): |
Ghanshyam | eac8324 | 2015-12-04 17:59:01 +0900 | [diff] [blame] | 22 | api_microversion = None |
| 23 | |
| 24 | def get_headers(self): |
| 25 | headers = super(BaseComputeClient, self).get_headers() |
| 26 | if self.api_microversion: |
| 27 | headers['X-OpenStack-Nova-API-Version'] = self.api_microversion |
| 28 | return headers |
Ghanshyam | 99018e0 | 2015-12-17 17:23:33 +0900 | [diff] [blame] | 29 | |
| 30 | def set_api_microversion(self, microversion): |
| 31 | self.api_microversion = microversion |
Ghanshyam | 77f3f90 | 2015-12-17 17:42:08 +0900 | [diff] [blame^] | 32 | |
| 33 | def get_schema(self, schema_versions_info): |
| 34 | """Get JSON schema |
| 35 | |
| 36 | This method provides the matching schema for requested |
| 37 | microversion (self.api_microversion). |
| 38 | :param schema_versions_info: List of dict which provides schema |
| 39 | information with range of valid versions. |
| 40 | Example - |
| 41 | schema_versions_info = [ |
| 42 | {'min': None, 'max': '2.1', 'schema': schemav21}, |
| 43 | {'min': '2.2', 'max': '2.9', 'schema': schemav22}, |
| 44 | {'min': '2.10', 'max': None, 'schema': schemav210}] |
| 45 | """ |
| 46 | schema = None |
| 47 | version = api_version_request.APIVersionRequest(self.api_microversion) |
| 48 | for items in schema_versions_info: |
| 49 | min_version = api_version_request.APIVersionRequest(items['min']) |
| 50 | max_version = api_version_request.APIVersionRequest(items['max']) |
| 51 | # This is case where self.api_microversion is None, which means |
| 52 | # request without microversion So select base v2.1 schema. |
| 53 | if version.is_null() and items['min'] is None: |
| 54 | schema = items['schema'] |
| 55 | break |
| 56 | # else select appropriate schema as per self.api_microversion |
| 57 | elif version.matches(min_version, max_version): |
| 58 | schema = items['schema'] |
| 59 | break |
| 60 | if schema is None: |
| 61 | raise exceptions.JSONSchemaNotFound( |
| 62 | version=version.get_string(), |
| 63 | schema_versions_info=schema_versions_info) |
| 64 | return schema |