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

Data Source Route 53 Zone has attribute name_servers #4336

Merged
merged 2 commits into from
Apr 25, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 35 additions & 2 deletions aws/data_source_aws_route53_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func dataSourceAwsRoute53Zone() *schema.Resource {
Optional: true,
Computed: true,
},
"name_servers": &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -135,7 +140,6 @@ func dataSourceAwsRoute53ZoneRead(d *schema.ResourceData, meta interface{}) erro
break
}
}

}

if matchingTags && matchingVPC {
Expand All @@ -146,7 +150,6 @@ func dataSourceAwsRoute53ZoneRead(d *schema.ResourceData, meta interface{}) erro
hostedZoneFound = hostedZone
}
}

}
if *resp.IsTruncated {
nextMarker = resp.NextMarker
Expand All @@ -166,6 +169,13 @@ func dataSourceAwsRoute53ZoneRead(d *schema.ResourceData, meta interface{}) erro
d.Set("private_zone", hostedZoneFound.Config.PrivateZone)
d.Set("caller_reference", hostedZoneFound.CallerReference)
d.Set("resource_record_set_count", hostedZoneFound.ResourceRecordSetCount)

nameServers, err := hostedZoneNameServers(idHostedZone, conn)
if err != nil {
return fmt.Errorf("Error finding Route 53 Hosted Zone: %v", err)
}
d.Set("name_servers", nameServers)

return nil
}

Expand All @@ -177,3 +187,26 @@ func hostedZoneName(name string) string {

return name + "."
}

// used to retrieve name servers
func hostedZoneNameServers(id string, conn *route53.Route53) ([]string, error) {
req := &route53.GetHostedZoneInput{}
req.Id = aws.String(id)

resp, err := conn.GetHostedZone(req)
if err != nil {
return []string{}, err
}

if resp.DelegationSet == nil {
return []string{}, nil
}

servers := []string{}
for _, server := range resp.DelegationSet.NameServers {
if server != nil {
servers = append(servers, *server)
}
}
return servers, nil
}
15 changes: 8 additions & 7 deletions aws/data_source_aws_route53_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ func TestAccDataSourceAwsRoute53Zone(t *testing.T) {
privateDomain := fmt.Sprintf("test.acc-%d.", rInt)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRoute53ZoneDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsRoute53ZoneConfig(rInt),
Expand Down Expand Up @@ -54,17 +55,17 @@ func testAccDataSourceAwsRoute53ZoneCheck(rsName, dsName, zName string) resource

attr := rs.Primary.Attributes
if attr["id"] != hostedZone.Primary.Attributes["id"] {
return fmt.Errorf(
"id is %s; want %s",
attr["id"],
hostedZone.Primary.Attributes["id"],
)
return fmt.Errorf("Route53 Zone id is %s; want %s", attr["id"], hostedZone.Primary.Attributes["id"])
}

if attr["name"] != zName {
return fmt.Errorf("Route53 Zone name is %q; want %q", attr["name"], zName)
}

if attr["private_zone"] == "false" && len(attr["name_servers"]) == 0 {
return fmt.Errorf("Route53 Zone %s has no name_servers", zName)
}

return nil
}
}
Expand Down