Edits history of script submission #14918 for ' Replace the roles for a user or service account with exactly those provided (cockroachdb)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Cockroachdb = {
      token: string;
    };
    /**
     * Replace the roles for a user or service account with exactly those provided
     * Replace the entire role set for a user or service account by providing its user_id or service_account_id.
    
    Roles that will be removed or added as a result of this call must follow the CC rules for role assignment:
    https://www.cockroachlabs.com/docs/cockroachcloud/authorization#which-roles-grant-the-ability-to-add-remove-and-manage-members-in-in-a-cockroachdb-cloud-organization
     */
    export async function main(
      auth: Cockroachdb,
      user_id: string,
      body: {
        roles: {
          name:
            | "BILLING_COORDINATOR"
            | "ORG_ADMIN"
            | "ORG_MEMBER"
            | "CLUSTER_ADMIN"
            | "CLUSTER_OPERATOR_WRITER"
            | "CLUSTER_DEVELOPER"
            | "CLUSTER_CREATOR"
            | "FOLDER_ADMIN"
            | "FOLDER_MOVER";
          resource: { id?: string; type: "ORGANIZATION" | "CLUSTER" | "FOLDER" };
        }[];
      },
    ) {
      const url = new URL(`https://cockroachlabs.cloud/api/v1/roles/${user_id}`);
    
      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 235 days ago