Handle openstacksdk < 0.10.0: fix AttributeError (#50285)

* openstacksdk < 0.10.0: fix AttributeError

`openstack.version.__version__` expression raises an `AttributeError`
exception when openstacksdk < 0.10.0 is used. `openstack.version` is
now imported as a module, which works for all openstacksdk versions.

Error was:

    The full traceback is:
    Traceback (most recent call last):
      File "$HOME/.ansible/tmp/ansible-tmp-1545612308.8-46792777824159/AnsiballZ_os_security_group.py", line 113, in <module>
        _ansiballz_main()
      File "$HOME/.ansible/tmp/ansible-tmp-1545612308.8-46792777824159/AnsiballZ_os_security_group.py", line 105, in _ansiballz_main
        invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
      File "$HOME/.ansible/tmp/ansible-tmp-1545612308.8-46792777824159/AnsiballZ_os_security_group.py", line 48, in invoke_module
        imp.load_module('__main__', mod, module, MOD_DESC)
      File "/tmp/ansible_os_security_group_payload_keFTIJ/__main__.py", line 163, in <module>
      File "/tmp/ansible_os_security_group_payload_keFTIJ/__main__.py", line 115, in main
      File "/tmp/ansible_os_security_group_payload_keFTIJ/ansible_os_security_group_payload.zip/ansible/module_utils/openstack.py", line 121, in openstack_cloud_from_module
    AttributeError: 'module' object has no attribute 'version'

* Add missing space & dot in error message

* Always require openstacksdk >= 0.12
This commit is contained in:
Pilou 2019-05-24 15:27:48 +00:00 committed by Monty Taylor
parent 6c74e29618
commit 4f25435cd2
2 changed files with 11 additions and 6 deletions

View file

@ -114,14 +114,19 @@ def openstack_cloud_from_module(module, min_version='0.12.0'):
# Due to the name shadowing we should import other way
import importlib
sdk = importlib.import_module('openstack')
sdk_version = importlib.import_module('openstack.version')
except ImportError:
module.fail_json(msg='openstacksdk is required for this module')
if min_version:
if StrictVersion(sdk.version.__version__) < StrictVersion(min_version):
min_version = max(StrictVersion('0.12.0'), StrictVersion(min_version))
else:
min_version = StrictVersion('0.12.0')
if 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(
msg="To utilize this module, the installed version of "
"the openstacksdk library MUST be >={min_version}.".format(
min_version=min_version))
cloud_config = module.params.pop('cloud', None)

View file

@ -88,7 +88,7 @@ options:
version_added: "2.3"
requirements:
- python >= 2.7
- openstacksdk
- openstacksdk >= 0.12.0
notes:
- The standard OpenStack environment variables, such as C(OS_USERNAME)
may be used instead of providing explicit values.