From bc6622e0e7dce7b1a0d90b501424fa8c28d3f405 Mon Sep 17 00:00:00 2001 From: Jakob Meng Date: Fri, 22 Apr 2022 12:14:44 +0200 Subject: [PATCH] Added support for specifying a maximum version of the OpenStack SDK Alongside OpenStack SDK 1.0.0 we will release a new collection version 2.0.0 which is compatible to OpenStack SDK 1.x.x series only. Code in branch stable/1.0.0 will remain compatible to OpenStack SDK 0.x.x series only. At Ansible OpenStack modules PTG on 2022-04-07 it was decided to raise an error if one is using a incompatible releases of the OpenStack SDK with our collection. We decided against showing warnings only because they can be missed easily and functionality will be broken but probably hardly detectable when using the wrong SDK. This patch implements the code to raise errors when users are trying to use our collection with an incompatible SDK release, e.g. use code from our stable/1.0.0 branch with a OpenStack SDK 1.x.x release. It does not yet change the minimum and maximum required SDK versions because OpenStack SDK 1.0.0 has not yet been released to PyPI and SDK's master branch still does not return a 1.x.x version number. Change-Id: I1052d21cf8f108dbc99619cd4c4072488645b855 --- plugins/module_utils/openstack.py | 49 +++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/plugins/module_utils/openstack.py b/plugins/module_utils/openstack.py index 86b968e3..80c89b1f 100644 --- a/plugins/module_utils/openstack.py +++ b/plugins/module_utils/openstack.py @@ -68,6 +68,7 @@ OVERRIDES = {'os_client_config': 'config', CUSTOM_VAR_PARAMS = ['min_ver', 'max_ver'] MINIMUM_SDK_VERSION = '0.36.0' +MAXIMUM_SDK_VERSION = None def openstack_argument_spec(): @@ -152,7 +153,7 @@ def openstack_module_kwargs(**kwargs): # for compatibility with old versions -def openstack_cloud_from_module(module, min_version=None): +def openstack_cloud_from_module(module, min_version=None, max_version=None): try: # Due to the name shadowing we should import other way sdk = importlib.import_module('openstack') @@ -160,18 +161,30 @@ def openstack_cloud_from_module(module, min_version=None): except ImportError: module.fail_json(msg='openstacksdk is required for this module') - if min_version: + if min_version and MINIMUM_SDK_VERSION: min_version = max(StrictVersion(MINIMUM_SDK_VERSION), StrictVersion(min_version)) - else: + elif MINIMUM_SDK_VERSION: min_version = StrictVersion(MINIMUM_SDK_VERSION) - if StrictVersion(sdk_version.__version__) < min_version: + if max_version and MAXIMUM_SDK_VERSION: + max_version = min(StrictVersion(MAXIMUM_SDK_VERSION), + StrictVersion(max_version)) + elif MAXIMUM_SDK_VERSION: + max_version = StrictVersion(MAXIMUM_SDK_VERSION) + + if min_version and StrictVersion(sdk_version.__version__) < min_version: module.fail_json( msg="To utilize this module, the installed version of " "the openstacksdk library MUST be >={min_version}.".format( min_version=min_version)) + if max_version and StrictVersion(sdk_version.__version__) > max_version: + module.fail_json( + msg="To utilize this module, the installed version of " + "the openstacksdk library MUST be <={max_version}.".format( + max_version=max_version)) + cloud_config = module.params.pop('cloud', None) try: if isinstance(cloud_config, dict): @@ -244,6 +257,7 @@ class OpenStackModule: argument_spec = {} module_kwargs = {} module_min_sdk_version = None + module_max_sdk_version = None def __init__(self): """Initialize Openstack base class. @@ -306,19 +320,36 @@ class OpenStackModule: except ImportError: self.fail_json(msg='openstacksdk is required for this module') - # Fail if the available SDK version doesn't meet the minimum version - # requirements - if self.module_min_sdk_version: + # Fail if the available SDK version doesn't meet the minimum + # and maximum version requirements + if self.module_min_sdk_version and MINIMUM_SDK_VERSION: min_version = max(StrictVersion(MINIMUM_SDK_VERSION), StrictVersion(self.module_min_sdk_version)) - else: + elif MINIMUM_SDK_VERSION: min_version = StrictVersion(MINIMUM_SDK_VERSION) - if StrictVersion(self.sdk_version) < min_version: + else: + min_version = None + + if self.module_max_sdk_version and MAXIMUM_SDK_VERSION: + max_version = min(StrictVersion(MAXIMUM_SDK_VERSION), + StrictVersion(self.module_max_sdk_version)) + elif MAXIMUM_SDK_VERSION: + max_version = StrictVersion(MAXIMUM_SDK_VERSION) + else: + max_version = None + + if min_version and StrictVersion(self.sdk_version) < min_version: self.fail( msg="To utilize this module, the installed version of " "the openstacksdk library MUST be >={min_version}.".format( min_version=min_version)) + if max_version and StrictVersion(self.sdk_version) > max_version: + self.fail( + msg="To utilize this module, the installed version of " + "the openstacksdk library MUST be <={max_version}.".format( + max_version=max_version)) + # Fail if there are set unsupported for this version parameters # New parameters should NOT use 'default' but rely on SDK defaults for param in self.argument_spec: