ModuleHelper module utils: delegates unknown attributes to AnsibleModule (#4600)

* ModuleHelper module util: delegates unknown attributes to AnsibleModule

* added changelog fragment

* delegate only a few selected attrs

* Update plugins/module_utils/mh/base.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update changelogs/fragments/4600-mh-delegate.yaml

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Alexei Znamensky 2022-05-09 17:10:49 +12:00 committed by GitHub
commit bca7f09b71
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 2 deletions

View file

@ -14,6 +14,9 @@ from ansible_collections.community.general.plugins.module_utils.mh.deco import m
class ModuleHelperBase(object):
module = None
ModuleHelperException = _MHE
_delegated_to_module = (
'check_mode', 'get_bin_path', 'warn', 'deprecate',
)
def __init__(self, module=None):
self._changed = False
@ -24,6 +27,15 @@ class ModuleHelperBase(object):
if not isinstance(self.module, AnsibleModule):
self.module = AnsibleModule(**self.module)
@property
def diff_mode(self):
return self.module._diff
def __getattr__(self, attr):
if attr in self._delegated_to_module:
return getattr(self.module, attr)
raise AttributeError("ModuleHelperBase has no attribute '%s'" % (attr, ))
def __init_module__(self):
pass