From ea8f109056a77b95fd2406d8a3d23a6c578a5abe Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 17 Apr 2023 04:05:24 +0000 Subject: [PATCH] [PR #6336/59cb5b2d backport][stable-6] dconf: minor refactor (#6353) dconf: minor refactor (#6336) * dconf: minor refactor * add changelog frag * Update plugins/modules/dconf.py Co-authored-by: Felix Fontein --------- Co-authored-by: Felix Fontein (cherry picked from commit 59cb5b2d6fd90c38bd6316874035f3818de67380) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- changelogs/fragments/6336-dconf-refactor.yml | 2 ++ plugins/modules/dconf.py | 27 ++++++++------------ 2 files changed, 12 insertions(+), 17 deletions(-) create mode 100644 changelogs/fragments/6336-dconf-refactor.yml diff --git a/changelogs/fragments/6336-dconf-refactor.yml b/changelogs/fragments/6336-dconf-refactor.yml new file mode 100644 index 0000000000..3e7ddfccd6 --- /dev/null +++ b/changelogs/fragments/6336-dconf-refactor.yml @@ -0,0 +1,2 @@ +minor_changes: + - dconf - minor refactoring improving parameters and dependencies validation (https://github.com/ansible-collections/community.general/pull/6336). diff --git a/plugins/modules/dconf.py b/plugins/modules/dconf.py index 9777102b0e..144440f70b 100644 --- a/plugins/modules/dconf.py +++ b/plugins/modules/dconf.py @@ -143,7 +143,10 @@ EXAMPLES = r""" import os -import traceback + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.common.text.converters import to_native +from ansible_collections.community.general.plugins.module_utils import deps try: from gi.repository.GLib import Variant, GError @@ -151,16 +154,8 @@ except ImportError: Variant = None GError = AttributeError -PSUTIL_IMP_ERR = None -try: +with deps.declare("psutil"): import psutil - HAS_PSUTIL = True -except ImportError: - PSUTIL_IMP_ERR = traceback.format_exc() - HAS_PSUTIL = False - -from ansible.module_utils.basic import AnsibleModule, missing_required_lib -from ansible.module_utils.common.text.converters import to_native class DBusWrapper(object): @@ -414,7 +409,10 @@ def main(): # Converted to str below after special handling of bool. value=dict(required=False, default=None, type='raw'), ), - supports_check_mode=True + supports_check_mode=True, + required_if=[ + ('state', 'present', ['value']), + ], ) # Try to be forgiving about the user specifying a boolean as the value, or @@ -435,12 +433,7 @@ def main(): 'using string comparison to check value equality. This fallback ' 'will be deprecated in a future version of community.general.') - if not HAS_PSUTIL: - module.fail_json(msg=missing_required_lib("psutil"), exception=PSUTIL_IMP_ERR) - - # If present state was specified, value must be provided. - if module.params['state'] == 'present' and module.params['value'] is None: - module.fail_json(msg='State "present" requires "value" to be set.') + deps.validate(module) # Create wrapper instance. dconf = DconfPreference(module, module.check_mode)