import { XMLParser } from "fast-xml-parser"
/**
* Commit
* Commit the candidate configuration on a firewall, or on Panorama itself (PAN-OS XML API, type=commit). Set admins for a partial commit of only those administrators' changes. Returns the commit job id to follow with Get job status, or a null job id with the device message when there is nothing to commit. On Panorama this does not push to firewalls: use Push to device group.
*/
export async function main(
auth: RT.PaloAltoPanos,
description: string | undefined,
admins: string[] | undefined
) {
let cmd = "<commit>"
if (description) cmd += `<description>${xmlEscape(description)}</description>`
if (admins?.length) {
cmd += `<partial><admin>${admins.map((a) => `<member>${xmlEscape(a)}</member>`).join("")}</admin></partial>`
}
cmd += "</commit>"
const res = await xmlApi(auth, { type: "commit", cmd })
return {
job_id: res.result?.job != null ? String(res.result.job) : null,
message:
res.result?.msg?.line ??
res.result?.msg ??
res.msg?.line ??
res.msg ??
null,
}
}
function xmlEscape(s: string) {
return s
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'")
}
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