blob: 433c94cc239cdf690feb350862fe941ed48b5e64 [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
ghanshyamc3b0b8b2016-03-16 11:58:34 +090026 This class adds microversion to API request header if that is set
27 and provides interface to select appropriate JSON schema file for
Ghanshyam1f47cf92016-02-25 04:57:18 +090028 response validation.
29
ghanshyamc3b0b8b2016-03-16 11:58:34 +090030 :param auth_provider: An auth provider object used to wrap requests in
Ghanshyam1f47cf92016-02-25 04:57:18 +090031 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
Ghanshyam1f47cf92016-02-25 04:57:18 +090034 :param kwargs: kwargs required by rest_client.RestClient
35 """
36
Ghanshyam05049dd2016-02-12 17:44:48 +090037 api_microversion_header_name = 'X-OpenStack-Nova-API-Version'
Ghanshyameac83242015-12-04 17:59:01 +090038
Ghanshyambbdb33b2016-01-08 11:51:07 +090039 def __init__(self, auth_provider, service, region,
Ghanshyambbdb33b2016-01-08 11:51:07 +090040 **kwargs):
41 super(BaseComputeClient, self).__init__(
Ghanshyam05049dd2016-02-12 17:44:48 +090042 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
Ghanshyam99018e02015-12-17 17:23:33 +090049
Ghanshyambbdb33b2016-01-08 11:51:07 +090050 def request(self, method, url, extra_headers=False, headers=None,
Jordan Pittier4408c4a2016-04-29 15:05:09 +020051 body=None, chunked=False):
Ghanshyambbdb33b2016-01-08 11:51:07 +090052 resp, resp_body = super(BaseComputeClient, self).request(
Jordan Pittier4408c4a2016-04-29 15:05:09 +020053 method, url, extra_headers, headers, body, chunked)
Ghanshyam05049dd2016-02-12 17:44:48 +090054 if (COMPUTE_MICROVERSION and
55 COMPUTE_MICROVERSION != api_version_utils.LATEST_MICROVERSION):
Ghanshyambbdb33b2016-01-08 11:51:07 +090056 api_version_utils.assert_version_header_matches_request(
57 self.api_microversion_header_name,
Ghanshyam05049dd2016-02-12 17:44:48 +090058 COMPUTE_MICROVERSION,
Ghanshyambbdb33b2016-01-08 11:51:07 +090059 resp)
60 return resp, resp_body
Ghanshyam77f3f902015-12-17 17:42:08 +090061
62 def get_schema(self, schema_versions_info):
63 """Get JSON schema
64
65 This method provides the matching schema for requested
Ghanshyam1f47cf92016-02-25 04:57:18 +090066 microversion.
67
Ghanshyam77f3f902015-12-17 17:42:08 +090068 :param schema_versions_info: List of dict which provides schema
Ghanshyam1f47cf92016-02-25 04:57:18 +090069 information with range of valid versions.
Masayuki Igawae63cf0f2016-05-25 10:25:21 +090070
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}]
Ghanshyam77f3f902015-12-17 17:42:08 +090077 """
78 schema = None
Ghanshyam05049dd2016-02-12 17:44:48 +090079 version = api_version_request.APIVersionRequest(COMPUTE_MICROVERSION)
Ghanshyam77f3f902015-12-17 17:42:08 +090080 for items in schema_versions_info:
81 min_version = api_version_request.APIVersionRequest(items['min'])
82 max_version = api_version_request.APIVersionRequest(items['max'])
ghanshyamc3b0b8b2016-03-16 11:58:34 +090083 # This is case where COMPUTE_MICROVERSION is None, which means
Ghanshyam77f3f902015-12-17 17:42:08 +090084 # 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
ghanshyamc3b0b8b2016-03-16 11:58:34 +090088 # else select appropriate schema as per COMPUTE_MICROVERSION
Ghanshyam77f3f902015-12-17 17:42:08 +090089 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