From 5d72d1be2fb4c0655121798dbfb70a057adba4d5 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Sat, 14 May 2022 06:40:17 +1200 Subject: [PATCH] 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 * Update plugins/module_utils/mh/base.py Co-authored-by: Felix Fontein * using do_raise in CmdMixin * simplified do_raise() Co-authored-by: Felix Fontein --- changelogs/fragments/4660-mh-added-do-raise.yaml | 2 ++ plugins/module_utils/mh/base.py | 3 +++ plugins/module_utils/mh/mixins/cmd.py | 7 +++---- 3 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/4660-mh-added-do-raise.yaml diff --git a/changelogs/fragments/4660-mh-added-do-raise.yaml b/changelogs/fragments/4660-mh-added-do-raise.yaml new file mode 100644 index 0000000000..01585afb96 --- /dev/null +++ b/changelogs/fragments/4660-mh-added-do-raise.yaml @@ -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). diff --git a/plugins/module_utils/mh/base.py b/plugins/module_utils/mh/base.py index 3d311de865..0871a527be 100644 --- a/plugins/module_utils/mh/base.py +++ b/plugins/module_utils/mh/base.py @@ -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) diff --git a/plugins/module_utils/mh/mixins/cmd.py b/plugins/module_utils/mh/mixins/cmd.py index 88b89d159b..58d50fbdf8 100644 --- a/plugins/module_utils/mh/mixins/cmd.py +++ b/plugins/module_utils/mh/mixins/cmd.py @@ -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