-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathteam.go
More file actions
65 lines (53 loc) · 1.79 KB
/
team.go
File metadata and controls
65 lines (53 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package api
import (
"fmt"
cserrors "github.com/codesphere-cloud/cs-go/api/errors"
"github.com/codesphere-cloud/cs-go/api/openapi_client"
)
// Fetches the team for a given team name.
// If the name is ambigous an error is thrown.
//
// 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()
if err != nil {
return Team{}, err
}
matchingTeams := []Team{}
for _, t := range teams {
if t.Name == name {
matchingTeams = append(matchingTeams, t)
}
}
if len(matchingTeams) == 0 {
return Team{}, cserrors.NotFound(fmt.Sprintf("no team with name %s found", name))
}
if len(matchingTeams) > 1 {
return Team{}, cserrors.Duplicated(fmt.Sprintf("multiple teams (%v) with the name %s found.", matchingTeams, name))
}
return matchingTeams[0], nil
}
func (c *Client) ListTeams() ([]Team, error) {
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)
}
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)
}
func (c *Client) DeleteTeam(teamId int) error {
r, err := c.api.TeamsAPI.TeamsDeleteTeam(c.ctx, float32(teamId)).Execute()
return cserrors.FormatAPIError(r, err)
}