From 90c83178995e8dfb770b3d0e60ed176e8c49d42b Mon Sep 17 00:00:00 2001 From: Clint Shryock Date: Wed, 11 Feb 2015 16:33:21 -0800 Subject: [PATCH] Auto add full domain suffix if not present --- .../aws/resource_aws_route53_record.go | 18 ++++++++++- .../aws/resource_aws_route53_record_test.go | 30 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_route53_record.go b/builtin/providers/aws/resource_aws_route53_record.go index 1145db374..3ac32435d 100644 --- a/builtin/providers/aws/resource_aws_route53_record.go +++ b/builtin/providers/aws/resource_aws_route53_record.go @@ -59,6 +59,22 @@ func resourceAwsRoute53Record() *schema.Resource { func resourceAwsRoute53RecordCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).route53 + zone := d.Get("zone_id").(string) + + zoneRecord, err := conn.GetHostedZone(zone) + if err != nil { + return err + } + + // Check if the current record name contains the zone suffix. + // If it does not, add the zone name to form a fully qualified name + // and keep AWS happy. + recordName := d.Get("name").(string) + zoneName := strings.Trim(zoneRecord.HostedZone.Name, ".") + if ok := strings.HasSuffix(recordName, zoneName); !ok { + d.Set("name", strings.Join([]string{recordName, zoneName}, ".")) + } + // Get the record rec, err := resourceAwsRoute53RecordBuildSet(d) if err != nil { @@ -77,7 +93,7 @@ func resourceAwsRoute53RecordCreate(d *schema.ResourceData, meta interface{}) er }, }, } - zone := d.Get("zone_id").(string) + log.Printf("[DEBUG] Creating resource records for zone: %s, name: %s", zone, d.Get("name").(string)) diff --git a/builtin/providers/aws/resource_aws_route53_record_test.go b/builtin/providers/aws/resource_aws_route53_record_test.go index 3f72a9297..ee5272fdf 100644 --- a/builtin/providers/aws/resource_aws_route53_record_test.go +++ b/builtin/providers/aws/resource_aws_route53_record_test.go @@ -26,6 +26,22 @@ func TestAccRoute53Record(t *testing.T) { }) } +func TestAccRoute53Record_generatesSuffix(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckRoute53RecordDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccRoute53RecordConfigSuffix, + Check: resource.ComposeTestCheckFunc( + testAccCheckRoute53RecordExists("aws_route53_record.default"), + ), + }, + }, + }) +} + func testAccCheckRoute53RecordDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).route53 for _, rs := range s.RootModule().Resources { @@ -100,3 +116,17 @@ resource "aws_route53_record" "default" { records = ["127.0.0.1", "127.0.0.27"] } ` + +const testAccRoute53RecordConfigSuffix = ` +resource "aws_route53_zone" "main" { + name = "notexample.com" +} + +resource "aws_route53_record" "default" { + zone_id = "${aws_route53_zone.main.zone_id}" + name = "subdomain" + type = "A" + ttl = "30" + records = ["127.0.0.1", "127.0.0.27"] +} +`