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 | 1f47cf9 | 2016-02-25 04:57:18 +0900 | [diff] [blame] | 15 | from tempest.lib.common import api_version_request |
| 16 | from tempest.lib.common import api_version_utils |
| 17 | from tempest.lib.common import rest_client |
| 18 | from tempest.lib import exceptions |
Ghanshyam | 05049dd | 2016-02-12 17:44:48 +0900 | [diff] [blame] | 19 | |
| 20 | COMPUTE_MICROVERSION = None |
Ghanshyam | 77f3f90 | 2015-12-17 17:42:08 +0900 | [diff] [blame] | 21 | |
Ghanshyam | eac8324 | 2015-12-04 17:59:01 +0900 | [diff] [blame] | 22 | |
Ghanshyam | 05049dd | 2016-02-12 17:44:48 +0900 | [diff] [blame] | 23 | class BaseComputeClient(rest_client.RestClient): |
Ghanshyam | 1f47cf9 | 2016-02-25 04:57:18 +0900 | [diff] [blame] | 24 | """Base compute service clients class to support microversion. |
| 25 | |
ghanshyam | c3b0b8b | 2016-03-16 11:58:34 +0900 | [diff] [blame] | 26 | This class adds microversion to API request header if that is set |
| 27 | and provides interface to select appropriate JSON schema file for |
Ghanshyam | 1f47cf9 | 2016-02-25 04:57:18 +0900 | [diff] [blame] | 28 | response validation. |
| 29 | |
ghanshyam | c3b0b8b | 2016-03-16 11:58:34 +0900 | [diff] [blame] | 30 | :param auth_provider: An auth provider object used to wrap requests in |
Ghanshyam | 1f47cf9 | 2016-02-25 04:57:18 +0900 | [diff] [blame] | 31 | auth |
| 32 | :param str service: The service name to use for the catalog lookup |
| 33 | :param str region: The region to use for the catalog lookup |
Ghanshyam | 1f47cf9 | 2016-02-25 04:57:18 +0900 | [diff] [blame] | 34 | :param kwargs: kwargs required by rest_client.RestClient |
| 35 | """ |
| 36 | |
Ghanshyam | 05049dd | 2016-02-12 17:44:48 +0900 | [diff] [blame] | 37 | api_microversion_header_name = 'X-OpenStack-Nova-API-Version' |
Ghanshyam | eac8324 | 2015-12-04 17:59:01 +0900 | [diff] [blame] | 38 | |
Ghanshyam | bbdb33b | 2016-01-08 11:51:07 +0900 | [diff] [blame] | 39 | def __init__(self, auth_provider, service, region, |
Ghanshyam | bbdb33b | 2016-01-08 11:51:07 +0900 | [diff] [blame] | 40 | **kwargs): |
| 41 | super(BaseComputeClient, self).__init__( |
Ghanshyam | 05049dd | 2016-02-12 17:44:48 +0900 | [diff] [blame] | 42 | auth_provider, service, region, **kwargs) |
| 43 | |
| 44 | def get_headers(self): |
| 45 | headers = super(BaseComputeClient, self).get_headers() |
| 46 | if COMPUTE_MICROVERSION: |
| 47 | headers[self.api_microversion_header_name] = COMPUTE_MICROVERSION |
| 48 | return headers |
Ghanshyam | 99018e0 | 2015-12-17 17:23:33 +0900 | [diff] [blame] | 49 | |
Ghanshyam | bbdb33b | 2016-01-08 11:51:07 +0900 | [diff] [blame] | 50 | def request(self, method, url, extra_headers=False, headers=None, |
Jordan Pittier | 4408c4a | 2016-04-29 15:05:09 +0200 | [diff] [blame] | 51 | body=None, chunked=False): |
Ghanshyam | bbdb33b | 2016-01-08 11:51:07 +0900 | [diff] [blame] | 52 | resp, resp_body = super(BaseComputeClient, self).request( |
Jordan Pittier | 4408c4a | 2016-04-29 15:05:09 +0200 | [diff] [blame] | 53 | method, url, extra_headers, headers, body, chunked) |
Ghanshyam | 05049dd | 2016-02-12 17:44:48 +0900 | [diff] [blame] | 54 | if (COMPUTE_MICROVERSION and |
| 55 | COMPUTE_MICROVERSION != api_version_utils.LATEST_MICROVERSION): |
Ghanshyam | bbdb33b | 2016-01-08 11:51:07 +0900 | [diff] [blame] | 56 | api_version_utils.assert_version_header_matches_request( |
| 57 | self.api_microversion_header_name, |
Ghanshyam | 05049dd | 2016-02-12 17:44:48 +0900 | [diff] [blame] | 58 | COMPUTE_MICROVERSION, |
Ghanshyam | bbdb33b | 2016-01-08 11:51:07 +0900 | [diff] [blame] | 59 | resp) |
| 60 | return resp, resp_body |
Ghanshyam | 77f3f90 | 2015-12-17 17:42:08 +0900 | [diff] [blame] | 61 | |
| 62 | def get_schema(self, schema_versions_info): |
| 63 | """Get JSON schema |
| 64 | |
| 65 | This method provides the matching schema for requested |
Ghanshyam | 1f47cf9 | 2016-02-25 04:57:18 +0900 | [diff] [blame] | 66 | microversion. |
| 67 | |
Ghanshyam | 77f3f90 | 2015-12-17 17:42:08 +0900 | [diff] [blame] | 68 | :param schema_versions_info: List of dict which provides schema |
Ghanshyam | 1f47cf9 | 2016-02-25 04:57:18 +0900 | [diff] [blame] | 69 | information with range of valid versions. |
Masayuki Igawa | e63cf0f | 2016-05-25 10:25:21 +0900 | [diff] [blame] | 70 | |
| 71 | Example:: |
| 72 | |
| 73 | schema_versions_info = [ |
| 74 | {'min': None, 'max': '2.1', 'schema': schemav21}, |
| 75 | {'min': '2.2', 'max': '2.9', 'schema': schemav22}, |
| 76 | {'min': '2.10', 'max': None, 'schema': schemav210}] |
Ghanshyam | 77f3f90 | 2015-12-17 17:42:08 +0900 | [diff] [blame] | 77 | """ |
| 78 | schema = None |
Ghanshyam | 05049dd | 2016-02-12 17:44:48 +0900 | [diff] [blame] | 79 | version = api_version_request.APIVersionRequest(COMPUTE_MICROVERSION) |
Ghanshyam | 77f3f90 | 2015-12-17 17:42:08 +0900 | [diff] [blame] | 80 | for items in schema_versions_info: |
| 81 | min_version = api_version_request.APIVersionRequest(items['min']) |
| 82 | max_version = api_version_request.APIVersionRequest(items['max']) |
ghanshyam | c3b0b8b | 2016-03-16 11:58:34 +0900 | [diff] [blame] | 83 | # This is case where COMPUTE_MICROVERSION is None, which means |
Ghanshyam | 77f3f90 | 2015-12-17 17:42:08 +0900 | [diff] [blame] | 84 | # request without microversion So select base v2.1 schema. |
| 85 | if version.is_null() and items['min'] is None: |
| 86 | schema = items['schema'] |
| 87 | break |
ghanshyam | c3b0b8b | 2016-03-16 11:58:34 +0900 | [diff] [blame] | 88 | # else select appropriate schema as per COMPUTE_MICROVERSION |
Ghanshyam | 77f3f90 | 2015-12-17 17:42:08 +0900 | [diff] [blame] | 89 | elif version.matches(min_version, max_version): |
| 90 | schema = items['schema'] |
| 91 | break |
| 92 | if schema is None: |
| 93 | raise exceptions.JSONSchemaNotFound( |
| 94 | version=version.get_string(), |
| 95 | schema_versions_info=schema_versions_info) |
| 96 | return schema |