Skip to content

Commit 2452e6c

Browse files
committed
feat(api): Add organization filtering to ListTeams
chore(mock): Update MockClient.ListTeams signature test(cli): Update ListTeams mock expectation in list_workspaces_test feat(api): Add ConvertOrgTeamToTeam helper for organization teams refactor(cli): Simplify ListTeamsCmd.RunE to use new ListTeams signature feat(cli): Update Client interface for ListTeams to include orgId feat(cli): Add --org flag and GlobalOptions.OrgId for organization context feat(pkg): Add GetOrgId to Environment for CS_ORG_ID feat(cli): Implement 'cs list org' command to list organizations
1 parent 99162d6 commit 2452e6c

12 files changed

Lines changed: 93 additions & 57 deletions

api/client.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,3 @@ func (c *Client) ListBaseimages() ([]Baseimage, error) {
145145
baseimages, r, err := c.api.MetadataAPI.MetadataGetWorkspaceBaseImages(c.ctx).Execute()
146146
return baseimages, errors.FormatAPIError(r, err)
147147
}
148-
149-
func (c *Client) ListOrganizations() ([]Organization, error) {
150-
organizations, r, err := c.api.OrganizationsAPI.OrganizationsListOrganizations(c.ctx).Execute()
151-
return organizations, errors.FormatAPIError(r, err)
152-
}

api/organization.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package api
2+
3+
import (
4+
cserrors "github.com/codesphere-cloud/cs-go/api/errors"
5+
)
6+
7+
func (c *Client) ListOrganizations() ([]Organization, error) {
8+
organizations, r, err := c.api.OrganizationsAPI.OrganizationsListOrganizations(c.ctx).Execute()
9+
return organizations, cserrors.FormatAPIError(r, err)
10+
}

api/team.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// Returns [NotFound] if no plan with the given Id could be found
1717
// Returns [Duplicated] if no plan with the given Id could be found
1818
func (client *Client) TeamIdByName(name string) (Team, error) {
19-
teams, err := client.ListTeams()
19+
teams, err := client.ListTeams("")
2020
if err != nil {
2121
return Team{}, err
2222
}
@@ -39,7 +39,20 @@ func (client *Client) TeamIdByName(name string) (Team, error) {
3939
return matchingTeams[0], nil
4040
}
4141

42-
func (c *Client) ListTeams() ([]Team, error) {
42+
func (c *Client) ListTeams(orgId string) ([]Team, error) {
43+
if orgId != "" {
44+
teams, r, err := c.api.OrganizationsAPI.OrganizationsListOrgTeams(c.ctx, orgId).Execute()
45+
if err != nil {
46+
return nil, cserrors.FormatAPIError(r, err)
47+
}
48+
49+
res := make([]Team, len(teams))
50+
for i, t := range teams {
51+
res[i] = ConvertOrgTeamToTeam(t, orgId)
52+
}
53+
return res, nil
54+
}
55+
4356
teams, r, err := c.api.TeamsAPI.TeamsListTeams(c.ctx).Execute()
4457
return teams, cserrors.FormatAPIError(r, err)
4558
}

api/types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ func ConvertToTeam(t *openapi.TeamsGetTeam200Response) *Team {
3939
}
4040
}
4141

42+
func ConvertOrgTeamToTeam(t openapi.OrganizationsListOrgTeams200ResponseInner, orgId string) Team {
43+
return Team{
44+
Id: t.Id,
45+
DefaultDataCenterId: t.DefaultDataCenterId,
46+
Name: t.Name,
47+
Description: *openapi.NewNullableString(t.Description),
48+
AvatarId: *openapi.NewNullableString(t.AvatarId),
49+
AvatarUrl: *openapi.NewNullableString(t.AvatarUrl),
50+
IsFirst: t.IsFirst,
51+
OrganizationId: &orgId,
52+
Role: 0, // Default to admin role if not specified by org API
53+
}
54+
}
55+
4256
type Time interface {
4357
Sleep(time.Duration)
4458
Now() time.Time

cli/cmd/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
type Client interface {
19-
ListTeams() ([]api.Team, error)
19+
ListTeams(orgId string) ([]api.Team, error)
2020
ListWorkspaces(teamId int) ([]api.Workspace, error)
2121
ListBaseimages() ([]api.Baseimage, error)
2222
ListOrganizations() ([]api.Organization, error)

cli/cmd/list_organizations_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var _ = Describe("Organization", func() {
3333
Opts: &cmd.ListOptions{
3434
GlobalOptions: &cmd.GlobalOptions{
3535
Env: mockEnv,
36-
OrgId: -1, // force using the env mock to get a org ID
36+
OrgId: "", // force using the env mock to get a org ID
3737
},
3838
},
3939
ClientFactory: cmd.NewClient, // Default to real client, will be overridden in specific tests
@@ -164,8 +164,6 @@ var _ = Describe("Organization", func() {
164164
Expect(err).NotTo(HaveOccurred())
165165
Expect(orgs).To(Equal(expectedOrgs))
166166

167-
168-
169167
// Restore Stdout
170168
err = w.Close()
171169
var buf bytes.Buffer

cli/cmd/list_teams.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ func (l *ListTeamsCmd) RunE(_ *cobra.Command, args []string) (err error) {
4040
return fmt.Errorf("failed to create Codesphere client: %w", err)
4141
}
4242

43-
teams, err := client.ListTeams()
43+
teams, err := client.ListTeams(l.opts.OrgId)
4444
if err != nil {
4545
return fmt.Errorf("failed to list teams: %w", err)
4646
}
47-
4847
switch l.opts.OutputFormat {
4948
case OutputFormatJSON:
5049
return io.PrintJSON(teams)

cli/cmd/list_workspaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (l *ListWorkspacesCmd) getTeamIds(client Client) (teams []int, err error) {
9696
return
9797
}
9898
var allTeams []api.Team
99-
allTeams, err = client.ListTeams()
99+
allTeams, err = client.ListTeams(l.Opts.OrgId)
100100
if err != nil {
101101
return
102102
}

cli/cmd/list_workspaces_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var _ = Describe("Workspace", func() {
5353

5454
Context("when team ID is not set", func() {
5555
It("lists workspaces of all teams when no team ID is set", func() {
56-
mockClient.EXPECT().ListTeams().Return([]api.Team{{Id: 0}, {Id: 1}}, nil)
56+
mockClient.EXPECT().ListTeams("").Return([]api.Team{{Id: 0}, {Id: 1}}, nil)
5757

5858
expectedWorkspaces := []api.Workspace{
5959
{Id: 0, Name: "fakeForTeam0"},

cli/cmd/mocks.go

Lines changed: 28 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)