-
Notifications
You must be signed in to change notification settings - Fork 29
/
project_user.go
185 lines (158 loc) · 5.59 KB
/
project_user.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package aiven
import (
"context"
"fmt"
"time"
)
type (
// ProjectUser represents a user who has accepted membership in a project
ProjectUser struct {
Email string `json:"user_email"`
RealName string `json:"real_name"`
MemberType string `json:"member_type"`
TeamId string `json:"team_id"`
TeamName string `json:"team_name"`
BillingContact bool `json:"billing_contact"`
AuthMethods []string `json:"auth"`
CreateTime *time.Time `json:"create_time"`
}
// ProjectInvitation represents a user who has been invited to join a project but has
// not yet accepted the invitation
ProjectInvitation struct {
UserEmail string `json:"invited_user_email"`
InvitingUserEmail string `json:"inviting_user_email"`
MemberType string `json:"member_type"`
InviteTime *time.Time `json:"invite_time"`
}
// ProjectUsersHandler is the client that interacts with project User and
// Invitation API endpoints on Aiven
ProjectUsersHandler struct {
client *Client
}
// CreateProjectInvitationRequest are the parameters to invite a user to a project
CreateProjectInvitationRequest struct {
UserEmail string `json:"user_email"`
MemberType string `json:"member_type"`
}
// UpdateProjectUserOrInvitationRequest are the parameters to update project user or invitation
UpdateProjectUserOrInvitationRequest struct {
MemberType string `json:"member_type"`
}
// ProjectInvitationsAndUsersListResponse represents the response from Aiven for
// listing project invitations and members.
ProjectInvitationsAndUsersListResponse struct {
APIResponse
ProjectInvitations []*ProjectInvitation `json:"invitations"`
ProjectUsers []*ProjectUser `json:"users"`
}
)
// Invite user to join a project on Aiven.
func (h *ProjectUsersHandler) Invite(ctx context.Context, project string, req CreateProjectInvitationRequest) error {
path := buildPath("project", project, "invite")
_, err := h.client.doPostRequest(ctx, path, req)
return err
}
// Get a specific project user or project invitation.
func (h *ProjectUsersHandler) Get(ctx context.Context, project, email string) (*ProjectUser, *ProjectInvitation, error) {
// There's no API for getting integration endpoint by ID. List all endpoints
// and pick the correct one instead. (There shouldn't ever be many endpoints.)
users, invitations, err := h.List(ctx, project)
if err != nil {
return nil, nil, err
}
for _, user := range users {
if user.Email == email && user.TeamId == "" {
return user, nil, nil
}
}
for _, invitation := range invitations {
if invitation.UserEmail == email {
return nil, invitation, nil
}
}
err = Error{Message: fmt.Sprintf("User / invitation with email %v not found", email), Status: 404}
return nil, nil, err
}
// UpdateUser updates the given project user with the given parameters.
func (h *ProjectUsersHandler) UpdateUser(
ctx context.Context,
project string,
email string,
req UpdateProjectUserOrInvitationRequest,
) error {
path := buildPath("project", project, "user", email)
_, err := h.client.doPutRequest(ctx, path, req)
return err
}
// UpdateInvitation updates the given project member with the given parameters.
// NB: The server does not support updating invitations so this is implemented as delete + create
func (h *ProjectUsersHandler) UpdateInvitation(
ctx context.Context,
project string,
email string,
req UpdateProjectUserOrInvitationRequest,
) error {
err := h.DeleteInvitation(ctx, project, email)
if err != nil {
return err
}
return h.Invite(ctx, project, CreateProjectInvitationRequest{UserEmail: email, MemberType: req.MemberType})
}
// UpdateUserOrInvitation updates either a user if the given email address is associated with a
// project member or project invitation if it isn't
func (h *ProjectUsersHandler) UpdateUserOrInvitation(
ctx context.Context,
project string,
email string,
req UpdateProjectUserOrInvitationRequest,
) error {
err := h.UpdateUser(ctx, project, email, req)
if err == nil {
return nil
}
if IsNotFound(err) {
return h.UpdateInvitation(ctx, project, email, req)
}
return err
}
// DeleteInvitation deletes the given project invitation from Aiven.
func (h *ProjectUsersHandler) DeleteInvitation(ctx context.Context, project, email string) error {
path := buildPath("project", project, "invite", email)
bts, err := h.client.doDeleteRequest(ctx, path, nil)
if err != nil {
return err
}
return checkAPIResponse(bts, nil)
}
// DeleteUser deletes the given project user from Aiven.
func (h *ProjectUsersHandler) DeleteUser(ctx context.Context, project, email string) error {
path := buildPath("project", project, "user", email)
bts, err := h.client.doDeleteRequest(ctx, path, nil)
if err != nil {
return err
}
return checkAPIResponse(bts, nil)
}
// DeleteUserOrInvitation deletes a user or a project invitation, whichever the email
// address is associated with
func (h *ProjectUsersHandler) DeleteUserOrInvitation(ctx context.Context, project, email string) error {
err := h.DeleteUser(ctx, project, email)
if err == nil {
return nil
}
if IsNotFound(err) {
return h.DeleteInvitation(ctx, project, email)
}
return err
}
// List all users and invitations for a given project.
func (h *ProjectUsersHandler) List(ctx context.Context, project string) ([]*ProjectUser, []*ProjectInvitation, error) {
path := buildPath("project", project, "users")
rsp, err := h.client.doGetRequest(ctx, path, nil)
if err != nil {
return nil, nil, err
}
var r ProjectInvitationsAndUsersListResponse
errR := checkAPIResponse(rsp, &r)
return r.ProjectUsers, r.ProjectInvitations, errR
}