Edits history of script submission #24025 for ' Get job status (palo_alto_panos)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { XMLParser } from "fast-xml-parser"
    
    /**
     * Get job status
     * 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.
     */
    export async function main(auth: RT.PaloAltoPanos, job_id: string) {
      if (!/^\d+$/.test(String(job_id))) {
        throw new Error(`job_id must be numeric, got ${job_id}`)
      }
      const res = await xmlApi(auth, {
        type: "op",
        cmd: `<show><jobs><id>${job_id}</id></jobs></show>`,
      })
      return res.result?.job ?? res.result
    }
    
    async function xmlApi(auth: RT.PaloAltoPanos, params: { [key: string]: string }) {
      const url = new URL("/api/", auth.base_url)
      for (const [key, value] of Object.entries(params)) {
        url.searchParams.set(key, value)
      }
      const response = await fetch(url, {
        method: "POST",
        headers: { "X-PAN-KEY": auth.api_key },
      })
      const text = await response.text()
      if (!response.ok) {
        throw new Error(`${response.status} ${text}`)
      }
      const parsed = new XMLParser({
        ignoreAttributes: false,
        attributeNamePrefix: "@",
        parseTagValue: false,
      }).parse(text)
      if (parsed.response?.["@status"] !== "success") {
        throw new Error(`${response.status} ${text}`)
      }
      return parsed.response
    }
    

    Submitted by hugo989 7 hours ago