blob: 40d305698ed4f467de6b4b7e61683be29e683493 [file] [log] [blame]
Ghanshyameac83242015-12-04 17:59:01 +09001# 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
Ghanshyam99018e02015-12-17 17:23:33 +090015from tempest_lib.common import rest_client
Ghanshyameac83242015-12-04 17:59:01 +090016
Ghanshyam77f3f902015-12-17 17:42:08 +090017from tempest.common import api_version_request
18from tempest import exceptions
19
Ghanshyameac83242015-12-04 17:59:01 +090020
Ghanshyam99018e02015-12-17 17:23:33 +090021class BaseComputeClient(rest_client.RestClient):
Ghanshyameac83242015-12-04 17:59:01 +090022 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
Ghanshyam99018e02015-12-17 17:23:33 +090029
30 def set_api_microversion(self, microversion):
31 self.api_microversion = microversion
Ghanshyam77f3f902015-12-17 17:42:08 +090032
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