//native
/**
* Get job
* Get a job or a workflow job: status (new, pending, waiting, running, successful, failed, error, canceled), failed, created, started, finished, elapsed and job_explanation. Set job_type to workflow_job for an id returned by Launch workflow job template.
*/
export async function main(
auth: RT.Awx,
job_id: number,
job_type: "job" | "workflow_job" | undefined
) {
const collection = job_type === "workflow_job" ? "workflow_jobs" : "jobs"
const url = new URL(`${apiRoot(auth)}/${collection}/${job_id}/`)
const response = await fetch(url, {
headers: {
Authorization: authorization(auth),
Accept: "application/json",
},
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
function apiRoot(auth: RT.Awx) {
const path = (auth.api_path || "/api/v2").replace(/^\/+|\/+$/g, "")
// Tolerate a base_url pasted with the API path already on it.
const base = auth.base_url
.replace(/\/+$/, "")
.replace(new RegExp(`/${path}$`), "")
return `${base}/${path}`
}
function authorization(auth: RT.Awx) {
if (auth.token) {
return `Bearer ${auth.token}`
}
if (auth.username) {
const bytes = new TextEncoder().encode(
`${auth.username}:${auth.password ?? ""}`
)
return `Basic ${btoa(String.fromCharCode(...bytes))}`
}
throw new Error(
"The AWX resource needs either a token or a username and password"
)
}
Submitted by hugo989 3 hours ago