2015-05-06 18:01:19 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-05-08 21:47:42 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-05-06 18:01:19 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
|
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
|
|
|
"github.com/awslabs/aws-sdk-go/service/route53"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccRoute53ZoneAssociation(t *testing.T) {
|
|
|
|
var zone route53.HostedZone
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckRoute53ZoneAssociationDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRoute53ZoneAssociationConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-05-08 21:47:42 +02:00
|
|
|
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),
|
2015-05-06 18:01:19 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckRoute53ZoneAssociationDestroy(s *terraform.State) error {
|
2015-05-08 21:47:42 +02:00
|
|
|
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
|
2015-05-06 18:01:19 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2015-05-08 21:47:42 +02:00
|
|
|
if rs.Type != "aws_route53_zone_association" {
|
2015-05-06 18:01:19 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-05-08 21:47:42 +02:00
|
|
|
zone_id, vpc_id := resourceAwsRoute53ZoneAssociationParseId(rs.Primary.ID)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2015-05-06 18:01:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckRoute53ZoneAssociationExists(n string, zone *route53.HostedZone) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2015-05-08 21:47:42 +02:00
|
|
|
return testAccCheckRoute53ZoneAssociationExistsWithProvider(s, n, zone, testAccProvider)
|
|
|
|
}
|
|
|
|
}
|
2015-05-06 18:01:19 +02:00
|
|
|
|
2015-05-08 21:47:42 +02:00
|
|
|
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
|
|
|
|
}
|
2015-05-06 18:01:19 +02:00
|
|
|
}
|
2015-05-08 21:47:42 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2015-05-06 18:01:19 +02:00
|
|
|
|
2015-05-08 21:47:42 +02:00
|
|
|
func testAccCheckRoute53ZoneAssociationExistsWithProvider(s *terraform.State, n string, zone *route53.HostedZone, provider *schema.Provider) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
2015-05-06 18:01:19 +02:00
|
|
|
|
2015-05-08 21:47:42 +02:00
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No zone association ID is set")
|
|
|
|
}
|
2015-05-06 18:01:19 +02:00
|
|
|
|
2015-05-08 21:47:42 +02:00
|
|
|
zone_id, vpc_id := resourceAwsRoute53ZoneAssociationParseId(rs.Primary.ID)
|
|
|
|
|
|
|
|
conn := provider.Meta().(*AWSClient).r53conn
|
|
|
|
resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(zone_id)})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Hosted zone err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exists := false
|
|
|
|
for _, vpc := range resp.VPCs {
|
|
|
|
if vpc_id == *vpc.VPCID {
|
|
|
|
exists = true
|
|
|
|
}
|
2015-05-06 18:01:19 +02:00
|
|
|
}
|
2015-05-08 21:47:42 +02:00
|
|
|
if !exists {
|
|
|
|
return fmt.Errorf("Hosted zone association not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*zone = *resp.HostedZone
|
|
|
|
return nil
|
2015-05-06 18:01:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const testAccRoute53ZoneAssociationConfig = `
|
2015-05-08 09:16:24 +02:00
|
|
|
resource "aws_vpc" "foo" {
|
2015-05-06 18:01:19 +02:00
|
|
|
cidr_block = "10.6.0.0/16"
|
|
|
|
enable_dns_hostnames = true
|
|
|
|
enable_dns_support = true
|
|
|
|
}
|
|
|
|
|
2015-05-08 09:16:24 +02:00
|
|
|
resource "aws_vpc" "bar" {
|
|
|
|
cidr_block = "10.7.0.0/16"
|
|
|
|
enable_dns_hostnames = true
|
|
|
|
enable_dns_support = true
|
|
|
|
}
|
2015-05-06 18:01:19 +02:00
|
|
|
|
2015-05-08 09:16:24 +02:00
|
|
|
resource "aws_route53_zone" "foo" {
|
|
|
|
name = "foo.com"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2015-05-06 18:01:19 +02:00
|
|
|
}
|
|
|
|
|
2015-05-08 09:16:24 +02:00
|
|
|
resource "aws_route53_zone_association" "foobar" {
|
|
|
|
zone_id = "${aws_route53_zone.foo.id}"
|
|
|
|
vpc_id = "${aws_vpc.bar.id}"
|
2015-05-06 18:01:19 +02:00
|
|
|
}
|
|
|
|
`
|
2015-05-08 21:47:42 +02:00
|
|
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
`
|