Merge pull request #1430 from hashicorp/b-aws-route53-record-update

provider/aws: Fix issue in updating Route 53 records on refresh/read.
This commit is contained in:
Clint 2015-04-09 11:54:27 -05:00
commit 02c81c479d
3 changed files with 55 additions and 12 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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")
}
}