Edits history of script submission #10698 for ' Acting on Tagged Droplets (digitalocean)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Digitalocean = {
      token: string;
    };
    /**
     * Acting on Tagged Droplets
     * Some actions can be performed in bulk on tagged Droplets. The actions can be
    initiated by sending a POST to `/v2/droplets/actions?tag_name=$TAG_NAME` with
    the action arguments.
    
    Only a sub-set of action types are supported:
    
    - `power_cycle`
    - `power_on`
    - `power_off`
    - `shutdown`
    - `enable_ipv6`
    - `enable_backups`
    - `disable_backups`
    - `snapshot`
    
     */
    export async function main(
      auth: Digitalocean,
      tag_name: string | undefined,
      body:
        | {
            type:
              | "enable_backups"
              | "disable_backups"
              | "reboot"
              | "power_cycle"
              | "shutdown"
              | "power_off"
              | "power_on"
              | "restore"
              | "password_reset"
              | "resize"
              | "rebuild"
              | "rename"
              | "change_kernel"
              | "enable_ipv6"
              | "snapshot";
          }
        | ({
            type:
              | "enable_backups"
              | "disable_backups"
              | "reboot"
              | "power_cycle"
              | "shutdown"
              | "power_off"
              | "power_on"
              | "restore"
              | "password_reset"
              | "resize"
              | "rebuild"
              | "rename"
              | "change_kernel"
              | "enable_ipv6"
              | "snapshot";
          } & { name?: string }),
    ) {
      const url = new URL(`https://api.digitalocean.com/v2/droplets/actions`);
      for (const [k, v] of [["tag_name", tag_name]]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      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