Skip to content

Commit b182a62

Browse files
ap00rvclaude
andcommitted
Use ListOrgOrganizations when switching orgs
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>
1 parent 7c4d9d6 commit b182a62

4 files changed

Lines changed: 25 additions & 6 deletions

File tree

internal/organization/command_use.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,26 @@ func (c *command) validArgs(cmd *cobra.Command, args []string) []string {
4848
func (c *command) use(_ *cobra.Command, args []string) error {
4949
id := args[0]
5050

51-
if _, httpResp, err := c.V2Client.GetOrgOrganization(id); err != nil {
52-
return errors.CatchCCloudV2ResourceNotFoundError(err, resource.Organization, httpResp)
51+
// Use the list endpoint instead of get-by-ID because the org-scoped JWT
52+
// only authorizes reading the *current* organization. The list endpoint
53+
// returns every organization the user belongs to regardless of JWT scope.
54+
organizations, err := c.V2Client.ListOrgOrganizations()
55+
if err != nil {
56+
return fmt.Errorf("failed to list organizations: %w", err)
57+
}
58+
59+
found := false
60+
for _, org := range organizations {
61+
if org.GetId() == id {
62+
found = true
63+
break
64+
}
65+
}
66+
if !found {
67+
return errors.NewErrorWithSuggestions(
68+
fmt.Sprintf(`organization "%s" not found or access forbidden`, id),
69+
"List available organizations with `organization list`.",
70+
)
5371
}
5472

5573
if id == c.Context.GetCurrentOrganization() {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Using organization "abc-123".
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Error: organization not found or access forbidden: Resource not found or access forbidden
1+
Error: organization "org-dne" not found or access forbidden
22

33
Suggestions:
44
List available organizations with `organization list`.

test/organization_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func (s *CLITestSuite) TestOrganization() {
1515
{args: "organization update --name default-updated", fixture: "organization/update.golden"},
1616
{args: "organization update --name default-updated -o json", fixture: "organization/update-json.golden"},
1717
{args: "organization use abc-456", fixture: "organization/use.golden"},
18+
{args: "organization use abc-123", fixture: "organization/use-already-active.golden"},
1819
}
1920

2021
for _, test := range tests {
@@ -49,10 +50,9 @@ func (s *CLITestSuite) TestOrganization() {
4950
s.runIntegrationTest(test)
5051
})
5152

53+
// The use command validates via the list endpoint, so org-dne simply
54+
// won't appear in the returned list — no need for TestOrgNotFound.
5255
s.T().Run("organization use not found", func(t *testing.T) {
53-
testserver.TestOrgNotFound = true
54-
defer func() { testserver.TestOrgNotFound = false }()
55-
5656
test := CLITest{
5757
args: "organization use org-dne",
5858
fixture: "organization/use-not-found.golden",

0 commit comments

Comments
 (0)