forked from hashicorp/terraform
/
validators.go
67 lines (58 loc) · 1.83 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
package opc
import (
"fmt"
"net"
"github.com/hashicorp/go-oracle-terraform/compute"
)
// Validate whether an IP Prefix CIDR is correct or not
func validateIPPrefixCIDR(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 while 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
}
return
}
// Admin distance can either be a 0, 1, or a 2. Defaults to 0.
func validateAdminDistance(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value < 0 || value > 2 {
errors = append(errors, fmt.Errorf(
"%q can only be an interger between 0-2. Got: %d", k, value))
}
return
}
// Admin distance can either be a 0, 1, or a 2. Defaults to 0.
func validateIPProtocol(v interface{}, k string) (ws []string, errors []error) {
validProtocols := map[string]struct{}{
string(compute.All): {},
string(compute.AH): {},
string(compute.ESP): {},
string(compute.ICMP): {},
string(compute.ICMPV6): {},
string(compute.IGMP): {},
string(compute.IPIP): {},
string(compute.GRE): {},
string(compute.MPLSIP): {},
string(compute.OSPF): {},
string(compute.PIM): {},
string(compute.RDP): {},
string(compute.SCTP): {},
string(compute.TCP): {},
string(compute.UDP): {},
}
value := v.(string)
if _, ok := validProtocols[value]; !ok {
errors = append(errors, fmt.Errorf(
`%q must contain a valid Image owner , expected ["all", "ah", "esp", "icmp", "icmpv6", "igmp", "ipip", "gre", "mplsip", "ospf", "pim", "rdp", "sctp", "tcp", "udp"] got %q`,
k, value))
}
return
}