Skip to content

Commit adb789f

Browse files
cakooseradeksimko
authored andcommitted
aws_route53_record: Only unquote the record types that we quote. (hashicorp#11257)
Also clean up the code a little.
1 parent c3b8995 commit adb789f

3 files changed

Lines changed: 53 additions & 19 deletions

File tree

builtin/providers/aws/resource_aws_route53_record.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ func resourceAwsRoute53RecordRead(d *schema.ResourceData, meta interface{}) erro
484484
}
485485
}
486486

487-
err = d.Set("records", flattenResourceRecords(record.ResourceRecords))
487+
err = d.Set("records", flattenResourceRecords(record.ResourceRecords, *record.Type))
488488
if err != nil {
489489
return fmt.Errorf("[DEBUG] Error setting records for: %s, error: %#v", d.Id(), err)
490490
}

builtin/providers/aws/structure.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -814,11 +814,14 @@ func flattenStepAdjustments(adjustments []*autoscaling.StepAdjustment) []map[str
814814
return result
815815
}
816816

817-
func flattenResourceRecords(recs []*route53.ResourceRecord) []string {
817+
func flattenResourceRecords(recs []*route53.ResourceRecord, typeStr string) []string {
818818
strs := make([]string, 0, len(recs))
819819
for _, r := range recs {
820820
if r.Value != nil {
821-
s := strings.Replace(*r.Value, "\"", "", 2)
821+
s := *r.Value
822+
if typeStr == "TXT" || typeStr == "SPF" {
823+
s = strings.Replace(s, "\"", "", 2)
824+
}
822825
strs = append(strs, s)
823826
}
824827
}
@@ -829,13 +832,11 @@ func expandResourceRecords(recs []interface{}, typeStr string) []*route53.Resour
829832
records := make([]*route53.ResourceRecord, 0, len(recs))
830833
for _, r := range recs {
831834
s := r.(string)
832-
switch typeStr {
833-
case "TXT", "SPF":
834-
str := fmt.Sprintf("\"%s\"", s)
835-
records = append(records, &route53.ResourceRecord{Value: aws.String(str)})
836-
default:
837-
records = append(records, &route53.ResourceRecord{Value: aws.String(s)})
835+
if typeStr == "TXT" || typeStr == "SPF" {
836+
// `flattenResourceRecords` removes quotes. Add them back.
837+
s = fmt.Sprintf("\"%s\"", s)
838838
}
839+
records = append(records, &route53.ResourceRecord{Value: aws.String(s)})
839840
}
840841
return records
841842
}

builtin/providers/aws/structure_test.go

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -819,23 +819,56 @@ func TestFlattenStepAdjustments(t *testing.T) {
819819
}
820820

821821
func TestFlattenResourceRecords(t *testing.T) {
822-
expanded := []*route53.ResourceRecord{
823-
&route53.ResourceRecord{
824-
Value: aws.String("127.0.0.1"),
825-
},
826-
&route53.ResourceRecord{
827-
Value: aws.String("127.0.0.3"),
828-
},
822+
original := []string{
823+
`127.0.0.1`,
824+
`"abc def"`,
829825
}
830826

831-
result := flattenResourceRecords(expanded)
827+
dequoted := []string{
828+
`127.0.0.1`,
829+
`abc def`,
830+
}
831+
832+
var wrapped []*route53.ResourceRecord = nil
833+
for _, original := range original {
834+
wrapped = append(wrapped, &route53.ResourceRecord{Value: aws.String(original)})
835+
}
836+
837+
sub := func(recordType string, expected []string) {
838+
t.Run(recordType, func(t *testing.T) {
839+
checkFlattenResourceRecords(t, recordType, wrapped, expected)
840+
})
841+
}
842+
843+
// These record types should be dequoted.
844+
sub("TXT", dequoted)
845+
sub("SPF", dequoted)
846+
847+
// These record types should not be touched.
848+
sub("CNAME", original)
849+
sub("MX", original)
850+
}
851+
852+
func checkFlattenResourceRecords(
853+
t *testing.T,
854+
recordType string,
855+
expanded []*route53.ResourceRecord,
856+
expected []string) {
857+
858+
result := flattenResourceRecords(expanded, recordType)
832859

833860
if result == nil {
834861
t.Fatal("expected result to have value, but got nil")
835862
}
836863

837-
if len(result) != 2 {
838-
t.Fatal("expected result to have value, but got nil")
864+
if len(result) != len(expected) {
865+
t.Fatalf("expected %v, got %v", expected, result)
866+
}
867+
868+
for i, e := range expected {
869+
if result[i] != e {
870+
t.Fatalf("expected %v, got %v", expected, result)
871+
}
839872
}
840873
}
841874

0 commit comments

Comments
 (0)