Edits history of script submission #11022 for ' Update Firewall Rules (Trusted Sources) for a Database (digitalocean)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Digitalocean = {
      token: string;
    };
    /**
     * Update Firewall Rules (Trusted Sources) for a Database
     * To update a database cluster's firewall rules (known as "trusted sources" in the control panel), send a PUT request to `/v2/databases/$DATABASE_ID/firewall` specifying which resources should be able to open connections to the database.
     */
    export async function main(
      auth: Digitalocean,
      database_cluster_uuid: string,
      body: {
        rules?: {
          uuid?: string;
          cluster_uuid?: string;
          type: "droplet" | "k8s" | "ip_addr" | "tag" | "app";
          value: string;
          created_at?: string;
        }[];
      },
    ) {
      const url = new URL(
        `https://api.digitalocean.com/v2/databases/${database_cluster_uuid}/firewall`,
      );
    
      const response = await fetch(url, {
        method: "PUT",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 536 days ago