0

Get task status

by
Published today

Get a FortiManager task, such as the one returned by install_policy_package. The task is finished when percent reaches 100 and succeeded when num_err is 0; line holds the per-device results.

Script fortimanager Verified

The script

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

3
/**
4
 * Get task status
5
 * Get a FortiManager task, such as the one returned by install_policy_package. The task is finished when percent reaches 100 and succeeded when num_err is 0; line holds the per-device results.
6
 */
7
export async function main(auth: RT.Fortimanager, task_id: number) {
8
  const response = await fetch(`${auth.base_url.replace(/\/+$/, "")}/jsonrpc`, {
9
    method: "POST",
10
    headers: {
11
      Authorization: `Bearer ${auth.token}`,
12
      "Content-Type": "application/json",
13
      Accept: "application/json",
14
    },
15
    body: JSON.stringify({
16
      id: 1,
17
      method: "get",
18
      params: [{ url: `/task/task/${task_id}` }],
19
      verbose: 1,
20
    }),
21
  })
22

23
  if (!response.ok) {
24
    throw new Error(`${response.status} ${await response.text()}`)
25
  }
26

27
  const json = await response.json()
28
  const result = json.result?.[0]
29
  if (result?.status?.code !== 0) {
30
    throw new Error(
31
      `FortiManager error: ${JSON.stringify(json.result ?? json)}`
32
    )
33
  }
34

35
  return result.data
36
}
37