forked from huaweicloud/terraform-provider-huaweicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators.go
276 lines (241 loc) · 7.24 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
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
package huaweicloud
import (
"fmt"
"net"
"regexp"
"strings"
"time"
)
func ValidateStringList(v interface{}, k string, l []string) (ws []string, errors []error) {
value := v.(string)
for i := range l {
if value == l[i] {
return
}
}
errors = append(errors, fmt.Errorf("%q must be one of %v", k, l))
return
}
func ValidateIntRange(v interface{}, k string, l int, h int) (ws []string, errors []error) {
i, ok := v.(int)
if !ok {
errors = append(errors, fmt.Errorf("%q must be an integer", k))
return
}
if i < l || i > h {
errors = append(errors, fmt.Errorf("%q must be between %d and %d", k, l, h))
return
}
return
}
func validateS3BucketLifecycleTimestamp(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
_, err := time.Parse(time.RFC3339, fmt.Sprintf("%sT00:00:00Z", value))
if err != nil {
errors = append(errors, fmt.Errorf(
"%q cannot be parsed as RFC3339 Timestamp Format", value))
}
return
}
func validateTrueOnly(v interface{}, k string) (ws []string, errors []error) {
if b, ok := v.(bool); ok && b {
return
}
if v, ok := v.(string); ok && v == "true" {
return
}
errors = append(errors, fmt.Errorf("%q must be true", k))
return
}
func validateS3BucketLifecycleExpirationDays(v interface{}, k string) (ws []string, errors []error) {
if v.(int) <= 0 {
errors = append(errors, fmt.Errorf(
"%q must be greater than 0", k))
}
return
}
func validateS3BucketLifecycleRuleId(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 255 {
errors = append(errors, fmt.Errorf(
"%q cannot exceed 255 characters", k))
}
return
}
func validateJsonString(v interface{}, k string) (ws []string, errors []error) {
if _, err := normalizeJsonString(v); err != nil {
errors = append(errors, fmt.Errorf("%q contains an invalid JSON: %s", k, err))
}
return
}
func validateKmsKeyStatus(v interface{}, k string) (ws []string, errors []error) {
status := v.(string)
if status != EnabledState && status != DisabledState && status != PendingDeletionState {
errors = append(errors, fmt.Errorf(
"%q must contain a valid status, expected %s or %s or %s, got %s.",
k, EnabledState, DisabledState, PendingDeletionState, status))
}
return
}
func looksLikeJsonString(s interface{}) bool {
return regexp.MustCompile(`^\s*{`).MatchString(s.(string))
}
func validateStackTemplate(v interface{}, k string) (ws []string, errors []error) {
if looksLikeJsonString(v) {
if _, err := normalizeJsonString(v); err != nil {
errors = append(errors, fmt.Errorf("%q contains an invalid JSON: %s", k, err))
}
} else {
if _, err := checkYamlString(v); err != nil {
errors = append(errors, fmt.Errorf("%q contains an invalid YAML: %s", k, err))
}
}
return
}
func validateName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 64 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 64 characters: %q", k, value))
}
pattern := `^[\.\-_A-Za-z0-9]+$`
if !regexp.MustCompile(pattern).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q doesn't comply with restrictions (%q): %q",
k, pattern, value))
}
return
}
func validateCIDR(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, got %q", k, value))
}
return
}
func validateIP(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
ipnet := net.ParseIP(value)
if ipnet == nil || value != ipnet.String() {
errors = append(errors, fmt.Errorf(
"%q must contain a valid network IP address, got %q", k, value))
}
return
}
func validateVBSPolicyName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if strings.HasPrefix(strings.ToLower(value), "default") {
errors = append(errors, fmt.Errorf(
"%q cannot start with default: %q", k, value))
}
if len(value) > 64 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 64 characters: %q", k, value))
}
pattern := `^[\.\-_A-Za-z0-9]+$`
if !regexp.MustCompile(pattern).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q doesn't comply with restrictions (%q): %q",
k, pattern, value))
}
return
}
func validateVBSPolicyFrequency(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value < 1 || value > 14 {
errors = append(errors, fmt.Errorf(
"%q should be in the range of 1-14: %d", k, value))
}
return
}
func validateVBSPolicyStatus(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if value != "ON" && value != "OFF" {
errors = append(errors, fmt.Errorf(
"%q should be either ON or OFF: %q", k, value))
}
return
}
func validateVBSPolicyRetentionNum(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value < 2 {
errors = append(errors, fmt.Errorf(
"%q cannot be less than 2: %d", k, value))
}
return
}
func validateVBSPolicyRetainBackup(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if value != "Y" && value != "N" {
errors = append(errors, fmt.Errorf(
"%q should be either N or Y: %q", k, value))
}
return
}
func validateVBSTagKey(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 36 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 36 characters: %q", k, value))
}
pattern := `^[\.\-_A-Za-z0-9]+$`
if !regexp.MustCompile(pattern).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q doesn't comply with restrictions (%q): %q",
k, pattern, value))
}
return
}
func validateVBSTagValue(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 43 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 43 characters: %q", k, value))
}
pattern := `^[\.\-_A-Za-z0-9]+$`
if !regexp.MustCompile(pattern).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q doesn't comply with restrictions (%q): %q",
k, pattern, value))
}
return
}
func validateVBSBackupName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if strings.HasPrefix(strings.ToLower(value), "autobk") {
errors = append(errors, fmt.Errorf(
"%q cannot start with autobk: %q", k, value))
}
if len(value) > 64 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 64 characters: %q", k, value))
}
pattern := `^[\.\-_A-Za-z0-9]+$`
if !regexp.MustCompile(pattern).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q doesn't comply with restrictions (%q): %q",
k, pattern, value))
}
return
}
func validateVBSBackupDescription(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 64 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 64 characters: %q", k, value))
}
pattern := `^[^<>]+$`
if !regexp.MustCompile(pattern).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q doesn't comply with restrictions (%q): %q",
k, pattern, value))
}
return
}