Edits history of script submission #24109 for ' List firewall addresses (fortigate)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List firewall addresses
     * List IPv4 firewall address objects, one page at a time (start/count). filter takes FortiOS filter expressions such as name=@web (separate entries are ANDed; conditions separated by commas inside one entry are ORed). format limits the returned properties, e.g. name|subnet|comment. next_start is null when this was the last page.
     */
    export async function main(
      auth: RT.Fortigate,
      vdom: string | undefined,
      filter: string[] | undefined,
      format: string | undefined,
      start: number | undefined,
      count: number | undefined
    ) {
      const first = start ?? 0
      const size = count || 100
      const url = new URL(
        `${auth.base_url.replace(/\/+$/, "")}/api/v2/cmdb/firewall/address`
      )
      url.searchParams.append("start", String(first))
      url.searchParams.append("count", String(size))
      if (vdom != null && vdom !== "") {
        url.searchParams.append("vdom", vdom)
      }
      for (const f of filter ?? []) {
        if (f) url.searchParams.append("filter", f)
      }
      if (format != null && format !== "") {
        url.searchParams.append("format", format)
      }
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const json = await response.json()
      const results: any[] = json.results ?? []
      return {
        ...json,
        next_start: results.length === size ? first + size : null,
      }
    }
    

    Submitted by hugo989 6 hours ago