terraform/builtin/providers/cloudflare/validators.go

70 lines
1.3 KiB
Go

package cloudflare
import (
"fmt"
"net"
"strings"
)
// validateRecordType ensures that the cloudflare record type is valid
func validateRecordType(t string, proxied bool) error {
switch t {
case "A":
return nil
case "AAAA":
return nil
case "CNAME":
return nil
case "TXT":
if !proxied {
return nil
}
case "SRV":
if !proxied {
return nil
}
case "LOC":
if !proxied {
return nil
}
case "MX":
if !proxied {
return nil
}
case "NS":
if !proxied {
return nil
}
case "SPF":
if !proxied {
return nil
}
default:
return fmt.Errorf(
`Invalid type %q. Valid types are "A", "AAAA", "CNAME", "TXT", "SRV", "LOC", "MX", "NS" or "SPF"`, 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)
}
}
return nil
}