Skip to content

Commit

Permalink
Merge pull request #1525 from Banno/route53-zone-nameservers
Browse files Browse the repository at this point in the history
providers/aws: Expose Route53 zone nameservers for parent zone NS record
  • Loading branch information
mitchellh committed Apr 22, 2015
2 parents 5f4e2d9 + c3f9c12 commit 8ca42f8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
21 changes: 20 additions & 1 deletion builtin/providers/aws/resource_aws_route53_zone.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package aws

import (
"fmt"
"log"
"strings"
"time"

"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"

Expand All @@ -31,6 +33,15 @@ func resourceAwsRoute53Zone() *schema.Resource {
Computed: true,
},

"name_servers": &schema.Schema{
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
},

"tags": tagsSchema(),
},
}
Expand Down Expand Up @@ -80,7 +91,7 @@ func resourceAwsRoute53ZoneCreate(d *schema.ResourceData, meta interface{}) erro

func resourceAwsRoute53ZoneRead(d *schema.ResourceData, meta interface{}) error {
r53 := meta.(*AWSClient).r53conn
_, err := r53.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(d.Id())})
zone, err := r53.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(d.Id())})
if err != nil {
// Handle a deleted zone
if r53err, ok := err.(aws.APIError); ok && r53err.Code == "NoSuchHostedZone" {
Expand All @@ -90,6 +101,14 @@ func resourceAwsRoute53ZoneRead(d *schema.ResourceData, meta interface{}) error
return err
}

ns := make([]string, len(zone.DelegationSet.NameServers))
for i := range zone.DelegationSet.NameServers {
ns[i] = *zone.DelegationSet.NameServers[i]
}
if err := d.Set("name_servers", ns); err != nil {
return fmt.Errorf("[DEBUG] Error setting name servers for: %s, error: %#v", d.Id(), err)
}

// get tags
req := &route53.ListTagsForResourceInput{
ResourceID: aws.String(d.Id()),
Expand Down
10 changes: 10 additions & 0 deletions builtin/providers/aws/resource_aws_route53_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"

Expand Down Expand Up @@ -114,6 +115,15 @@ func testAccCheckRoute53ZoneExists(n string, zone *route53.HostedZone) resource.
if err != nil {
return fmt.Errorf("Hosted zone err: %v", err)
}

for _, ns := range resp.DelegationSet.NameServers {
attribute := fmt.Sprintf("name_servers.%d", hashcode.String(*ns))
dsns := rs.Primary.Attributes[attribute]
if dsns != *ns {
return fmt.Errorf("Got: %v for %v, Expected: %v", dsns, attribute, ns)
}
}

*zone = *resp.HostedZone
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ resource "aws_route53_zone" "main" {
resource "aws_route53_zone" "dev" {
name = "dev.example.com"
parent_route53_zone = "${aws_route53_zone.main.zone_id}"
}
resource "aws_route53_record" "dev-ns" {
zone_id = "${aws_route53_zone.main.zone_id}"
name = "dev.example.com"
type = "NS"
ttl = "30"
records = ["127.0.0.1", "127.0.0.27"]
records = [
"${aws_route53_zone.dev.delegation_set_name_servers.0}",
"${aws_route53_zone.dev.delegation_set_name_servers.1}",
"${aws_route53_zone.dev.delegation_set_name_servers.2}",
"${aws_route53_zone.dev.delegation_set_name_servers.3}"
]
}
```

Expand Down

0 comments on commit 8ca42f8

Please sign in to comment.