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:
parent
4cd91ba4d4
commit
f9e583dae2
7 changed files with 21 additions and 24 deletions
|
|
@ -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']}")
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue