Auto add full domain suffix if not present

This commit is contained in:
Clint Shryock 2015-02-11 16:33:21 -08:00
parent 4c7b732dad
commit 90c8317899
2 changed files with 47 additions and 1 deletions

View File

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

View File

@ -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"]
}
`