Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
ModuleHelper module utils: delegates unknown attributes to AnsibleMod…
Browse files Browse the repository at this point in the history
…ule (ansible-collections#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 <[email protected]>

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

Co-authored-by: Felix Fontein <[email protected]>

Co-authored-by: Felix Fontein <[email protected]>
  • Loading branch information
russoz and felixfontein authored May 9, 2022
1 parent f17d266 commit bca7f09
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/4600-mh-delegate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ModuleHelper module utils - ``ModuleHelperBase` now delegates the attributes ``check_mode``, ``get_bin_path``, ``warn``, and ``deprecate`` to the underlying ``AnsibleModule`` instance (https://github.com/ansible-collections/community.general/pull/4600).
12 changes: 12 additions & 0 deletions plugins/module_utils/mh/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
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
Expand All @@ -24,6 +27,15 @@ def __init__(self, module=None):
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

Expand Down
5 changes: 3 additions & 2 deletions plugins/module_utils/mh/module_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def __init__(self, module=None):
version="6.0.0",
collection_name="community.general",
target=ModuleHelper,
module=self.module)
module=self.module,
)

def update_output(self, **kwargs):
self.update_vars(meta={"output": True}, **kwargs)
Expand All @@ -65,7 +66,7 @@ def output(self):
facts = self.vars.facts()
if facts is not None:
result['ansible_facts'] = {self.facts_name: facts}
if self.module._diff:
if self.diff_mode:
diff = result.get('diff', {})
vars_diff = self.vars.diff() or {}
result['diff'] = dict_merge(dict(diff), vars_diff)
Expand Down

0 comments on commit bca7f09

Please sign in to comment.