This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
invitations.go
155 lines (126 loc) · 4.91 KB
/
invitations.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package packngo
import (
"path"
)
const (
invitationsBasePath = "/invitations"
)
// InvitationService interface defines available invitation methods
type InvitationService interface {
Create(string, *InvitationCreateRequest, *GetOptions) (*Invitation, *Response, error)
List(string, *ListOptions) ([]Invitation, *Response, error)
Get(string, *GetOptions) (*Invitation, *Response, error)
Accept(string, *InvitationUpdateRequest) (*Invitation, *Response, error)
Resend(string) (*Invitation, *Response, error)
Delete(string) (*Response, error)
}
type invitationsRoot struct {
Invitations []Invitation `json:"invitations"`
Meta meta `json:"meta"`
}
// Invitation represents an Equinix Metal invitation
type Invitation struct {
*Href `json:",inline"`
ID string `json:"id,omitempty"`
Invitation Href `json:"invitation,omitempty"`
InvitedBy Href `json:"invited_by,omitempty"`
Invitee string `json:"invitee,omitempty"`
Nonce string `json:"nonce,omitempty"`
Organization Href `json:"organization,omitempty"`
Projects []Href `json:"projects,omitempty"`
Roles []string `json:"roles,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
UpdatedAt *Timestamp `json:"updated_at,omitempty"`
}
// InvitationCreateRequest struct for InvitationService.Create
type InvitationCreateRequest struct {
// Invitee is the email address of the recipient
Invitee string `json:"invitee"`
Message string `json:"message,omitempty"`
ProjectsIDs []string `json:"projects_ids,omitempty"`
Roles []string `json:"roles,omitempty"`
}
// InvitationUpdateRequest struct for InvitationService.Update
type InvitationUpdateRequest struct{}
func (u Invitation) String() string {
return Stringify(u)
}
// InvitationServiceOp implements InvitationService
type InvitationServiceOp struct {
client *Client
}
var _ InvitationService = (*InvitationServiceOp)(nil)
// Lists open invitations to the project
func (s *InvitationServiceOp) List(organizationID string, opts *ListOptions) (invitations []Invitation, resp *Response, err error) {
endpointPath := path.Join(organizationBasePath, organizationID, invitationsBasePath)
apiPathQuery := opts.WithQuery(endpointPath)
for {
subset := new(invitationsRoot)
resp, err = s.client.DoRequest("GET", apiPathQuery, nil, subset)
if err != nil {
return nil, resp, err
}
invitations = append(invitations, subset.Invitations...)
if apiPathQuery = nextPage(subset.Meta, opts); apiPathQuery != "" {
continue
}
return
}
}
// Create a Invitation with the given InvitationCreateRequest. New invitation VerificationStage
// will be AccountCreated, unless InvitationCreateRequest contains an valid
// InvitationID and Nonce in which case the VerificationStage will be Verified.
func (s *InvitationServiceOp) Create(organizationID string, createRequest *InvitationCreateRequest, opts *GetOptions) (*Invitation, *Response, error) {
endpointPath := path.Join(organizationBasePath, organizationID, invitationsBasePath)
apiPathQuery := opts.WithQuery(endpointPath)
invitation := new(Invitation)
resp, err := s.client.DoRequest("POST", apiPathQuery, createRequest, invitation)
if err != nil {
return nil, resp, err
}
return invitation, resp, err
}
func (s *InvitationServiceOp) Get(invitationID string, opts *GetOptions) (*Invitation, *Response, error) {
if validateErr := ValidateUUID(invitationID); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(invitationsBasePath, invitationID)
apiPathQuery := opts.WithQuery(endpointPath)
invitation := new(Invitation)
resp, err := s.client.DoRequest("GET", apiPathQuery, nil, invitation)
if err != nil {
return nil, resp, err
}
return invitation, resp, err
}
// Update updates the current invitation
func (s *InvitationServiceOp) Delete(id string) (*Response, error) {
opts := &GetOptions{}
endpointPath := path.Join(invitationsBasePath, id)
apiPathQuery := opts.WithQuery(endpointPath)
return s.client.DoRequest("DELETE", apiPathQuery, nil, nil)
}
// Update updates the current invitation
func (s *InvitationServiceOp) Accept(id string, updateRequest *InvitationUpdateRequest) (*Invitation, *Response, error) {
opts := &GetOptions{}
endpointPath := path.Join(invitationsBasePath, id)
apiPathQuery := opts.WithQuery(endpointPath)
invitation := new(Invitation)
resp, err := s.client.DoRequest("PUT", apiPathQuery, updateRequest, invitation)
if err != nil {
return nil, resp, err
}
return invitation, resp, err
}
// Update updates the current invitation
func (s *InvitationServiceOp) Resend(id string) (*Invitation, *Response, error) {
opts := &GetOptions{}
endpointPath := path.Join(invitationsBasePath, id)
apiPathQuery := opts.WithQuery(endpointPath)
invitation := new(Invitation)
resp, err := s.client.DoRequest("POST", apiPathQuery, nil, invitation)
if err != nil {
return nil, resp, err
}
return invitation, resp, err
}