diff --git a/builtin/providers/aws/resource_aws_route53_record.go b/builtin/providers/aws/resource_aws_route53_record.go index a9884d8fa..24421849f 100644 --- a/builtin/providers/aws/resource_aws_route53_record.go +++ b/builtin/providers/aws/resource_aws_route53_record.go @@ -196,7 +196,10 @@ func resourceAwsRoute53RecordRead(d *schema.ResourceData, meta interface{}) erro found = true - d.Set("records", record.ResourceRecords) + err := d.Set("records", flattenResourceRecords(record.ResourceRecords)) + if err != nil { + return fmt.Errorf("[DEBUG] Error setting records for: %s, error: %#v", en, err) + } d.Set("ttl", record.TTL) break @@ -278,18 +281,8 @@ func resourceAwsRoute53RecordDelete(d *schema.ResourceData, meta interface{}) er func resourceAwsRoute53RecordBuildSet(d *schema.ResourceData, zoneName string) (*route53.ResourceRecordSet, error) { recs := d.Get("records").(*schema.Set).List() - records := make([]route53.ResourceRecord, 0, len(recs)) - typeStr := d.Get("type").(string) - for _, r := range recs { - switch typeStr { - case "TXT": - str := fmt.Sprintf("\"%s\"", r.(string)) - records = append(records, route53.ResourceRecord{Value: aws.String(str)}) - default: - records = append(records, route53.ResourceRecord{Value: aws.String(r.(string))}) - } - } + records := expandResourceRecords(recs, d.Get("type").(string)) // get expanded name en := expandRecordName(d.Get("name").(string), zoneName) diff --git a/builtin/providers/aws/structure.go b/builtin/providers/aws/structure.go index 7a36f5304..03a9bca8c 100644 --- a/builtin/providers/aws/structure.go +++ b/builtin/providers/aws/structure.go @@ -1,12 +1,14 @@ package aws import ( + "fmt" "strings" "github.com/hashicorp/aws-sdk-go/aws" "github.com/hashicorp/aws-sdk-go/gen/ec2" "github.com/hashicorp/aws-sdk-go/gen/elb" "github.com/hashicorp/aws-sdk-go/gen/rds" + "github.com/hashicorp/aws-sdk-go/gen/route53" "github.com/hashicorp/terraform/helper/schema" ) @@ -251,3 +253,29 @@ func flattenAttachment(a *ec2.NetworkInterfaceAttachment) map[string]interface{} att["attachment_id"] = *a.AttachmentID return att } + +func flattenResourceRecords(recs []route53.ResourceRecord) []string { + strs := make([]string, 0, len(recs)) + for _, r := range recs { + if r.Value != nil { + s := strings.Replace(*r.Value, "\"", "", 2) + strs = append(strs, s) + } + } + return strs +} + +func expandResourceRecords(recs []interface{}, typeStr string) []route53.ResourceRecord { + records := make([]route53.ResourceRecord, 0, len(recs)) + for _, r := range recs { + s := r.(string) + switch typeStr { + case "TXT": + str := fmt.Sprintf("\"%s\"", s) + records = append(records, route53.ResourceRecord{Value: aws.String(str)}) + default: + records = append(records, route53.ResourceRecord{Value: aws.String(s)}) + } + } + return records +} diff --git a/builtin/providers/aws/structure_test.go b/builtin/providers/aws/structure_test.go index aafdeabbc..ca27f6ddf 100644 --- a/builtin/providers/aws/structure_test.go +++ b/builtin/providers/aws/structure_test.go @@ -8,6 +8,7 @@ import ( ec2 "github.com/hashicorp/aws-sdk-go/gen/ec2" "github.com/hashicorp/aws-sdk-go/gen/elb" "github.com/hashicorp/aws-sdk-go/gen/rds" + "github.com/hashicorp/aws-sdk-go/gen/route53" "github.com/hashicorp/terraform/flatmap" "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" @@ -442,3 +443,24 @@ func TestFlattenAttachment(t *testing.T) { t.Fatalf("expected attachment_id to be at-002, but got %s", result["attachment_id"]) } } + +func TestFlattenResourceRecords(t *testing.T) { + expanded := []route53.ResourceRecord{ + route53.ResourceRecord{ + Value: aws.String("127.0.0.1"), + }, + route53.ResourceRecord{ + Value: aws.String("127.0.0.3"), + }, + } + + result := flattenResourceRecords(expanded) + + if result == nil { + t.Fatal("expected result to have value, but got nil") + } + + if len(result) != 2 { + t.Fatal("expected result to have value, but got nil") + } +}