From bd84cf4a7af50a499c3355f8ae490ea5cafb060f Mon Sep 17 00:00:00 2001 From: Chris Yeoh Date: Thu, 20 Nov 2014 18:16:30 +1030 Subject: [PATCH] Adds APIVersionRequest class for API Microversions Adds the APIVersionRequest class which handles converting the API version requested from a request header string and supplies comparison and matching methods used by microversions and REST API code to determine what code paths to take. Partially implements blueprint api-microversions Change-Id: Ic75d36fc0d27b615e70e1fe56c1626e9e501b1d6 --- nova/api/openstack/api_version_request.py | 76 ++++++++++++ nova/exception.py | 5 + .../api/openstack/test_api_version_request.py | 111 ++++++++++++++++++ 3 files changed, 192 insertions(+) create mode 100644 nova/api/openstack/api_version_request.py create mode 100644 nova/tests/unit/api/openstack/test_api_version_request.py diff --git a/nova/api/openstack/api_version_request.py b/nova/api/openstack/api_version_request.py new file mode 100644 index 0000000000..b46bc22116 --- /dev/null +++ b/nova/api/openstack/api_version_request.py @@ -0,0 +1,76 @@ +# Copyright 2014 IBM Corp. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import re + +from nova import exception + + +class APIVersionRequest(object): + """This class represents an API Version Request with convenience + methods for manipulation and comparison of version + numbers that we need to do to implement microversions. + """ + + def __init__(self, version_string=None): + """Create an API version request object.""" + self.ver_major = None + self.ver_minor = None + + if version_string is not None: + match = re.match(r"^([1-9]\d*)\.([1-9]\d*|0)$", + version_string) + if match: + self.ver_major = int(match.group(1)) + self.ver_minor = int(match.group(2)) + else: + raise exception.InvalidAPIVersionString(version=version_string) + + def __str__(self): + return ("API Version Request Major: %s, Minor: %s" + % (self.ver_major, self.ver_minor)) + + def is_null(self): + return self.ver_major is None and self.ver_minor is None + + def __cmp__(self, other): + if not isinstance(other, APIVersionRequest): + raise TypeError + return cmp((self.ver_major, self.ver_minor), + (other.ver_major, other.ver_minor)) + + def matches(self, min_version, max_version): + """Returns whether the version object represents a version + greater than or equal to the minimum version and less than + or equal to the maximum version. + + @param min_version: Minimum acceptable version. + @param max_version: Maximum acceptable version. + @returns: boolean + + If min_version is null then there is no minimum limit. + If max_version is null then there is no maximum limit. + If self is null then raise ValueError + """ + + if self.is_null(): + raise ValueError + if max_version.is_null() and min_version.is_null(): + return True + elif max_version.is_null(): + return min_version <= self + elif min_version.is_null(): + return self <= max_version + else: + return min_version <= self <= max_version diff --git a/nova/exception.py b/nova/exception.py index 18f697d902..1a639fa987 100644 --- a/nova/exception.py +++ b/nova/exception.py @@ -321,6 +321,11 @@ class InvalidUnicodeParameter(Invalid): "Unicode is not supported by the current database.") +class InvalidAPIVersionString(Invalid): + msg_fmt = _("API Version String %(version)s is of invalid format. Must " + "be of format MajorNum.MinorNum.") + + # Cannot be templated as the error syntax varies. # msg needs to be constructed when raised. class InvalidParameterValue(Invalid): diff --git a/nova/tests/unit/api/openstack/test_api_version_request.py b/nova/tests/unit/api/openstack/test_api_version_request.py new file mode 100644 index 0000000000..14430738eb --- /dev/null +++ b/nova/tests/unit/api/openstack/test_api_version_request.py @@ -0,0 +1,111 @@ +# Copyright 2014 IBM Corp. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from nova.api.openstack import api_version_request +from nova import exception +from nova import test + + +class APIVersionRequestTests(test.NoDBTestCase): + def test_valid_version_strings(self): + def _test_string(version, exp_major, exp_minor): + v = api_version_request.APIVersionRequest(version) + self.assertEqual(v.ver_major, exp_major) + self.assertEqual(v.ver_minor, exp_minor) + + _test_string("1.1", 1, 1) + _test_string("2.10", 2, 10) + _test_string("5.234", 5, 234) + _test_string("12.5", 12, 5) + _test_string("2.0", 2, 0) + _test_string("2.200", 2, 200) + + def test_null_version(self): + v = api_version_request.APIVersionRequest() + self.assertTrue(v.is_null()) + + def test_invalid_version_strings(self): + self.assertRaises(exception.InvalidAPIVersionString, + api_version_request.APIVersionRequest, "2") + + self.assertRaises(exception.InvalidAPIVersionString, + api_version_request.APIVersionRequest, "200") + + self.assertRaises(exception.InvalidAPIVersionString, + api_version_request.APIVersionRequest, "2.1.4") + + self.assertRaises(exception.InvalidAPIVersionString, + api_version_request.APIVersionRequest, "200.23.66.3") + + self.assertRaises(exception.InvalidAPIVersionString, + api_version_request.APIVersionRequest, "5 .3") + + self.assertRaises(exception.InvalidAPIVersionString, + api_version_request.APIVersionRequest, "5. 3") + + self.assertRaises(exception.InvalidAPIVersionString, + api_version_request.APIVersionRequest, "5.03") + + self.assertRaises(exception.InvalidAPIVersionString, + api_version_request.APIVersionRequest, "02.1") + + self.assertRaises(exception.InvalidAPIVersionString, + api_version_request.APIVersionRequest, "2.001") + + self.assertRaises(exception.InvalidAPIVersionString, + api_version_request.APIVersionRequest, "") + + self.assertRaises(exception.InvalidAPIVersionString, + api_version_request.APIVersionRequest, " 2.1") + + self.assertRaises(exception.InvalidAPIVersionString, + api_version_request.APIVersionRequest, "2.1 ") + + def test_version_comparisons(self): + v1 = api_version_request.APIVersionRequest("2.0") + v2 = api_version_request.APIVersionRequest("2.5") + v3 = api_version_request.APIVersionRequest("5.23") + v4 = api_version_request.APIVersionRequest("2.0") + v_null = api_version_request.APIVersionRequest() + + self.assertTrue(v1 < v2) + self.assertTrue(v3 > v2) + self.assertTrue(v1 != v2) + self.assertTrue(v1 == v4) + self.assertTrue(v1 != v_null) + self.assertTrue(v_null == v_null) + self.assertRaises(TypeError, v1.__cmp__, "2.1") + + def test_version_matches(self): + v1 = api_version_request.APIVersionRequest("2.0") + v2 = api_version_request.APIVersionRequest("2.5") + v3 = api_version_request.APIVersionRequest("2.45") + v4 = api_version_request.APIVersionRequest("3.3") + v5 = api_version_request.APIVersionRequest("3.23") + v6 = api_version_request.APIVersionRequest("2.0") + v7 = api_version_request.APIVersionRequest("3.3") + v8 = api_version_request.APIVersionRequest("4.0") + v_null = api_version_request.APIVersionRequest() + + self.assertTrue(v2.matches(v1, v3)) + self.assertTrue(v2.matches(v1, v_null)) + self.assertTrue(v1.matches(v6, v2)) + self.assertTrue(v4.matches(v2, v7)) + self.assertTrue(v4.matches(v_null, v7)) + self.assertTrue(v4.matches(v_null, v8)) + self.assertFalse(v1.matches(v2, v3)) + self.assertFalse(v5.matches(v2, v4)) + self.assertFalse(v2.matches(v3, v1)) + + self.assertRaises(ValueError, v_null.matches, v1, v3)