From 3cb5bc98573e8eb7d3487121b7b59ebd535035c5 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Fri, 26 May 2023 11:05:49 +0100 Subject: [PATCH] utils: Add 'supports_version' Not all services use microversion (looking at you, Glance), meaning the 'supports_microversion' helper is of no use to them. Add a 'support_version' helper for these services. We will use this shortly in the cloud layer. Change-Id: I02fa4037b8119681379bb3c4b7bb667efff506ee Signed-off-by: Stephen Finucane --- openstack/utils.py | 55 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/openstack/utils.py b/openstack/utils.py index 78d9a22e9..29d917547 100644 --- a/openstack/utils.py +++ b/openstack/utils.py @@ -9,6 +9,9 @@ # 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 __future__ import annotations + from collections.abc import Mapping import hashlib import queue @@ -17,6 +20,7 @@ import time import keystoneauth1 +from keystoneauth1 import adapter as ks_adapter from keystoneauth1 import discover from openstack import _log @@ -97,25 +101,56 @@ def __getitem__(self, key): return keys +def supports_version( + adapter: ks_adapter.Adapter, + version: str, + raise_exception: bool = False, +) -> bool: + """Determine if the given adapter supports the given version. + + Checks the version asserted by the service and ensures this matches the + provided version. ``version`` can be a major version of a major-minor + version + + :param adapter: :class:`~keystoneauth1.adapter.Adapter` instance. + :param version: String containing the desired version. + :param raise_exception: Raise exception when requested version + is not supported by the server. + :returns: ``True`` if the service supports the version, else ``False``. + :raises: :class:`~openstack.exceptions.SDKException` when + ``raise_exception`` is ``True`` and requested version is not supported. + """ + required = discover.normalize_version_number(version) + + if discover.version_match(required, adapter.get_api_major_version()): + return True + + if raise_exception: + raise exceptions.SDKException( + f'Required version {version} is not supported by the server' + ) + + return False + + def supports_microversion(adapter, microversion, raise_exception=False): """Determine if the given adapter supports the given microversion. - Checks the min and max microversion asserted by the service and checks to - make sure that ``min <= microversion <= max``. Current default microversion - is taken into consideration if set and verifies that ``microversion <= - default``. + Checks the min and max microversion asserted by the service and ensures + ``min <= microversion <= max``. If set, the current default microversion is + taken into consideration to ensure ``microversion <= default``. :param adapter: :class:`~keystoneauth1.adapter.Adapter` instance. :param str microversion: String containing the desired microversion. :param bool raise_exception: Raise exception when requested microversion - is not supported be the server side or is higher than the current - default microversion. - :returns: True if the service supports the microversion. + is not supported by the server or is higher than the current default + microversion. + :returns: True if the service supports the microversion, else False. :rtype: bool - :raises: :class:`~openstack.exceptions.SDKException` when requested - microversion is not supported. + :raises: :class:`~openstack.exceptions.SDKException` when + ``raise_exception`` is ``True`` and requested microversion is not + supported. """ - endpoint_data = adapter.get_endpoint_data() if ( endpoint_data.min_microversion