syncthing_folder: Always share a folder with the current device
All checks were successful
buildbot/travis_bionic Build done.
buildbot/multibuild_parent Build done.

There is no need to require that the device being configured is
explictly mentioned in its configuration. We can get it through
the status endpoint and automatically add it to the set of devices
the folder is shared with.
This commit is contained in:
Borjan Tchakaloff 2021-02-19 17:24:40 +01:00
parent c3cd1e450d
commit 3a49d17dfd

View File

@ -165,6 +165,10 @@ def get_data_from_rest_api(module, resource):
def get_config(module): def get_config(module):
return get_data_from_rest_api(module, 'system/config') return get_data_from_rest_api(module, 'system/config')
# Fetch Syncthing status
def get_status(module):
return get_data_from_rest_api(module, 'system/status')
# Get the device name -> device ID mapping. # Get the device name -> device ID mapping.
def get_devices_mapping(config): def get_devices_mapping(config):
return { return {
@ -197,23 +201,28 @@ def post_config(module, config, result):
module.fail_json(**result) module.fail_json(**result)
# Returns an object of a new folder # Returns an object of a new folder
def create_folder(params, current_device_ids, devices_mapping): def create_folder(params, self_id, current_device_ids, devices_mapping):
wanted_device_ids = [] # We need the current device ID as per the Syncthing API.
# If missing, Syncthing will add it alright, but we don't want to give
# the false idea that this configuration is different just because of that.
wanted_device_ids = {self_id}
for device_name_or_id in params['devices']: for device_name_or_id in params['devices']:
if device_name_or_id in devices_mapping: if device_name_or_id in devices_mapping:
wanted_device_ids.append(devices_mapping[device_name_or_id]) wanted_device_ids.add(devices_mapping[device_name_or_id])
else: else:
# Purposefully do not validate we already know this device ID or # Purposefully do not validate we already know this device ID or
# name as per previous behavior. This will need to be fixed. # name as per previous behavior. This will need to be fixed.
wanted_device_ids.append(device_name_or_id) wanted_device_ids.add(device_name_or_id)
# Collect wanted devices to share folder with. # Keep the original ordering if collections are equivalent.
# Note that the sequence ordering matters, so we stick with lists # Again, for idempotency reasons.
# instead of sets.
device_ids = ( device_ids = (
current_device_ids if set(current_device_ids) == set(wanted_device_ids) current_device_ids
else wanted_device_ids if set(current_device_ids) == wanted_device_ids
else sorted(wanted_device_ids)
) )
# Sort the device IDs to keep idem-potency
devices = [ devices = [
{ {
'deviceID': device_id, 'deviceID': device_id,
@ -300,6 +309,7 @@ def run_module():
module.params['api_key'] = get_key_from_filesystem(module) module.params['api_key'] = get_key_from_filesystem(module)
config = get_config(module) config = get_config(module)
self_id = get_status(module)['myID']
devices_mapping = get_devices_mapping(config) devices_mapping = get_devices_mapping(config)
if module.params['state'] == 'absent': if module.params['state'] == 'absent':
# Remove folder from list, if found # Remove folder from list, if found
@ -314,7 +324,7 @@ def run_module():
[d['deviceID'] for d in folder_config['devices']] if folder_config else [] [d['deviceID'] for d in folder_config['devices']] if folder_config else []
) )
folder_config_wanted = create_folder( folder_config_wanted = create_folder(
module.params, folder_config_devices, devices_mapping module.params, self_id, folder_config_devices, devices_mapping
) )
if folder_config is None: if folder_config is None: