1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-03-21 20:59:10 +00:00

fix: remove HTTPStatus constructs introduced in Python 3.11 (#11573)

* fix: remove HTTPStatus constructs introduced in Python 3.11

* add changelog frag
This commit is contained in:
Alexei Znamensky 2026-03-13 08:46:55 +13:00 committed by GitHub
parent 4cd91ba4d4
commit f9e583dae2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 21 additions and 24 deletions

View file

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

View file

@ -174,7 +174,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
response_body = response.read() response_body = response.read()
json_data = json.loads(response_body.decode("utf-8")) json_data = json.loads(response_body.decode("utf-8"))
self.display.vvv(f"Returned Data: {json.dumps(json_data, indent=4, sort_keys=True)}") 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 return json_data
if response.status == HTTPStatus.NOT_FOUND and json_data["status"] == "No objects found.": 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']}") raise AnsibleParserError(f"API returned no data -- Response: {response.status} - {json_data['status']}")

View file

@ -42,8 +42,7 @@ class RequestError(Exception):
def handle_consul_response_error(response): def handle_consul_response_error(response):
status = HTTPStatus(response.status_code) if response.status_code >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors
if status.is_client_error or status.is_server_error:
raise RequestError(f"{response.status_code} {response.content}") raise RequestError(f"{response.status_code} {response.content}")
@ -323,8 +322,7 @@ class _ConsulModule:
) )
raise raise
_status = HTTPStatus(status) if status >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors
if _status.is_client_error or _status.is_server_error:
raise RequestError(status, response_data) raise RequestError(status, response_data)
if response_data: if response_data:

View file

@ -71,7 +71,7 @@ def session_method_wrapper(f):
raise HwcClientException(0, f"Parsing response to json failed, error: {ex}") from ex raise HwcClientException(0, f"Parsing response to json failed, error: {ex}") from ex
code = r.status_code code = r.status_code
if not HTTPStatus(code).is_success: if not (HTTPStatus.OK <= code < HTTPStatus.MULTIPLE_CHOICES): # not 2xx codes
msg = "" msg = ""
for i in ["message", "error.message"]: for i in ["message", "error.message"]:
try: try:

View file

@ -443,8 +443,7 @@ class RedfishUtils:
""" """
msg = http_client.responses.get(error.code, "") msg = http_client.responses.get(error.code, "")
data = None data = None
code = HTTPStatus(error.code) if error.code >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors
if code.is_client_error or code.is_server_error:
try: try:
body = error.read().decode("utf-8") body = error.read().decode("utf-8")
data = json.loads(body) data = json.loads(body)
@ -1893,17 +1892,15 @@ class RedfishUtils:
else: else:
# Error response body, which is a bit of a misnomer since it is used in successful action responses # Error response body, which is a bit of a misnomer since it is used in successful action responses
operation_results["status"] = "Completed" operation_results["status"] = "Completed"
_status = HTTPStatus(response.status) if response.status >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors
if _status.is_client_error or _status.is_server_error:
operation_results["status"] = "Exception" operation_results["status"] = "Exception"
operation_results["messages"] = data.get("error", {}).get("@Message.ExtendedInfo", []) operation_results["messages"] = data.get("error", {}).get("@Message.ExtendedInfo", [])
else: else:
# No response body (or malformed); build based on status code # No response body (or malformed); build based on status code
operation_results["status"] = "Completed" operation_results["status"] = "Completed"
_status = HTTPStatus(response.status) if response.status == HTTPStatus.ACCEPTED:
if _status == HTTPStatus.ACCEPTED:
operation_results["status"] = "New" 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" operation_results["status"] = "Exception"
# Clear out the handle if the operation is complete # Clear out the handle if the operation is complete

View file

@ -83,7 +83,7 @@ def api_request(
return None, info return None, info
elif _status == HTTPStatus.CONFLICT: elif _status == HTTPStatus.CONFLICT:
module.fail_json(msg="Job executions limit reached", execution_info=json.loads(info["body"])) 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"])) module.fail_json(msg="Rundeck API error", execution_info=json.loads(info["body"]))
try: try:

View file

@ -117,8 +117,7 @@ class UTM:
returns the info for an object in utm returns the info for an object in utm
""" """
info, result = self._lookup_entry(self.module, self.request_url) info, result = self._lookup_entry(self.module, self.request_url)
_status = HTTPStatus(info["status"]) if info["status"] >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors
if _status.is_client_error or _status.is_server_error:
self.module.fail_json(result=json.loads(info)) self.module.fail_json(result=json.loads(info))
else: else:
if result is None: if result is None:
@ -135,8 +134,7 @@ class UTM:
is_changed = False is_changed = False
info, result = self._lookup_entry(self.module, self.request_url) info, result = self._lookup_entry(self.module, self.request_url)
_status = HTTPStatus(info["status"]) if info["status"] >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors
if _status.is_client_error or _status.is_server_error:
self.module.fail_json(result=json.loads(info)) self.module.fail_json(result=json.loads(info))
else: else:
data_as_json_string = self.module.jsonify(self.module.params) data_as_json_string = self.module.jsonify(self.module.params)
@ -144,8 +142,7 @@ class UTM:
response, info = fetch_url( response, info = fetch_url(
self.module, self.request_url, method="POST", headers=combined_headers, data=data_as_json_string self.module, self.request_url, method="POST", headers=combined_headers, data=data_as_json_string
) )
_status = HTTPStatus(info["status"]) if info["status"] >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors
if _status.is_client_error or _status.is_server_error:
self.module.fail_json(msg=json.loads(info["body"])) self.module.fail_json(msg=json.loads(info["body"]))
is_changed = True is_changed = True
result = self._clean_result(json.loads(response.read())) result = self._clean_result(json.loads(response.read()))
@ -158,8 +155,7 @@ class UTM:
headers=combined_headers, headers=combined_headers,
data=data_as_json_string, data=data_as_json_string,
) )
_status = HTTPStatus(info["status"]) if info["status"] >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors
if _status.is_client_error or _status.is_server_error:
self.module.fail_json(msg=json.loads(info["body"])) self.module.fail_json(msg=json.loads(info["body"]))
is_changed = True is_changed = True
result = self._clean_result(json.loads(response.read())) result = self._clean_result(json.loads(response.read()))
@ -192,8 +188,7 @@ class UTM:
headers={"Accept": "application/json", "X-Restd-Err-Ack": "all"}, headers={"Accept": "application/json", "X-Restd-Err-Ack": "all"},
data=self.module.jsonify(self.module.params), data=self.module.jsonify(self.module.params),
) )
_status = HTTPStatus(info["status"]) if info["status"] >= HTTPStatus.BAD_REQUEST: # 4xx and 5xx errors
if _status.is_client_error or _status.is_server_error:
self.module.fail_json(msg=json.loads(info["body"])) self.module.fail_json(msg=json.loads(info["body"]))
else: else:
is_changed = True is_changed = True