-
Notifications
You must be signed in to change notification settings - Fork 32
/
org_invites.go
114 lines (95 loc) · 3.08 KB
/
org_invites.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package registry
import (
"context"
"errors"
"net/url"
"time"
"github.com/manifoldco/torus-cli/apitypes"
"github.com/manifoldco/torus-cli/envelope"
"github.com/manifoldco/torus-cli/identity"
"github.com/manifoldco/torus-cli/primitive"
)
// OrgInvitesClient represents the `/org-invites` registry endpoint, used for
// sending, accepting, and approving invitations to organizations in Torus.
type OrgInvitesClient struct {
client RoundTripper
}
// Approve sends an approval notification to the registry regarding a specific
// invitation.
func (o *OrgInvitesClient) Approve(ctx context.Context, inviteID *identity.ID) (*envelope.OrgInvite, error) {
invite := envelope.OrgInvite{}
path := "/org-invites/" + inviteID.String() + "/approve"
err := o.client.RoundTrip(ctx, "POST", path, nil, nil, &invite)
return &invite, err
}
// Get returns a specific Org Invite based on it's ID
func (o *OrgInvitesClient) Get(ctx context.Context, inviteID *identity.ID) (*envelope.OrgInvite, error) {
if inviteID == nil {
return nil, errors.New("an inviteID must be provided")
}
invite := envelope.OrgInvite{}
path := "/org-invites/" + inviteID.String()
err := o.client.RoundTrip(ctx, "GET", path, nil, nil, &invite)
return &invite, err
}
// List lists all invites for a given org with the given states
func (o *OrgInvitesClient) List(ctx context.Context, orgID *identity.ID, states []string, email string) ([]envelope.OrgInvite, error) {
v := &url.Values{}
v.Set("org_id", orgID.String())
for _, state := range states {
v.Add("state", state)
}
if email != "" {
v.Add("email", email)
}
var invites []envelope.OrgInvite
err := o.client.RoundTrip(ctx, "GET", "/org-invites", v, nil, &invites)
return invites, err
}
// Accept executes the accept invite request
func (o *OrgInvitesClient) Accept(ctx context.Context, org, email, code string) error {
data := apitypes.InviteAccept{
Org: org,
Email: email,
Code: code,
}
return o.client.RoundTrip(ctx, "POST", "/org-invites/accept", nil, &data, nil)
}
// Send creates a new org invitation
func (o *OrgInvitesClient) Send(ctx context.Context, email string, orgID, inviterID identity.ID, teamIDs []identity.ID) error {
now := time.Now()
inviteBody := primitive.OrgInvite{
OrgID: &orgID,
InviterID: &inviterID,
PendingTeams: teamIDs,
Email: email,
Created: &now,
// Null values below
InviteeID: nil,
ApproverID: nil,
Accepted: nil,
Approved: nil,
}
ID, err := identity.NewMutable(&inviteBody)
if err != nil {
return err
}
invite := envelope.OrgInvite{
ID: &ID,
Version: 1,
Body: &inviteBody,
}
return o.client.RoundTrip(ctx, "POST", "/org-invites", nil, &invite, nil)
}
// Associate executes the associate invite request
func (o *OrgInvitesClient) Associate(ctx context.Context, org, email, code string) (*envelope.OrgInvite, error) {
// Same payload as accept, re-use type
data := apitypes.InviteAccept{
Org: org,
Email: email,
Code: code,
}
invite := envelope.OrgInvite{}
err := o.client.RoundTrip(ctx, "POST", "/org-invites/associate", nil, &data, &invite)
return &invite, err
}