Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

312 route 53 default record name #969

Merged
merged 3 commits into from
Feb 12, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ok := strings.HasSuffix(recordName, zoneName); !ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you aren't using ok elsewhere, you can just boolean this:

if !strings.HasSuffix(recordName, zoneName) {
  // ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

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