This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
forked from cloudflare/terraform-provider-cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 5
/
validators.go
100 lines (88 loc) · 2.9 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
package cloudflare
import (
"fmt"
"net"
"net/url"
"strings"
"github.com/hashicorp/terraform/helper/schema"
)
var allowedHTTPMethods = []string{"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "_ALL_"}
var allowedSchemes = []string{"HTTP", "HTTPS", "_ALL_"}
// validateRecordType ensures that the cloudflare record type is valid
func validateRecordType(t string, proxied bool) error {
switch t {
case "A", "AAAA", "CNAME":
return nil
case "TXT", "SRV", "LOC", "MX", "NS", "SPF", "CAA", "CERT", "DNSKEY", "DS", "NAPTR", "SMIMEA", "SSHFP", "TLSA", "URI", "PTR":
if !proxied {
return nil
}
default:
return fmt.Errorf(
`Invalid type %q. Valid types are "A", "AAAA", "CNAME", "TXT", "SRV", "LOC", "MX", "NS", "SPF", "CAA", "CERT", "DNSKEY", "DS", "NAPTR", "SMIMEA", "SSHFP", "TLSA", "URI" or "PTR.`, t)
}
return fmt.Errorf("Type %q cannot be proxied", t)
}
// validateRecordName ensures that based on supplied record type, the name content matches
// Currently only validates A and AAAA types
func validateRecordName(t string, value string) error {
switch t {
case "A":
// Must be ipv4 addr
addr := net.ParseIP(value)
if addr == nil || !strings.Contains(value, ".") {
return fmt.Errorf("A record must be a valid IPv4 address, got: %q", value)
}
case "AAAA":
// Must be ipv6 addr
addr := net.ParseIP(value)
if addr == nil || !strings.Contains(value, ":") {
return fmt.Errorf("AAAA record must be a valid IPv6 address, got: %q", value)
}
case "TXT":
// Must be printable ASCII
for i := 0; i < len(value); i++ {
char := value[i]
if (char < 0x20) || (0x7F < char) {
return fmt.Errorf("TXT record must contain printable ASCII, found: %q", char)
}
}
}
return nil
}
func validateStringIP(v interface{}, k string) (warnings []string, errors []error) {
ip := net.ParseIP(v.(string))
if ip == nil {
errors = append(errors, fmt.Errorf("%q is not a valid IP: %q", k, v.(string)))
}
return
}
// validateIntInSlice returns a SchemaValidateFunc which tests if the provided value
// is of type int
func validateIntInSlice(valid []int) schema.SchemaValidateFunc {
return func(i interface{}, k string) ([]string, []error) {
var es []error
v, ok := i.(int)
if !ok {
es = append(es, fmt.Errorf("expected type of %q to be int", k))
return nil, es
}
for _, str := range valid {
if v == str {
return nil, nil
}
}
es = append(es, fmt.Errorf("expected %q to be one of %v, got %d", k, valid, v))
return nil, es
}
}
// validateURL provides a method to test whether the provided string
// is a valid URL. Relying on `url.ParseRequestURI` isn't the most
// robust solution it will catch majority of the issues we're looking to
// handle here but there _could_ be edge cases.
func validateURL(v interface{}, k string) (s []string, errors []error) {
if _, err := url.ParseRequestURI(v.(string)); err != nil {
errors = append(errors, fmt.Errorf("%q: %s", k, err))
}
return
}