From 9767d592a8fdfab93b1e65da12d9a283c365aa0b Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Thu, 14 May 2020 19:50:29 +0200 Subject: [PATCH] Add resource_from_fqcr utility function. (#68474) --- .../executor/powershell/module_manifest.py | 3 ++- lib/ansible/plugins/action/__init__.py | 5 +++-- lib/ansible/plugins/cache/__init__.py | 4 +++- lib/ansible/utils/collection_loader.py | 15 +++++++++++++++ 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/ansible/executor/powershell/module_manifest.py b/lib/ansible/executor/powershell/module_manifest.py index 853ee264ce9..321ff83455e 100644 --- a/lib/ansible/executor/powershell/module_manifest.py +++ b/lib/ansible/executor/powershell/module_manifest.py @@ -19,6 +19,7 @@ from ansible.errors import AnsibleError from ansible.module_utils._text import to_bytes, to_native, to_text from ansible.module_utils.compat.importlib import import_module from ansible.plugins.loader import ps_module_utils_loader +from ansible.utils.collection_loader import resource_from_fqcr class PSModuleDepFinder(object): @@ -309,7 +310,7 @@ def _create_powershell_wrapper(b_module_data, module_path, module_args, exec_manifest["async_timeout_sec"] = async_timeout exec_manifest["async_startup_timeout"] = C.config.get_config_value("WIN_ASYNC_STARTUP_TIMEOUT", variables=task_vars) - if become and become_method.split('.')[-1] == 'runas': # runas and namespace.collection.runas + if become and resource_from_fqcr(become_method) == 'runas': # runas and namespace.collection.runas finder.scan_exec_script('exec_wrapper') finder.scan_exec_script('become_wrapper') diff --git a/lib/ansible/plugins/action/__init__.py b/lib/ansible/plugins/action/__init__.py index 9f96848503e..2f70946ee3c 100644 --- a/lib/ansible/plugins/action/__init__.py +++ b/lib/ansible/plugins/action/__init__.py @@ -28,6 +28,7 @@ from ansible.module_utils.six.moves import shlex_quote from ansible.module_utils._text import to_bytes, to_native, to_text from ansible.parsing.utils.jsonify import jsonify from ansible.release import __version__ +from ansible.utils.collection_loader import resource_from_fqcr from ansible.utils.display import Display from ansible.utils.unsafe_proxy import wrap_var, AnsibleUnsafeText from ansible.vars.clean import remove_internal_keys @@ -178,7 +179,7 @@ class ActionBase(with_metaclass(ABCMeta, object)): module_name = '%s.%s' % (win_collection, module_name) # Remove extra quotes surrounding path parameters before sending to module. - if module_name.split('.')[-1] in ['win_stat', 'win_file', 'win_copy', 'slurp'] and module_args and \ + if resource_from_fqcr(module_name) in ['win_stat', 'win_file', 'win_copy', 'slurp'] and module_args and \ hasattr(self._connection._shell, '_unquote'): for key in ('src', 'dest', 'path'): if key in module_args: @@ -1060,7 +1061,7 @@ class ActionBase(with_metaclass(ABCMeta, object)): ruser = self._get_remote_user() buser = self.get_become_option('become_user') if (sudoable and self._connection.become and # if sudoable and have become - self._connection.transport.split('.')[-1] != 'network_cli' and # if not using network_cli + resource_from_fqcr(self._connection.transport) != 'network_cli' and # if not using network_cli (C.BECOME_ALLOW_SAME_USER or (buser != ruser or not any((ruser, buser))))): # if we allow same user PE or users are different and either is set display.debug("_low_level_execute_command(): using become for this command") cmd = self._connection.become.build_become_command(cmd, self._connection._shell) diff --git a/lib/ansible/plugins/cache/__init__.py b/lib/ansible/plugins/cache/__init__.py index da6f489b874..a7a1338ba44 100644 --- a/lib/ansible/plugins/cache/__init__.py +++ b/lib/ansible/plugins/cache/__init__.py @@ -31,6 +31,7 @@ from ansible.module_utils._text import to_bytes, to_text from ansible.module_utils.common._collections_compat import MutableMapping from ansible.plugins import AnsiblePlugin from ansible.plugins.loader import cache_loader +from ansible.utils.collection_loader import resource_from_fqcr from ansible.utils.display import Display from ansible.vars.fact_cache import FactCache as RealFactCache @@ -63,6 +64,7 @@ class BaseCacheModule(AnsiblePlugin): if not hasattr(self, '_load_name'): display.deprecated('Rather than importing custom CacheModules directly, use ansible.plugins.loader.cache_loader', version='2.14') self._load_name = self.__module__.split('.')[-1] + self._load_name = resource_from_fqcr(self.__module__) super(BaseCacheModule, self).__init__() self.set_options(var_options=args, direct=kwargs) @@ -108,7 +110,7 @@ class BaseFileCacheModule(BaseCacheModule): except KeyError: self._cache_dir = self._get_cache_connection(C.CACHE_PLUGIN_CONNECTION) self._timeout = float(C.CACHE_PLUGIN_TIMEOUT) - self.plugin_name = self.__module__.split('.')[-1] + self.plugin_name = resource_from_fqcr(self.__module__) self._cache = {} self.validate_cache_connection() diff --git a/lib/ansible/utils/collection_loader.py b/lib/ansible/utils/collection_loader.py index 560a5c60b10..e38d4137f07 100644 --- a/lib/ansible/utils/collection_loader.py +++ b/lib/ansible/utils/collection_loader.py @@ -586,3 +586,18 @@ def get_collection_name_from_path(path): def set_collection_playbook_paths(b_playbook_paths): AnsibleCollectionLoader().set_playbook_paths(b_playbook_paths) + + +def resource_from_fqcr(ref): + """ + Return resource from a fully-qualified collection reference, + or from a simple resource name. + + For fully-qualified collection references, this is equivalent to + ``AnsibleCollectionRef.from_fqcr(ref).resource``. + + :param ref: collection reference to parse + :return: the resource as a unicode string + """ + ref = to_text(ref, errors='strict') + return ref.split(u'.')[-1]