From 3099f1bf27b5e280ddc38713ded5b64e799b84f9 Mon Sep 17 00:00:00 2001 From: "max.sickora" Date: Fri, 24 May 2024 16:34:07 +0200 Subject: [PATCH 1/4] Initial commit for the cleanup --- plugins/module_utils/api.py | 2 +- plugins/module_utils/utils.py | 20 +++++-- plugins/modules/bakery.py | 24 -------- plugins/modules/discovery.py | 16 ----- plugins/modules/password.py | 80 ------------------------- plugins/modules/tag_group.py | 33 ----------- plugins/modules/timeperiod.py | 106 ---------------------------------- 7 files changed, 15 insertions(+), 266 deletions(-) diff --git a/plugins/module_utils/api.py b/plugins/module_utils/api.py index a16f3c69d..bdb01927b 100644 --- a/plugins/module_utils/api.py +++ b/plugins/module_utils/api.py @@ -44,7 +44,7 @@ def __init__(self, module): # may be "present", "abesent" or an individual one self.state = "" - def _fetch(self, code_mapping, endpoint="", data=None, method="GET"): + def _fetch(self, code_mapping="", endpoint="", data=None, method="GET"): http_mapping = GENERIC_HTTP_CODES.copy() http_mapping.update(code_mapping) diff --git a/plugins/module_utils/utils.py b/plugins/module_utils/utils.py index bbdf05a88..5412e3df8 100644 --- a/plugins/module_utils/utils.py +++ b/plugins/module_utils/utils.py @@ -20,12 +20,20 @@ def result_as_dict(result): GENERIC_HTTP_CODES = { - 204: (True, False, "Successfully executed"), + 200: (True, False, "OK: The operation was done successfully"), + 204: (True, False, "Operation done successfully. No further output."), 400: (False, True, "Bad request: Parameter or validation failure"), + 401: (False, True, "The user is not authorized to do this request"), 403: (False, True, "Forbidden: Configuration via Setup is disabled"), - 404: (False, True, "Not found"), - 406: (False, True, "Required headers are not satisfied"), - 412: (False, True, "If-Match does not match ETag"), - 415: (False, True, "Wrong content-type in header"), - 428: (False, True, "If-Match header is missing"), + 404: (False, True, "Not Found: The requested object has not been found"), + 405: ( + False, + True, + "This request is only allowed with other HTTP methods", + ), + 406: (False, True, "The requests accept headers can not be satisfied"), + 412: (False, True, "If-Match header doesn't match the object's ETag"), + 415: (False, True, "The submitted content-type is not supported"), + 428: (False, True, "The required If-Match header is missing"), + 500: (False, True, "General Server Error"), } diff --git a/plugins/modules/bakery.py b/plugins/modules/bakery.py index 07badc2c6..bfa613ad6 100644 --- a/plugins/modules/bakery.py +++ b/plugins/modules/bakery.py @@ -95,29 +95,6 @@ result_as_dict, ) -HTTP_CODES = { - # http_code: (changed, failed, "Message") - 200: (True, False, "The operation was done successfully."), - 204: ( - True, - False, - "No Content: Operation done successfully. No further output.", - ), - 400: (False, True, "Bad Request: Parameter or validation failure."), - 403: (False, True, "Forbidden: Configuration via WATO is disabled."), - 406: ( - False, - True, - "Not Acceptable: The requests accept headers can not be satisfied.", - ), - 415: ( - False, - True, - "Unsupported Media Type: The submitted content-type is not supported.", - ), - 500: (False, True, "General Server Error."), -} - class BakeryAPI(CheckmkAPI): def post(self): @@ -137,7 +114,6 @@ def post(self): action = "bake_and_sign" return self._fetch( - code_mapping=HTTP_CODES, endpoint="/domain-types/agent/actions/%s/invoke" % action, data=data, method="POST", diff --git a/plugins/modules/discovery.py b/plugins/modules/discovery.py index 3dbbe729c..ed32700fb 100644 --- a/plugins/modules/discovery.py +++ b/plugins/modules/discovery.py @@ -130,13 +130,8 @@ False, "The service discovery background job has been initialized. Redirecting to the 'Wait for service discovery completion' endpoint.", ), - 400: (False, True, "Bad Request."), - 403: (False, True, "Forbidden: Configuration via WATO is disabled."), 404: (False, True, "Not Found: Host could not be found."), - 406: (False, True, "Not Acceptable."), 409: (False, False, "Conflict: A discovery background job is already running"), - 415: (False, True, "Unsupported Media Type."), - 500: (False, True, "General Server Error."), } HTTP_CODES_SC = { @@ -147,30 +142,19 @@ False, "The service discovery is still running. Redirecting to the 'Wait for completion' endpoint.", ), - 403: (False, True, "Forbidden: Configuration via Setup is disabled."), 404: (False, False, "Not Found: There is no running service discovery"), - 406: (False, True, "Not Acceptable."), - 500: (False, True, "General Server Error."), } HTTP_CODES_BULK = { # http_code: (changed, failed, "Message") 200: (True, False, "Discovery successful."), - 400: (False, True, "Bad Request."), - 403: (False, True, "Forbidden: Configuration via WATO is disabled."), - 406: (False, True, "Not Acceptable."), 409: (False, False, "Conflict: A bulk discovery job is already active"), - 415: (False, True, "Unsupported Media Type."), - 500: (False, True, "General Server Error."), } HTTP_CODES_BULK_SC = { # http_code: (changed, failed, "Message") 200: (True, False, "The service discovery has been completed."), - 403: (False, True, "Forbidden: Configuration via WATO is disabled."), 404: (False, False, "Not Found: There is no running bulk_discovery job"), - 406: (False, True, "Not Acceptable."), - 500: (False, True, "General Server Error."), } diff --git a/plugins/modules/password.py b/plugins/modules/password.py index 8f7f771da..bc3bdefc5 100644 --- a/plugins/modules/password.py +++ b/plugins/modules/password.py @@ -132,90 +132,12 @@ # We count 404 not as failed, because we want to know if the password exists or not. HTTP_CODES_GET = { # http_code: (changed, failed, "Message") - 200: (True, False, "OK: The operation was done successfully."), - 400: (False, True, "Bad Request: Parameter or validation failure."), - 403: (False, True, "Forbidden: Configuration via Setup is disabled."), 404: (False, False, "Not Found: The requested object has not been found."), - 406: ( - False, - True, - "Not Acceptable: The requests accept headers can not be satisfied.", - ), - 415: ( - False, - True, - "Unsupported Media Type: The submitted content-type is not supported.", - ), - 500: (False, True, "General Server Error."), } HTTP_CODES_DELETE = { # http_code: (changed, failed, "Message") - 200: (True, False, "OK: The operation was done successfully."), - 400: (False, True, "Bad Request: Parameter or validation failure."), - 403: (False, True, "Forbidden: Configuration via Setup is disabled."), 404: (False, False, "Not Found: The requested object has not been found."), - 406: ( - False, - True, - "Not Acceptable: The requests accept headers can not be satisfied.", - ), - 415: ( - False, - True, - "Unsupported Media Type: The submitted content-type is not supported.", - ), - 500: (False, True, "General Server Error."), -} - -HTTP_CODES_CREATE = { - # http_code: (changed, failed, "Message") - 200: (True, False, "OK: The operation was done successfully."), - 400: (False, True, "Bad Request: Parameter or validation failure."), - 403: (False, True, "Forbidden: Configuration via Setup is disabled."), - 406: ( - False, - True, - "Not Acceptable: The requests accept headers can not be satisfied.", - ), - 415: ( - False, - True, - "Unsupported Media Type: The submitted content-type is not supported.", - ), - 500: (False, True, "General Server Error."), -} - -HTTP_CODES_UPDATE = { - # http_code: (changed, failed, "Message") - 200: ( - True, - False, - "No Content: Operation was done successfully. No further output", - ), - 403: (False, True, "Forbidden: Configuration via Setup is disabled."), - 404: (False, True, "Not Found: The requested object has not been found."), - 406: ( - False, - True, - "Not Acceptable: The requests accept headers can not be satisfied.", - ), - 412: ( - False, - True, - "Precondition Failed: The value of the If-Match header doesn't match the object's ETag.", - ), - 415: ( - False, - True, - "Unsupported Media Type: The submitted content-type is not supported.", - ), - 428: ( - False, - True, - "Precondition Required: The required If-Match header is missing.", - ), - 500: (False, True, "General Server Error."), } @@ -236,7 +158,6 @@ def post(self): data = {key: val for key, val in data.items() if val} return self._fetch( - code_mapping=HTTP_CODES_CREATE, endpoint="/domain-types/password/collections/all", data=data, method="POST", @@ -259,7 +180,6 @@ def put(self): data = {key: val for key, val in data.items() if val} return self._fetch( - code_mapping=HTTP_CODES_UPDATE, endpoint="/objects/password/%s" % self.params.get("name"), data=data, method="PUT", diff --git a/plugins/modules/tag_group.py b/plugins/modules/tag_group.py index 054439fa1..d131cbcd8 100644 --- a/plugins/modules/tag_group.py +++ b/plugins/modules/tag_group.py @@ -138,37 +138,7 @@ # We count 404 not as failed, because we want to know if the taggroup exists or not. HTTP_CODES_GET = { # http_code: (changed, failed, "Message") - 200: (True, False, "OK: The operation was done successfully."), 404: (False, False, "Not Found: The requested object has not been found."), - 500: (False, True, "General Server Error."), -} - -HTTP_CODES_DELETE = { - # http_code: (changed, failed, "Message") - 405: ( - False, - True, - "Method Not Allowed: This request is only allowed with other HTTP methods", - ), - 500: (False, True, "General Server Error."), -} - -HTTP_CODES_CREATE = { - # http_code: (changed, failed, "Message") - 200: (True, False, "OK: The operation was done successfully."), - 500: (False, True, "General Server Error."), -} - -HTTP_CODES_UPDATE = { - # http_code: (changed, failed, "Message") - 200: (True, False, "OK: The operation was done successfully."), - 401: (False, True, "Unauthorized: The user is not authorized to do this request"), - 405: ( - False, - True, - "Method Not Allowed: This request is only allowed with other HTTP methods", - ), - 500: (False, True, "General Server Error."), } @@ -211,7 +181,6 @@ def post(self): data["ident"] = self.params.get("name") return self._fetch( - code_mapping=HTTP_CODES_CREATE, endpoint="/domain-types/host_tag_group/collections/all", data=data, method="POST", @@ -223,7 +192,6 @@ def put(self): data = normalize_data(self.params) return self._fetch( - code_mapping=HTTP_CODES_UPDATE, endpoint="/objects/host_tag_group/%s" % self.params.get("name"), data=data, method="PUT", @@ -235,7 +203,6 @@ def delete(self): data = {} return self._fetch( - code_mapping=HTTP_CODES_DELETE, endpoint="/objects/host_tag_group/%s?repair=%s" % (self.params.get("name"), self.params.get("repair")), # data=data, diff --git a/plugins/modules/timeperiod.py b/plugins/modules/timeperiod.py index a6f8ca9cf..6e831ab33 100644 --- a/plugins/modules/timeperiod.py +++ b/plugins/modules/timeperiod.py @@ -148,110 +148,7 @@ # We count 404 not as failed, because we want to know if the time period exists or not. HTTP_CODES_GET = { # http_code: (changed, failed, "Message") - 200: (True, False, "OK: The operation was done successfully."), - 400: (False, True, "Bad Request: Parameter or validation failure."), - 403: (False, True, "Forbidden: Configuration via Setup is disabled."), 404: (False, False, "Not Found: The requested object has not been found."), - 406: ( - False, - True, - "Not Acceptable: The requests accept headers can not be satisfied.", - ), - 415: ( - False, - True, - "Unsupported Media Type: The submitted content-type is not supported.", - ), - 500: (False, True, "General Server Error."), -} - -HTTP_CODES_DELETE = { - # http_code: (changed, failed, "Message") - 204: (True, False, "No Content: Operation done successfully. No further output."), - 400: (False, True, "Bad Request: Parameter or validation failure."), - 403: (False, True, "Forbidden: Configuration via Setup is disabled."), - 404: (False, True, "Not Found: The requested object has not been found."), - 405: ( - False, - True, - "Method Not Allowed: This request is only allowed with other HTTP methods", - ), - 406: ( - False, - True, - "Not Acceptable: The requests accept headers can not be satisfied.", - ), - 412: ( - False, - True, - "Precondition Failed: The value of the If-Match header doesn't match the object's ETag.", - ), - 415: ( - False, - True, - "Unsupported Media Type: The submitted content-type is not supported.", - ), - 428: ( - False, - True, - "Precondition Required: The required If-Match header is missing", - ), - 500: (False, True, "General Server Error."), -} - -HTTP_CODES_CREATE = { - # http_code: (changed, failed, "Message") - 200: (True, False, "OK: The operation was done successfully."), - 400: (False, True, "Bad Request: Parameter or validation failure."), - 403: (False, True, "Forbidden: Configuration via Setup is disabled."), - 406: ( - False, - True, - "Not Acceptable: The requests accept headers can not be satisfied.", - ), - 415: ( - False, - True, - "Unsupported Media Type: The submitted content-type is not supported.", - ), - 500: (False, True, "General Server Error."), -} - -HTTP_CODES_UPDATE = { - # http_code: (changed, failed, "Message") - 200: ( - True, - False, - "No Content: Operation was done successfully. No further output", - ), - 403: (False, True, "Forbidden: Configuration via Setup is disabled."), - 404: (False, True, "Not Found: The requested object has not been found."), - 405: ( - False, - True, - "Method Not Allowed: This request is only allowed with other HTTP methods", - ), - 406: ( - False, - True, - "Not Acceptable: The requests accept headers can not be satisfied.", - ), - 412: ( - False, - True, - "Precondition Failed: The value of the If-Match header doesn't match the object's ETag.", - ), - 415: ( - False, - True, - "Unsupported Media Type: The submitted content-type is not supported.", - ), - 428: ( - False, - True, - "Precondition Required: The required If-Match header is missing", - ), - 500: (False, True, "General Server Error."), } updatevalues = ("alias", "active_time_ranges", "exceptions", "exclude") @@ -272,7 +169,6 @@ def post(self): data["exclude"] = self.params.get("exclude") return self._fetch( - code_mapping=HTTP_CODES_CREATE, endpoint="/domain-types/time_period/collections/all", data=data, method="POST", @@ -299,7 +195,6 @@ def put(self, existingalias): data["exclude"] = self.params.get("exclude") return self._fetch( - code_mapping=HTTP_CODES_UPDATE, endpoint="/objects/time_period/%s" % self.params.get("name"), data=data, method="PUT", @@ -311,7 +206,6 @@ def delete(self): data = {} return self._fetch( - code_mapping=HTTP_CODES_DELETE, endpoint="/objects/time_period/%s" % self.params.get("name"), data=data, method="DELETE", From 5b155190d82969c2b7727ac969e2f5ef4cb0a53e Mon Sep 17 00:00:00 2001 From: Max-checkmk <127388954+Max-checkmk@users.noreply.github.com> Date: Fri, 31 May 2024 10:27:11 +0200 Subject: [PATCH 2/4] Small change --- plugins/module_utils/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/module_utils/api.py b/plugins/module_utils/api.py index bdb01927b..00c60f5c9 100644 --- a/plugins/module_utils/api.py +++ b/plugins/module_utils/api.py @@ -44,7 +44,7 @@ def __init__(self, module): # may be "present", "abesent" or an individual one self.state = "" - def _fetch(self, code_mapping="", endpoint="", data=None, method="GET"): + def _fetch(self, code_mapping={}, endpoint="", data=None, method="GET"): http_mapping = GENERIC_HTTP_CODES.copy() http_mapping.update(code_mapping) From 9ab0c92b3e82637dff9048c7e540998f22337d2c Mon Sep 17 00:00:00 2001 From: Max-checkmk <127388954+Max-checkmk@users.noreply.github.com> Date: Fri, 31 May 2024 10:50:52 +0200 Subject: [PATCH 3/4] Optimized small change --- plugins/module_utils/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/module_utils/api.py b/plugins/module_utils/api.py index 00c60f5c9..3d16b3995 100644 --- a/plugins/module_utils/api.py +++ b/plugins/module_utils/api.py @@ -44,7 +44,7 @@ def __init__(self, module): # may be "present", "abesent" or an individual one self.state = "" - def _fetch(self, code_mapping={}, endpoint="", data=None, method="GET"): + def _fetch(self, code_mapping=None, endpoint="", data=None, method="GET"): http_mapping = GENERIC_HTTP_CODES.copy() http_mapping.update(code_mapping) From 5ad7d2b1d36f70fc4d2ab9c558d34147be7e70cf Mon Sep 17 00:00:00 2001 From: Max-checkmk <127388954+Max-checkmk@users.noreply.github.com> Date: Fri, 31 May 2024 11:39:15 +0200 Subject: [PATCH 4/4] Revert small change --- plugins/module_utils/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/module_utils/api.py b/plugins/module_utils/api.py index 3d16b3995..bdb01927b 100644 --- a/plugins/module_utils/api.py +++ b/plugins/module_utils/api.py @@ -44,7 +44,7 @@ def __init__(self, module): # may be "present", "abesent" or an individual one self.state = "" - def _fetch(self, code_mapping=None, endpoint="", data=None, method="GET"): + def _fetch(self, code_mapping="", endpoint="", data=None, method="GET"): http_mapping = GENERIC_HTTP_CODES.copy() http_mapping.update(code_mapping)