-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutil.go
149 lines (129 loc) · 4.3 KB
/
util.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
package proxysql
// this file is for validation, and error specification
import (
"errors"
)
type vOpts func(*hostQuery) error
var (
ErrConfigBadTable = errors.New("Bad table value, must be one of 'mysql_servers', 'runtime_mysql_servers'")
ErrConfigBadHostgroupID = errors.New("Bad hostgroup value, must be in [0, 2147483648]")
ErrConfigBadPort = errors.New("Bad port value, must be in [0, 65535]")
ErrConfigBadMaxConnections = errors.New("Bad max_connections value, must be > 0")
ErrConfigBadStatus = errors.New("Bad status value, must be one of 'ONLINE','SHUNNED','OFFLINE_SOFT', 'OFFLINE_HARD'")
ErrConfigBadWeight = errors.New("Bad weight value, must be > 0")
ErrConfigBadCompression = errors.New("Bad compression value, must be in [0, 102400]")
ErrConfigBadMaxReplicationLag = errors.New("Bad max_replication_lag value, must be in [0, 126144000]")
ErrConfigBadUseSSL = errors.New("Bad use_ssl value, must be one of 0, 1")
ErrConfigBadMaxLatencyMS = errors.New("Bad max_latency_ms value, must be > 0")
ErrConfigDuplicateSpec = errors.New("Bad function call, a value was specified twice")
ErrConfigNoHostname = errors.New("Bad hostname, must not be empty")
validationFuncs []vOpts
)
func init() {
// add all validators to the validation array for validateHostQuery
validationFuncs = append(validationFuncs, validateTableOpts)
validationFuncs = append(validationFuncs, validateHostgroupID)
validationFuncs = append(validationFuncs, validatePort)
validationFuncs = append(validationFuncs, validateMaxConnections)
validationFuncs = append(validationFuncs, validateStatus)
validationFuncs = append(validationFuncs, validateWeight)
validationFuncs = append(validationFuncs, validateCompression)
validationFuncs = append(validationFuncs, validateMaxReplicationLag)
validationFuncs = append(validationFuncs, validateUseSSL)
validationFuncs = append(validationFuncs, validateMaxLatencyMS)
validationFuncs = append(validationFuncs, validateSpecifiedFields)
}
func validateTableOpts(opts *hostQuery) error {
return validateTable(opts.table)
}
func validateTable(t string) error {
if t != "mysql_servers" && t != "runtime_mysql_servers" {
return ErrConfigBadTable
}
return nil
}
func validateHostgroupID(opts *hostQuery) error {
if opts.host.hostgroup_id < 0 || opts.host.hostgroup_id > 2147483648 {
return ErrConfigBadHostgroupID
}
return nil
}
func validatePort(opts *hostQuery) error {
if opts.host.port < 0 || opts.host.port > 65535 {
return ErrConfigBadPort
}
return nil
}
func validateMaxConnections(opts *hostQuery) error {
if opts.host.max_connections < 0 {
return ErrConfigBadMaxConnections
}
return nil
}
func validateStatus(opts *hostQuery) error {
s := opts.host.status
if s != "ONLINE" && s != "SHUNNED" && s != "OFFLINE_SOFT" && s != "OFFLINE_HARD" {
return ErrConfigBadStatus
}
return nil
}
func validateWeight(opts *hostQuery) error {
if opts.host.weight < 0 {
return ErrConfigBadWeight
}
return nil
}
func validateCompression(opts *hostQuery) error {
c := opts.host.compression
if c < 0 || c > 102400 {
return ErrConfigBadCompression
}
return nil
}
func validateMaxReplicationLag(opts *hostQuery) error {
m := opts.host.max_replication_lag
if m < 0 || m > 126144000 {
return ErrConfigBadMaxReplicationLag
}
return nil
}
func validateUseSSL(opts *hostQuery) error {
u := opts.host.use_ssl
if u != 0 && u != 1 {
return ErrConfigBadUseSSL
}
return nil
}
func validateMaxLatencyMS(opts *hostQuery) error {
if opts.host.max_latency_ms < 0 {
return ErrConfigBadMaxLatencyMS
}
return nil
}
// returns ErrConfigDuplicateSpec if a duplicate occurs
func validateSpecifiedFields(opts *hostQuery) error {
encountered := make(map[string]struct{})
for _, field := range opts.specifiedFields {
if _, exists := encountered[field]; exists {
return ErrConfigDuplicateSpec
}
encountered[field] = struct{}{}
}
return nil
}
// This is called by functions that need a hostname
// it is not a default validation
func validateHostname(opts *hostQuery) error {
if opts.host.hostname == "" {
return ErrConfigNoHostname
}
return nil
}
func validateHostQuery(opts *hostQuery) error {
for _, validate := range validationFuncs {
if err := validate(opts); err != nil {
return err
}
}
return nil
}