0

Get job status

by
Published today

Get the status of a commit or push job on a firewall or Panorama (PAN-OS XML API, show jobs id). status is PEND or ACT until the job is done, then FIN; result is OK on success (anything else is a failure); a push job lists each firewall under devices.

Script palo_alto_panos Verified

The script

Submitted by hugo989 Bun
Verified 7 hours ago
1
import { XMLParser } from "fast-xml-parser"
2

3
/**
4
 * Get job status
5
 * Get the status of a commit or push job on a firewall or Panorama (PAN-OS XML API, show jobs id). status is PEND or ACT until the job is done, then FIN; result is OK on success (anything else is a failure); a push job lists each firewall under devices.
6
 */
7
export async function main(auth: RT.PaloAltoPanos, job_id: string) {
8
  if (!/^\d+$/.test(String(job_id))) {
9
    throw new Error(`job_id must be numeric, got ${job_id}`)
10
  }
11
  const res = await xmlApi(auth, {
12
    type: "op",
13
    cmd: `<show><jobs><id>${job_id}</id></jobs></show>`,
14
  })
15
  return res.result?.job ?? res.result
16
}
17

18
async function xmlApi(auth: RT.PaloAltoPanos, params: { [key: string]: string }) {
19
  const url = new URL("/api/", auth.base_url)
20
  for (const [key, value] of Object.entries(params)) {
21
    url.searchParams.set(key, value)
22
  }
23
  const response = await fetch(url, {
24
    method: "POST",
25
    headers: { "X-PAN-KEY": auth.api_key },
26
  })
27
  const text = await response.text()
28
  if (!response.ok) {
29
    throw new Error(`${response.status} ${text}`)
30
  }
31
  const parsed = new XMLParser({
32
    ignoreAttributes: false,
33
    attributeNamePrefix: "@",
34
    parseTagValue: false,
35
  }).parse(text)
36
  if (parsed.response?.["@status"] !== "success") {
37
    throw new Error(`${response.status} ${text}`)
38
  }
39
  return parsed.response
40
}
41