1
0
Fork 0
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:
Alexei Znamensky 2022-05-14 06:40:17 +12:00 committed by GitHub
parent 7c9ad3082d
commit 5d72d1be2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 4 deletions

View 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).

View file

@ -31,6 +31,9 @@ class ModuleHelperBase(object):
def diff_mode(self): def diff_mode(self):
return self.module._diff return self.module._diff
def do_raise(self, *args, **kwargs):
raise _MHE(*args, **kwargs)
def __getattr__(self, attr): def __getattr__(self, attr):
if attr in self._delegated_to_module: if attr in self._delegated_to_module:
return getattr(self.module, attr) return getattr(self.module, attr)

View file

@ -128,8 +128,7 @@ class CmdMixin(object):
for param in param_list: for param in param_list:
if isinstance(param, dict): if isinstance(param, dict):
if len(param) != 1: if len(param) != 1:
raise self.ModuleHelperException("run_command parameter as a dict must " self.do_raise("run_command parameter as a dict must contain only one key: {0}".format(param))
"contain only one key: {0}".format(param))
_param = list(param.keys())[0] _param = list(param.keys())[0]
fmt = find_format(_param) fmt = find_format(_param)
value = param[_param] value = param[_param]
@ -141,9 +140,9 @@ class CmdMixin(object):
fmt = find_format(param) fmt = find_format(param)
value = extra_params[param] value = extra_params[param]
else: else:
raise self.ModuleHelperException('Cannot determine value for parameter: {0}'.format(param)) self.do_raise('Cannot determine value for parameter: {0}'.format(param))
else: 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) cmd_args = add_arg_formatted_param(cmd_args, fmt, value)
return cmd_args return cmd_args