mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
ModuleHelper: added a do_raise() method to MH base class (#4660)
* ModuleHelper: added a do_raise() method to MH base class * added changelog fragment * Update changelogs/fragments/4660-mh-added-do-raise.yaml Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/module_utils/mh/base.py Co-authored-by: Felix Fontein <felix@fontein.de> * using do_raise in CmdMixin * simplified do_raise() Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
7c9ad3082d
commit
5d72d1be2f
3 changed files with 8 additions and 4 deletions
2
changelogs/fragments/4660-mh-added-do-raise.yaml
Normal file
2
changelogs/fragments/4660-mh-added-do-raise.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
minor_changes:
|
||||
- ModuleHelper module utils - ``ModuleHelperBase`` now has a convenience method ``do_raise`` (https://github.com/ansible-collections/community.general/pull/4660).
|
|
@ -31,6 +31,9 @@ class ModuleHelperBase(object):
|
|||
def diff_mode(self):
|
||||
return self.module._diff
|
||||
|
||||
def do_raise(self, *args, **kwargs):
|
||||
raise _MHE(*args, **kwargs)
|
||||
|
||||
def __getattr__(self, attr):
|
||||
if attr in self._delegated_to_module:
|
||||
return getattr(self.module, attr)
|
||||
|
|
|
@ -128,8 +128,7 @@ class CmdMixin(object):
|
|||
for param in param_list:
|
||||
if isinstance(param, dict):
|
||||
if len(param) != 1:
|
||||
raise self.ModuleHelperException("run_command parameter as a dict must "
|
||||
"contain only one key: {0}".format(param))
|
||||
self.do_raise("run_command parameter as a dict must contain only one key: {0}".format(param))
|
||||
_param = list(param.keys())[0]
|
||||
fmt = find_format(_param)
|
||||
value = param[_param]
|
||||
|
@ -141,9 +140,9 @@ class CmdMixin(object):
|
|||
fmt = find_format(param)
|
||||
value = extra_params[param]
|
||||
else:
|
||||
raise self.ModuleHelperException('Cannot determine value for parameter: {0}'.format(param))
|
||||
self.do_raise('Cannot determine value for parameter: {0}'.format(param))
|
||||
else:
|
||||
raise self.ModuleHelperException("run_command parameter must be either a str or a dict: {0}".format(param))
|
||||
self.do_raise("run_command parameter must be either a str or a dict: {0}".format(param))
|
||||
cmd_args = add_arg_formatted_param(cmd_args, fmt, value)
|
||||
|
||||
return cmd_args
|
||||
|
|
Loading…
Reference in a new issue