-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
http_server_dbrp.go
361 lines (317 loc) · 8.19 KB
/
http_server_dbrp.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package dbrp
import (
"encoding/json"
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/kit/platform/errors"
kithttp "github.com/influxdata/influxdb/v2/kit/transport/http"
"go.uber.org/zap"
)
const (
PrefixDBRP = "/api/v2/dbrps"
)
type Handler struct {
chi.Router
api *kithttp.API
log *zap.Logger
dbrpSvc influxdb.DBRPMappingService
orgSvc influxdb.OrganizationService
}
// NewHTTPHandler constructs a new http server.
func NewHTTPHandler(log *zap.Logger, dbrpSvc influxdb.DBRPMappingService, orgSvc influxdb.OrganizationService) *Handler {
h := &Handler{
api: kithttp.NewAPI(kithttp.WithLog(log)),
log: log,
dbrpSvc: dbrpSvc,
orgSvc: orgSvc,
}
r := chi.NewRouter()
r.Use(
middleware.Recoverer,
middleware.RequestID,
middleware.RealIP,
)
r.Route("/", func(r chi.Router) {
r.Post("/", h.handlePostDBRP)
r.Get("/", h.handleGetDBRPs)
r.Route("/{id}", func(r chi.Router) {
r.Get("/", h.handleGetDBRP)
r.Patch("/", h.handlePatchDBRP)
r.Delete("/", h.handleDeleteDBRP)
})
})
h.Router = r
return h
}
type createDBRPRequest struct {
Database string `json:"database"`
RetentionPolicy string `json:"retention_policy"`
Default bool `json:"default"`
Org string `json:"org"`
// N.B. These are purposefully typed as string instead of
// influxdb.ID so we can provide more specific error messages.
// If they have the ID type, our JSON decoder will just return
// a generic "invalid ID" error without stating which ID is
// the problem.
//
// Ideally we'd fix the decoder so we could get more useful
// errors everywhere, but I'm worried about the impact of a
// system-wide change to our "invalid ID" error format.
OrganizationID string `json:"orgID"`
BucketID string `json:"bucketID"`
}
func (h *Handler) handlePostDBRP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var req createDBRPRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
h.api.Err(w, r, &errors.Error{
Code: errors.EInvalid,
Msg: "invalid json structure",
Err: err,
})
return
}
var orgID platform.ID
var bucketID platform.ID
if req.OrganizationID == "" {
if req.Org == "" {
h.api.Err(w, r, ErrNoOrgProvided)
return
}
org, err := h.orgSvc.FindOrganization(r.Context(), influxdb.OrganizationFilter{
Name: &req.Org,
})
if err != nil {
h.api.Err(w, r, ErrOrgNotFound(req.Org))
return
}
orgID = org.ID
} else if err := orgID.DecodeFromString(req.OrganizationID); err != nil {
h.api.Err(w, r, ErrInvalidOrgID(req.OrganizationID, err))
return
}
if err := bucketID.DecodeFromString(req.BucketID); err != nil {
h.api.Err(w, r, ErrInvalidBucketID(req.BucketID, err))
return
}
dbrp := &influxdb.DBRPMapping{
Database: req.Database,
RetentionPolicy: req.RetentionPolicy,
Default: req.Default,
OrganizationID: orgID,
BucketID: bucketID,
}
if err := h.dbrpSvc.Create(ctx, dbrp); err != nil {
h.api.Err(w, r, err)
return
}
h.api.Respond(w, r, http.StatusCreated, dbrp)
}
type getDBRPsResponse struct {
Content []*influxdb.DBRPMapping `json:"content"`
}
func (h *Handler) handleGetDBRPs(w http.ResponseWriter, r *http.Request) {
filter, err := h.getFilterFromHTTPRequest(r)
if err != nil {
h.api.Err(w, r, err)
return
}
dbrps, _, err := h.dbrpSvc.FindMany(r.Context(), filter)
if err != nil {
h.api.Err(w, r, err)
return
}
h.api.Respond(w, r, http.StatusOK, getDBRPsResponse{
Content: dbrps,
})
}
type getDBRPResponse struct {
Content *influxdb.DBRPMapping `json:"content"`
}
func (h *Handler) handleGetDBRP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
id := chi.URLParam(r, "id")
if id == "" {
h.api.Err(w, r, &errors.Error{
Code: errors.EInvalid,
Msg: "url missing id",
})
return
}
var i platform.ID
if err := i.DecodeFromString(id); err != nil {
h.api.Err(w, r, err)
return
}
orgID, err := h.mustGetOrgIDFromHTTPRequest(r)
if err != nil {
h.api.Err(w, r, err)
return
}
dbrp, err := h.dbrpSvc.FindByID(ctx, *orgID, i)
if err != nil {
h.api.Err(w, r, err)
return
}
h.api.Respond(w, r, http.StatusOK, getDBRPResponse{
Content: dbrp,
})
}
func (h *Handler) handlePatchDBRP(w http.ResponseWriter, r *http.Request) {
bodyRequest := struct {
Default *bool `json:"default"`
RetentionPolicy *string `json:"retention_policy"`
}{}
ctx := r.Context()
id := chi.URLParam(r, "id")
if id == "" {
h.api.Err(w, r, &errors.Error{
Code: errors.EInvalid,
Msg: "url missing id",
})
return
}
var i platform.ID
if err := i.DecodeFromString(id); err != nil {
h.api.Err(w, r, err)
return
}
orgID, err := h.mustGetOrgIDFromHTTPRequest(r)
if err != nil {
h.api.Err(w, r, err)
return
}
dbrp, err := h.dbrpSvc.FindByID(ctx, *orgID, i)
if err != nil {
h.api.Err(w, r, err)
return
}
if err := json.NewDecoder(r.Body).Decode(&bodyRequest); err != nil {
h.api.Err(w, r, &errors.Error{
Code: errors.EInvalid,
Msg: "invalid json structure",
Err: err,
})
return
}
if bodyRequest.Default != nil && dbrp.Default != *bodyRequest.Default {
dbrp.Default = *bodyRequest.Default
}
if bodyRequest.RetentionPolicy != nil && *bodyRequest.RetentionPolicy != dbrp.RetentionPolicy {
dbrp.RetentionPolicy = *bodyRequest.RetentionPolicy
}
if err := h.dbrpSvc.Update(ctx, dbrp); err != nil {
h.api.Err(w, r, err)
return
}
h.api.Respond(w, r, http.StatusOK, struct {
Content *influxdb.DBRPMapping `json:"content"`
}{
Content: dbrp,
})
}
func (h *Handler) handleDeleteDBRP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
id := chi.URLParam(r, "id")
if id == "" {
h.api.Err(w, r, &errors.Error{
Code: errors.EInvalid,
Msg: "url missing id",
})
return
}
var i platform.ID
if err := i.DecodeFromString(id); err != nil {
h.api.Err(w, r, err)
return
}
orgID, err := h.mustGetOrgIDFromHTTPRequest(r)
if err != nil {
h.api.Err(w, r, err)
return
}
if err := h.dbrpSvc.Delete(ctx, *orgID, i); err != nil {
h.api.Err(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (h *Handler) getFilterFromHTTPRequest(r *http.Request) (f influxdb.DBRPMappingFilter, err error) {
// Always provide OrgID.
f.OrgID, err = h.mustGetOrgIDFromHTTPRequest(r)
if err != nil {
return f, err
}
f.ID, err = getDBRPIDFromHTTPRequest(r)
if err != nil {
return f, err
}
f.BucketID, err = getBucketIDFromHTTPRequest(r)
if err != nil {
return f, err
}
rawDB := r.URL.Query().Get("db")
if rawDB != "" {
f.Database = &rawDB
}
rawRP := r.URL.Query().Get("rp")
if rawRP != "" {
f.RetentionPolicy = &rawRP
}
rawDefault := r.URL.Query().Get("default")
if rawDefault != "" {
d, err := strconv.ParseBool(rawDefault)
if err != nil {
return f, &errors.Error{
Code: errors.EInvalid,
Msg: "invalid default parameter",
}
}
f.Default = &d
}
return f, nil
}
func getIDFromHTTPRequest(r *http.Request, key string, onErr func(string, error) error) (*platform.ID, error) {
var id platform.ID
raw := r.URL.Query().Get(key)
if raw != "" {
if err := id.DecodeFromString(raw); err != nil {
return nil, onErr(raw, err)
}
} else {
return nil, nil
}
return &id, nil
}
// mustGetOrgIDFromHTTPRequest returns the org ID parameter from the request, falling
// back to looking up the org ID by org name if the ID parameter is not present.
func (h *Handler) mustGetOrgIDFromHTTPRequest(r *http.Request) (*platform.ID, error) {
orgID, err := getIDFromHTTPRequest(r, "orgID", ErrInvalidOrgID)
if err != nil {
return nil, err
}
if orgID == nil {
name := r.URL.Query().Get("org")
if name == "" {
return nil, ErrNoOrgProvided
}
org, err := h.orgSvc.FindOrganization(r.Context(), influxdb.OrganizationFilter{
Name: &name,
})
if err != nil {
return nil, ErrOrgNotFound(name)
}
orgID = &org.ID
}
return orgID, nil
}
func getDBRPIDFromHTTPRequest(r *http.Request) (*platform.ID, error) {
return getIDFromHTTPRequest(r, "id", ErrInvalidDBRPID)
}
func getBucketIDFromHTTPRequest(r *http.Request) (*platform.ID, error) {
return getIDFromHTTPRequest(r, "bucketID", ErrInvalidBucketID)
}