aws_route53_record: Only unquote the record types that we quote. (#11257)

Also clean up the code a little.
This commit is contained in:
Kannan Goundan 2017-05-02 02:31:51 -07:00 committed by Radek Simko
parent c3b89954b9
commit adb789f0f8
3 changed files with 53 additions and 19 deletions

View File

@ -484,7 +484,7 @@ func resourceAwsRoute53RecordRead(d *schema.ResourceData, meta interface{}) erro
} }
} }
err = d.Set("records", flattenResourceRecords(record.ResourceRecords)) err = d.Set("records", flattenResourceRecords(record.ResourceRecords, *record.Type))
if err != nil { if err != nil {
return fmt.Errorf("[DEBUG] Error setting records for: %s, error: %#v", d.Id(), err) return fmt.Errorf("[DEBUG] Error setting records for: %s, error: %#v", d.Id(), err)
} }

View File

@ -814,11 +814,14 @@ func flattenStepAdjustments(adjustments []*autoscaling.StepAdjustment) []map[str
return result return result
} }
func flattenResourceRecords(recs []*route53.ResourceRecord) []string { func flattenResourceRecords(recs []*route53.ResourceRecord, typeStr string) []string {
strs := make([]string, 0, len(recs)) strs := make([]string, 0, len(recs))
for _, r := range recs { for _, r := range recs {
if r.Value != nil { if r.Value != nil {
s := strings.Replace(*r.Value, "\"", "", 2) s := *r.Value
if typeStr == "TXT" || typeStr == "SPF" {
s = strings.Replace(s, "\"", "", 2)
}
strs = append(strs, s) strs = append(strs, s)
} }
} }
@ -829,13 +832,11 @@ func expandResourceRecords(recs []interface{}, typeStr string) []*route53.Resour
records := make([]*route53.ResourceRecord, 0, len(recs)) records := make([]*route53.ResourceRecord, 0, len(recs))
for _, r := range recs { for _, r := range recs {
s := r.(string) s := r.(string)
switch typeStr { if typeStr == "TXT" || typeStr == "SPF" {
case "TXT", "SPF": // `flattenResourceRecords` removes quotes. Add them back.
str := fmt.Sprintf("\"%s\"", s) s = fmt.Sprintf("\"%s\"", s)
records = append(records, &route53.ResourceRecord{Value: aws.String(str)})
default:
records = append(records, &route53.ResourceRecord{Value: aws.String(s)})
} }
records = append(records, &route53.ResourceRecord{Value: aws.String(s)})
} }
return records return records
} }

View File

@ -819,23 +819,56 @@ func TestFlattenStepAdjustments(t *testing.T) {
} }
func TestFlattenResourceRecords(t *testing.T) { func TestFlattenResourceRecords(t *testing.T) {
expanded := []*route53.ResourceRecord{ original := []string{
&route53.ResourceRecord{ `127.0.0.1`,
Value: aws.String("127.0.0.1"), `"abc def"`,
},
&route53.ResourceRecord{
Value: aws.String("127.0.0.3"),
},
} }
result := flattenResourceRecords(expanded) dequoted := []string{
`127.0.0.1`,
`abc def`,
}
var wrapped []*route53.ResourceRecord = nil
for _, original := range original {
wrapped = append(wrapped, &route53.ResourceRecord{Value: aws.String(original)})
}
sub := func(recordType string, expected []string) {
t.Run(recordType, func(t *testing.T) {
checkFlattenResourceRecords(t, recordType, wrapped, expected)
})
}
// These record types should be dequoted.
sub("TXT", dequoted)
sub("SPF", dequoted)
// These record types should not be touched.
sub("CNAME", original)
sub("MX", original)
}
func checkFlattenResourceRecords(
t *testing.T,
recordType string,
expanded []*route53.ResourceRecord,
expected []string) {
result := flattenResourceRecords(expanded, recordType)
if result == nil { if result == nil {
t.Fatal("expected result to have value, but got nil") t.Fatal("expected result to have value, but got nil")
} }
if len(result) != 2 { if len(result) != len(expected) {
t.Fatal("expected result to have value, but got nil") t.Fatalf("expected %v, got %v", expected, result)
}
for i, e := range expected {
if result[i] != e {
t.Fatalf("expected %v, got %v", expected, result)
}
} }
} }