forked from couchbase/cbft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_options.go
148 lines (127 loc) · 4.35 KB
/
rest_options.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
// Copyright (c) 2017 Couchbase, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an "AS
// IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language
// governing permissions and limitations under the License.
package cbft
import (
"fmt"
"net/http"
"strconv"
"github.com/couchbase/cbgt"
"github.com/couchbase/cbgt/rest"
log "github.com/couchbase/clog"
)
// List of log levels that maps strings to integers.
// (Works with clog - https://github.com/couchbase/clog)
var LogLevels map[string]uint32
func init() {
LogLevels = make(map[string]uint32)
LogLevels["DEBU"] = 0
LogLevels["INFO"] = 1
LogLevels["CRIT"] = 4
LogLevels["ERRO"] = 3
LogLevels["FATA"] = 4
LogLevels["WARN"] = 2
}
// ManagerOptionsExt is a REST handler that serves as a wrapper for
// ManagerOptions - where it sets the manager options, and updates
// the logLevel upon request.
type ManagerOptionsExt struct {
mgr *cbgt.Manager
mgrOptions *rest.ManagerOptions
}
func NewManagerOptionsExt(mgr *cbgt.Manager) *ManagerOptionsExt {
mgrOptions := rest.NewManagerOptions(mgr)
mgrOptions.Validate = func(options map[string]string) (map[string]string, error) {
// Validate logLevel
logLevelStr := options["logLevel"]
if logLevelStr != "" {
_, exists := LogLevels[logLevelStr]
if !exists {
return nil, fmt.Errorf("invalid setting for"+
" logLevel: %v", logLevelStr)
}
}
// Validate maxReplicasAllowed
if options["maxReplicasAllowed"] != mgr.Options()["maxReplicasAllowed"] {
return nil, fmt.Errorf("maxReplicasAllowed setting is at '%v',"+
" but request is for '%v'", mgr.Options()["maxReplicasAllowed"],
options["maxReplicasAllowed"])
}
// Validate bucketTypesAllowed
if options["bucketTypesAllowed"] != mgr.Options()["bucketTypesAllowed"] {
return nil, fmt.Errorf("bucketTypesAllowed setting is at '%v',"+
" but request is for: '%v'", mgr.Options()["bucketTypesAllowed"],
options["bucketTypesAllowed"])
}
// Validate gcMinThreshold
if options["gcMinThreshold"] != "" {
gcMinThreshold, err := strconv.Atoi(options["gcMinThreshold"])
if err != nil || gcMinThreshold < 0 {
return nil, fmt.Errorf("illegal value for gcMinThreshold: '%v'",
options["gcMinThreshold"])
}
}
// Validate gcTriggerPct
if options["gcTriggerPct"] != "" {
gcTriggerPct, err := strconv.Atoi(options["gcTriggerPct"])
if err != nil || gcTriggerPct < 0 {
return nil, fmt.Errorf("illegal value for gcTriggerPct: '%v'",
options["gcTriggerPct"])
}
}
// Validate memStatsLoggingInterval
if options["memStatsLoggingInterval"] != "" {
memStatsLoggingInterval, err := strconv.Atoi(options["memStatsLoggingInterval"])
if err != nil || memStatsLoggingInterval < 0 {
return nil, fmt.Errorf("illegal value for memStatsLoggingInterval: '%v'",
options["memStatsLoggingInterval"])
}
}
return options, nil
}
return &ManagerOptionsExt{
mgr: mgr,
mgrOptions: mgrOptions,
}
}
func (h *ManagerOptionsExt) ServeHTTP(
w http.ResponseWriter, req *http.Request) {
h.mgrOptions.ServeHTTP(w, req)
// Update log level if requested.
logLevelStr := h.mgr.Options()["logLevel"]
if logLevelStr != "" {
logLevel, _ := LogLevels[logLevelStr]
log.SetLevel(log.LogLevel(logLevel))
}
}
type ConciseOptions struct {
mgr *cbgt.Manager
}
func NewConciseOptions(mgr *cbgt.Manager) *ConciseOptions {
return &ConciseOptions{mgr: mgr}
}
func (h *ConciseOptions) ServeHTTP(
w http.ResponseWriter, req *http.Request) {
maxReplicasAllowed, _ := strconv.Atoi(h.mgr.Options()["maxReplicasAllowed"])
bucketTypesAllowed := h.mgr.Options()["bucketTypesAllowed"]
hideUI := h.mgr.Options()["hideUI"]
rv := struct {
Status string `json:"status"`
MaxReplicasAllowed int `json:"maxReplicasAllowed"`
BucketTypesAllowed string `json:"bucketTypesAllowed"`
HideUI string `json:"hideUI"`
}{
Status: "ok",
MaxReplicasAllowed: maxReplicasAllowed,
BucketTypesAllowed: bucketTypesAllowed,
HideUI: hideUI,
}
rest.MustEncode(w, rv)
}