Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added errorhandling to lookup API #475

Merged
merged 7 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions plugins/lookup/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

import json

from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
from ansible_collections.checkmk.general.plugins.module_utils.lookup_api import (
CheckMKLookupAPI,
Expand Down Expand Up @@ -92,6 +93,11 @@ def run(self, terms, variables, **kwargs):
response = json.loads(
api.get("/objects/folder_config/" + term.replace("/", "~"))
)
if "code" in response:
raise AnsibleError(
"Received error for %s - %s: %s"
% (response["url"], response["code"], response["msg"])
Max-checkmk marked this conversation as resolved.
Show resolved Hide resolved
)
ret.append(response.get("extensions"))

return ret
7 changes: 6 additions & 1 deletion plugins/lookup/folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@

import json

from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
from ansible_collections.checkmk.general.plugins.module_utils.lookup_api import (
CheckMKLookupAPI,
Expand Down Expand Up @@ -134,7 +135,11 @@ def run(self, terms, variables, **kwargs):
api.get("/domain-types/folder_config/collections/all", parameters)
)

if "code" in response:
raise AnsibleError(
"Received error for %s - %s: %s"
% (response["url"], response["code"], response["msg"])
)
ret.append(response.get("value"))
# ret.append([{"name": "debug %s" % term, "extensions": { "attributes": { "tag_criticality": "prod" } } }])

return ret
7 changes: 7 additions & 0 deletions plugins/lookup/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@

import json

from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
from ansible_collections.checkmk.general.plugins.module_utils.lookup_api import (
CheckMKLookupAPI,
Expand Down Expand Up @@ -103,6 +104,12 @@ def run(self, terms, variables, **kwargs):
api_endpoint = "/objects/host_config/" + term

response = json.loads(api.get("/objects/host_config/" + term, parameters))

if "code" in response:
raise AnsibleError(
"Received error for %s - %s: %s"
% (response["url"], response["code"], response["msg"])
)
ret.append(response.get("extensions"))

return ret
6 changes: 6 additions & 0 deletions plugins/lookup/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

import json

from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
from ansible_collections.checkmk.general.plugins.module_utils.lookup_api import (
CheckMKLookupAPI,
Expand Down Expand Up @@ -99,6 +100,11 @@ def run(self, terms, variables, **kwargs):
response = json.loads(
api.get("/domain-types/host_config/collections/all", parameters)
)
if "code" in response:
raise AnsibleError(
"Received error for %s - %s: %s"
% (response["url"], response["code"], response["msg"])
)
ret.append(response.get("value"))

return ret
7 changes: 7 additions & 0 deletions plugins/lookup/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

import json

from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
from ansible_collections.checkmk.general.plugins.module_utils.lookup_api import (
CheckMKLookupAPI,
Expand All @@ -81,5 +82,11 @@ def run(self, terms, variables, **kwargs):
)

response = json.loads(api.get("/version"))

if "code" in response:
raise AnsibleError(
"Received error for %s - %s: %s"
% (response["url"], response["code"], response["msg"])
)
ret.append(response.get("versions", {}).get("checkmk"))
return ret
31 changes: 27 additions & 4 deletions plugins/module_utils/lookup_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@

__metaclass__ = type

import json

from ansible.module_utils.common.text.converters import to_text
from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError
from ansible.module_utils.six.moves.urllib.parse import urlencode
from ansible.module_utils.urls import open_url

HTTP_ERROR_CODES = {
400: "Bad Request: Parameter or validation failure.",
403: "Forbidden: Configuration via Setup is disabled.",
404: "Not Found: The requested object has not been found.",
406: "Not Acceptable: The requests accept headers can not be satisfied.",
}


class CheckMKLookupAPI:
"""Base class to contact a Checkmk server for ~Lookup calls"""
Expand All @@ -35,9 +45,22 @@ def get(self, endpoint="", parameters=None):
url = "%s?%s" % (url, urlencode(parameters))

response = ""
raw_response = open_url(
url, headers=self.headers, validate_certs=self.validate_certs
)
response = to_text(raw_response.read())

try:
raw_response = open_url(
url, headers=self.headers, validate_certs=self.validate_certs
)
response = to_text(raw_response.read())
Max-checkmk marked this conversation as resolved.
Show resolved Hide resolved
except HTTPError as e:
if e.code in HTTP_ERROR_CODES:
response = json.dumps(
Max-checkmk marked this conversation as resolved.
Show resolved Hide resolved
{"code": e.code, "msg": HTTP_ERROR_CODES[e.code], "url": url}
)
else:
response = json.dumps({"code": e.code, "msg": e.reason, "url": url})
except URLError as e:
response = 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