-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
307 lines (242 loc) · 8.52 KB
/
group.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package turso
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
)
const (
groupEndpoint = "v1/organizations/%s/groups"
locationEndpoint = groupEndpoint + "%s/locations/%s"
)
// GroupService is the interface for the Turso API group endpoint
type GroupService service
type groupService interface {
// ListGroups lists all groups in the organization
ListGroups(ctx context.Context) (*ListGroupResponse, error)
// CreateGroup creates a new group in the organization
CreateGroup(ctx context.Context, req CreateGroupRequest) (*CreateGroupResponse, error)
// GetGroup gets a group by name
GetGroup(ctx context.Context, groupName string) (*GetGroupResponse, error)
// DeleteGroup deletes a group by name
DeleteGroup(ctx context.Context, groupName string) (*DeleteGroupResponse, error)
// AddLocation adds a location to a group
AddLocation(ctx context.Context, eq GroupLocationRequest) (*GroupLocationResponse, error)
// RemoveLocation removes a location from a group
RemoveLocation(ctx context.Context, req GroupLocationRequest) (*GroupLocationResponse, error)
}
// Group is the struct for the Turso API group service
type Group struct {
Archived bool `json:"archived"`
Locations []string `json:"locations"`
Name string `json:"name"`
Primary string `json:"primary"`
UUID string `json:"uuid"`
Version string `json:"version"`
}
// ListGroupResponse is the struct for the Turso API group list response
type ListGroupResponse struct {
Groups []Group `json:"groups"`
}
// GetGroupResponse is the struct for the Turso API group get response
type GetGroupResponse struct {
Group Group `json:"group"`
}
// CreateGroupResponse is the struct for the Turso API group create response
type CreateGroupResponse struct {
Group Group `json:"group"`
}
// GroupLocationRequest is the struct for the Turso API to add or remove a location to a group request
type GroupLocationRequest struct {
// GroupName is the name of the group to add the location
GroupName string
// Location is the location to add to the group
Location string
}
// GroupLocationResponse is the struct for the Turso API to add or remove location to group response
type GroupLocationResponse struct {
Group Group `json:"group"`
}
// DeleteGroupResponse is the struct for the Turso API group delete response
type DeleteGroupResponse struct {
Group Group `json:"group"`
}
// CreateGroupRequest is the struct for the Turso API group create request
type CreateGroupRequest struct {
Extensions string `json:"extensions"`
Location string `json:"location"`
Name string `json:"name"`
}
// getGroupEndpoint returns the endpoint for the Turso API group service
func getGroupEndpoint(baseURL, orgName string) string {
groupEndpoint := fmt.Sprintf(groupEndpoint, orgName)
return fmt.Sprintf("%s/%s", baseURL, groupEndpoint)
}
// getGroupLocationsEndpoint returns the endpoint for the Turso API group locations service
func getGroupLocationsEndpoint(baseURL, orgName, groupName, locationName string) string {
locEndpoint := fmt.Sprintf(locationEndpoint, orgName, groupName, locationName)
return fmt.Sprintf("%s/%s", baseURL, locEndpoint)
}
// ListGroups satisfies the groupService interface
func (s *GroupService) ListGroups(ctx context.Context) (*ListGroupResponse, error) {
endpoint := getGroupEndpoint(s.client.cfg.BaseURL, s.client.cfg.OrgName)
resp, err := s.client.DoRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var out ListGroupResponse
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, newBadRequestError("groups", "listing", resp.StatusCode)
}
return &out, nil
}
// CreateGroup satisfies the groupService interface
func (s *GroupService) CreateGroup(ctx context.Context, group CreateGroupRequest) (*CreateGroupResponse, error) {
// Validate the request
if err := validateGroupCreateRequest(group); err != nil {
return nil, err
}
// Create the group
endpoint := getGroupEndpoint(s.client.cfg.BaseURL, s.client.cfg.OrgName)
resp, err := s.client.DoRequest(ctx, http.MethodPost, endpoint, group)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Decode the response
var out CreateGroupResponse
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, newBadRequestError("group", "creating", resp.StatusCode)
}
return &out, nil
}
// GetGroup satisfies the groupService interface
func (s *GroupService) GetGroup(ctx context.Context, groupName string) (*GetGroupResponse, error) {
// get endpoint and append the group name
endpoint := getGroupEndpoint(s.client.cfg.BaseURL, s.client.cfg.OrgName)
endpoint = fmt.Sprintf("%s/%s", endpoint, groupName)
resp, err := s.client.DoRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var out *GetGroupResponse
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, newBadRequestError("group", "getting", resp.StatusCode)
}
return out, nil
}
// DeleteGroup satisfies the groupService interface
func (s *GroupService) DeleteGroup(ctx context.Context, groupName string) (*DeleteGroupResponse, error) {
// Create the group
endpoint := getGroupEndpoint(s.client.cfg.BaseURL, s.client.cfg.OrgName)
endpoint = fmt.Sprintf("%s/%s", endpoint, groupName)
resp, err := s.client.DoRequest(ctx, http.MethodDelete, endpoint, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Decode the response
var out DeleteGroupResponse
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, newBadRequestError("group", "deleting", resp.StatusCode)
}
return &out, nil
}
// AddLocation satisfies the groupService interface
func (s *GroupService) AddLocation(ctx context.Context, req GroupLocationRequest) (*GroupLocationResponse, error) {
if err := validateLocationRequest(req); err != nil {
return nil, err
}
endpoint := getGroupLocationsEndpoint(s.client.cfg.BaseURL, s.client.cfg.OrgName, req.GroupName, req.Location)
resp, err := s.client.DoRequest(ctx, http.MethodPost, endpoint, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Decode the response
var out GroupLocationResponse
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, newBadRequestError("group", "creating", resp.StatusCode)
}
return &out, nil
}
// RemoveLocation satisfies the groupService interface
func (s *GroupService) RemoveLocation(ctx context.Context, req GroupLocationRequest) (*GroupLocationResponse, error) {
if err := validateLocationRequest(req); err != nil {
return nil, err
}
endpoint := getGroupLocationsEndpoint(s.client.cfg.BaseURL, s.client.cfg.OrgName, req.GroupName, req.Location)
resp, err := s.client.DoRequest(ctx, http.MethodDelete, endpoint, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Decode the response
var out GroupLocationResponse
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, newBadRequestError("group", "creating", resp.StatusCode)
}
return &out, nil
}
// validateGroupCreateRequest validates the group create request
func validateGroupCreateRequest(req CreateGroupRequest) error {
if err := validateGroupName(req.Name); err != nil {
return err
}
if err := validateLocation(req.Location); err != nil {
return err
}
return nil
}
// validateGroupName validates the group name
func validateGroupName(groupName string) error {
if groupName == "" {
return newMissingRequiredFieldError("name")
}
if strings.Contains(groupName, " ") {
return newInvalidFieldError("name", "spaces are not allowed")
}
return nil
}
// validateGroupName validates the group name
func validateLocation(location string) error {
if location == "" {
return newMissingRequiredFieldError("location")
}
// all Turso locations are 3 characters
if len(location) != 3 { // nolint:mnd
return newInvalidFieldError("location", "must be 3 characters")
}
return nil
}
// validateLocationRequest validates the group location request fields
func validateLocationRequest(req GroupLocationRequest) error {
if req.GroupName == "" {
return newMissingRequiredFieldError("name")
}
if req.Location == "" {
return newMissingRequiredFieldError("location")
}
return nil
}