2018-11-28 20:22:58 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright: (c) 2018, Rafael Bodill <justrafi at google mail>
|
2021-01-01 16:01:13 +01:00
|
|
|
# Copyright: (c) 2020, Borjan Tchakaloff <first name at last name dot fr>
|
2018-11-28 20:22:58 +01:00
|
|
|
# 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_device
|
|
|
|
|
|
|
|
short_description: Manage Syncthing devices
|
|
|
|
|
|
|
|
version_added: "2.7"
|
|
|
|
|
|
|
|
description:
|
|
|
|
- "This is my longer description explaining my sample module"
|
|
|
|
|
|
|
|
options:
|
|
|
|
id:
|
|
|
|
description:
|
|
|
|
- This is the unique id of this new device
|
|
|
|
required: true
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The name for this new device
|
|
|
|
required: false
|
|
|
|
host:
|
|
|
|
description:
|
|
|
|
- Host to connect to, including port
|
|
|
|
default: http://127.0.0.1:8384
|
2024-04-22 09:11:11 +02:00
|
|
|
unix_socket:
|
|
|
|
description:
|
|
|
|
- Use this unix socket instead of TCP
|
|
|
|
required: false
|
2018-11-28 20:22:58 +01:00
|
|
|
api_key:
|
|
|
|
description:
|
|
|
|
- API key to use for authentication with host.
|
|
|
|
If not provided, will try to auto-configure from filesystem.
|
|
|
|
required: false
|
2021-01-01 16:01:13 +01:00
|
|
|
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
|
2018-11-28 20:22:58 +01:00
|
|
|
timeout:
|
|
|
|
description:
|
|
|
|
- The socket level timeout in seconds
|
|
|
|
default: 30
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Use present/absent to ensure device is added, or not.
|
|
|
|
default: present
|
|
|
|
choices: ['absent', 'present', 'paused']
|
|
|
|
|
|
|
|
author:
|
|
|
|
- Rafael Bodill (@rafi)
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Add a device to share with
|
|
|
|
- name: Add syncthing device
|
|
|
|
syncthing_device:
|
|
|
|
id: 1234-1234-1234-1234
|
|
|
|
name: my-server-name
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
response:
|
|
|
|
description: The API response, in-case of an error.
|
|
|
|
type: dict
|
|
|
|
'''
|
|
|
|
|
2024-04-23 05:51:43 +02:00
|
|
|
from ansible.collections.syncthing.plugins.module_utils.syncthing_api \
|
|
|
|
import SyncthingModule
|
2018-11-28 20:22:58 +01:00
|
|
|
|
|
|
|
# Returns an object of a new device
|
|
|
|
def create_device(params):
|
|
|
|
device = {
|
|
|
|
'addresses': [
|
|
|
|
'dynamic'
|
|
|
|
],
|
|
|
|
'allowedNetworks': [],
|
|
|
|
'autoAcceptFolders': False,
|
|
|
|
'certName': '',
|
|
|
|
'compression': 'metadata',
|
|
|
|
'deviceID': params['id'],
|
|
|
|
'ignoredFolders': [],
|
|
|
|
'introducedBy': '',
|
|
|
|
'introducer': False,
|
|
|
|
'maxRecvKbps': 0,
|
|
|
|
'maxSendKbps': 0,
|
|
|
|
'name': params['name'],
|
|
|
|
'paused': True if params['state'] == 'paused' else False,
|
|
|
|
'pendingFolders': [],
|
|
|
|
'skipIntroductionRemovals': False
|
|
|
|
}
|
|
|
|
return device
|
|
|
|
|
|
|
|
def run_module():
|
|
|
|
# module arguments
|
2024-04-23 05:51:43 +02:00
|
|
|
module_args = {}
|
2018-11-28 20:22:58 +01:00
|
|
|
module_args.update(dict(
|
|
|
|
id=dict(type='str', required=True),
|
|
|
|
name=dict(type='str', required=False),
|
|
|
|
state=dict(type='str', default='present',
|
2024-04-22 16:54:40 +02:00
|
|
|
choices=['absent', 'present', 'paused']),
|
2018-11-28 20:22:58 +01:00
|
|
|
))
|
|
|
|
|
|
|
|
# the AnsibleModule object will be our abstraction working with Ansible
|
2024-04-23 05:51:43 +02:00
|
|
|
module = SyncthingModule(
|
|
|
|
api_url='/rest/config/devices',
|
|
|
|
argument_spec=module_args
|
2018-11-28 20:22:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if module.params['state'] != 'absent' and not module.params['name']:
|
2024-04-23 05:51:43 +02:00
|
|
|
module.fail_json(msg='You must provide a name when creating')
|
2018-11-28 20:22:58 +01:00
|
|
|
|
2024-04-23 05:51:43 +02:00
|
|
|
want_pause = module.params['state'] == 'paused'
|
2024-04-21 15:55:15 +02:00
|
|
|
device_exists = False
|
2024-04-23 05:51:43 +02:00
|
|
|
device = module.get_call(target=module.params['id'])
|
2024-04-21 15:55:15 +02:00
|
|
|
if 'deviceID' in device and device['deviceID'] == module.params['id']:
|
|
|
|
device_exists = True
|
|
|
|
|
2018-11-28 20:22:58 +01:00
|
|
|
if module.params['state'] == 'absent':
|
2024-04-21 15:55:15 +02:00
|
|
|
if device_exists:
|
2024-04-23 05:51:43 +02:00
|
|
|
if module.check_mode:
|
|
|
|
module.result['changed'] = True
|
|
|
|
else:
|
|
|
|
module.delete_call(target=device['deviceID'])
|
2024-04-21 15:55:15 +02:00
|
|
|
elif device_exists: # exists but maybe needs changing
|
|
|
|
if device['paused'] != want_pause:
|
|
|
|
device['paused'] = want_pause
|
2024-04-23 05:51:43 +02:00
|
|
|
if module.check_mode:
|
|
|
|
module.result['changed'] = True
|
|
|
|
else:
|
|
|
|
module.patch_call(data=device, target=device['deviceID'])
|
2024-04-21 15:55:15 +02:00
|
|
|
else: # Doesn't exist but needs to be added
|
2024-04-23 05:51:43 +02:00
|
|
|
if module.check_mode:
|
|
|
|
module.result['changed'] = True
|
|
|
|
else:
|
|
|
|
device = create_device(module.params)
|
|
|
|
module.post_call(data=device)
|
2018-11-28 20:22:58 +01:00
|
|
|
|
2024-04-23 05:51:43 +02:00
|
|
|
module.exit_json()
|
2018-11-28 20:22:58 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
run_module()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|