Edits history of script submission #10941 for ' Remove Rules from a Firewall (digitalocean)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Digitalocean = {
      token: string;
    };
    /**
     * Remove Rules from a Firewall
     * To remove access rules from a firewall, send a DELETE request to
    `/v2/firewalls/$FIREWALL_ID/rules`. The body of the request may include an
    `inbound_rules` and/or `outbound_rules` attribute containing an array of rules
    to be removed.
    
    No response body will be sent back, but the response code will indicate
    success. Specifically, the response code will be a 204, which means that the
    action was successful with no returned body data.
    
     */
    export async function main(
      auth: Digitalocean,
      firewall_id: string,
      body:
        | ({
            inbound_rules?: { protocol: "tcp" | "udp" | "icmp"; ports: string } & {
              sources: {
                addresses?: string[];
                droplet_ids?: number[];
                load_balancer_uids?: string[];
                kubernetes_ids?: string[];
                tags?: string[] & {};
              } & {};
            }[];
            outbound_rules?: { protocol: "tcp" | "udp" | "icmp"; ports: string } & {
              destinations: {
                addresses?: string[];
                droplet_ids?: number[];
                load_balancer_uids?: string[];
                kubernetes_ids?: string[];
                tags?: string[] & {};
              } & {};
            }[];
          } & {})
        | ({
            inbound_rules?: { protocol: "tcp" | "udp" | "icmp"; ports: string } & {
              sources: {
                addresses?: string[];
                droplet_ids?: number[];
                load_balancer_uids?: string[];
                kubernetes_ids?: string[];
                tags?: string[] & {};
              } & {};
            }[];
            outbound_rules?: { protocol: "tcp" | "udp" | "icmp"; ports: string } & {
              destinations: {
                addresses?: string[];
                droplet_ids?: number[];
                load_balancer_uids?: string[];
                kubernetes_ids?: string[];
                tags?: string[] & {};
              } & {};
            }[];
          } & {}),
    ) {
      const url = new URL(
        `https://api.digitalocean.com/v2/firewalls/${firewall_id}/rules`,
      );
    
      const response = await fetch(url, {
        method: "DELETE",
        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 537 days ago