-
Notifications
You must be signed in to change notification settings - Fork 32
/
teams.go
74 lines (61 loc) · 1.85 KB
/
teams.go
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
66
67
68
69
70
71
72
73
74
package registry
import (
"context"
"errors"
"net/url"
"github.com/manifoldco/torus-cli/envelope"
"github.com/manifoldco/torus-cli/identity"
"github.com/manifoldco/torus-cli/primitive"
)
// TeamsClient makes proxied requests to the registry's teams endpoints
type TeamsClient struct {
client RoundTripper
}
// List retrieves all teams for an org based on the filtered values
func (t *TeamsClient) List(ctx context.Context, orgID *identity.ID, name string, teamType primitive.TeamType) ([]envelope.Team, error) {
v := &url.Values{}
if orgID != nil {
v.Set("org_id", orgID.String())
}
if name != "" {
v.Set("name", name)
}
if teamType != primitive.AnyTeamType {
v.Set("type", string(teamType))
}
var teams []envelope.Team
err := t.client.RoundTrip(ctx, "GET", "/teams", v, nil, &teams)
return teams, err
}
// GetByOrg retrieves all teams for an org id
func (t *TeamsClient) GetByOrg(ctx context.Context, orgID *identity.ID) ([]envelope.Team, error) {
return t.List(ctx, orgID, "", primitive.AnyTeamType)
}
// GetByName retrieves the team with the specified name
func (t *TeamsClient) GetByName(ctx context.Context, orgID *identity.ID, name string) ([]envelope.Team, error) {
return t.List(ctx, orgID, name, primitive.AnyTeamType)
}
// Create performs a request to create a new team object
func (t *TeamsClient) Create(ctx context.Context, orgID *identity.ID, name string,
teamType primitive.TeamType) (*envelope.Team, error) {
if orgID == nil {
return nil, errors.New("invalid org")
}
teamBody := primitive.Team{
Name: name,
OrgID: orgID,
TeamType: teamType,
}
ID, err := identity.NewMutable(&teamBody)
if err != nil {
return nil, err
}
team := envelope.Team{
ID: &ID,
Version: 1,
Body: &teamBody,
}
result := &envelope.Team{}
err = t.client.RoundTrip(ctx, "POST", "/teams", nil, &team, result)
return result, err
}