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/aws_route53_zone: add linked service attributes #9390

Merged
merged 1 commit into from
Jul 18, 2019
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
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,
Copy link
Contributor

Choose a reason for hiding this comment

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

🙇 thank you

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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️ thank you!

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`).