1 | |
2 |
|
3 | |
4 | * Get job |
5 | * 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. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Awx, |
9 | job_id: number, |
10 | job_type: "job" | "workflow_job" | undefined |
11 | ) { |
12 | const collection = job_type === "workflow_job" ? "workflow_jobs" : "jobs" |
13 | const url = new URL(`${apiRoot(auth)}/${collection}/${job_id}/`) |
14 |
|
15 | const response = await fetch(url, { |
16 | headers: { |
17 | Authorization: authorization(auth), |
18 | Accept: "application/json", |
19 | }, |
20 | }) |
21 |
|
22 | if (!response.ok) { |
23 | throw new Error(`${response.status} ${await response.text()}`) |
24 | } |
25 |
|
26 | return await response.json() |
27 | } |
28 |
|
29 | function apiRoot(auth: RT.Awx) { |
30 | const path = (auth.api_path || "/api/v2").replace(/^\/+|\/+$/g, "") |
31 | |
32 | const base = auth.base_url |
33 | .replace(/\/+$/, "") |
34 | .replace(new RegExp(`/${path}$`), "") |
35 | return `${base}/${path}` |
36 | } |
37 |
|
38 | function authorization(auth: RT.Awx) { |
39 | if (auth.token) { |
40 | return `Bearer ${auth.token}` |
41 | } |
42 | if (auth.username) { |
43 | const bytes = new TextEncoder().encode( |
44 | `${auth.username}:${auth.password ?? ""}` |
45 | ) |
46 | return `Basic ${btoa(String.fromCharCode(...bytes))}` |
47 | } |
48 | throw new Error( |
49 | "The AWX resource needs either a token or a username and password" |
50 | ) |
51 | } |
52 |
|