diff --git a/roles/ipaclient/module_utils/ansible_ipa_client.py b/roles/ipaclient/module_utils/ansible_ipa_client.py index 304db30b3e9511c4b414c8dfc7760c2d9ab1972b..3116c7de34e0f0c92d41987c5d77314e38c04dae 100644 --- a/roles/ipaclient/module_utils/ansible_ipa_client.py +++ b/roles/ipaclient/module_utils/ansible_ipa_client.py @@ -5,7 +5,7 @@ # # Based on ipa-client-install code # -# Copyright (C) 2017 Red Hat +# Copyright (C) 2017-2022 Red Hat # see file 'COPYING' for use and warranty information # # This program is free software; you can redistribute it and/or modify @@ -46,17 +46,36 @@ __all__ = ["gssapi", "version", "ipadiscovery", "api", "errors", "x509", "configure_nslcd_conf", "configure_ssh_config", "configure_sshd_config", "configure_automount", "configure_firefox", "sync_time", "check_ldap_conf", - "sssd_enable_ifp", "getargspec"] + "sssd_enable_ifp", "getargspec", "paths", "options", + "IPA_PYTHON_VERSION", "NUM_VERSION", "certdb", "get_ca_cert", + "ipalib", "logger", "ipautil", "installer"] import sys -# HACK: workaround for Ansible 2.9 -# https://github.com/ansible/ansible/issues/68361 -if 'ansible.executor' in sys.modules: - for attr in __all__: - setattr(sys.modules[__name__], attr, None) - -else: +# Import getargspec from inspect or provide own getargspec for +# Python 2 compatibility with Python 3.11+. +try: + from inspect import getargspec +except ImportError: + from collections import namedtuple + from inspect import getfullargspec + + # The code is copied from Python 3.10 inspect.py + # Authors: Ka-Ping Yee <ping@lfw.org> + # Yury Selivanov <yselivanov@sprymix.com> + ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults') + + def getargspec(func): + args, varargs, varkw, defaults, kwonlyargs, _kwonlydefaults, \ + ann = getfullargspec(func) + if kwonlyargs or ann: + raise ValueError( + "Function has keyword-only parameters or annotations" + ", use inspect.signature() API which can support them") + return ArgSpec(args, varargs, varkw, defaults) + + +try: from ipapython.version import NUM_VERSION, VERSION if NUM_VERSION < 30201: @@ -113,33 +132,12 @@ else: import gssapi import logging - # Import getargspec from inspect or provide own getargspec for - # Python 2 compatibility with Python 3.11+. - try: - from inspect import getargspec - except ImportError: - from collections import namedtuple - from inspect import getfullargspec - - # The code is copied from Python 3.10 inspect.py - # Authors: Ka-Ping Yee <ping@lfw.org> - # Yury Selivanov <yselivanov@sprymix.com> - ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults') - - def getargspec(func): - args, varargs, varkw, defaults, kwonlyargs, _kwonlydefaults, \ - ann = getfullargspec(func) - if kwonlyargs or ann: - raise ValueError( - "Function has keyword-only parameters or annotations" - ", use inspect.signature() API which can support them") - return ArgSpec(args, varargs, varkw, defaults) - from ipapython import version try: from ipaclient.install import ipadiscovery except ImportError: from ipaclient import ipadiscovery + import ipalib from ipalib import api, errors, x509 from ipalib import constants try: @@ -312,6 +310,15 @@ else: raise Exception("freeipa version '%s' is too old" % VERSION) +except ImportError as _err: + ANSIBLE_IPA_CLIENT_MODULE_IMPORT_ERROR = str(_err) + + for attr in __all__: + setattr(sys.modules[__name__], attr, None) + +else: + ANSIBLE_IPA_CLIENT_MODULE_IMPORT_ERROR = None + def setup_logging(): standard_logging_setup( @@ -333,3 +340,8 @@ def ansible_module_get_parsed_ip_addresses(ansible_module, ansible_module.fail_json(msg="Invalid IP Address %s: %s" % (ip, e)) ip_addrs.append(ip_parsed) return ip_addrs + + +def check_imports(module): + if ANSIBLE_IPA_CLIENT_MODULE_IMPORT_ERROR is not None: + module.fail_json(msg=ANSIBLE_IPA_CLIENT_MODULE_IMPORT_ERROR)