Skip to content

Commit

Permalink
Merge pull request #9390 from jstewmon/route53-zone-linked-service
Browse files Browse the repository at this point in the history
data-source/aws_route53_zone: add linked service attributes
  • Loading branch information
bflad committed Jul 18, 2019
2 parents afe5288 + 93f1cc8 commit 85abeab
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 51 deletions.
14 changes: 12 additions & 2 deletions aws/data_source_aws_route53_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ func dataSourceAwsRoute53Zone() *schema.Resource {
},
"comment": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"caller_reference": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"vpc_id": {
Expand All @@ -56,6 +54,14 @@ func dataSourceAwsRoute53Zone() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"linked_service_principal": {
Type: schema.TypeString,
Computed: true,
},
"linked_service_description": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -169,6 +175,10 @@ 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)
if hostedZoneFound.LinkedService != nil {
d.Set("linked_service_principal", hostedZoneFound.LinkedService.ServicePrincipal)
d.Set("linked_service_description", hostedZoneFound.LinkedService.Description)
}

nameServers, err := hostedZoneNameServers(idHostedZone, conn)
if err != nil {
Expand Down
79 changes: 31 additions & 48 deletions aws/data_source_aws_route53_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ package aws

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccDataSourceAwsRoute53Zone(t *testing.T) {
rInt := acctest.RandInt()
publicResourceName := "aws_route53_zone.test"
publicDomain := fmt.Sprintf("terraformtestacchz-%d.com.", rInt)
privateResourceName := "aws_route53_zone.test_private"
privateDomain := fmt.Sprintf("test.acc-%d.", rInt)
serviceDiscoveryResourceName := "aws_service_discovery_private_dns_namespace.test_service_discovery"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -24,52 +23,26 @@ func TestAccDataSourceAwsRoute53Zone(t *testing.T) {
{
Config: testAccDataSourceAwsRoute53ZoneConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsRoute53ZoneCheck(
publicResourceName, "data.aws_route53_zone.by_zone_id", publicDomain),
testAccDataSourceAwsRoute53ZoneCheck(
publicResourceName, "data.aws_route53_zone.by_name", publicDomain),
testAccDataSourceAwsRoute53ZoneCheck(
privateResourceName, "data.aws_route53_zone.by_vpc", privateDomain),
testAccDataSourceAwsRoute53ZoneCheck(
privateResourceName, "data.aws_route53_zone.by_tag", privateDomain),
resource.TestCheckResourceAttrPair(publicResourceName, "id", "data.aws_route53_zone.by_zone_id", "id"),
resource.TestCheckResourceAttrPair(publicResourceName, "name", "data.aws_route53_zone.by_zone_id", "name"),
resource.TestCheckResourceAttrPair(publicResourceName, "name_servers", "data.aws_route53_zone.by_zone_id", "name_servers"),
resource.TestCheckResourceAttrPair(publicResourceName, "id", "data.aws_route53_zone.by_name", "id"),
resource.TestCheckResourceAttrPair(publicResourceName, "name", "data.aws_route53_zone.by_name", "name"),
resource.TestCheckResourceAttrPair(publicResourceName, "name_servers", "data.aws_route53_zone.by_name", "name_servers"),
resource.TestCheckResourceAttrPair(privateResourceName, "id", "data.aws_route53_zone.by_vpc", "id"),
resource.TestCheckResourceAttrPair(privateResourceName, "name", "data.aws_route53_zone.by_vpc", "name"),
resource.TestCheckResourceAttrPair(privateResourceName, "id", "data.aws_route53_zone.by_tag", "id"),
resource.TestCheckResourceAttrPair(privateResourceName, "name", "data.aws_route53_zone.by_tag", "name"),
resource.TestCheckResourceAttrPair(serviceDiscoveryResourceName, "hosted_zone", "data.aws_route53_zone.service_discovery_by_vpc", "id"),
resource.TestCheckResourceAttrPair(serviceDiscoveryResourceName, "name", "data.aws_route53_zone.service_discovery_by_vpc", "name"),
resource.TestCheckResourceAttr("data.aws_route53_zone.service_discovery_by_vpc", "linked_service_principal", "servicediscovery.amazonaws.com"),
resource.TestMatchResourceAttr("data.aws_route53_zone.service_discovery_by_vpc", "linked_service_description", regexp.MustCompile(`^arn:[^:]+:servicediscovery:[^:]+:[^:]+:namespace/ns-\w+$`)),
),
},
},
})
}

// rsName for the name of the created resource
// dsName for the name of the created data source
// zName for the name of the domain
func testAccDataSourceAwsRoute53ZoneCheck(rsName, dsName, zName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[rsName]
if !ok {
return fmt.Errorf("root module has no resource called %s", rsName)
}

hostedZone, ok := s.RootModule().Resources[dsName]
if !ok {
return fmt.Errorf("can't find zone %q in state", dsName)
}

attr := rs.Primary.Attributes
if 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
}
}

func testAccDataSourceAwsRoute53ZoneConfig(rInt int) string {
return fmt.Sprintf(`
provider "aws" {
Expand All @@ -85,14 +58,14 @@ resource "aws_vpc" "test" {
}
resource "aws_route53_zone" "test_private" {
name = "test.acc-%d."
name = "test.acc-%[1]d."
vpc {
vpc_id = "${aws_vpc.test.id}"
}
tags = {
Environment = "dev-%d"
Environment = "dev-%[1]d"
}
}
Expand All @@ -106,12 +79,12 @@ data "aws_route53_zone" "by_tag" {
private_zone = true
tags = {
Environment = "dev-%d"
Environment = "dev-%[1]d"
}
}
resource "aws_route53_zone" "test" {
name = "terraformtestacchz-%d.com."
name = "terraformtestacchz-%[1]d.com."
}
data "aws_route53_zone" "by_zone_id" {
Expand All @@ -121,5 +94,15 @@ data "aws_route53_zone" "by_zone_id" {
data "aws_route53_zone" "by_name" {
name = "${data.aws_route53_zone.by_zone_id.name}"
}
`, rInt, rInt, rInt, rInt)
resource "aws_service_discovery_private_dns_namespace" "test_service_discovery" {
name = "test.acc-sd-%[1]d."
vpc = "${aws_vpc.test.id}"
}
data "aws_route53_zone" "service_discovery_by_vpc" {
name = "${aws_service_discovery_private_dns_namespace.test_service_discovery.name}"
vpc_id = "${aws_vpc.test.id}"
}
`, rInt)
}
4 changes: 3 additions & 1 deletion website/docs/d/route53_zone.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ The following attribute is additionally exported:
* `caller_reference` - Caller Reference of the Hosted Zone.
* `comment` - The comment field of the Hosted Zone.
* `name_servers` - The list of DNS name servers for the Hosted Zone.
* `resource_record_set_count` - the number of Record Set in the Hosted Zone
* `resource_record_set_count` - The number of Record Set in the Hosted Zone.
* `linked_service_principal` - The service that created the Hosted Zone (e.g. `servicediscovery.amazonaws.com`).
* `linked_service_description` - The description provided by the service that created the Hosted Zone (e.g. `arn:aws:servicediscovery:us-east-1:1234567890:namespace/ns-xxxxxxxxxxxxxxxx`).

0 comments on commit 85abeab

Please sign in to comment.