Edits history of script submission #24112 for ' Get task status (fortimanager)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Get task status
     * 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.
     */
    export async function main(auth: RT.Fortimanager, task_id: number) {
      const response = await fetch(`${auth.base_url.replace(/\/+$/, "")}/jsonrpc`, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({
          id: 1,
          method: "get",
          params: [{ url: `/task/task/${task_id}` }],
          verbose: 1,
        }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const json = await response.json()
      const result = json.result?.[0]
      if (result?.status?.code !== 0) {
        throw new Error(
          `FortiManager error: ${JSON.stringify(json.result ?? json)}`
        )
      }
    
      return result.data
    }
    

    Submitted by hugo989 6 hours ago