1 | |
2 |
|
3 | |
4 | * List policy packages |
5 | * List the policy packages of an ADOM. Packages inside folders are flattened with their full path (folder/subfolder/package), which is the name install_policy_package expects; the raw tree is returned too. |
6 | */ |
7 | export async function main(auth: RT.Fortimanager, adom: string) { |
8 | |
9 | if (adom.includes("/")) throw new Error(`Invalid adom: ${adom}`) |
10 | const response = await fetch(`${auth.base_url.replace(/\/+$/, "")}/jsonrpc`, { |
11 | method: "POST", |
12 | headers: { |
13 | Authorization: `Bearer ${auth.token}`, |
14 | "Content-Type": "application/json", |
15 | Accept: "application/json", |
16 | }, |
17 | body: JSON.stringify({ |
18 | id: 1, |
19 | method: "get", |
20 | params: [{ url: `/pm/pkg/adom/${adom}` }], |
21 | verbose: 1, |
22 | }), |
23 | }) |
24 |
|
25 | if (!response.ok) { |
26 | throw new Error(`${response.status} ${await response.text()}`) |
27 | } |
28 |
|
29 | const json = await response.json() |
30 | const result = json.result?.[0] |
31 | if (result?.status?.code !== 0) { |
32 | throw new Error( |
33 | `FortiManager error: ${JSON.stringify(json.result ?? json)}` |
34 | ) |
35 | } |
36 |
|
37 | |
38 | const packages: { |
39 | path: string |
40 | name: string |
41 | oid: number |
42 | scope_member: any[] |
43 | }[] = [] |
44 | const walk = (nodes: any[], prefix: string) => { |
45 | for (const node of nodes ?? []) { |
46 | const path = prefix ? `${prefix}/${node.name}` : node.name |
47 | if (node.type === "folder") { |
48 | walk(node.subobj, path) |
49 | } else { |
50 | packages.push({ |
51 | path, |
52 | name: node.name, |
53 | oid: node.oid, |
54 | scope_member: node["scope member"] ?? [], |
55 | }) |
56 | } |
57 | } |
58 | } |
59 | walk(result.data ?? [], "") |
60 |
|
61 | return { packages, tree: result.data ?? [] } |
62 | } |
63 |
|