0

Install policy package

by
Published today

Install a policy package (and the pending device settings) on its targets. pkg is the package path, including folders (folder/package). Leave scope empty to install on all of the package's installation targets. Returns the task id to follow with get_task_status.

Script fortimanager Verified

The script

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

3
/**
4
 * Install policy package
5
 * Install a policy package (and the pending device settings) on its targets. pkg is the package path, including folders (folder/package). Leave scope empty to install on all of the package's installation targets. Returns the task id to follow with get_task_status.
6
 */
7
export async function main(
8
  auth: RT.Fortimanager,
9
  adom: string,
10
  pkg: string,
11
  scope: { name: string; vdom: string }[] | undefined,
12
  flags:
13
    | (
14
        | "none"
15
        | "preview"
16
        | "generate_rev"
17
        | "copy_only"
18
        | "ifpolicy_only"
19
        | "no_ifpolicy"
20
        | "objs_only"
21
        | "auto_lock_ws"
22
        | "check_pkg_st"
23
      )[]
24
    | undefined,
25
  dev_rev_comments: string | undefined
26
) {
27
  const data: { [key: string]: any } = {
28
    adom,
29
    pkg,
30
    flags: flags?.length ? flags : ["none"],
31
  }
32
  if (scope?.length) {
33
    data.scope = scope
34
  }
35
  if (dev_rev_comments) {
36
    data.dev_rev_comments = dev_rev_comments
37
  }
38

39
  const response = await fetch(`${auth.base_url.replace(/\/+$/, "")}/jsonrpc`, {
40
    method: "POST",
41
    headers: {
42
      Authorization: `Bearer ${auth.token}`,
43
      "Content-Type": "application/json",
44
      Accept: "application/json",
45
    },
46
    body: JSON.stringify({
47
      id: 1,
48
      method: "exec",
49
      params: [{ url: "/securityconsole/install/package", data }],
50
    }),
51
  })
52

53
  if (!response.ok) {
54
    throw new Error(`${response.status} ${await response.text()}`)
55
  }
56

57
  const json = await response.json()
58
  const result = json.result?.[0]
59
  if (result?.status?.code !== 0) {
60
    throw new Error(
61
      `FortiManager error: ${JSON.stringify(json.result ?? json)}`
62
    )
63
  }
64

65
  return { task: result.data?.task, data: result.data }
66
}
67