github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/cloudflare/validators.go (about)

     1  package cloudflare
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"strings"
     7  )
     8  
     9  // validateRecordType ensures that the cloudflare record type is valid
    10  func validateRecordType(v interface{}, k string) (ws []string, errors []error) {
    11  	value := v.(string)
    12  
    13  	validTypes := map[string]struct{}{
    14  		"A":     {},
    15  		"AAAA":  {},
    16  		"CNAME": {},
    17  		"TXT":   {},
    18  		"SRV":   {},
    19  		"LOC":   {},
    20  		"MX":    {},
    21  		"NS":    {},
    22  		"SPF":   {},
    23  	}
    24  
    25  	if _, ok := validTypes[value]; !ok {
    26  		errors = append(errors, fmt.Errorf(
    27  			`%q contains an invalid type %q. Valid types are "A", "AAAA", "CNAME", "TXT", "SRV", "LOC", "MX", "NS" or "SPF"`, k, value))
    28  	}
    29  	return
    30  }
    31  
    32  // validateRecordName ensures that based on supplied record type, the name content matches
    33  // Currently only validates A and AAAA types
    34  func validateRecordName(t string, value string) error {
    35  	switch t {
    36  	case "A":
    37  		// Must be ipv4 addr
    38  		addr := net.ParseIP(value)
    39  		if addr == nil || !strings.Contains(value, ".") {
    40  			return fmt.Errorf("A record must be a valid IPv4 address, got: %q", value)
    41  		}
    42  	case "AAAA":
    43  		// Must be ipv6 addr
    44  		addr := net.ParseIP(value)
    45  		if addr == nil || !strings.Contains(value, ":") {
    46  			return fmt.Errorf("AAAA record must be a valid IPv6 address, got: %q", value)
    47  		}
    48  	}
    49  
    50  	return nil
    51  }