opentofu/builtin/providers/cloudflare/validators.go
Jake Champlin 179aa62ad4
provider/cloudflare: Fix record validation
Previously we only validated that the cloudflare record provided was a valid record type. However, a record can be of a valid type, and still not be proxied, making it an invalid record type.

The main downside to having to check for whether or not the record type is proxied or not during validation, is that it relies on having two schema keys populated. This means that we can only catch the improper record type during `apply` time, instead of `plan` time.

```
$ go test -v -run "TestValidateRecordType" ./builtin/providers/cloudflare
=== RUN   TestValidateRecordType
--- PASS: TestValidateRecordType (0.00s)
PASS
ok      github.com/hashicorp/terraform/builtin/providers/cloudflare     0.004s
```
2017-02-15 21:52:04 -05:00

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
}