//native
/**
* List address objects
* List the address objects of a firewall vsys, a Panorama device group or the shared location (PAN-OS REST API). PAN-OS returns every object of the location in one response; the API does not paginate.
*/
export async function main(
auth: RT.PaloAltoPanos,
location: "vsys" | "device-group" | "shared" | undefined,
vsys: string | undefined,
device_group: string | undefined
) {
const version = await restApiVersion(auth)
const url = new URL(`/restapi/${version}/Objects/Addresses`, auth.base_url)
setLocation(url, location, vsys, device_group)
const response = await fetch(url, {
headers: { "X-PAN-KEY": auth.api_key, Accept: "application/json" },
})
const text = await response.text()
if (!response.ok) {
throw new Error(`${response.status} ${text}`)
}
const data = JSON.parse(text)
if (data["@status"] !== "success") {
throw new Error(`${response.status} ${text}`)
}
return data
}
function setLocation(
url: URL,
location: string | undefined,
vsys: string | undefined,
device_group: string | undefined
) {
const loc = location || "vsys"
url.searchParams.set("location", loc)
if (loc === "vsys") {
url.searchParams.set("vsys", vsys || "vsys1")
} else if (loc === "device-group") {
if (!device_group) {
throw new Error("device_group is required when location is device-group")
}
url.searchParams.set("device-group", device_group)
}
}
async function restApiVersion(auth: RT.PaloAltoPanos) {
if (auth.rest_api_version) return auth.rest_api_version
const url = new URL("/api/", auth.base_url)
url.searchParams.set("type", "op")
url.searchParams.set("cmd", "<show><system><info></info></system></show>")
const response = await fetch(url, { headers: { "X-PAN-KEY": auth.api_key } })
const text = await response.text()
if (!response.ok || text.includes('status="error"')) {
throw new Error(`${response.status} ${text}`)
}
const match = text.match(/<sw-version>(\d+)\.(\d+)/)
if (!match) {
throw new Error(
`Could not read sw-version from show system info; set rest_api_version on the resource. ${text}`
)
}
return `v${match[1]}.${match[2]}`
}
Submitted by hugo989 7 hours ago