From 4b476a8c3df66256f98175f78fa047213a7ab5dc Mon Sep 17 00:00:00 2001 From: Shailaja Date: Tue, 4 Jun 2024 05:45:28 +0530 Subject: [PATCH] created common.py for extracting deep comparision function. --- collection/plugins/module_utils/common.py | 36 +++++++++++++++++++ collection/plugins/modules/device_defaults.py | 4 +-- 2 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 collection/plugins/module_utils/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/device_defaults.py b/collection/plugins/modules/device_defaults.py index 55a6f02..049314b 100644 --- a/collection/plugins/modules/device_defaults.py +++ b/collection/plugins/modules/device_defaults.py @@ -151,11 +151,9 @@ response: ''' from ansible_collections.community.syncthing.plugins.module_utils.syncthing_api import SyncthingModule +from ansible_collections.community.syncthing.plugins.module_utils.common import get_changes import json -def deep_equal(a, b): - return json.dumps(a, sort_keys=True) == json.dumps(b, sort_keys=True) - def run_module(): module_args = dict( addresses=dict(type='list', required=False),