From 7f340539db756e9ed5f5f63951da7737d1becb62 Mon Sep 17 00:00:00 2001 From: Shailaja Date: Thu, 6 Jun 2024 09:56:56 +0530 Subject: [PATCH] Added enhancements --- collection/plugins/module_utils/common.py | 36 +++++++++++++++++++ collection/plugins/modules/common.py | 14 -------- collection/plugins/modules/device_defaults.py | 2 +- 3 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 collection/plugins/module_utils/common.py delete mode 100644 collection/plugins/modules/common.py diff --git a/collection/plugins/module_utils/common.py b/collection/plugins/module_utils/common.py new file mode 100644 index 0000000..162d694 --- /dev/null +++ b/collection/plugins/module_utils/common.py @@ -0,0 +1,36 @@ +import json + +def deep_equal(a, b): + """ + Compare two data structures for deep equality by converting them to JSON strings. + + Parameters: + a (any): First data structure to compare. + b (any): Second data structure to compare. + + Returns: + bool: True if the two data structures are equal, False otherwise. + """ + return json.dumps(a, sort_keys=True) == json.dumps(b, sort_keys=True) + +def get_changes(module_params, module_args_keys_list, current_config): + """ + Function to get changes by comparing module parameters with current configuration. + + Args: + module_params (dict): The parameters of the module. + module_args_keys_list (list): List of keys to check in module parameters. + current_config (dict): The current configuration to compare against. + + Returns: + dict: A dictionary containing the changes. + """ + changes = {} + + for key, value in module_params.items(): + if key in module_args_keys_list: + if value is not None: + if not deep_equal(value, current_config.get(key)): + changes[key] = value + + return changes \ No newline at end of file diff --git a/collection/plugins/modules/common.py b/collection/plugins/modules/common.py deleted file mode 100644 index d631f87..0000000 --- a/collection/plugins/modules/common.py +++ /dev/null @@ -1,14 +0,0 @@ -import json - -def deep_equal(a, b): - """ - Compare two data structures for deep equality by converting them to JSON strings. - - Parameters: - a (any): First data structure to compare. - b (any): Second data structure to compare. - - Returns: - bool: True if the two data structures are equal, False otherwise. - """ - return json.dumps(a, sort_keys=True) == json.dumps(b, sort_keys=True) diff --git a/collection/plugins/modules/device_defaults.py b/collection/plugins/modules/device_defaults.py index 170c15f..29898a0 100644 --- a/collection/plugins/modules/device_defaults.py +++ b/collection/plugins/modules/device_defaults.py @@ -151,7 +151,7 @@ response: ''' from ansible_collections.community.syncthing.plugins.module_utils.syncthing_api import SyncthingModule -from common.py import deep_equal +from common.py import deep_equalfrom ansible_collections.community.syncthing.plugins.module_utils.common import get_changes import json def run_module():