Edits history of script submission #24113 for ' Install policy package (fortimanager)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Install policy package
     * 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.
     */
    export async function main(
      auth: RT.Fortimanager,
      adom: string,
      pkg: string,
      scope: { name: string; vdom: string }[] | undefined,
      flags:
        | (
            | "none"
            | "preview"
            | "generate_rev"
            | "copy_only"
            | "ifpolicy_only"
            | "no_ifpolicy"
            | "objs_only"
            | "auto_lock_ws"
            | "check_pkg_st"
          )[]
        | undefined,
      dev_rev_comments: string | undefined
    ) {
      const data: { [key: string]: any } = {
        adom,
        pkg,
        flags: flags?.length ? flags : ["none"],
      }
      if (scope?.length) {
        data.scope = scope
      }
      if (dev_rev_comments) {
        data.dev_rev_comments = dev_rev_comments
      }
    
      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: "exec",
          params: [{ url: "/securityconsole/install/package", data }],
        }),
      })
    
      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 { task: result.data?.task, data: result.data }
    }
    

    Submitted by hugo989 6 hours ago