mirror of
https://github.com/ansible/awx.git
synced 2026-05-20 07:17:40 -02:30
clean up Tower inventory error handling
related: https://github.com/ansible/tower/pull/1697 related: https://github.com/ansible/tower-qa/pull/1746
This commit is contained in:
@@ -90,7 +90,6 @@ def read_tower_inventory(tower_host, tower_user, tower_pass, inventory, license_
|
|||||||
tower_host = "https://{}".format(tower_host)
|
tower_host = "https://{}".format(tower_host)
|
||||||
inventory_url = urljoin(tower_host, "/api/v2/inventories/{}/script/?hostvars=1&towervars=1&all=1".format(inventory.replace('/', '')))
|
inventory_url = urljoin(tower_host, "/api/v2/inventories/{}/script/?hostvars=1&towervars=1&all=1".format(inventory.replace('/', '')))
|
||||||
config_url = urljoin(tower_host, "/api/v2/config/")
|
config_url = urljoin(tower_host, "/api/v2/config/")
|
||||||
reason = None
|
|
||||||
try:
|
try:
|
||||||
if license_type != "open":
|
if license_type != "open":
|
||||||
config_response = requests.get(config_url,
|
config_response = requests.get(config_url,
|
||||||
@@ -102,22 +101,22 @@ def read_tower_inventory(tower_host, tower_user, tower_pass, inventory, license_
|
|||||||
raise RuntimeError("Tower server licenses must match: source: {} local: {}".format(source_type,
|
raise RuntimeError("Tower server licenses must match: source: {} local: {}".format(source_type,
|
||||||
license_type))
|
license_type))
|
||||||
else:
|
else:
|
||||||
raise RuntimeError("Failed to validate the license of the remote Tower: {}".format(config_response.data))
|
raise RuntimeError("Failed to validate the license of the remote Tower: {}".format(config_response))
|
||||||
|
|
||||||
response = requests.get(inventory_url,
|
response = requests.get(inventory_url,
|
||||||
auth=HTTPBasicAuth(tower_user, tower_pass),
|
auth=HTTPBasicAuth(tower_user, tower_pass),
|
||||||
verify=not ignore_ssl)
|
verify=not ignore_ssl)
|
||||||
|
if not response.ok:
|
||||||
|
# If the GET /api/v2/inventories/N/script is not HTTP 200, print the error code
|
||||||
|
raise RuntimeError("Connection to remote host failed: {}".format(response))
|
||||||
try:
|
try:
|
||||||
json_response = response.json()
|
# Attempt to parse JSON
|
||||||
|
return response.json()
|
||||||
except (ValueError, TypeError) as e:
|
except (ValueError, TypeError) as e:
|
||||||
reason = "Failed to parse json from host: {}".format(e)
|
# If the JSON parse fails, print the ValueError
|
||||||
if response.ok:
|
raise RuntimeError("Failed to parse json from host: {}".format(e)
|
||||||
return json_response
|
|
||||||
if not reason:
|
|
||||||
reason = json_response.get('detail', 'Retrieving Tower Inventory Failed')
|
|
||||||
except requests.ConnectionError as e:
|
except requests.ConnectionError as e:
|
||||||
reason = "Connection to remote host failed: {}".format(e)
|
raise RuntimeError("Connection to remote host failed: {}".format(e))
|
||||||
raise RuntimeError(reason)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
Reference in New Issue
Block a user