Edits history of script submission #24122 for ' Update named list items (infoblox_csp)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Update named list items
     * Add items to and/or remove items from a Threat Defense named list without replacing the whole list. Items are FQDNs or IPv4/IPv6 addresses/CIDRs; additions can carry a description via add_described. Removals run after additions.
     */
    export async function main(
      auth: RT.InfobloxCsp,
      id: number,
      add: string[] | undefined,
      add_described: { item: string; description?: string }[] | undefined,
      remove: string[] | undefined
    ) {
      if (!add?.length && !add_described?.length && !remove?.length) {
        throw new Error("Provide at least one item in add, add_described or remove")
      }
    
      const url = new URL(
        `${(auth.base_url || "https://csp.infoblox.com").replace(/\/+$/, "")}/api/atcfw/v1/named_lists/${id}/items`
      )
      const headers = {
        Authorization: `Token ${auth.api_key}`,
        "Content-Type": "application/json",
        Accept: "application/json",
      }
    
      let added: unknown = null
      if (add?.length || add_described?.length) {
        const response = await fetch(url, {
          method: "POST",
          headers,
          body: JSON.stringify({
            items: add?.length ? add : undefined,
            items_described: add_described?.length ? add_described : undefined,
          }),
        })
        if (!response.ok) {
          throw new Error(`${response.status} ${await response.text()}`)
        }
        const text = await response.text()
        added = text ? JSON.parse(text) : { success: true }
      }
    
      let removed: unknown = null
      if (remove?.length) {
        const response = await fetch(url, {
          method: "DELETE",
          headers,
          body: JSON.stringify({ items: remove }),
        })
        if (!response.ok) {
          throw new Error(`${response.status} ${await response.text()}`)
        }
        const text = await response.text()
        removed = text ? JSON.parse(text) : { success: true }
      }
    
      return { added, removed }
    }
    

    Submitted by hugo989 6 hours ago