Skip to content
Draft
42 changes: 24 additions & 18 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ License URL: https://github.com/go-git/gcfg/blob/3a3c6141e376/LICENSE

----------
Module: github.com/go-git/go-billy/v5
Version: v5.8.0
Version: v5.9.0
License: Apache-2.0
License URL: https://github.com/go-git/go-billy/blob/v5.8.0/LICENSE
License URL: https://github.com/go-git/go-billy/blob/v5.9.0/LICENSE

----------
Module: github.com/go-git/go-git/v5
Version: v5.18.0
Version: v5.19.0
License: Apache-2.0
License URL: https://github.com/go-git/go-git/blob/v5.18.0/LICENSE
License URL: https://github.com/go-git/go-git/blob/v5.19.0/LICENSE

----------
Module: github.com/go-logr/logr
Expand Down Expand Up @@ -159,6 +159,12 @@ Version: v1.2.0
License: BSD-3-Clause
License URL: https://github.com/google/go-querystring/blob/v1.2.0/LICENSE

----------
Module: github.com/google/uuid
Version: v1.6.0
License: BSD-3-Clause
License URL: https://github.com/google/uuid/blob/v1.6.0/LICENSE

----------
Module: github.com/hashicorp/go-cleanhttp
Version: v0.5.2
Expand Down Expand Up @@ -257,9 +263,9 @@ License URL: https://github.com/onsi/gomega/blob/v1.40.0/LICENSE

----------
Module: github.com/pjbgf/sha1cd
Version: v0.5.0
Version: v0.6.0
License: Apache-2.0
License URL: https://github.com/pjbgf/sha1cd/blob/v0.5.0/LICENSE
License URL: https://github.com/pjbgf/sha1cd/blob/v0.6.0/LICENSE

----------
Module: github.com/pmezard/go-difflib/difflib
Expand Down Expand Up @@ -461,39 +467,39 @@ License URL: https://github.com/go-yaml/yaml/blob/v3.0.1/LICENSE

----------
Module: k8s.io/api
Version: v0.36.0
Version: v0.36.1
License: Apache-2.0
License URL: https://github.com/kubernetes/api/blob/v0.36.0/LICENSE
License URL: https://github.com/kubernetes/api/blob/v0.36.1/LICENSE

----------
Module: k8s.io/apimachinery/pkg
Version: v0.36.0
Version: v0.36.1
License: Apache-2.0
License URL: https://github.com/kubernetes/apimachinery/blob/v0.36.0/LICENSE
License URL: https://github.com/kubernetes/apimachinery/blob/v0.36.1/LICENSE

----------
Module: k8s.io/apimachinery/third_party/forked/golang/reflect
Version: v0.36.0
Version: v0.36.1
License: BSD-3-Clause
License URL: https://github.com/kubernetes/apimachinery/blob/v0.36.0/third_party/forked/golang/LICENSE
License URL: https://github.com/kubernetes/apimachinery/blob/v0.36.1/third_party/forked/golang/LICENSE

----------
Module: k8s.io/cli-runtime/pkg/printers
Version: v0.36.0
Version: v0.36.1
License: Apache-2.0
License URL: https://github.com/kubernetes/cli-runtime/blob/v0.36.0/LICENSE
License URL: https://github.com/kubernetes/cli-runtime/blob/v0.36.1/LICENSE

----------
Module: k8s.io/client-go/third_party/forked/golang/template
Version: v0.36.0
Version: v0.36.1
License: BSD-3-Clause
License URL: https://github.com/kubernetes/client-go/blob/v0.36.0/third_party/forked/golang/LICENSE
License URL: https://github.com/kubernetes/client-go/blob/v0.36.1/third_party/forked/golang/LICENSE

----------
Module: k8s.io/client-go/util/jsonpath
Version: v0.36.0
Version: v0.36.1
License: Apache-2.0
License URL: https://github.com/kubernetes/client-go/blob/v0.36.0/LICENSE
License URL: https://github.com/kubernetes/client-go/blob/v0.36.1/LICENSE

----------
Module: k8s.io/klog/v2
Expand Down
5 changes: 0 additions & 5 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,3 @@ func (c *Client) ListBaseimages() ([]Baseimage, error) {
baseimages, r, err := c.api.MetadataAPI.MetadataGetWorkspaceBaseImages(c.ctx).Execute()
return baseimages, errors.FormatAPIError(r, err)
}

func (c *Client) ListOrganizations() ([]Organization, error) {
organizations, r, err := c.api.OrganizationsAPI.OrganizationsListOrganizations(c.ctx).Execute()
return organizations, errors.FormatAPIError(r, err)
}
13 changes: 13 additions & 0 deletions api/organization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0

package api

import (
cserrors "github.com/codesphere-cloud/cs-go/api/errors"
)

