Add tests for cross region VPC associations.

This commit is contained in:
John Engelman 2015-05-08 14:47:42 -05:00
parent 3507c0618b
commit 766aead4a5
4 changed files with 272 additions and 58 deletions

View File

@ -38,6 +38,7 @@ func resourceAwsRoute53Zone() *schema.Resource {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
ForceNew: true, ForceNew: true,
Computed: true,
}, },
"zone_id": &schema.Schema{ "zone_id": &schema.Schema{
@ -85,6 +86,7 @@ func resourceAwsRoute53ZoneCreate(d *schema.ResourceData, meta interface{}) erro
zone := cleanZoneID(*resp.HostedZone.ID) zone := cleanZoneID(*resp.HostedZone.ID)
d.Set("zone_id", zone) d.Set("zone_id", zone)
d.SetId(zone) d.SetId(zone)
d.Set("vpc_region", req.VPC.VPCRegion)
// Wait until we are done initializing // Wait until we are done initializing
wait := resource.StateChangeConf{ wait := resource.StateChangeConf{

View File

@ -34,6 +34,7 @@ func resourceAwsRoute53ZoneAssociation() *schema.Resource {
"vpc_region": &schema.Schema{ "vpc_region": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Computed: true,
}, },
}, },
@ -55,7 +56,7 @@ func resourceAwsRoute53ZoneAssociationCreate(d *schema.ResourceData, meta interf
req.VPC.VPCRegion = aws.String(w.(string)) req.VPC.VPCRegion = aws.String(w.(string))
} }
log.Printf("[DEBUG] Associating Route53 Private Zone %s with VPC %s", *req.HostedZoneID, *req.VPC.VPCID) log.Printf("[DEBUG] Associating Route53 Private Zone %s with VPC %s with region %s", *req.HostedZoneID, *req.VPC.VPCID, *req.VPC.VPCRegion)
resp, err := r53.AssociateVPCWithHostedZone(req) resp, err := r53.AssociateVPCWithHostedZone(req)
if err != nil { if err != nil {
return err return err

View File

@ -5,6 +5,7 @@ import (
"testing" "testing"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/awslabs/aws-sdk-go/aws" "github.com/awslabs/aws-sdk-go/aws"
@ -22,7 +23,36 @@ func TestAccRoute53ZoneAssociation(t *testing.T) {
resource.TestStep{ resource.TestStep{
Config: testAccRoute53ZoneAssociationConfig, Config: testAccRoute53ZoneAssociationConfig,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53ZoneAssociationExists("aws_route53_zone_association.main", &zone), testAccCheckRoute53ZoneAssociationExists("aws_route53_zone_association.foobar", &zone),
),
},
},
})
}
func TestAccRoute53ZoneAssociationWithRegion(t *testing.T) {
var zone route53.HostedZone
// record the initialized providers so that we can use them to
// check for the instances in each region
var providers []*schema.Provider
providerFactories := map[string]terraform.ResourceProviderFactory{
"aws": func() (terraform.ResourceProvider, error) {
p := Provider()
providers = append(providers, p.(*schema.Provider))
return p, nil
},
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckRoute53ZoneAssociationDestroyWithProviders(&providers),
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRoute53ZoneAssociationRegionConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53ZoneAssociationExistsWithProviders("aws_route53_zone_association.foobar", &zone, &providers),
), ),
}, },
}, },
@ -30,15 +60,43 @@ func TestAccRoute53ZoneAssociation(t *testing.T) {
} }
func testAccCheckRoute53ZoneAssociationDestroy(s *terraform.State) error { func testAccCheckRoute53ZoneAssociationDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).r53conn return testAccCheckRoute53ZoneAssociationDestroyWithProvider(s, testAccProvider)
}
func testAccCheckRoute53ZoneAssociationDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, provider := range *providers {
if provider.Meta() == nil {
continue
}
if err := testAccCheckRoute53ZoneAssociationDestroyWithProvider(s, provider); err != nil {
return err
}
}
return nil
}
}
func testAccCheckRoute53ZoneAssociationDestroyWithProvider(s *terraform.State, provider *schema.Provider) error {
conn := provider.Meta().(*AWSClient).r53conn
for _, rs := range s.RootModule().Resources { for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_route53_zone" { if rs.Type != "aws_route53_zone_association" {
continue continue
} }
_, err := conn.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(rs.Primary.ID)}) zone_id, vpc_id := resourceAwsRoute53ZoneAssociationParseId(rs.Primary.ID)
if err == nil {
return fmt.Errorf("Hosted zone still exists") resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(zone_id)})
if err != nil {
exists := false
for _, vpc := range resp.VPCs {
if vpc_id == *vpc.VPCID {
exists = true
}
}
if exists {
return fmt.Errorf("VPC: %v is still associated to HostedZone: %v", vpc_id, zone_id)
}
} }
} }
return nil return nil
@ -46,24 +104,45 @@ func testAccCheckRoute53ZoneAssociationDestroy(s *terraform.State) error {
func testAccCheckRoute53ZoneAssociationExists(n string, zone *route53.HostedZone) resource.TestCheckFunc { func testAccCheckRoute53ZoneAssociationExists(n string, zone *route53.HostedZone) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
return testAccCheckRoute53ZoneAssociationExistsWithProvider(s, n, zone, testAccProvider)
}
}
func testAccCheckRoute53ZoneAssociationExistsWithProviders(n string, zone *route53.HostedZone, providers *[]*schema.Provider) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, provider := range *providers {
if provider.Meta() == nil {
continue
}
if err := testAccCheckRoute53ZoneAssociationExistsWithProvider(s, n, zone, provider); err != nil {
return err
}
}
return nil
}
}
func testAccCheckRoute53ZoneAssociationExistsWithProvider(s *terraform.State, n string, zone *route53.HostedZone, provider *schema.Provider) error {
rs, ok := s.RootModule().Resources[n] rs, ok := s.RootModule().Resources[n]
if !ok { if !ok {
return fmt.Errorf("Not found: %s", n) return fmt.Errorf("Not found: %s", n)
} }
if rs.Primary.ID == "" { if rs.Primary.ID == "" {
return fmt.Errorf("No hosted zone ID is set") return fmt.Errorf("No zone association ID is set")
} }
conn := testAccProvider.Meta().(*AWSClient).r53conn zone_id, vpc_id := resourceAwsRoute53ZoneAssociationParseId(rs.Primary.ID)
resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(rs.Primary.ID)})
conn := provider.Meta().(*AWSClient).r53conn
resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(zone_id)})
if err != nil { if err != nil {
return fmt.Errorf("Hosted zone err: %v", err) return fmt.Errorf("Hosted zone err: %v", err)
} }
exists := false exists := false
for _, vpc := range resp.VPCs { for _, vpc := range resp.VPCs {
if rs.Primary.Meta["vpc_id"] == *vpc.VPCID { if vpc_id == *vpc.VPCID {
exists = true exists = true
} }
} }
@ -74,7 +153,6 @@ func testAccCheckRoute53ZoneAssociationExists(n string, zone *route53.HostedZone
*zone = *resp.HostedZone *zone = *resp.HostedZone
return nil return nil
} }
}
const testAccRoute53ZoneAssociationConfig = ` const testAccRoute53ZoneAssociationConfig = `
resource "aws_vpc" "foo" { resource "aws_vpc" "foo" {
@ -99,3 +177,42 @@ resource "aws_route53_zone_association" "foobar" {
vpc_id = "${aws_vpc.bar.id}" vpc_id = "${aws_vpc.bar.id}"
} }
` `
const testAccRoute53ZoneAssociationRegionConfig = `
provider "aws" {
alias = "west"
region = "us-west-2"
}
provider "aws" {
alias = "east"
region = "us-east-1"
}
resource "aws_vpc" "foo" {
provider = "aws.west"
cidr_block = "10.6.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
}
resource "aws_vpc" "bar" {
provider = "aws.east"
cidr_block = "10.7.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
}
resource "aws_route53_zone" "foo" {
provider = "aws.west"
name = "foo.com"
vpc_id = "${aws_vpc.foo.id}"
}
resource "aws_route53_zone_association" "foobar" {
provider = "aws.west"
zone_id = "${aws_route53_zone.foo.id}"
vpc_id = "${aws_vpc.bar.id}"
vpc_region = "us-east-1"
}
`

View File

@ -6,6 +6,7 @@ import (
"testing" "testing"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/awslabs/aws-sdk-go/aws" "github.com/awslabs/aws-sdk-go/aws"
@ -103,8 +104,56 @@ func TestAccRoute53PrivateZone(t *testing.T) {
}) })
} }
func TestAccRoute53PrivateZoneWithRegion(t *testing.T) {
var zone route53.GetHostedZoneOutput
// record the initialized providers so that we can use them to
// check for the instances in each region
var providers []*schema.Provider
providerFactories := map[string]terraform.ResourceProviderFactory{
"aws": func() (terraform.ResourceProvider, error) {
p := Provider()
providers = append(providers, p.(*schema.Provider))
return p, nil
},
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckRoute53ZoneDestroyWithProviders(&providers),
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRoute53PrivateZoneRegionConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53ZoneExistsWithProviders("aws_route53_zone.main", &zone, &providers),
testAccCheckRoute53ZoneAssociatesWithVpc("aws_vpc.main", &zone),
),
},
},
})
}
func testAccCheckRoute53ZoneDestroy(s *terraform.State) error { func testAccCheckRoute53ZoneDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).r53conn return testAccCheckRoute53ZoneDestroyWithProvider(s, testAccProvider)
}
func testAccCheckRoute53ZoneDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, provider := range *providers {
if provider.Meta() == nil {
continue
}
if err := testAccCheckRoute53ZoneDestroyWithProvider(s, provider); err != nil {
return err
}
}
return nil
}
}
func testAccCheckRoute53ZoneDestroyWithProvider(s *terraform.State, provider *schema.Provider) error {
conn := provider.Meta().(*AWSClient).r53conn
for _, rs := range s.RootModule().Resources { for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_route53_zone" { if rs.Type != "aws_route53_zone" {
continue continue
@ -120,6 +169,25 @@ func testAccCheckRoute53ZoneDestroy(s *terraform.State) error {
func testAccCheckRoute53ZoneExists(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc { func testAccCheckRoute53ZoneExists(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
return testAccCheckRoute53ZoneExistsWithProvider(s, n, zone, testAccProvider)
}
}
func testAccCheckRoute53ZoneExistsWithProviders(n string, zone *route53.GetHostedZoneOutput, providers *[]*schema.Provider) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, provider := range *providers {
if provider.Meta() == nil {
continue
}
if err := testAccCheckRoute53ZoneExistsWithProvider(s, n, zone, provider); err != nil {
return err
}
}
return nil
}
}
func testAccCheckRoute53ZoneExistsWithProvider(s *terraform.State, n string, zone *route53.GetHostedZoneOutput, provider *schema.Provider) error {
rs, ok := s.RootModule().Resources[n] rs, ok := s.RootModule().Resources[n]
if !ok { if !ok {
return fmt.Errorf("Not found: %s", n) return fmt.Errorf("Not found: %s", n)
@ -129,7 +197,7 @@ func testAccCheckRoute53ZoneExists(n string, zone *route53.GetHostedZoneOutput)
return fmt.Errorf("No hosted zone ID is set") return fmt.Errorf("No hosted zone ID is set")
} }
conn := testAccProvider.Meta().(*AWSClient).r53conn conn := provider.Meta().(*AWSClient).r53conn
resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(rs.Primary.ID)}) resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(rs.Primary.ID)})
if err != nil { if err != nil {
return fmt.Errorf("Hosted zone err: %v", err) return fmt.Errorf("Hosted zone err: %v", err)
@ -153,7 +221,6 @@ func testAccCheckRoute53ZoneExists(n string, zone *route53.GetHostedZoneOutput)
*zone = *resp *zone = *resp
return nil return nil
} }
}
func testAccCheckRoute53ZoneAssociatesWithVpc(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc { func testAccCheckRoute53ZoneAssociatesWithVpc(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
@ -226,3 +293,30 @@ resource "aws_route53_zone" "main" {
vpc_id = "${aws_vpc.main.id}" vpc_id = "${aws_vpc.main.id}"
} }
` `
const testAccRoute53PrivateZoneRegionConfig = `
provider "aws" {
alias = "west"
region = "us-west-2"
}
provider "aws" {
alias = "east"
region = "us-east-1"
}
resource "aws_vpc" "main" {
provider = "aws.east"
cidr_block = "172.29.0.0/24"
instance_tenancy = "default"
enable_dns_support = true
enable_dns_hostnames = true
}
resource "aws_route53_zone" "main" {
provider = "aws.west"
name = "hashicorp.com"
vpc_id = "${aws_vpc.main.id}"
vpc_region = "us-east-1"
}
`