This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
validators.go
232 lines (204 loc) · 6.81 KB
/
validators.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
package tencentcloud
import (
"fmt"
"net"
"regexp"
"strconv"
"strings"
"github.com/athom/goset"
"github.com/hashicorp/terraform/helper/schema"
)
func validateNameRegex(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if _, err := regexp.Compile(value); err != nil {
errors = append(errors, fmt.Errorf(
"%q contains an invalid regular expression: %s",
k, err))
}
return
}
func validateStorageType(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !goset.IsIncluded(availableStorageTypeFamilies, value) {
errors = append(errors, fmt.Errorf("Invalid storage type: %v. Valid choice: %s.", value, availableStorageTypeFamilies))
}
return
}
func validateStorageName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if value == "" {
errors = append(errors, fmt.Errorf("Storage name cannot be empty."))
}
if len(value) > MaxStorageNameLength {
errors = append(errors, fmt.Errorf("%q cannot be longer than %v characters", k, MaxStorageNameLength))
}
return
}
func validateNotEmpty(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) == 0 {
errors = append(errors, fmt.Errorf("should not use empty string"))
}
return
}
func validateInstanceType(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
words := strings.Split(value, ".")
if len(words) <= 1 {
errors = append(errors, fmt.Errorf("invalid instance_type: %v, should be like S1.SMALL1", value))
return
}
return
}
func validateInstanceChargeType(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !goset.IsIncluded(availableInstanceChargeTypes, value) {
errors = append(errors, fmt.Errorf("invalid instance_charge_type: %v", value))
}
return
}
func validateInstanceChargeTypePrePaidPeriod(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if !goset.IsIncluded(availableInstanceChargeTypePrePaidPeriodValues, value) {
errors = append(errors, fmt.Errorf("invalid instance_charge_type_prepaid_period: %v", value))
}
return
}
func validateInstanceChargeTypePrePaidRenewFlag(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !goset.IsIncluded(availableInstanceChargeTypePrePaidRenewFlagValues, value) {
errors = append(errors, fmt.Errorf("invalid instance_charge_type_prepaid_period: %v", value))
}
return
}
// validateCIDRNetworkAddress ensures that the string value is a valid CIDR that
// represents a network address - it adds an error otherwise
func validateCIDRNetworkAddress(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
_, ipnet, err := net.ParseCIDR(value)
if err != nil {
errors = append(errors, fmt.Errorf("%q must contain a valid CIDR, got error parsing: %s", k, err))
return
}
if ipnet == nil || value != ipnet.String() {
errors = append(errors, fmt.Errorf("%q must contain a valid network CIDR, expected %q, got %q", k, ipnet, value))
}
return
}
func validateIp(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
ip := net.ParseIP(value)
if ip == nil {
errors = append(errors, fmt.Errorf("%q must contain a valid IP", k))
}
return
}
func validateStoragePeriod(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if !goset.IsIncluded(availablePeriodValue, value) {
errors = append(errors, fmt.Errorf("Storage period out of range: %v.", availablePeriodValue))
}
return
}
func validateInternetChargeType(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !goset.IsIncluded(availableInternetChargeTypes, value) {
errors = append(errors, fmt.Errorf("invalid internet_charge_type: %v", value))
}
return
}
func validateInternetMaxBandwidthOut(v interface{}, k string) (ws []string, errors []error) {
return
}
func validateDiskType(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !goset.IsIncluded(availableDiskTypes, value) {
errors = append(errors, fmt.Errorf("invalid disk type: %v", value))
}
return
}
// NOTE not exactly strict, but ok for now
func validateIntegerInRange(min, max int) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value < min {
errors = append(errors, fmt.Errorf(
"%q cannot be lower than %d: %d", k, min, value))
}
if value > max {
errors = append(errors, fmt.Errorf(
"%q cannot be higher than %d: %d", k, max, value))
}
return
}
}
func validateStringLengthInRange(min, max int) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := len(v.(string))
if value < min {
errors = append(errors, fmt.Errorf(
"length of %q cannot be lower than %d: %d", k, min, value))
}
if value > max {
errors = append(errors, fmt.Errorf(
"length of %q cannot be higher than %d: %d", k, max, value))
}
return
}
}
func validateDiskSize(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value < 0 || value%10 != 0 {
errors = append(errors, fmt.Errorf("invalid data disk size: %v", value))
}
ws2, err2 := validateIntegerInRange(50, 16000)(v, k)
ws = append(ws, ws2...)
errors = append(errors, err2...)
return
}
func validateKeyPairName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 25 || len(value) == 0 {
errors = append(errors, fmt.Errorf("invalid key pair: %v, size too long or too short", value))
}
pattern := `^[a-zA-Z0-9_]+$`
if match, _ := regexp.Match(pattern, []byte(value)); !match {
errors = append(errors, fmt.Errorf("invalid key pair: %v, wrong format", value))
}
return
}
func validateInstanceName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) < 2 || len(value) > 60 {
errors = append(errors, fmt.Errorf("%q cannot be longer than 60 characters", k))
}
if strings.HasPrefix(value, "http://") || strings.HasPrefix(value, "https://") {
errors = append(errors, fmt.Errorf("%s cannot starts with http:// or https://", k))
}
return
}
func validateAllowedStringValue(ss []string) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !goset.IsIncluded(ss, value) {
errors = append(errors, fmt.Errorf("%q must contain a valid string value should in array %#v, got %q", k, ss, value))
}
return
}
}
func validatePort(v interface{}, k string) (ws []string, errors []error) {
value := 0
switch v.(type) {
case string:
value, _ = strconv.Atoi(v.(string))
case int:
value = v.(int)
default:
errors = append(errors, fmt.Errorf("%q data type error ", k))
return
}
if value < 1 || value > 65535 {
errors = append(errors, fmt.Errorf("%q must be a valid port between 1 and 65535", k))
}
return
}