forked from influxdata/chronograf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabases.go
526 lines (446 loc) · 14 KB
/
databases.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package server
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/bouk/httprouter"
"github.com/influxdata/chronograf"
)
const (
limitQuery = "limit"
offsetQuery = "offset"
)
type dbLinks struct {
Self string `json:"self"` // Self link mapping to this resource
RPs string `json:"retentionPolicies"` // URL for retention policies for this database
Measurements string `json:"measurements"` // URL for measurements for this database
}
type dbResponse struct {
Name string `json:"name"` // a unique string identifier for the database
Duration string `json:"duration,omitempty"` // the duration (when creating a default retention policy)
Replication int32 `json:"replication,omitempty"` // the replication factor (when creating a default retention policy)
ShardDuration string `json:"shardDuration,omitempty"` // the shard duration (when creating a default retention policy)
RPs []rpResponse `json:"retentionPolicies"` // RPs are the retention policies for a database
Links dbLinks `json:"links"` // Links are URI locations related to the database
}
// newDBResponse creates the response for the /databases endpoint
func newDBResponse(srcID int, db string, rps []rpResponse) dbResponse {
base := "/chronograf/v1/sources"
return dbResponse{
Name: db,
RPs: rps,
Links: dbLinks{
Self: fmt.Sprintf("%s/%d/dbs/%s", base, srcID, db),
RPs: fmt.Sprintf("%s/%d/dbs/%s/rps", base, srcID, db),
Measurements: fmt.Sprintf("%s/%d/dbs/%s/measurements?limit=100&offset=0", base, srcID, db),
},
}
}
type dbsResponse struct {
Databases []dbResponse `json:"databases"`
}
type rpLinks struct {
Self string `json:"self"` // Self link mapping to this resource
}
type rpResponse struct {
Name string `json:"name"` // a unique string identifier for the retention policy
Duration string `json:"duration"` // the duration
Replication int32 `json:"replication"` // the replication factor
ShardDuration string `json:"shardDuration"` // the shard duration
Default bool `json:"isDefault"` // whether the RP should be the default
Links rpLinks `json:"links"` // Links are URI locations related to the database
}
// WithLinks adds links to an rpResponse in place
func (r *rpResponse) WithLinks(srcID int, db string) {
base := "/chronograf/v1/sources"
r.Links = rpLinks{
Self: fmt.Sprintf("%s/%d/dbs/%s/rps/%s", base, srcID, db, r.Name),
}
}
type rpsResponse struct {
RetentionPolicies []rpResponse `json:"retentionPolicies"`
}
type measurementLinks struct {
Self string `json:"self"`
First string `json:"first"`
Next string `json:"next,omitempty"`
Prev string `json:"prev,omitempty"`
}
func newMeasurementLinks(src int, db string, limit, offset int) measurementLinks {
base := "/chronograf/v1/sources"
res := measurementLinks{
Self: fmt.Sprintf("%s/%d/dbs/%s/measurements?limit=%d&offset=%d", base, src, db, limit, offset),
First: fmt.Sprintf("%s/%d/dbs/%s/measurements?limit=%d&offset=0", base, src, db, limit),
Next: fmt.Sprintf("%s/%d/dbs/%s/measurements?limit=%d&offset=%d", base, src, db, limit, offset+limit),
}
if offset-limit > 0 {
res.Prev = fmt.Sprintf("%s/%d/dbs/%s/measurements?limit=%d&offset=%d", base, src, db, limit, offset-limit)
}
return res
}
type measurementsResponse struct {
Measurements []chronograf.Measurement `json:"measurements"` // names of all measurements
Links measurementLinks `json:"links"` // Links are the URI locations for measurements pages
}
// GetDatabases queries the list of all databases for a source
func (h *Service) GetDatabases(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
srcID, err := paramID("id", r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
return
}
src, err := h.Store.Sources(ctx).Get(ctx, srcID)
if err != nil {
notFound(w, srcID, h.Logger)
return
}
dbsvc := h.Databases
if err = dbsvc.Connect(ctx, &src); err != nil {
msg := fmt.Sprintf("Unable to connect to source %d: %v", srcID, err)
Error(w, http.StatusBadRequest, msg, h.Logger)
return
}
databases, err := dbsvc.AllDB(ctx)
if err != nil {
Error(w, http.StatusBadRequest, err.Error(), h.Logger)
return
}
dbs := make([]dbResponse, 0, len(databases))
for _, d := range databases {
rps, err := h.allRPs(ctx, dbsvc, srcID, d.Name)
if err != nil {
// https://github.com/influxdata/chronograf/issues/5531
// ignore the database if it can be shown, but retention policies cannot be listed
h.Logger.
WithField("component", "server").
WithField("db", d.Name).
Error("Unable to get retention policies: ", err.Error())
continue
}
dbs = append(dbs, newDBResponse(srcID, d.Name, rps))
}
res := dbsResponse{
Databases: dbs,
}
encodeJSON(w, http.StatusOK, res, h.Logger)
}
// NewDatabase creates a new database within the datastore
func (h *Service) NewDatabase(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
srcID, err := paramID("id", r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
return
}
src, err := h.Store.Sources(ctx).Get(ctx, srcID)
if err != nil {
notFound(w, srcID, h.Logger)
return
}
dbsvc := h.Databases
if err = dbsvc.Connect(ctx, &src); err != nil {
msg := fmt.Sprintf("Unable to connect to source %d: %v", srcID, err)
Error(w, http.StatusBadRequest, msg, h.Logger)
return
}
postedDB := &chronograf.Database{}
if err := json.NewDecoder(r.Body).Decode(postedDB); err != nil {
invalidJSON(w, h.Logger)
return
}
if err := ValidDatabaseRequest(postedDB); err != nil {
invalidData(w, err, h.Logger)
return
}
database, err := dbsvc.CreateDB(ctx, postedDB)
if err != nil {
Error(w, http.StatusBadRequest, err.Error(), h.Logger)
return
}
rps, err := h.allRPs(ctx, dbsvc, srcID, database.Name)
if err != nil {
Error(w, http.StatusBadRequest, err.Error(), h.Logger)
return
}
res := newDBResponse(srcID, database.Name, rps)
encodeJSON(w, http.StatusCreated, res, h.Logger)
}
// DropDatabase removes a database from a data source
func (h *Service) DropDatabase(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
srcID, err := paramID("id", r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
return
}
src, err := h.Store.Sources(ctx).Get(ctx, srcID)
if err != nil {
notFound(w, srcID, h.Logger)
return
}
dbsvc := h.Databases
if err = dbsvc.Connect(ctx, &src); err != nil {
msg := fmt.Sprintf("Unable to connect to source %d: %v", srcID, err)
Error(w, http.StatusBadRequest, msg, h.Logger)
return
}
db := httprouter.GetParamFromContext(ctx, "db")
dropErr := dbsvc.DropDB(ctx, db)
if dropErr != nil {
Error(w, http.StatusBadRequest, dropErr.Error(), h.Logger)
return
}
w.WriteHeader(http.StatusNoContent)
}
// RetentionPolicies lists retention policies within a database
func (h *Service) RetentionPolicies(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
srcID, err := paramID("id", r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
return
}
src, err := h.Store.Sources(ctx).Get(ctx, srcID)
if err != nil {
notFound(w, srcID, h.Logger)
return
}
dbsvc := h.Databases
if err = dbsvc.Connect(ctx, &src); err != nil {
msg := fmt.Sprintf("Unable to connect to source %d: %v", srcID, err)
Error(w, http.StatusBadRequest, msg, h.Logger)
return
}
db := httprouter.GetParamFromContext(ctx, "db")
res, err := h.allRPs(ctx, dbsvc, srcID, db)
if err != nil {
msg := fmt.Sprintf("Unable to connect get RPs %d: %v", srcID, err)
Error(w, http.StatusBadRequest, msg, h.Logger)
return
}
encodeJSON(w, http.StatusOK, res, h.Logger)
}
func (h *Service) allRPs(ctx context.Context, dbsvc chronograf.Databases, srcID int, db string) ([]rpResponse, error) {
allRP, err := dbsvc.AllRP(ctx, db)
if err != nil {
return nil, err
}
rps := make([]rpResponse, len(allRP))
for i, rp := range allRP {
rp := rpResponse{
Name: rp.Name,
Duration: rp.Duration,
Replication: rp.Replication,
ShardDuration: rp.ShardDuration,
Default: rp.Default,
}
rp.WithLinks(srcID, db)
rps[i] = rp
}
return rps, nil
}
// NewRetentionPolicy creates a new retention policy for a database
func (h *Service) NewRetentionPolicy(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
srcID, err := paramID("id", r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
return
}
src, err := h.Store.Sources(ctx).Get(ctx, srcID)
if err != nil {
notFound(w, srcID, h.Logger)
return
}
dbsvc := h.Databases
if err = dbsvc.Connect(ctx, &src); err != nil {
msg := fmt.Sprintf("Unable to connect to source %d: %v", srcID, err)
Error(w, http.StatusBadRequest, msg, h.Logger)
return
}
postedRP := &chronograf.RetentionPolicy{}
if err := json.NewDecoder(r.Body).Decode(postedRP); err != nil {
invalidJSON(w, h.Logger)
return
}
if err := ValidRetentionPolicyRequest(postedRP); err != nil {
invalidData(w, err, h.Logger)
return
}
db := httprouter.GetParamFromContext(ctx, "db")
rp, err := dbsvc.CreateRP(ctx, db, postedRP)
if err != nil {
Error(w, http.StatusBadRequest, err.Error(), h.Logger)
return
}
res := rpResponse{
Name: rp.Name,
Duration: rp.Duration,
Replication: rp.Replication,
ShardDuration: rp.ShardDuration,
Default: rp.Default,
}
res.WithLinks(srcID, db)
encodeJSON(w, http.StatusCreated, res, h.Logger)
}
// UpdateRetentionPolicy modifies an existing retention policy for a database
func (h *Service) UpdateRetentionPolicy(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
srcID, err := paramID("id", r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
return
}
src, err := h.Store.Sources(ctx).Get(ctx, srcID)
if err != nil {
notFound(w, srcID, h.Logger)
return
}
dbsvc := h.Databases
if err = dbsvc.Connect(ctx, &src); err != nil {
msg := fmt.Sprintf("Unable to connect to source %d: %v", srcID, err)
Error(w, http.StatusBadRequest, msg, h.Logger)
return
}
postedRP := &chronograf.RetentionPolicy{}
if err := json.NewDecoder(r.Body).Decode(postedRP); err != nil {
invalidJSON(w, h.Logger)
return
}
if err := ValidRetentionPolicyRequest(postedRP); err != nil {
invalidData(w, err, h.Logger)
return
}
db := httprouter.GetParamFromContext(ctx, "db")
rp := httprouter.GetParamFromContext(ctx, "rp")
p, err := dbsvc.UpdateRP(ctx, db, rp, postedRP)
if err != nil {
Error(w, http.StatusBadRequest, err.Error(), h.Logger)
return
}
res := rpResponse{
Name: p.Name,
Duration: p.Duration,
Replication: p.Replication,
ShardDuration: p.ShardDuration,
Default: p.Default,
}
res.WithLinks(srcID, db)
encodeJSON(w, http.StatusCreated, res, h.Logger)
}
// DropRetentionPolicy removes a retention policy from a database
func (h *Service) DropRetentionPolicy(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
srcID, err := paramID("id", r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
return
}
src, err := h.Store.Sources(ctx).Get(ctx, srcID)
if err != nil {
notFound(w, srcID, h.Logger)
return
}
dbsvc := h.Databases
if err = dbsvc.Connect(ctx, &src); err != nil {
msg := fmt.Sprintf("Unable to connect to source %d: %v", srcID, err)
Error(w, http.StatusBadRequest, msg, h.Logger)
return
}
db := httprouter.GetParamFromContext(ctx, "db")
rp := httprouter.GetParamFromContext(ctx, "rp")
dropErr := dbsvc.DropRP(ctx, db, rp)
if dropErr != nil {
Error(w, http.StatusBadRequest, dropErr.Error(), h.Logger)
return
}
w.WriteHeader(http.StatusNoContent)
}
// Measurements lists measurements within a database
func (h *Service) Measurements(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
srcID, err := paramID("id", r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
return
}
limit, offset, err := validMeasurementQuery(r.URL.Query())
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
return
}
src, err := h.Store.Sources(ctx).Get(ctx, srcID)
if err != nil {
notFound(w, srcID, h.Logger)
return
}
dbsvc := h.Databases
if err = dbsvc.Connect(ctx, &src); err != nil {
msg := fmt.Sprintf("Unable to connect to source %d: %v", srcID, err)
Error(w, http.StatusBadRequest, msg, h.Logger)
return
}
db := httprouter.GetParamFromContext(ctx, "db")
measurements, err := dbsvc.GetMeasurements(ctx, db, limit, offset)
if err != nil {
msg := fmt.Sprintf("Unable to get measurements %d: %v", srcID, err)
Error(w, http.StatusBadRequest, msg, h.Logger)
return
}
res := measurementsResponse{
Measurements: measurements,
Links: newMeasurementLinks(srcID, db, limit, offset),
}
encodeJSON(w, http.StatusOK, res, h.Logger)
}
func validMeasurementQuery(query url.Values) (limit, offset int, err error) {
limitParam := query.Get(limitQuery)
if limitParam == "" {
limit = 100
} else {
limit, err = strconv.Atoi(limitParam)
if err != nil {
return
}
if limit <= 0 {
limit = 100
}
}
offsetParam := query.Get(offsetQuery)
if offsetParam == "" {
offset = 0
} else {
offset, err = strconv.Atoi(offsetParam)
if err != nil {
return
}
if offset < 0 {
offset = 0
}
}
return
}
// ValidDatabaseRequest checks if the database posted is valid
func ValidDatabaseRequest(d *chronograf.Database) error {
if len(d.Name) == 0 {
return fmt.Errorf("name is required")
}
return nil
}
// ValidRetentionPolicyRequest checks if a retention policy is valid on POST
func ValidRetentionPolicyRequest(rp *chronograf.RetentionPolicy) error {
if len(rp.Name) == 0 {
return fmt.Errorf("name is required")
}
if len(rp.Duration) == 0 {
return fmt.Errorf("duration is required")
}
if rp.Replication == 0 {
return fmt.Errorf("replication factor is invalid")
}
return nil
}