forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
733 lines (628 loc) · 19.8 KB
/
api.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
package http
import (
log "code.google.com/p/log4go"
"common"
"coordinator"
"encoding/json"
"engine"
"fmt"
"github.com/bmizerany/pat"
"io/ioutil"
"net"
libhttp "net/http"
"protocol"
"strings"
)
type HttpServer struct {
conn net.Listener
httpAddr string
engine engine.EngineI
coordinator coordinator.Coordinator
userManager coordinator.UserManager
shutdown chan bool
}
func NewHttpServer(httpAddr string, theEngine engine.EngineI, theCoordinator coordinator.Coordinator, userManager coordinator.UserManager) *HttpServer {
self := &HttpServer{}
self.httpAddr = httpAddr
self.engine = theEngine
self.coordinator = theCoordinator
self.userManager = userManager
self.shutdown = make(chan bool)
return self
}
func (self *HttpServer) ListenAndServe() {
conn, err := net.Listen("tcp", self.httpAddr)
if err != nil {
log.Error("Listen: ", err)
}
self.Serve(conn)
}
func (self *HttpServer) registerEndpoint(p *pat.PatternServeMux, method string, pattern string, f libhttp.HandlerFunc) {
switch method {
case "get":
p.Get(pattern, CorsHeaderHandler(f))
case "post":
p.Post(pattern, CorsHeaderHandler(f))
case "del":
p.Del(pattern, CorsHeaderHandler(f))
}
p.Options(pattern, CorsHeaderHandler(self.sendCrossOriginHeader))
}
func (self *HttpServer) Serve(listener net.Listener) {
self.conn = listener
p := pat.New()
// Run the given query and return an array of series or a chunked response
// with each batch of points we get back
self.registerEndpoint(p, "get", "/db/:db/series", self.query)
// Write points to the given database
self.registerEndpoint(p, "post", "/db/:db/series", self.writePoints)
self.registerEndpoint(p, "get", "/dbs", self.listDatabases)
self.registerEndpoint(p, "post", "/db", self.createDatabase)
self.registerEndpoint(p, "del", "/db/:name", self.dropDatabase)
// cluster admins management interface
self.registerEndpoint(p, "get", "/cluster_admins", self.listClusterAdmins)
self.registerEndpoint(p, "get", "/cluster_admins/authenticate", self.authenticateClusterAdmin)
self.registerEndpoint(p, "post", "/cluster_admins", self.createClusterAdmin)
self.registerEndpoint(p, "post", "/cluster_admins/:user", self.updateClusterAdmin)
self.registerEndpoint(p, "del", "/cluster_admins/:user", self.deleteClusterAdmin)
// db users management interface
self.registerEndpoint(p, "get", "/db/:db/authenticate", self.authenticateDbUser)
self.registerEndpoint(p, "get", "/db/:db/users", self.listDbUsers)
self.registerEndpoint(p, "post", "/db/:db/users", self.createDbUser)
self.registerEndpoint(p, "del", "/db/:db/users/:user", self.deleteDbUser)
self.registerEndpoint(p, "post", "/db/:db/users/:user", self.updateDbUser)
self.registerEndpoint(p, "post", "/db/:db/admins/:user", self.setDbAdmin)
self.registerEndpoint(p, "del", "/db/:db/admins/:user", self.unsetDbAdmin)
if err := libhttp.Serve(listener, p); err != nil && !strings.Contains(err.Error(), "closed network") {
panic(err)
}
self.shutdown <- true
}
func (self *HttpServer) Close() {
log.Info("Closing http server")
self.conn.Close()
log.Info("Waiting for all requests to finish before killing the process")
<-self.shutdown
}
type Writer interface {
yield(*protocol.Series) error
done()
}
type AllPointsWriter struct {
memSeries map[string]*protocol.Series
w libhttp.ResponseWriter
precision TimePrecision
}
func (self *AllPointsWriter) yield(series *protocol.Series) error {
oldSeries := self.memSeries[*series.Name]
if oldSeries == nil {
self.memSeries[*series.Name] = series
return nil
}
oldSeries.Points = append(oldSeries.Points, series.Points...)
return nil
}
func (self *AllPointsWriter) done() {
data, err := serializeMultipleSeries(self.memSeries, self.precision)
if err != nil {
self.w.Write([]byte(err.Error()))
self.w.WriteHeader(libhttp.StatusInternalServerError)
return
}
self.w.Write(data)
self.w.WriteHeader(libhttp.StatusOK)
}
type ChunkWriter struct {
w libhttp.ResponseWriter
precision TimePrecision
}
func (self *ChunkWriter) yield(series *protocol.Series) error {
data, err := serializeSingleSeries(series, self.precision)
if err != nil {
return err
}
self.w.Write(data)
self.w.WriteHeader(libhttp.StatusOK)
self.w.(libhttp.Flusher).Flush()
return nil
}
func (self *ChunkWriter) done() {
}
type TimePrecision int
const (
MicrosecondPrecision TimePrecision = iota
MillisecondPrecision
SecondPrecision
)
func TimePrecisionFromString(s string) (TimePrecision, error) {
switch s {
case "u":
return MicrosecondPrecision, nil
case "m":
return MillisecondPrecision, nil
case "s":
return SecondPrecision, nil
case "":
return MillisecondPrecision, nil
}
return 0, fmt.Errorf("Unknown time precision %s", s)
}
func (self *HttpServer) sendCrossOriginHeader(w libhttp.ResponseWriter, r *libhttp.Request) {
w.WriteHeader(libhttp.StatusOK)
}
func (self *HttpServer) query(w libhttp.ResponseWriter, r *libhttp.Request) {
query := r.URL.Query().Get("q")
db := r.URL.Query().Get(":db")
statusCode, body := self.tryAsDbUser(w, r, func(user common.User) (int, interface{}) {
precision, err := TimePrecisionFromString(r.URL.Query().Get("time_precision"))
if err != nil {
w.WriteHeader(libhttp.StatusBadRequest)
w.Write([]byte(err.Error()))
}
var writer Writer
if r.URL.Query().Get("chunked") == "true" {
writer = &ChunkWriter{w, precision}
} else {
writer = &AllPointsWriter{map[string]*protocol.Series{}, w, precision}
}
err = self.engine.RunQuery(user, db, query, writer.yield)
if err != nil {
return libhttp.StatusInternalServerError, err.Error()
}
writer.done()
return libhttp.StatusOK, nil
})
w.WriteHeader(statusCode)
if len(body) > 0 {
w.Write(body)
}
}
func removeTimestampFieldDefinition(fields []string) []string {
timestampIdx := -1
for idx, field := range fields {
if field == "time" {
timestampIdx = idx
break
}
}
if timestampIdx == -1 {
return fields
}
fields[len(fields)-1], fields[timestampIdx] = fields[timestampIdx], fields[len(fields)-1]
return fields[:len(fields)-1]
}
func convertToDataStoreSeries(s *SerializedSeries, precision TimePrecision) (*protocol.Series, error) {
points := []*protocol.Point{}
for _, point := range s.Points {
values := []*protocol.FieldValue{}
var timestamp *int64
for idx, field := range s.Columns {
value := point[idx]
if field == "time" {
_timestamp := int64(value.(float64))
switch precision {
case SecondPrecision:
_timestamp *= 1000
fallthrough
case MillisecondPrecision:
_timestamp *= 1000
}
timestamp = &_timestamp
continue
}
switch v := value.(type) {
case string:
values = append(values, &protocol.FieldValue{StringValue: &v})
case float64:
if i := int64(v); float64(i) == v {
values = append(values, &protocol.FieldValue{Int64Value: &i})
} else {
values = append(values, &protocol.FieldValue{DoubleValue: &v})
}
continue
case bool:
values = append(values, &protocol.FieldValue{BoolValue: &v})
continue
default:
// if we reached this line then the dynamic type didn't match
return nil, fmt.Errorf("Unknown type %T", value)
}
}
points = append(points, &protocol.Point{
Values: values,
Timestamp: timestamp,
})
}
fields := removeTimestampFieldDefinition(s.Columns)
series := &protocol.Series{
Name: &s.Name,
Fields: fields,
Points: points,
}
return series, nil
}
func (self *HttpServer) writePoints(w libhttp.ResponseWriter, r *libhttp.Request) {
db := r.URL.Query().Get(":db")
precision, err := TimePrecisionFromString(r.URL.Query().Get("time_precision"))
if err != nil {
w.WriteHeader(libhttp.StatusBadRequest)
w.Write([]byte(err.Error()))
}
statusCode, body := self.tryAsDbUser(w, r, func(user common.User) (int, interface{}) {
series, err := ioutil.ReadAll(r.Body)
if err != nil {
return libhttp.StatusInternalServerError, err.Error()
}
serializedSeries := []*SerializedSeries{}
err = json.Unmarshal(series, &serializedSeries)
if err != nil {
return libhttp.StatusBadRequest, err.Error()
}
// convert the wire format to the internal representation of the time series
for _, s := range serializedSeries {
if len(s.Points) == 0 {
continue
}
series, err := convertToDataStoreSeries(s, precision)
if err != nil {
return libhttp.StatusBadRequest, err.Error()
}
err = self.coordinator.WriteSeriesData(user, db, series)
if err != nil {
return libhttp.StatusUnauthorized, err.Error()
}
}
return libhttp.StatusOK, nil
})
w.WriteHeader(statusCode)
if len(body) > 0 {
w.Write(body)
}
}
type createDatabaseRequest struct {
Name string `json:"name"`
ApiKey string `json:apiKey"`
}
type Database struct {
Name string `json:"name"`
}
func (self *HttpServer) listDatabases(w libhttp.ResponseWriter, r *libhttp.Request) {
self.tryAsClusterAdmin(w, r, func(u common.User) (int, interface{}) {
dbNames, err := self.coordinator.ListDatabases(u)
if err != nil {
return libhttp.StatusUnauthorized, err.Error()
}
databases := make([]*Database, 0, len(dbNames))
for _, db := range dbNames {
databases = append(databases, &Database{db})
}
body, err := json.Marshal(databases)
if err != nil {
return libhttp.StatusInternalServerError, err.Error()
}
return libhttp.StatusOK, body
})
}
func (self *HttpServer) createDatabase(w libhttp.ResponseWriter, r *libhttp.Request) {
self.tryAsClusterAdmin(w, r, func(user common.User) (int, interface{}) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return libhttp.StatusInternalServerError, err.Error()
}
createRequest := &createDatabaseRequest{}
err = json.Unmarshal(body, createRequest)
if err != nil {
return libhttp.StatusBadRequest, err.Error()
}
err = self.coordinator.CreateDatabase(user, createRequest.Name)
if err != nil {
return libhttp.StatusUnauthorized, err.Error()
}
return libhttp.StatusCreated, nil
})
}
func (self *HttpServer) dropDatabase(w libhttp.ResponseWriter, r *libhttp.Request) {
self.tryAsClusterAdmin(w, r, func(user common.User) (int, interface{}) {
name := r.URL.Query().Get(":name")
err := self.coordinator.DropDatabase(user, name)
if err != nil {
return libhttp.StatusBadRequest, err.Error()
}
return libhttp.StatusNoContent, nil
})
}
type Point struct {
Timestamp int64 `json:"timestamp"`
SequenceNumber uint32 `json:"sequenceNumber"`
Values []interface{} `json:"values"`
}
type SerializedSeries struct {
Name string `json:"name"`
Columns []string `json:"columns"`
Points [][]interface{} `json:"points"`
}
func serializeSingleSeries(series *protocol.Series, precision TimePrecision) ([]byte, error) {
arg := map[string]*protocol.Series{"": series}
return json.Marshal(serializeSeries(arg, precision)[0])
}
func serializeMultipleSeries(series map[string]*protocol.Series, precision TimePrecision) ([]byte, error) {
return json.Marshal(serializeSeries(series, precision))
}
func serializeSeries(memSeries map[string]*protocol.Series, precision TimePrecision) []*SerializedSeries {
serializedSeries := []*SerializedSeries{}
for _, series := range memSeries {
columns := []string{"time", "sequence_number"}
for _, field := range series.Fields {
columns = append(columns, field)
}
points := [][]interface{}{}
for _, row := range series.Points {
timestamp := *row.GetTimestampInMicroseconds()
switch precision {
case SecondPrecision:
timestamp /= 1000
fallthrough
case MillisecondPrecision:
timestamp /= 1000
}
rowValues := []interface{}{timestamp, *row.SequenceNumber}
for _, value := range row.Values {
if value != nil {
rowValues = append(rowValues, value.GetValue())
} else {
rowValues = append(rowValues, nil)
}
}
points = append(points, rowValues)
}
serializedSeries = append(serializedSeries, &SerializedSeries{
Name: *series.Name,
Columns: columns,
Points: points,
})
}
return serializedSeries
}
// // cluster admins management interface
func toBytes(body interface{}) ([]byte, error) {
if body == nil {
return nil, nil
}
switch x := body.(type) {
case string:
return []byte(x), nil
case []byte:
return x, nil
default:
return json.Marshal(body)
}
}
func yieldUser(user common.User, yield func(common.User) (int, interface{})) (int, []byte) {
statusCode, body := yield(user)
bodyContent, err := toBytes(body)
if err != nil {
return libhttp.StatusInternalServerError, []byte(err.Error())
}
return statusCode, bodyContent
}
func (self *HttpServer) tryAsClusterAdmin(w libhttp.ResponseWriter, r *libhttp.Request, yield func(common.User) (int, interface{})) {
username := r.URL.Query().Get("u")
password := r.URL.Query().Get("p")
if username == "" {
w.WriteHeader(libhttp.StatusUnauthorized)
w.Write([]byte("Invalid username/password"))
return
}
user, err := self.userManager.AuthenticateClusterAdmin(username, password)
if err != nil {
w.WriteHeader(libhttp.StatusUnauthorized)
w.Write([]byte(err.Error()))
return
}
statusCode, body := yieldUser(user, yield)
w.WriteHeader(statusCode)
if len(body) > 0 {
w.Write(body)
}
}
type NewUser struct {
Name string `json:"username"`
Password string `json:"password"`
}
type UpdateUser struct {
Password string `json:"password"`
}
type User struct {
Name string `json:"username"`
}
func (self *HttpServer) listClusterAdmins(w libhttp.ResponseWriter, r *libhttp.Request) {
self.tryAsClusterAdmin(w, r, func(u common.User) (int, interface{}) {
names, err := self.userManager.ListClusterAdmins(u)
if err != nil {
return libhttp.StatusUnauthorized, err.Error()
}
users := make([]*User, 0, len(names))
for _, name := range names {
users = append(users, &User{name})
}
return libhttp.StatusOK, users
})
}
func (self *HttpServer) authenticateClusterAdmin(w libhttp.ResponseWriter, r *libhttp.Request) {
self.tryAsClusterAdmin(w, r, func(u common.User) (int, interface{}) {
return libhttp.StatusOK, nil
})
}
func (self *HttpServer) createClusterAdmin(w libhttp.ResponseWriter, r *libhttp.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(libhttp.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
newUser := &NewUser{}
err = json.Unmarshal(body, newUser)
if err != nil {
w.WriteHeader(libhttp.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
self.tryAsClusterAdmin(w, r, func(u common.User) (int, interface{}) {
if err := self.userManager.CreateClusterAdminUser(u, newUser.Name); err != nil {
return libhttp.StatusUnauthorized, err.Error()
}
if err := self.userManager.ChangeClusterAdminPassword(u, newUser.Name, newUser.Password); err != nil {
return libhttp.StatusInternalServerError, err.Error()
}
return libhttp.StatusOK, nil
})
}
func (self *HttpServer) deleteClusterAdmin(w libhttp.ResponseWriter, r *libhttp.Request) {
newUser := r.URL.Query().Get(":user")
self.tryAsClusterAdmin(w, r, func(u common.User) (int, interface{}) {
if err := self.userManager.DeleteClusterAdminUser(u, newUser); err != nil {
return libhttp.StatusUnauthorized, err.Error()
}
return libhttp.StatusOK, nil
})
}
func (self *HttpServer) updateClusterAdmin(w libhttp.ResponseWriter, r *libhttp.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(libhttp.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
updateUser := &UpdateUser{}
json.Unmarshal(body, updateUser)
newUser := r.URL.Query().Get(":user")
self.tryAsClusterAdmin(w, r, func(u common.User) (int, interface{}) {
if err := self.userManager.ChangeClusterAdminPassword(u, newUser, updateUser.Password); err != nil {
return libhttp.StatusUnauthorized, err.Error()
}
return libhttp.StatusOK, nil
})
}
// // db users management interface
func (self *HttpServer) authenticateDbUser(w libhttp.ResponseWriter, r *libhttp.Request) {
code, body := self.tryAsDbUser(w, r, func(u common.User) (int, interface{}) {
return libhttp.StatusOK, nil
})
w.WriteHeader(code)
if len(body) > 0 {
w.Write(body)
}
}
func (self *HttpServer) tryAsDbUser(w libhttp.ResponseWriter, r *libhttp.Request, yield func(common.User) (int, interface{})) (int, []byte) {
username := r.URL.Query().Get("u")
password := r.URL.Query().Get("p")
db := r.URL.Query().Get(":db")
if username == "" {
return libhttp.StatusUnauthorized, []byte("Invalid username/password")
}
user, err := self.userManager.AuthenticateDbUser(db, username, password)
if err != nil {
return libhttp.StatusUnauthorized, []byte(err.Error())
}
return yieldUser(user, yield)
}
func (self *HttpServer) tryAsDbUserAndClusterAdmin(w libhttp.ResponseWriter, r *libhttp.Request, yield func(common.User) (int, interface{})) {
statusCode, body := self.tryAsDbUser(w, r, yield)
if statusCode == libhttp.StatusUnauthorized {
self.tryAsClusterAdmin(w, r, yield)
return
}
w.WriteHeader(statusCode)
if len(body) > 0 {
w.Write(body)
}
return
}
func (self *HttpServer) listDbUsers(w libhttp.ResponseWriter, r *libhttp.Request) {
db := r.URL.Query().Get(":db")
self.tryAsDbUserAndClusterAdmin(w, r, func(u common.User) (int, interface{}) {
names, err := self.userManager.ListDbUsers(u, db)
if err != nil {
return libhttp.StatusUnauthorized, err.Error()
}
users := make([]*User, 0, len(names))
for _, name := range names {
users = append(users, &User{name})
}
return libhttp.StatusOK, users
})
}
func (self *HttpServer) createDbUser(w libhttp.ResponseWriter, r *libhttp.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(libhttp.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
newUser := &NewUser{}
err = json.Unmarshal(body, newUser)
if err != nil {
w.WriteHeader(libhttp.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
db := r.URL.Query().Get(":db")
self.tryAsDbUserAndClusterAdmin(w, r, func(u common.User) (int, interface{}) {
if err := self.userManager.CreateDbUser(u, db, newUser.Name); err != nil {
return libhttp.StatusUnauthorized, err.Error()
}
if err := self.userManager.ChangeDbUserPassword(u, db, newUser.Name, newUser.Password); err != nil {
return libhttp.StatusUnauthorized, err.Error()
}
return libhttp.StatusOK, nil
})
}
func (self *HttpServer) deleteDbUser(w libhttp.ResponseWriter, r *libhttp.Request) {
newUser := r.URL.Query().Get(":user")
db := r.URL.Query().Get(":db")
self.tryAsDbUserAndClusterAdmin(w, r, func(u common.User) (int, interface{}) {
if err := self.userManager.DeleteDbUser(u, db, newUser); err != nil {
return libhttp.StatusUnauthorized, err.Error()
}
return libhttp.StatusOK, nil
})
}
func (self *HttpServer) updateDbUser(w libhttp.ResponseWriter, r *libhttp.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(libhttp.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
updateUser := &UpdateUser{}
err = json.Unmarshal(body, updateUser)
if err != nil {
w.WriteHeader(libhttp.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
newUser := r.URL.Query().Get(":user")
db := r.URL.Query().Get(":db")
self.tryAsDbUserAndClusterAdmin(w, r, func(u common.User) (int, interface{}) {
if err := self.userManager.ChangeDbUserPassword(u, db, newUser, updateUser.Password); err != nil {
return libhttp.StatusUnauthorized, err.Error()
}
return libhttp.StatusOK, nil
})
}
func (self *HttpServer) setDbAdmin(w libhttp.ResponseWriter, r *libhttp.Request) {
self.commonSetDbAdmin(w, r, true)
}
func (self *HttpServer) unsetDbAdmin(w libhttp.ResponseWriter, r *libhttp.Request) {
self.commonSetDbAdmin(w, r, false)
}
func (self *HttpServer) commonSetDbAdmin(w libhttp.ResponseWriter, r *libhttp.Request, isAdmin bool) {
newUser := r.URL.Query().Get(":user")
db := r.URL.Query().Get(":db")
self.tryAsDbUserAndClusterAdmin(w, r, func(u common.User) (int, interface{}) {
if err := self.userManager.SetDbAdmin(u, db, newUser, isAdmin); err != nil {
return libhttp.StatusUnauthorized, err.Error()
}
return libhttp.StatusOK, nil
})
}