Merge pull request #11993 from hashicorp/b-fix-cloudflare-validator

provider/cloudflare: Fix record validation
This commit is contained in:
Jake Champlin 2017-02-16 10:15:46 -05:00 committed by GitHub
commit 18bb3aade3
3 changed files with 80 additions and 56 deletions

View File

@ -18,51 +18,50 @@ func resourceCloudFlareRecord() *schema.Resource {
SchemaVersion: 1, SchemaVersion: 1,
MigrateState: resourceCloudFlareRecordMigrateState, MigrateState: resourceCloudFlareRecordMigrateState,
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"domain": &schema.Schema{ "domain": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
}, },
"name": &schema.Schema{ "name": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
}, },
"hostname": &schema.Schema{ "hostname": {
Type: schema.TypeString, Type: schema.TypeString,
Computed: true, Computed: true,
}, },
"type": &schema.Schema{ "type": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
ForceNew: true, ForceNew: true,
ValidateFunc: validateRecordType,
}, },
"value": &schema.Schema{ "value": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
}, },
"ttl": &schema.Schema{ "ttl": {
Type: schema.TypeInt, Type: schema.TypeInt,
Optional: true, Optional: true,
Computed: true, Computed: true,
}, },
"priority": &schema.Schema{ "priority": {
Type: schema.TypeInt, Type: schema.TypeInt,
Optional: true, Optional: true,
}, },
"proxied": &schema.Schema{ "proxied": {
Default: false, Default: false,
Optional: true, Optional: true,
Type: schema.TypeBool, Type: schema.TypeBool,
}, },
"zone_id": &schema.Schema{ "zone_id": {
Type: schema.TypeString, Type: schema.TypeString,
Computed: true, Computed: true,
}, },
@ -94,6 +93,11 @@ func resourceCloudFlareRecordCreate(d *schema.ResourceData, meta interface{}) er
return fmt.Errorf("Error validating record name %q: %s", newRecord.Name, err) return fmt.Errorf("Error validating record name %q: %s", newRecord.Name, err)
} }
// Validate type
if err := validateRecordType(newRecord.Type, newRecord.Proxied); err != nil {
return fmt.Errorf("Error validating record type %q: %s", newRecord.Type, err)
}
zoneId, err := client.ZoneIDByName(newRecord.ZoneName) zoneId, err := client.ZoneIDByName(newRecord.ZoneName)
if err != nil { if err != nil {
return fmt.Errorf("Error finding zone %q: %s", newRecord.ZoneName, err) return fmt.Errorf("Error finding zone %q: %s", newRecord.ZoneName, err)

View File

@ -7,26 +7,44 @@ import (
) )
// validateRecordType ensures that the cloudflare record type is valid // validateRecordType ensures that the cloudflare record type is valid
func validateRecordType(v interface{}, k string) (ws []string, errors []error) { func validateRecordType(t string, proxied bool) error {
value := v.(string) switch t {
case "A":
validTypes := map[string]struct{}{ return nil
"A": {}, case "AAAA":
"AAAA": {}, return nil
"CNAME": {}, case "CNAME":
"TXT": {}, return nil
"SRV": {}, case "TXT":
"LOC": {}, if !proxied {
"MX": {}, return nil
"NS": {}, }
"SPF": {}, 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)
} }
if _, ok := validTypes[value]; !ok { return fmt.Errorf("Type %q cannot be proxied", t)
errors = append(errors, fmt.Errorf(
`%q contains an invalid type %q. Valid types are "A", "AAAA", "CNAME", "TXT", "SRV", "LOC", "MX", "NS" or "SPF"`, k, value))
}
return
} }
// validateRecordName ensures that based on supplied record type, the name content matches // validateRecordName ensures that based on supplied record type, the name content matches

View File

@ -3,36 +3,38 @@ package cloudflare
import "testing" import "testing"
func TestValidateRecordType(t *testing.T) { func TestValidateRecordType(t *testing.T) {
validTypes := []string{ validTypes := map[string]bool{
"A", "A": true,
"AAAA", "AAAA": true,
"CNAME", "CNAME": true,
"TXT", "TXT": false,
"SRV", "SRV": false,
"LOC", "LOC": false,
"MX", "MX": false,
"NS", "NS": false,
"SPF", "SPF": false,
} }
for _, v := range validTypes { for k, v := range validTypes {
_, errors := validateRecordType(v, "type") err := validateRecordType(k, v)
if len(errors) != 0 { if err != nil {
t.Fatalf("%q should be a valid record type: %q", v, errors) t.Fatalf("%s should be a valid record type: %s", k, err)
} }
} }
invalidTypes := []string{ invalidTypes := map[string]bool{
"a", "a": false,
"cName", "cName": false,
"txt", "txt": false,
"SRv", "SRv": false,
"foo", "foo": false,
"bar", "bar": false,
"TXT": true,
"SRV": true,
"SPF": true,
} }
for _, v := range invalidTypes { for k, v := range invalidTypes {
_, errors := validateRecordType(v, "type") if err := validateRecordType(k, v); err == nil {
if len(errors) == 0 { t.Fatalf("%s should be an invalid record type", k)
t.Fatalf("%q should be an invalid record type", v)
} }
} }
} }