mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-03-21 20:59:10 +00:00
replace literal HTTP codes with http.HTTPStatus (#11561)
* replace literal HTTP codes with http.HTTPStatus * add changelog frag
This commit is contained in:
parent
1554f23bfb
commit
7436c0c9ba
18 changed files with 135 additions and 78 deletions
|
|
@ -93,6 +93,7 @@ compose:
|
|||
"""
|
||||
|
||||
import json
|
||||
from http import HTTPStatus
|
||||
from urllib.error import HTTPError
|
||||
|
||||
from ansible.errors import AnsibleParserError
|
||||
|
|
@ -164,7 +165,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
|||
self.display.vvv(f"Error returned: {error_body}")
|
||||
except Exception:
|
||||
error_body = {"status": None}
|
||||
if e.code == 404 and error_body.get("status") == "No objects found.":
|
||||
if e.code == HTTPStatus.NOT_FOUND and error_body.get("status") == "No objects found.":
|
||||
raise AnsibleParserError(
|
||||
"Host filter returned no data. Please confirm your host_filter value is valid"
|
||||
) from e
|
||||
|
|
@ -173,15 +174,15 @@ 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 200 <= response.status <= 299:
|
||||
if HTTPStatus(response.status).is_success:
|
||||
return json_data
|
||||
if response.status == 404 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']}")
|
||||
if response.status == 401:
|
||||
if response.status == HTTPStatus.UNAUTHORIZED:
|
||||
raise AnsibleParserError(
|
||||
f"API was unable to complete query -- Response: {response.status} - {json_data['status']}"
|
||||
)
|
||||
if response.status == 500:
|
||||
if response.status == HTTPStatus.INTERNAL_SERVER_ERROR:
|
||||
raise AnsibleParserError(f"API Response - {json_data['status']} - {json_data['errors']}")
|
||||
raise AnsibleParserError(f"Unexpected data returned - {json_data['status']} - {json_data['errors']}")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue