From 0b5fd3a2cf7170abe9975b1dacc14fa8f0896388 Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Mon, 22 Apr 2024 16:11:17 +0800 Subject: [PATCH] Additional handling for 404 - sometimes 404 is acceptable --- collection/plugins/modules/device.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/collection/plugins/modules/device.py b/collection/plugins/modules/device.py index 964ebe9..61c1f24 100644 --- a/collection/plugins/modules/device.py +++ b/collection/plugins/modules/device.py @@ -120,7 +120,8 @@ def get_key_from_filesystem(module): "the API key manually.") # Fetch Syncthing configuration -def remote_config(module, method='GET', config=None, result=None, device=None): +def remote_config(module, method='GET', config=None, result=None, device=None, + missing_ok=False): unix_socket = None if 'unix_socket' in module.params: unix_socket = module.params['unix_socket'] @@ -140,23 +141,32 @@ def remote_config(module, method='GET', config=None, result=None, device=None): module, url=url, unix_socket=unix_socket, data=data, headers=headers, method=method, timeout=module.params['timeout']) - - if not info or info['status'] != 200: + if info: result['response'] = info + else: module.fail_json(msg='Error occured while calling host', **result) + if info['status'] not in [200, 404]: + module.fail_json(msg='Error occured while calling host', **result) + + if info['status'] == 404: + if missing_ok: + return {} + else: + module.fail_json(msg='Error occured while calling host', **result) + try: content = resp.read() - except AttributeError: + return json.loads(content) + except (AttributeError, json.decoder.JSONDecodeError): result['content'] = info.pop('body', '') - result['response'] = str(info) module.fail_json(msg='Error occured while reading response', **result) - return json.loads(content) + return {} # not reachable but prevents linter complaints def get_device(module, device=None): - return remote_config(module, device=device) + return remote_config(module, missing_ok=True, device=device) def delete_device(module, device, result=None): return remote_config(module, method='DELETE', device=device, result=result)