Edits history of script submission #10849 for ' Initiate a Floating IP Action (digitalocean)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Digitalocean = {
      token: string;
    };
    /**
     * Initiate a Floating IP Action
     * To initiate an action on a floating IP send a POST request to
    `/v2/floating_ips/$FLOATING_IP/actions`. In the JSON body to the request,
    set the `type` attribute to on of the supported action types:
    
    | Action     | Details
    |------------|--------
    | `assign`   | Assigns a floating IP to a Droplet
    | `unassign` | Unassign a floating IP from a Droplet
    
     */
    export async function main(
      auth: Digitalocean,
      floating_ip: string,
      body:
        | ({ type: "assign" | "unassign" } & {})
        | ({ type: "assign" | "unassign" } & { droplet_id: number }),
    ) {
      const url = new URL(
        `https://api.digitalocean.com/v2/floating_ips/${floating_ip}/actions`,
      );
    
      const response = await fetch(url, {
        method: "POST",
        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