Edits history of script submission #10850 for ' Initiate a Reserved IP Action (digitalocean)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Digitalocean = {
      token: string;
    };
    /**
     * Initiate a Reserved IP Action
     * To initiate an action on a reserved IP send a POST request to
    `/v2/reserved_ips/$RESERVED_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 reserved IP to a Droplet
    | `unassign` | Unassign a reserved IP from a Droplet
    
     */
    export async function main(
      auth: Digitalocean,
      reserved_ip: string,
      body:
        | ({ type: "assign" | "unassign" } & {})
        | ({ type: "assign" | "unassign" } & { droplet_id: number }),
    ) {
      const url = new URL(
        `https://api.digitalocean.com/v2/reserved_ips/${reserved_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