Additional handling for 404

- sometimes 404 is acceptable
This commit is contained in:
Peter Šurda 2024-04-22 16:11:17 +08:00
parent 00ca2d2133
commit 0b5fd3a2cf
Signed by untrusted user: PeterSurda
GPG Key ID: 3E47497CF67ABB95

View File

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