From f2b68c4ca8317f814f522fe756a4d4dfab4d0985 Mon Sep 17 00:00:00 2001 From: Clint Shryock Date: Wed, 8 Apr 2015 09:06:15 -0500 Subject: [PATCH 1/3] provider/aws: Fix issue in updating Route 53 records on refresh/read. Route 53 records were silently erroring out when saving the records returned from AWS, because they weren't being presented as an array of strings like we expected. --- .../aws/resource_aws_route53_record.go | 5 ++++- builtin/providers/aws/structure.go | 9 ++++++++ builtin/providers/aws/structure_test.go | 22 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_route53_record.go b/builtin/providers/aws/resource_aws_route53_record.go index 71ddff056..3a8ef2e5c 100644 --- a/builtin/providers/aws/resource_aws_route53_record.go +++ b/builtin/providers/aws/resource_aws_route53_record.go @@ -184,7 +184,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 { + log.Printf("[DEBUG] Error setting records for: %s, error: %#v", en, err) + } d.Set("ttl", record.TTL) break diff --git a/builtin/providers/aws/structure.go b/builtin/providers/aws/structure.go index 7a36f5304..b4ff209d6 100644 --- a/builtin/providers/aws/structure.go +++ b/builtin/providers/aws/structure.go @@ -7,6 +7,7 @@ import ( "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 +252,11 @@ 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 { + strs = append(strs, *r.Value) + } + return strs +} 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") + } +} From 29a5d9fc1a39bbc6c52a895b46d19b59aa0240a9 Mon Sep 17 00:00:00 2001 From: Clint Shryock Date: Wed, 8 Apr 2015 13:58:23 -0500 Subject: [PATCH 2/3] Update how we expand/flatten TXT records for Route 53 --- .../aws/resource_aws_route53_record.go | 12 +---------- builtin/providers/aws/structure.go | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/builtin/providers/aws/resource_aws_route53_record.go b/builtin/providers/aws/resource_aws_route53_record.go index 3a8ef2e5c..58d54b4e7 100644 --- a/builtin/providers/aws/resource_aws_route53_record.go +++ b/builtin/providers/aws/resource_aws_route53_record.go @@ -269,18 +269,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 b4ff209d6..03a9bca8c 100644 --- a/builtin/providers/aws/structure.go +++ b/builtin/providers/aws/structure.go @@ -1,6 +1,7 @@ package aws import ( + "fmt" "strings" "github.com/hashicorp/aws-sdk-go/aws" @@ -256,7 +257,25 @@ func flattenAttachment(a *ec2.NetworkInterfaceAttachment) map[string]interface{} func flattenResourceRecords(recs []route53.ResourceRecord) []string { strs := make([]string, 0, len(recs)) for _, r := range recs { - strs = append(strs, *r.Value) + 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 +} From 22e487c5d0677c38c3dcdac36d54d4f3fef73333 Mon Sep 17 00:00:00 2001 From: Clint Shryock Date: Wed, 8 Apr 2015 14:01:00 -0500 Subject: [PATCH 3/3] surface error with setting records on Route 53 record --- builtin/providers/aws/resource_aws_route53_record.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_route53_record.go b/builtin/providers/aws/resource_aws_route53_record.go index 58d54b4e7..8b2afa7b1 100644 --- a/builtin/providers/aws/resource_aws_route53_record.go +++ b/builtin/providers/aws/resource_aws_route53_record.go @@ -186,7 +186,7 @@ func resourceAwsRoute53RecordRead(d *schema.ResourceData, meta interface{}) erro err := d.Set("records", flattenResourceRecords(record.ResourceRecords)) if err != nil { - log.Printf("[DEBUG] Error setting records for: %s, error: %#v", en, err) + return fmt.Errorf("[DEBUG] Error setting records for: %s, error: %#v", en, err) } d.Set("ttl", record.TTL)