Edits history of script submission #14825 for ' Add a role to a user or service account (cockroachdb)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Cockroachdb = {
      token: string;
    };
    /**
     * Add a role to a user or service account
     * Add a single role to a user or service account by providing its user_id or service_account_id.
    
    Roles that will be 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,
      resource_type: "ORGANIZATION" | "CLUSTER" | "FOLDER",
      resource_id: string,
      role_name:
        | "BILLING_COORDINATOR"
        | "ORG_ADMIN"
        | "ORG_MEMBER"
        | "CLUSTER_ADMIN"
        | "CLUSTER_OPERATOR_WRITER"
        | "CLUSTER_DEVELOPER"
        | "CLUSTER_CREATOR"
        | "FOLDER_ADMIN"
        | "FOLDER_MOVER",
    ) {
      const url = new URL(
        `https://cockroachlabs.cloud/api/v1/roles/${user_id}/${resource_type}/${resource_id}/${role_name}`,
      );
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago