Skip to content

Commit

Permalink
Merge pull request #969 from hashicorp/312-route-53-default-record-name
Browse files Browse the repository at this point in the history
312 route 53 default record name
  • Loading branch information
pearkes committed Feb 12, 2015
2 parents 5f4e2cb + 1ee3d23 commit 2651dfd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
18 changes: 17 additions & 1 deletion builtin/providers/aws/resource_aws_route53_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 !strings.HasSuffix(recordName, zoneName) {
d.Set("name", strings.Join([]string{recordName, zoneName}, "."))
}

// Get the record
rec, err := resourceAwsRoute53RecordBuildSet(d)
if err != nil {
Expand All @@ -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))

Expand Down
30 changes: 30 additions & 0 deletions builtin/providers/aws/resource_aws_route53_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"]
}
`
2 changes: 1 addition & 1 deletion helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Schema struct {
//
// If Required is true above, then Default cannot be set. DefaultFunc
// can be set with Required. If the DefaultFunc returns nil, then there
// will no default and the user will be asked to fill it in.
// will be no default and the user will be asked to fill it in.
//
// If either of these is set, then the user won't be asked for input
// for this key if the default is not nil.
Expand Down

0 comments on commit 2651dfd

Please sign in to comment.