diff --git a/packages/dbos-cloud/cli.ts b/packages/dbos-cloud/cli.ts index 9524451da..2dabd10fd 100755 --- a/packages/dbos-cloud/cli.ts +++ b/packages/dbos-cloud/cli.ts @@ -17,7 +17,7 @@ import updateNotifier, { Package } from "update-notifier"; import { profile } from "./users/profile.js"; import { revokeRefreshToken } from "./users/authentication.js"; import { listAppVersions } from "./applications/list-app-versions.js"; -import { orgInvite, orgListUsers, renameOrganization, joinOrganization } from "./organizations/organization.js"; +import { orgInvite, orgListUsers, renameOrganization, joinOrganization, removeUserFromOrg } from "./organizations/organization.js"; import { ListWorkflowsInput, listWorkflows } from "./applications/manage-workflows.js"; import { importSecrets } from "./applications/secrets.js"; @@ -438,6 +438,15 @@ orgCommands process.exit(exitCode); }); + orgCommands + .command("remove") + .description("Remove a user from an organization") + .argument("", "User to remove") + .action(async (username: string) => { + const exitCode = await removeUserFromOrg(DBOSCloudHost, username); + process.exit(exitCode); + }); + //////////////////////////// /* WORKFLOW COMMANDS */ //////////////////////////// diff --git a/packages/dbos-cloud/organizations/organization.ts b/packages/dbos-cloud/organizations/organization.ts index 30496d8d3..9672105fa 100644 --- a/packages/dbos-cloud/organizations/organization.ts +++ b/packages/dbos-cloud/organizations/organization.ts @@ -142,3 +142,33 @@ export async function joinOrganization(host: string, orgname: string, secret: st return 1; } } + +export async function removeUserFromOrg(host: string, usernameToDelete: string) { + const logger = getLogger(); + const userCredentials = await getCloudCredentials(host, logger); + const bearerToken = "Bearer " + userCredentials.token; + + try { + await axios.delete( + `https://${host}/v1alpha1/${userCredentials.organization}/${usernameToDelete}`, + { + headers: { + "Content-Type": "application/json", + Authorization: bearerToken, + }, + } + ); + + logger.info(`Successfully removed ${usernameToDelete} from organization ${userCredentials.organization}`); + return 0; + } catch (e) { + const errorLabel = `Failed to remove ${usernameToDelete} from organization ${userCredentials.organization}`; + const axiosError = e as AxiosError; + if (isCloudAPIErrorResponse(axiosError.response?.data)) { + handleAPIErrors(errorLabel, axiosError); + } else { + logger.error(`${errorLabel}: ${(e as Error).message}`); + } + return 1; + } +}