1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Improvements and fixes to ModuleHelper, with (some) tests. (#2024) (#2034)

* Improvements and fixes to ModuleHelper, with (some) tests.

* added changelog fragment

* adjusted changelog frag - get_bin_path() handling is actually a bugfix

(cherry picked from commit 4fbef900e1)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2021-03-17 14:15:48 +01:00 committed by GitHub
parent a83556af80
commit bbb155409e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 2 deletions

View file

@ -0,0 +1,4 @@
bugfixes:
- module_helper module utils - actually ignoring formatting of parameters with value ``None`` (https://github.com/ansible-collections/community.general/pull/2024).
- module_helper module utils - handling ``ModuleHelperException`` now properly calls ``fail_json()`` (https://github.com/ansible-collections/community.general/pull/2024).
- module_helper module utils - use the command name as-is in ``CmdMixin`` if it fails ``get_bin_path()`` - allowing full path names to be passed (https://github.com/ansible-collections/community.general/pull/2024).

View file

@ -93,6 +93,8 @@ class ArgFormat(object):
self.arg_format = (self.stars_deco(stars))(self.arg_format)
def to_text(self, value):
if value is None:
return []
func = self.arg_format
return [str(p) for p in func(value)]
@ -121,6 +123,7 @@ def module_fails_on_exception(func):
except ModuleHelperException as e:
if e.update_output:
self.update_output(e.update_output)
self.module.fail_json(changed=False, msg=e.msg, exception=traceback.format_exc(), output=self.output, vars=self.vars)
except Exception as e:
self.vars.msg = "Module failed with exception: {0}".format(str(e).strip())
self.vars.exception = traceback.format_exc()
@ -292,7 +295,10 @@ class CmdMixin(object):
extra_params = extra_params or dict()
cmd_args = list([self.command]) if isinstance(self.command, str) else list(self.command)
cmd_args[0] = self.module.get_bin_path(cmd_args[0])
try:
cmd_args[0] = self.module.get_bin_path(cmd_args[0], required=True)
except ValueError:
pass
param_list = params if params else self.module.params.keys()
for param in param_list:

View file

@ -22,24 +22,38 @@ ARG_FORMATS = dict(
True, ["--superflag"]),
simple_boolean_false=("--superflag", ArgFormat.BOOLEAN, 0,
False, []),
simple_boolean_none=("--superflag", ArgFormat.BOOLEAN, 0,
None, []),
single_printf=("--param=%s", ArgFormat.PRINTF, 0,
"potatoes", ["--param=potatoes"]),
single_printf_no_substitution=("--param", ArgFormat.PRINTF, 0,
"potatoes", ["--param"]),
single_printf_none=("--param=%s", ArgFormat.PRINTF, 0,
None, []),
multiple_printf=(["--param", "free-%s"], ArgFormat.PRINTF, 0,
"potatoes", ["--param", "free-potatoes"]),
single_format=("--param={0}", ArgFormat.FORMAT, 0,
"potatoes", ["--param=potatoes"]),
single_format_none=("--param={0}", ArgFormat.FORMAT, 0,
None, []),
single_format_no_substitution=("--param", ArgFormat.FORMAT, 0,
"potatoes", ["--param"]),
multiple_format=(["--param", "free-{0}"], ArgFormat.FORMAT, 0,
"potatoes", ["--param", "free-potatoes"]),
multiple_format_none=(["--param", "free-{0}"], ArgFormat.FORMAT, 0,
None, []),
single_lambda_0star=((lambda v: ["piggies=[{0},{1},{2}]".format(v[0], v[1], v[2])]), None, 0,
['a', 'b', 'c'], ["piggies=[a,b,c]"]),
single_lambda_0star_none=((lambda v: ["piggies=[{0},{1},{2}]".format(v[0], v[1], v[2])]), None, 0,
None, []),
single_lambda_1star=((lambda a, b, c: ["piggies=[{0},{1},{2}]".format(a, b, c)]), None, 1,
['a', 'b', 'c'], ["piggies=[a,b,c]"]),
single_lambda_1star_none=((lambda a, b, c: ["piggies=[{0},{1},{2}]".format(a, b, c)]), None, 1,
None, []),
single_lambda_2star=(single_lambda_2star, None, 2,
dict(z='c', x='a', y='b'), ["piggies=[a,b,c]"])
dict(z='c', x='a', y='b'), ["piggies=[a,b,c]"]),
single_lambda_2star_none=(single_lambda_2star, None, 2,
None, []),
)
ARG_FORMATS_IDS = sorted(ARG_FORMATS.keys())