blob: bbadc8fb3287ceae2be8f4a6b2a889f8922652bf [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
Ghanshyam1f47cf92016-02-25 04:57:18 +090015from tempest.lib.common import api_version_request
16from tempest.lib.common import api_version_utils
17from tempest.lib.common import rest_client
18from tempest.lib import exceptions
Ghanshyam05049dd2016-02-12 17:44:48 +090019
20COMPUTE_MICROVERSION = None
Ghanshyam77f3f902015-12-17 17:42:08 +090021
Ghanshyameac83242015-12-04 17:59:01 +090022
Ghanshyam05049dd2016-02-12 17:44:48 +090023class BaseComputeClient(rest_client.RestClient):
Ghanshyam1f47cf92016-02-25 04:57:18 +090024 """Base compute service clients class to support microversion.
25
26 This class adds microversion to API request header if same is set
27 and provide interface to select appropriate JSON schema file for
28 response validation.
29
30 :param auth_provider: an auth provider object used to wrap requests in
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
34 request with microversion
35 :param kwargs: kwargs required by rest_client.RestClient
36 """
37
Ghanshyam05049dd2016-02-12 17:44:48 +090038 api_microversion_header_name = 'X-OpenStack-Nova-API-Version'
Ghanshyameac83242015-12-04 17:59:01 +090039
Ghanshyambbdb33b2016-01-08 11:51:07 +090040 def __init__(self, auth_provider, service, region,
Ghanshyambbdb33b2016-01-08 11:51:07 +090041 **kwargs):
42 super(BaseComputeClient, self).__init__(
Ghanshyam05049dd2016-02-12 17:44:48 +090043 auth_provider, service, region, **kwargs)
44
45 def get_headers(self):
46 headers = super(BaseComputeClient, self).get_headers()
47 if COMPUTE_MICROVERSION:
48 headers[self.api_microversion_header_name] = COMPUTE_MICROVERSION
49 return headers
Ghanshyam99018e02015-12-17 17:23:33 +090050
Ghanshyambbdb33b2016-01-08 11:51:07 +090051 def request(self, method, url, extra_headers=False, headers=None,
52 body=None):
53 resp, resp_body = super(BaseComputeClient, self).request(
54 method, url, extra_headers, headers, body)
Ghanshyam05049dd2016-02-12 17:44:48 +090055 if (COMPUTE_MICROVERSION and
56 COMPUTE_MICROVERSION != api_version_utils.LATEST_MICROVERSION):
Ghanshyambbdb33b2016-01-08 11:51:07 +090057 api_version_utils.assert_version_header_matches_request(
58 self.api_microversion_header_name,
Ghanshyam05049dd2016-02-12 17:44:48 +090059 COMPUTE_MICROVERSION,
Ghanshyambbdb33b2016-01-08 11:51:07 +090060 resp)
61 return resp, resp_body
Ghanshyam77f3f902015-12-17 17:42:08 +090062
63 def get_schema(self, schema_versions_info):
64 """Get JSON schema
65
66 This method provides the matching schema for requested
Ghanshyam1f47cf92016-02-25 04:57:18 +090067 microversion.
68
Ghanshyam77f3f902015-12-17 17:42:08 +090069 :param schema_versions_info: List of dict which provides schema
Ghanshyam1f47cf92016-02-25 04:57:18 +090070 information with range of valid versions.
Ghanshyam77f3f902015-12-17 17:42:08 +090071 Example -
72 schema_versions_info = [
73 {'min': None, 'max': '2.1', 'schema': schemav21},
74 {'min': '2.2', 'max': '2.9', 'schema': schemav22},
75 {'min': '2.10', 'max': None, 'schema': schemav210}]
76 """
77 schema = None
Ghanshyam05049dd2016-02-12 17:44:48 +090078 version = api_version_request.APIVersionRequest(COMPUTE_MICROVERSION)
Ghanshyam77f3f902015-12-17 17:42:08 +090079 for items in schema_versions_info:
80 min_version = api_version_request.APIVersionRequest(items['min'])
81 max_version = api_version_request.APIVersionRequest(items['max'])
82 # This is case where self.api_microversion is None, which means
83 # request without microversion So select base v2.1 schema.
84 if version.is_null() and items['min'] is None:
85 schema = items['schema']
86 break
87 # else select appropriate schema as per self.api_microversion
88 elif version.matches(min_version, max_version):
89 schema = items['schema']
90 break
91 if schema is None:
92 raise exceptions.JSONSchemaNotFound(
93 version=version.get_string(),
94 schema_versions_info=schema_versions_info)
95 return schema