This repository has been archived by the owner on Aug 29, 2020. It is now read-only.
forked from PaloAltoNetworks/terraform-provider-panos
/
validation.go
76 lines (63 loc) · 1.82 KB
/
validation.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
package panos
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
const (
SkipPanoramaAccTest = "Skipping panorama test"
SkipFirewallAccTest = "Skipping firewall test"
SkipL2AccTest = "Skipping L2 test for PAN-OS model that does not have L2 support"
SkipAggregateTest = "Skipping test as aggregate ethernet interfaces are not supported by PAN-OS"
)
func validateStringIn(vals ...string) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
ok := false
for i := range vals {
if vals[i] == value {
ok = true
break
}
}
if !ok {
errors = append(errors, fmt.Errorf("%q (%q) not in %#v", k, value, vals))
}
return
}
}
func validateIntInRange(low, high int) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value < low || value > high {
errors = append(errors, fmt.Errorf("%q (%d) not in range [%d, %d]", k, value, low, high))
}
return
}
}
func validateSetKeyIsUnique(key string) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
counts := make(map[string]int)
list := v.(*schema.Set).List()
for i := range list {
group := list[i].(map[string]interface{})
val := group[key].(string)
counts[val] = counts[val] + 1
}
for ck, cv := range counts {
if cv > 1 {
errors = append(errors, fmt.Errorf("%q (%s) is not unique - repeated %d times", k, ck, cv))
}
}
return
}
}
func validateStringHasPrefix(p string) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
val := v.(string)
if !strings.HasPrefix(val, p) {
errors = append(errors, fmt.Errorf("Param value must start with %q", p))
}
return
}
}