From 7415220cad97648b004e490ab66e42ac939c13fb Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Thu, 12 Mar 2026 20:59:26 +0100 Subject: [PATCH] [PR #11573/f9e583da backport][stable-12] fix: remove HTTPStatus constructs introduced in Python 3.11 (#11575) fix: remove HTTPStatus constructs introduced in Python 3.11 (#11573) * fix: remove HTTPStatus constructs introduced in Python 3.11 * add changelog frag (cherry picked from commit f9e583dae25f922b514da6ae1f082f60796dc77c) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- changelogs/fragments/11573-httpstatus-fix.yml | 7 +++++++ plugins/inventory/icinga2.py | 2 +- plugins/module_utils/consul.py | 6 ++---- plugins/module_utils/hwc_utils.py | 2 +- plugins/module_utils/redfish_utils.py | 11 ++++------- plugins/module_utils/rundeck.py | 2 +- plugins/module_utils/utm_utils.py | 15 +++++---------- 7 files changed, 21 insertions(+), 24 deletions(-) create mode 100644 changelogs/fragments/11573-httpstatus-fix.yml diff --git a/changelogs/fragments/11573-httpstatus-fix.yml b/changelogs/fragments/11573-httpstatus-fix.yml new file mode 100644 index 0000000000..63a6b90e2c --- /dev/null +++ b/changelogs/fragments/11573-httpstatus-fix.yml @@ -0,0 +1,7 @@ +bugfixes: + - icinga2 inventory plugin - remove Python 3.11 constructs added by mistake (https://github.com/ansible-collections/community.general/pull/11573). + - consul module utils - remove Python 3.11 constructs added by mistake (https://github.com/ansible-collections/community.general/pull/11573). + - hwc_utils module utils - remove Python 3.11 constructs added by mistake (https://github.com/ansible-collections/community.general/pull/11573). + - redfish_utils module utils - remove Python 3.11 constructs added by mistake (https://github.com/ansible-collections/community.general/pull/11573). + - rundeck module utils - remove Python 3.11 constructs added by mistake (https://github.com/ansible-collections/community.general/pull/11573). + - utm_utils module utils - remove Python 3.11 constructs added by mistake (https://github.com/ansible-collections/community.general/pull/11573). diff --git a/plugins/inventory/icinga2.py b/plugins/inventory/icinga2.py index 47fcca99a0..f179dee1a4 100644 --- a/plugins/inventory/icinga2.py +++ b/plugins/inventory/icinga2.py @@ -174,7 +174,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable): response_body = response.read() json_data = json.loads(response_body.decode("utf-8")) self.display.vvv(f"Returned Data: {json.dumps(json_data, indent=4, sort_keys=True)}") - if HTTPStatus(response.status).is_success: + if HTTPStatus.OK <= response.status < HTTPStatus.MULTIPLE_CHOICES: # 2xx codes return json_data if response.status == HTTPStatus.NOT_FOUND and json_data["status"] == "No objects found.": raise AnsibleParserError(f"API returned no data -- Response: {response.status} - {json_data['status']}") diff --git a/plugins/module_utils/consul.py b/plugins/module_utils/consul.py index 4547a1c91c..9afd912d15 100644 --- a/plugins/module_utils/consul.py +++ b/plugins/module_utils/consul.py @@ -42,8 +42,7 @@ class RequestError(Exception): def handle_consul_response_error(response): - status = HTTPStatus(response.status_code) - if status.is_client_error or status.is_server_error: + if response.status_code >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors raise RequestError(f"{response.status_code} {response.content}") @@ -323,8 +322,7 @@ class _ConsulModule: ) raise - _status = HTTPStatus(status) - if _status.is_client_error or _status.is_server_error: + if status >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors raise RequestError(status, response_data) if response_data: diff --git a/plugins/module_utils/hwc_utils.py b/plugins/module_utils/hwc_utils.py index 9401501cc3..78672941b4 100644 --- a/plugins/module_utils/hwc_utils.py +++ b/plugins/module_utils/hwc_utils.py @@ -71,7 +71,7 @@ def session_method_wrapper(f): raise HwcClientException(0, f"Parsing response to json failed, error: {ex}") from ex code = r.status_code - if not HTTPStatus(code).is_success: + if not (HTTPStatus.OK <= code < HTTPStatus.MULTIPLE_CHOICES): # not 2xx codes msg = "" for i in ["message", "error.message"]: try: diff --git a/plugins/module_utils/redfish_utils.py b/plugins/module_utils/redfish_utils.py index 714a0dc930..32033e0df7 100644 --- a/plugins/module_utils/redfish_utils.py +++ b/plugins/module_utils/redfish_utils.py @@ -443,8 +443,7 @@ class RedfishUtils: """ msg = http_client.responses.get(error.code, "") data = None - code = HTTPStatus(error.code) - if code.is_client_error or code.is_server_error: + if error.code >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors try: body = error.read().decode("utf-8") data = json.loads(body) @@ -1893,17 +1892,15 @@ class RedfishUtils: else: # Error response body, which is a bit of a misnomer since it is used in successful action responses operation_results["status"] = "Completed" - _status = HTTPStatus(response.status) - if _status.is_client_error or _status.is_server_error: + if response.status >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors operation_results["status"] = "Exception" operation_results["messages"] = data.get("error", {}).get("@Message.ExtendedInfo", []) else: # No response body (or malformed); build based on status code operation_results["status"] = "Completed" - _status = HTTPStatus(response.status) - if _status == HTTPStatus.ACCEPTED: + if response.status == HTTPStatus.ACCEPTED: operation_results["status"] = "New" - elif _status.is_client_error or _status.is_server_error: + elif response.status >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors operation_results["status"] = "Exception" # Clear out the handle if the operation is complete diff --git a/plugins/module_utils/rundeck.py b/plugins/module_utils/rundeck.py index 4156ccf8b1..f044a007d0 100644 --- a/plugins/module_utils/rundeck.py +++ b/plugins/module_utils/rundeck.py @@ -83,7 +83,7 @@ def api_request( return None, info elif _status == HTTPStatus.CONFLICT: module.fail_json(msg="Job executions limit reached", execution_info=json.loads(info["body"])) - elif _status.is_server_error: + elif _status >= HTTPStatus.INTERNAL_SERVER_ERROR: # 5xx errors module.fail_json(msg="Rundeck API error", execution_info=json.loads(info["body"])) try: diff --git a/plugins/module_utils/utm_utils.py b/plugins/module_utils/utm_utils.py index 71cfa8db8e..7cc9486799 100644 --- a/plugins/module_utils/utm_utils.py +++ b/plugins/module_utils/utm_utils.py @@ -117,8 +117,7 @@ class UTM: returns the info for an object in utm """ info, result = self._lookup_entry(self.module, self.request_url) - _status = HTTPStatus(info["status"]) - if _status.is_client_error or _status.is_server_error: + if info["status"] >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors self.module.fail_json(result=json.loads(info)) else: if result is None: @@ -135,8 +134,7 @@ class UTM: is_changed = False info, result = self._lookup_entry(self.module, self.request_url) - _status = HTTPStatus(info["status"]) - if _status.is_client_error or _status.is_server_error: + if info["status"] >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors self.module.fail_json(result=json.loads(info)) else: data_as_json_string = self.module.jsonify(self.module.params) @@ -144,8 +142,7 @@ class UTM: response, info = fetch_url( self.module, self.request_url, method="POST", headers=combined_headers, data=data_as_json_string ) - _status = HTTPStatus(info["status"]) - if _status.is_client_error or _status.is_server_error: + if info["status"] >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors self.module.fail_json(msg=json.loads(info["body"])) is_changed = True result = self._clean_result(json.loads(response.read())) @@ -158,8 +155,7 @@ class UTM: headers=combined_headers, data=data_as_json_string, ) - _status = HTTPStatus(info["status"]) - if _status.is_client_error or _status.is_server_error: + if info["status"] >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors self.module.fail_json(msg=json.loads(info["body"])) is_changed = True result = self._clean_result(json.loads(response.read())) @@ -192,8 +188,7 @@ class UTM: headers={"Accept": "application/json", "X-Restd-Err-Ack": "all"}, data=self.module.jsonify(self.module.params), ) - _status = HTTPStatus(info["status"]) - if _status.is_client_error or _status.is_server_error: + if info["status"] >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors self.module.fail_json(msg=json.loads(info["body"])) else: is_changed = True