Edits history of script submission #24092 for ' List snapshots (ip_fabric)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    const COLUMNS = [
      "id",
      "status",
      "finishStatus",
      "loadedSize",
      "unloadedSize",
      "name",
      "note",
      "sites",
      "fromArchive",
      "loading",
      "locked",
      "deviceAddedCount",
      "deviceRemovedCount",
      "interfaceActiveCount",
      "interfaceCount",
      "interfaceEdgeCount",
      "totalDevCount",
      "isLastSnapshot",
      "tsChange",
      "tsEnd",
      "tsStart",
      "userCount",
    ]
    
    /**
     * List snapshots
     * List one page of snapshots from the snapshot table (tables/management/snapshots), newest first by default. Filters use the table filter syntax, e.g. {"and": [{"locked": ["eq", true]}]}. Returns the rows, the table metadata and next_start (null on the last page).
     */
    export async function main(
      auth: RT.IpFabric,
      filters: { [key: string]: any } | undefined,
      sort_order: "asc" | "desc" | undefined,
      limit: number | undefined,
      start: number | undefined
    ) {
      const pageLimit = limit || 100
      const pageStart = start || 0
    
      const version = (auth.api_version ?? "").trim()
      // Same normalization as the official SDK: "7.2" / "v7.2" -> v7.2, "7" -> v7.0.
      const match = version.match(/^v?(\d+)(?:\.(\d+))?/)
      if (version && !match) {
        throw new Error(
          `api_version "${version}" is not valid, use v#.# (e.g. v7.2)`
        )
      }
      const prefix = match ? `api/v${match[1]}.${match[2] ?? "0"}/` : "api/"
      const url = new URL(
        `${prefix}tables/management/snapshots`,
        auth.base_url.replace(/\/*$/, "/")
      )
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "X-API-Token": auth.token,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({
          columns: COLUMNS,
          filters: filters ?? {},
          sort: { column: "tsEnd", order: sort_order || "desc" },
          pagination: { limit: pageLimit, start: pageStart },
        }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const result = (await response.json()) as {
        data: { [key: string]: any }[]
        _meta: { [key: string]: any }
      }
      return {
        ...result,
        next_start:
          result.data.length === pageLimit ? pageStart + result.data.length : null,
      }
    }
    

    Submitted by hugo989 7 hours ago