0

List firewall policies

by
Published today

List IPv4 firewall policies in sequence order, one page at a time (start/count). filter takes FortiOS filter expressions such as action==accept (separate entries are ANDed; conditions separated by commas inside one entry are ORed). format limits the returned properties, e.g. policyid|name|action. next_start is null when this was the last page.

Script fortigate Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 hours ago
1
//native
2

3
/**
4
 * List firewall policies
5
 * List IPv4 firewall policies in sequence order, one page at a time (start/count). filter takes FortiOS filter expressions such as action==accept (separate entries are ANDed; conditions separated by commas inside one entry are ORed). format limits the returned properties, e.g. policyid|name|action. next_start is null when this was the last page.
6
 */
7
export async function main(
8
  auth: RT.Fortigate,
9
  vdom: string | undefined,
10
  filter: string[] | undefined,
11
  format: string | undefined,
12
  start: number | undefined,
13
  count: number | undefined
14
) {
15
  const first = start ?? 0
16
  const size = count || 100
17
  const url = new URL(
18
    `${auth.base_url.replace(/\/+$/, "")}/api/v2/cmdb/firewall/policy`
19
  )
20
  url.searchParams.append("start", String(first))
21
  url.searchParams.append("count", String(size))
22
  if (vdom != null && vdom !== "") {
23
    url.searchParams.append("vdom", vdom)
24
  }
25
  for (const f of filter ?? []) {
26
    if (f) url.searchParams.append("filter", f)
27
  }
28
  if (format != null && format !== "") {
29
    url.searchParams.append("format", format)
30
  }
31

32
  const response = await fetch(url, {
33
    headers: {
34
      Authorization: `Bearer ${auth.token}`,
35
      Accept: "application/json",
36
    },
37
  })
38

39
  if (!response.ok) {
40
    throw new Error(`${response.status} ${await response.text()}`)
41
  }
42

43
  const json = await response.json()
44
  const results: any[] = json.results ?? []
45
  return {
46
    ...json,
47
    next_start: results.length === size ? first + size : null,
48
  }
49
}
50