created common.py for extracting deep comparision function. #11

Merged
PeterSurda merged 1 commits from shailaja/ansible-modules-syncthing:common_file into master 2024-06-10 15:38:52 +02:00
2 changed files with 37 additions and 3 deletions

View File

@ -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

View File

@ -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.syncthing_api import SyncthingModule
from ansible_collections.community.syncthing.plugins.module_utils.common import get_changes
PeterSurda marked this conversation as resolved Outdated

due to how ansible loads modules, this needs to have a different format, I think the correct one is:

from ansible_collections.community.syncthing.plugins.module_utils.common import deep_equal

due to how ansible loads modules, this needs to have a different format, I think the correct one is: `from ansible_collections.community.syncthing.plugins.module_utils.common import deep_equal`

This doesn't look syntactically correct

This doesn't look syntactically correct
import json import json
def deep_equal(a, b):
return json.dumps(a, sort_keys=True) == json.dumps(b, sort_keys=True)
def run_module(): def run_module():
module_args = dict( module_args = dict(
addresses=dict(type='list', required=False), addresses=dict(type='list', required=False),