func (c *Client) ListOrganizations() ([]Organization, error) {
organizations, r, err := c.api.OrganizationsAPI.OrganizationsListOrganizations(c.ctx).Execute()
return organizations, cserrors.FormatAPIError(r, err)
}
53 changes: 47 additions & 6 deletions api/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// Returns [NotFound] if no plan with the given Id could be found
// Returns [Duplicated] if no plan with the given Id could be found
func (client *Client) TeamIdByName(name string) (Team, error) {
teams, err := client.ListTeams()
teams, err := client.ListTeams("")
if err != nil {
return Team{}, err
}
Expand All @@ -39,27 +39,68 @@ func (client *Client) TeamIdByName(name string) (Team, error) {
return matchingTeams[0], nil
}

func (c *Client) ListTeams() ([]Team, error) {
func (c *Client) ListTeams(orgId string) ([]Team, error) {
if orgId != "" {
teams, r, err := c.api.OrganizationsAPI.OrganizationsListOrgTeams(c.ctx, orgId).Execute()
if err != nil {
return nil, cserrors.FormatAPIError(r, err)
}

res := make([]Team, len(teams))
for i, t := range teams {
res[i] = *ConvertOrgTeamToTeam(t, orgId)
}
return res, nil
}

teams, r, err := c.api.TeamsAPI.TeamsListTeams(c.ctx).Execute()
return teams, cserrors.FormatAPIError(r, err)
}

func (c *Client) GetTeam(teamId int) (*Team, error) {
team, r, err := c.api.TeamsAPI.TeamsGetTeam(c.ctx, float32(teamId)).Execute()
return ConvertToTeam(team), cserrors.FormatAPIError(r, err)
if err != nil {
return nil, cserrors.FormatAPIError(r, err)
}
return ConvertToTeam(team), nil
}

func (c *Client) CreateTeam(orgId string, name string, dc int) (*Team, error) {
if orgId == "" {
return c.createTeam(name, dc)
}
return c.createOrgTeam(orgId, name, dc)

}

func (c *Client) CreateTeam(name string, dc int) (*Team, error) {
func (c *Client) createTeam(name string, dc int) (*Team, error) {
team, r, err := c.api.TeamsAPI.TeamsCreateTeam(c.ctx).
TeamsCreateTeamRequest(openapi_client.TeamsCreateTeamRequest{
Name: name,
Dc: dc,
}).
Execute()
return ConvertToTeam(team), cserrors.FormatAPIError(r, err)
if err != nil {
return nil, cserrors.FormatAPIError(r, err)
}
return ConvertToTeam(team), nil
}

func (c *Client) createOrgTeam(orgId string, name string, dc int) (*Team, error) {
team, r, err := c.api.TeamsAPI.TeamsCreateTeam(c.ctx).
TeamsCreateTeamRequest(openapi_client.TeamsCreateTeamRequest{
Name: name,
Dc: dc,
OrganizationId: &orgId,
}).
Execute()
if err != nil {
return nil, cserrors.FormatAPIError(r, err)
}
return ConvertToTeam(team), nil
}

func (c *Client) DeleteTeam(teamId int) error {
func (c *Client) DeleteTeam(orgId string, teamId int) error {
r, err := c.api.TeamsAPI.TeamsDeleteTeam(c.ctx, float32(teamId)).Execute()
return cserrors.FormatAPIError(r, err)
}
14 changes: 14 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ func ConvertToTeam(t *openapi.TeamsGetTeam200Response) *Team {
}
}

func ConvertOrgTeamToTeam(t openapi.OrganizationsListOrgTeams200ResponseInner, orgId string) *Team {
return &Team{
Id: t.Id,
DefaultDataCenterId: t.DefaultDataCenterId,
Name: t.Name,
Description: *openapi.NewNullableString(t.Description),
AvatarId: *openapi.NewNullableString(t.AvatarId),
AvatarUrl: *openapi.NewNullableString(t.AvatarUrl),
IsFirst: t.IsFirst,
OrganizationId: &orgId,
Role: 0, // Default to admin role if not specified by org API
}
}

type Time interface {
Sleep(time.Duration)
Now() time.Time
Expand Down
4 changes: 3 additions & 1 deletion cli/cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

type Client interface {
ListTeams() ([]api.Team, error)
ListTeams(orgId string) ([]api.Team, error)
ListWorkspaces(teamId int) ([]api.Workspace, error)
ListBaseimages() ([]api.Baseimage, error)
ListOrganizations() ([]api.Organization, error)
Expand All @@ -34,6 +34,8 @@ type Client interface {
GetPipelineState(wsId int, stage string) ([]api.PipelineStatus, error)
GitPull(wsId int, remote string, branch string) error
DeployLandscape(wsId int, profile string) error
CreateTeam(orgId string, name string, dcId int) (*api.Team, error)
DeleteTeam(orgId string, teamId int) error
}

// CommandExecutor abstracts command execution for testing
Expand Down
4 changes: 1 addition & 3 deletions cli/cmd/list_organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var _ = Describe("Organization", func() {
Opts: &cmd.ListOptions{
GlobalOptions: &cmd.GlobalOptions{
Env: mockEnv,
OrgId: -1, // force using the env mock to get a org ID
OrgId: "", // force using the env mock to get a org ID
},
},
ClientFactory: cmd.NewClient, // Default to real client, will be overridden in specific tests
Expand Down Expand Up @@ -164,8 +164,6 @@ var _ = Describe("Organization", func() {
Expect(err).NotTo(HaveOccurred())
Expect(orgs).To(Equal(expectedOrgs))



// Restore Stdout
err = w.Close()
var buf bytes.Buffer
Expand Down
3 changes: 1 addition & 2 deletions cli/cmd/list_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ func (l *ListTeamsCmd) RunE(_ *cobra.Command, args []string) (err error) {
return fmt.Errorf("failed to create Codesphere client: %w", err)
}

teams, err := client.ListTeams()
teams, err := client.ListTeams(l.opts.OrgId)
if err != nil {
return fmt.Errorf("failed to list teams: %w", err)
}

switch l.opts.OutputFormat {
case OutputFormatJSON:
return io.PrintJSON(teams)
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/list_workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (l *ListWorkspacesCmd) getTeamIds(client Client) (teams []int, err error) {
return
}
var allTeams []api.Team
allTeams, err = client.ListTeams()
allTeams, err = client.ListTeams(l.Opts.OrgId)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/list_workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var _ = Describe("Workspace", func() {

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

expectedWorkspaces := []api.Workspace{
{Id: 0, Name: "fakeForTeam0"},
Expand Down
Loading
Loading