Compare commits
2 Commits
feature/fo
...
master
Author | SHA1 | Date | |
---|---|---|---|
a4050ac521 | |||
4b476a8c3d |
36
collection/plugins/module_utils/common.py
Normal file
36
collection/plugins/module_utils/common.py
Normal 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
|
|
@ -151,10 +151,7 @@ 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
|
||||||
import json
|
from ansible_collections.community.syncthing.plugins.module_utils.common import get_changes
|
||||||
|
|
||||||
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(
|
||||||
|
@ -187,17 +184,8 @@ def run_module():
|
||||||
|
|
||||||
module_args_keys_list = list(module_args.keys())
|
module_args_keys_list = list(module_args.keys())
|
||||||
|
|
||||||
changes = {}
|
# Using get_changes function to determine what has changed
|
||||||
|
changes = get_changes(module.params, current_config)
|
||||||
for key, value in module.params.items():
|
|
||||||
# Check if the key is in module_args_keys_list
|
|
||||||
if key in module_args_keys_list:
|
|
||||||
# Check if the value is not None
|
|
||||||
if value is not None:
|
|
||||||
# Check if the value is different from the current_config
|
|
||||||
if not deep_equal(value, current_config.get(key)):
|
|
||||||
# If all conditions are met, add the key-value pair to changes
|
|
||||||
changes[key] = value
|
|
||||||
|
|
||||||
if module.check_mode or len(changes.keys()) == 0:
|
if module.check_mode or len(changes.keys()) == 0:
|
||||||
module.result['device_defaults'] = current_config
|
module.result['device_defaults'] = current_config
|
||||||
|
|
|
@ -1,89 +0,0 @@
|
||||||
#!/usr/bin/python
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
# Copyright: (c) 2018, Rafael Bodill <justrafi at google mail>
|
|
||||||
# Copyright: (c) 2020, Borjan Tchakaloff <first name at last name dot fr>
|
|
||||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
||||||
|
|
||||||
ANSIBLE_METADATA = {
|
|
||||||
'metadata_version': '1.1',
|
|
||||||
'status': ['preview'],
|
|
||||||
'supported_by': 'community'
|
|
||||||
}
|
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
|
||||||
---
|
|
||||||
module: syncthing_folders
|
|
||||||
|
|
||||||
short_description: Retrieve Syncthing folders
|
|
||||||
|
|
||||||
version_added: "2.7"
|
|
||||||
|
|
||||||
description:
|
|
||||||
- "This module retrieves the list of folders from Syncthing"
|
|
||||||
|
|
||||||
options:
|
|
||||||
host:
|
|
||||||
description:
|
|
||||||
- Host to connect to, including port
|
|
||||||
default: http://127.0.0.1:8384
|
|
||||||
unix_socket:
|
|
||||||
description:
|
|
||||||
- Use this unix socket instead of TCP
|
|
||||||
required: false
|
|
||||||
config_file:
|
|
||||||
description:
|
|
||||||
- Path to the Syncthing configuration file for automatic
|
|
||||||
discovery (`api_key`). Note that the running user needs read
|
|
||||||
access to the file.
|
|
||||||
required: false
|
|
||||||
timeout:
|
|
||||||
description:
|
|
||||||
- The socket level timeout in seconds
|
|
||||||
default: 30
|
|
||||||
|
|
||||||
author:
|
|
||||||
- Rafael Bodill (@rafi)
|
|
||||||
'''
|
|
||||||
|
|
||||||
EXAMPLES = '''
|
|
||||||
# Get folders
|
|
||||||
- name: Get folders
|
|
||||||
become: yes
|
|
||||||
become_user: syncthing
|
|
||||||
community.syncthing.folders:
|
|
||||||
host: http://localhost
|
|
||||||
unix_socket: /run/syncthing/syncthing.sock
|
|
||||||
config_file: /var/lib/syncthing/.config/syncthing/config.xml
|
|
||||||
register: folders
|
|
||||||
'''
|
|
||||||
|
|
||||||
RETURN = '''
|
|
||||||
folders:
|
|
||||||
description: The list of folders retrieved from Syncthing.
|
|
||||||
type: dict
|
|
||||||
response:
|
|
||||||
description: The API response, in-case of an error.
|
|
||||||
type: dict
|
|
||||||
'''
|
|
||||||
|
|
||||||
from ansible_collections.community.syncthing.plugins.module_utils.syncthing_api import SyncthingModule
|
|
||||||
|
|
||||||
def run_module():
|
|
||||||
module = SyncthingModule(
|
|
||||||
api_url='/rest/config/folders',
|
|
||||||
)
|
|
||||||
|
|
||||||
folders = {}
|
|
||||||
folders_list = module.get_call()
|
|
||||||
for folder in folders_list:
|
|
||||||
folders[folder['id']] = folder
|
|
||||||
module.result['folders'] = folders
|
|
||||||
|
|
||||||
module.exit_json()
|
|
||||||
|
|
||||||
def main():
|
|
||||||
run_module()
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
Loading…
Reference in New Issue
Block a user