-
Notifications
You must be signed in to change notification settings - Fork 32
/
memberships.go
75 lines (63 loc) · 1.89 KB
/
memberships.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
75
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"
)
// MembershipsClient represents the `/memberships` registry
// endpoint, used for accessing the relationship between users,
// organization, and teams.
type MembershipsClient struct {
client RoundTripper
}
// List returns all memberships for a given organization, team, or user/machine
func (m *MembershipsClient) List(ctx context.Context, orgID *identity.ID,
teamID *identity.ID, ownerID *identity.ID) ([]envelope.Membership, error) {
query := &url.Values{}
if orgID != nil {
query.Set("org_id", orgID.String())
}
if teamID != nil {
query.Set("team_id", teamID.String())
}
if ownerID != nil {
query.Set("owner_id", ownerID.String())
}
var memberships []envelope.Membership
err := m.client.RoundTrip(ctx, "GET", "/memberships", query, nil, &memberships)
return memberships, err
}
// Create requests addition of a user to a team
func (m *MembershipsClient) Create(ctx context.Context, userID, orgID, teamID *identity.ID) error {
if orgID == nil {
return errors.New("invalid org")
}
if userID == nil {
return errors.New("invalid user")
}
if teamID == nil {
return errors.New("invalid team")
}
membershipBody := primitive.Membership{
OwnerID: userID,
OrgID: orgID,
TeamID: teamID,
}
ID, err := identity.NewMutable(&membershipBody)
if err != nil {
return err
}
membership := envelope.Membership{
ID: &ID,
Version: 1,
Body: &membershipBody,
}
return m.client.RoundTrip(ctx, "POST", "/memberships", nil, &membership, nil)
}
// Delete requests deletion of a specific membership row by ID
func (m *MembershipsClient) Delete(ctx context.Context, membership *identity.ID) error {
return m.client.RoundTrip(ctx, "DELETE", "/memberships/"+membership.String(), nil, nil, nil)
}