[CLI-2354] Add confluent organization use command for org switching#3277
[CLI-2354] Add confluent organization use command for org switching#3277Apoorv Munshi (ap00rv) wants to merge 2 commits intomainfrom
confluent organization use command for org switching#3277Conversation
|
🎉 All Contributor License Agreements have been signed. Ready to merge. |
There was a problem hiding this comment.
Pull request overview
Adds support for switching Confluent Cloud organizations within an existing login session by introducing confluent organization use <id>. This fits into the CLI’s organization management commands and relies on refreshing an org-scoped JWT using the existing refresh token while clearing org-scoped local state.
Changes:
- Added
organization use <id>command with autocomplete suggestions and not-found handling. - Introduced
Context.SwitchOrganization()to refresh tokens and clear environment/Kafka cluster selections with rollback on refresh failure. - Added unit + integration coverage and updated golden fixtures/help output for the new command.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
internal/organization/command.go |
Registers the new use subcommand under organization. |
internal/organization/command_use.go |
Implements organization use execution and shell completion suggestions. |
pkg/config/context.go |
Adds SwitchOrganization() for token refresh + clearing org-scoped state. |
pkg/config/context_test.go |
Adds unit tests for switching orgs, clearing state, and rollback behavior. |
test/organization_test.go |
Adds CLI integration tests for organization use and not-found behavior. |
test/fixtures/output/organization/use.golden |
Golden output for successful org switch message. |
test/fixtures/output/organization/use-not-found.golden |
Golden output for org not found / forbidden error path. |
test/fixtures/output/organization/use-help.golden |
Golden help output for the new subcommand (covered via recursive help tests). |
test/fixtures/output/organization/help.golden |
Updates organization command help to include the new use command. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| previousOrgId := c.LastOrgId | ||
| previousAuthToken := c.State.AuthToken | ||
| previousRefreshToken := c.State.AuthRefreshToken | ||
|
|
||
| // Set the target org before refreshing so RefreshSession picks it up | ||
| // via GetCurrentOrganization(). | ||
| c.LastOrgId = orgId | ||
|
|
||
| if err := c.RefreshSession(client); err != nil { | ||
| c.LastOrgId = previousOrgId | ||
| c.State.AuthToken = previousAuthToken | ||
| c.State.AuthRefreshToken = previousRefreshToken | ||
| return err | ||
| } | ||
|
|
||
| // Clear all environment-scoped state: environments and Kafka clusters | ||
| // belong to a specific org, so stale IDs from the previous org must not | ||
| // leak into the new context. | ||
| c.CurrentEnvironment = "" | ||
| c.Environments = map[string]*EnvironmentContext{} | ||
| if c.KafkaClusterContext != nil { | ||
| c.KafkaClusterContext.ActiveKafkaCluster = "" | ||
| c.KafkaClusterContext.ActiveKafkaClusterEndpoint = "" | ||
| c.KafkaClusterContext.KafkaClusterConfigs = map[string]*KafkaClusterConfig{} | ||
| c.KafkaClusterContext.KafkaEnvContexts = map[string]*KafkaEnvContext{} | ||
| } |
There was a problem hiding this comment.
SwitchOrganization updates Context.LastOrgId but does not update/clear c.State.Auth.Organization (or related org-scoped auth metadata). After a successful switch, GetCurrentOrganization() and GetOrganization() can diverge, and org suspension checks (Config.isOrgSuspended / isLoginBlockedByOrgSuspension) may still evaluate using the previous org’s suspension status. Consider updating the in-memory AuthConfig to reflect the new org (or clearing it and forcing a refresh) and include it in the rollback path if RefreshSession fails.
|
|
||
| require.Equal(t, newOrgId, ctx.LastOrgId) | ||
| require.Equal(t, newToken, ctx.State.AuthToken) | ||
| require.Equal(t, newRefreshToken, ctx.State.AuthRefreshToken) |
There was a problem hiding this comment.
The new SwitchOrganization unit tests assert token/org ID changes and that environment/Kafka maps are cleared, but they don’t verify that org-scoped auth metadata is updated (e.g., ctx.State.Auth.Organization.ResourceId) or cleared. Adding assertions for this would prevent regressions where the context switches org IDs but still reports/uses the previous organization in suspension checks or other metadata paths.
| require.Equal(t, newRefreshToken, ctx.State.AuthRefreshToken) | |
| require.Equal(t, newRefreshToken, ctx.State.AuthRefreshToken) | |
| // Ensure org-scoped auth metadata is updated to reflect the new organization. | |
| require.NotNil(t, ctx.State.Auth) | |
| require.NotNil(t, ctx.State.Auth.Organization) | |
| require.Equal(t, newOrgId, ctx.State.Auth.Organization.ResourceId) |
Allow users in multiple organizations to switch between them without re-logging in. The command refreshes the org-scoped JWT using the existing user-scoped refresh token, clears environment and Kafka cluster state, and performs full rollback on failure. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace GetOrgOrganization (GET by ID) with ListOrgOrganizations (list) for validation in the use command, since the org-scoped JWT only allows reading the current org. Add integration test for already-active org path to increase coverage. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2a4f2c9 to
b182a62
Compare
|


Release Notes
Breaking Changes
New Features
confluent org usecommand. This automatically clears the env and clusters from current context.Bug Fixes
Checklist
Whatsection below whether this PR applies to Confluent Cloud, Confluent Platform, or both.Test & Reviewsection below.Blast Radiussection below.What
Problem :
Confluent CLI did not have feature to switch orgs after logging in. To switch orgs, one had to re-login using the
--organizationflagA new
org usecommand allows users in multiple organizations to switch between them without re-logging in (Confluent cloud only). The command refreshes the org-scoped JWT using the existing user-scoped refresh token, clears environment and Kafka cluster state, and performs full rollback on failure.Blast Radius
References
Test & Review