Skip to content

Commit

Permalink
Update awx.py - Better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nusantara-self authored Oct 29, 2024
1 parent 8aa2d87 commit c49ac70
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions responders/AWX/awx.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,42 @@ def run(self):
payload = {
'extra_vars': json.dumps(self.observable_all)
}
print("start awx job")
# Start the job
job_start_endpoint = self.url + '/api/v2/job_templates/' + self.workflow_id + '/launch/'

try:
response = requests.post(
job_start_endpoint,
headers=headers,
auth=(self.username, self.password),
data=json.dumps(payload),
verify=self.cert_path if self.cert_path else False
)

if self.cert_path == '':
response = requests.post(job_start_endpoint, headers=headers, auth=(self.username, self.password), data=json.dumps(payload))
else:
response = requests.post(job_start_endpoint, headers=headers, auth=(self.username, self.password), data=json.dumps(payload), verify=self.cert_path)
response.raise_for_status()

if response.status_code == 201:
self.report({"Message": "Executed AWX job successfully"})
else:
error_msg = response.json().get('detail', 'Unknown error')
self.error(f"AWX job execution returned unexpected status {response.status_code}: {error_msg}")
except requests.exceptions.SSLError as ssl_err:
self.error(f"SSL Error: {str(ssl_err)}")
except requests.exceptions.ConnectionError as conn_err:
self.error(f"Connection Error: {str(conn_err)}")
except requests.exceptions.Timeout as timeout_err:
self.error(f"Request Timeout: {str(timeout_err)}")
except requests.exceptions.RequestException as req_err:
try:
# Try to get additional details from the JSON response
error_details = response.json().get('detail', 'No additional error details available.')
except json.JSONDecodeError:
error_details = 'Failed to parse error details from response.'

self.error(f"Request Error: {str(req_err)} - Details: {error_details}")
except Exception as unexpected_err:
self.error(f"Unexpected Error: {str(unexpected_err)}")

# Check the response status
if response.status_code == 201:
print("success")
self.report({"Message": "Executed workflow"})
else:
print("error")
self.error(response.status_code)

if __name__ == '__main__':
AWX().run()

0 comments on commit c49ac70

Please sign in to comment.