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
This commit is contained in:
parent
5d2ea10f52
commit
bd84cf4a7a
|
@ -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
|
|
@ -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):
|
||||
|
|
|
@ -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)
|
Loading…
Reference in New Issue