This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
forked from huaweicloud/terraform-provider-huaweicloudstack
-
Notifications
You must be signed in to change notification settings - Fork 2
/
validators.go
161 lines (139 loc) · 4.05 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
package huaweicloudstack
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
}
// Not currently used.
func ValidateInsensitiveStringList(v interface{}, k string, l []string) (ws []string, errors []error) {
value := v.(string)
for i := range l {
if strings.EqualFold(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 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 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 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 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 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 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 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
}