Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Max-checkmk committed Nov 15, 2023
1 parent 00307d1 commit b03b4ce
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion plugins/lookup/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def run(self, terms, variables, **kwargs):
if "code" in response:
raise AnsibleError(
"Received error for %s - %s: %s"
% (response["url"], response["code"], response["msg"])
% (response.get("url", ""), response.get("code", ""), response.get("msg", ""))
)
ret.append(response.get("extensions"))

Expand Down
2 changes: 1 addition & 1 deletion plugins/lookup/folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def run(self, terms, variables, **kwargs):
if "code" in response:
raise AnsibleError(
"Received error for %s - %s: %s"
% (response["url"], response["code"], response["msg"])
% (response.get("url", ""), response.get("code", ""), response.get("msg", ""))
)
ret.append(response.get("value"))

Expand Down
2 changes: 1 addition & 1 deletion plugins/lookup/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def run(self, terms, variables, **kwargs):
if "code" in response:
raise AnsibleError(
"Received error for %s - %s: %s"
% (response["url"], response["code"], response["msg"])
% (response.get("url", ""), response.get("code", ""), response.get("msg", ""))
)
ret.append(response.get("extensions"))

Expand Down
2 changes: 1 addition & 1 deletion plugins/lookup/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def run(self, terms, variables, **kwargs):
if "code" in response:
raise AnsibleError(
"Received error for %s - %s: %s"
% (response["url"], response["code"], response["msg"])
% (response.get("url", ""), response.get("code", ""), response.get("msg", ""))
)
ret.append(response.get("value"))

Expand Down
2 changes: 1 addition & 1 deletion plugins/lookup/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def run(self, terms, variables, **kwargs):
if "code" in response:
raise AnsibleError(
"Received error for %s - %s: %s"
% (response["url"], response["code"], response["msg"])
% (response.get("url", ""), response.get("code", ""), response.get("msg", ""))
)
ret.append(response.get("versions", {}).get("checkmk"))
return ret
14 changes: 5 additions & 9 deletions plugins/module_utils/lookup_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,19 @@ def get(self, endpoint="", parameters=None):
if parameters:
url = "%s?%s" % (url, urlencode(parameters))

response = ""

try:
raw_response = open_url(
url, headers=self.headers, validate_certs=self.validate_certs
)
response = to_text(raw_response.read())
return to_text(raw_response.read())
except HTTPError as e:
if e.code in HTTP_ERROR_CODES:
response = json.dumps(
return json.dumps(
{"code": e.code, "msg": HTTP_ERROR_CODES[e.code], "url": url}
)
else:
response = json.dumps({"code": e.code, "msg": e.reason, "url": url})
return json.dumps({"code": e.code, "msg": e.reason, "url": url})
except URLError as e:
response = json.dumps({"code": 0, "msg": str(e), "url": url})
return json.dumps({"code": 0, "msg": str(e), "url": url})
except Exception as e:
response = json.dumps({"code": 0, "msg": str(e), "url": url})

return response
return json.dumps({"code": 0, "msg": str(e), "url": url})

0 comments on commit b03b4ce

Please sign in to comment.