1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 16:51:11 +03:00

fix error-handling bug for tower inventory source

This commit is contained in:
AlanCoding 2018-05-07 08:38:44 -04:00
parent 34a2128af8
commit b275d13da2
No known key found for this signature in database
GPG Key ID: FD2C3C012A72926B

View File

@ -90,6 +90,7 @@ 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,
@ -106,14 +107,16 @@ def read_tower_inventory(tower_host, tower_user, tower_pass, inventory, license_
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)
try:
json_response = response.json()
except (ValueError, TypeError) as e:
reason = "Failed to parse json from host: {}".format(e)
if response.ok: if response.ok:
return response.json() return json_response
json_reason = response.json() if not reason:
reason = json_reason.get('detail', 'Retrieving Tower Inventory Failed') 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) reason = "Connection to remote host failed: {}".format(e)
except json.JSONDecodeError as e:
reason = "Failed to parse json from host: {}".format(e)
raise RuntimeError(reason) raise RuntimeError(reason)