github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_route53_zone_association_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/helper/schema" 9 "github.com/hashicorp/terraform/terraform" 10 11 "github.com/aws/aws-sdk-go/aws" 12 "github.com/aws/aws-sdk-go/service/route53" 13 ) 14 15 func TestAccAWSRoute53ZoneAssociation_basic(t *testing.T) { 16 var zone route53.HostedZone 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckRoute53ZoneAssociationDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: testAccRoute53ZoneAssociationConfig, 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckRoute53ZoneAssociationExists("aws_route53_zone_association.foobar", &zone), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func TestAccAWSRoute53ZoneAssociation_region(t *testing.T) { 34 var zone route53.HostedZone 35 36 // record the initialized providers so that we can use them to 37 // check for the instances in each region 38 var providers []*schema.Provider 39 providerFactories := map[string]terraform.ResourceProviderFactory{ 40 "aws": func() (terraform.ResourceProvider, error) { 41 p := Provider() 42 providers = append(providers, p.(*schema.Provider)) 43 return p, nil 44 }, 45 } 46 47 resource.Test(t, resource.TestCase{ 48 PreCheck: func() { testAccPreCheck(t) }, 49 ProviderFactories: providerFactories, 50 CheckDestroy: testAccCheckRoute53ZoneAssociationDestroyWithProviders(&providers), 51 Steps: []resource.TestStep{ 52 resource.TestStep{ 53 Config: testAccRoute53ZoneAssociationRegionConfig, 54 Check: resource.ComposeTestCheckFunc( 55 testAccCheckRoute53ZoneAssociationExistsWithProviders("aws_route53_zone_association.foobar", &zone, &providers), 56 ), 57 }, 58 }, 59 }) 60 } 61 62 func testAccCheckRoute53ZoneAssociationDestroy(s *terraform.State) error { 63 return testAccCheckRoute53ZoneAssociationDestroyWithProvider(s, testAccProvider) 64 } 65 66 func testAccCheckRoute53ZoneAssociationDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc { 67 return func(s *terraform.State) error { 68 for _, provider := range *providers { 69 if provider.Meta() == nil { 70 continue 71 } 72 if err := testAccCheckRoute53ZoneAssociationDestroyWithProvider(s, provider); err != nil { 73 return err 74 } 75 } 76 return nil 77 } 78 } 79 80 func testAccCheckRoute53ZoneAssociationDestroyWithProvider(s *terraform.State, provider *schema.Provider) error { 81 conn := provider.Meta().(*AWSClient).r53conn 82 for _, rs := range s.RootModule().Resources { 83 if rs.Type != "aws_route53_zone_association" { 84 continue 85 } 86 87 zone_id, vpc_id := resourceAwsRoute53ZoneAssociationParseId(rs.Primary.ID) 88 89 resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{Id: aws.String(zone_id)}) 90 if err != nil { 91 exists := false 92 for _, vpc := range resp.VPCs { 93 if vpc_id == *vpc.VPCId { 94 exists = true 95 } 96 } 97 if exists { 98 return fmt.Errorf("VPC: %v is still associated to HostedZone: %v", vpc_id, zone_id) 99 } 100 } 101 } 102 return nil 103 } 104 105 func testAccCheckRoute53ZoneAssociationExists(n string, zone *route53.HostedZone) resource.TestCheckFunc { 106 return func(s *terraform.State) error { 107 return testAccCheckRoute53ZoneAssociationExistsWithProvider(s, n, zone, testAccProvider) 108 } 109 } 110 111 func testAccCheckRoute53ZoneAssociationExistsWithProviders(n string, zone *route53.HostedZone, providers *[]*schema.Provider) resource.TestCheckFunc { 112 return func(s *terraform.State) error { 113 for _, provider := range *providers { 114 if provider.Meta() == nil { 115 continue 116 } 117 if err := testAccCheckRoute53ZoneAssociationExistsWithProvider(s, n, zone, provider); err != nil { 118 return err 119 } 120 } 121 return nil 122 } 123 } 124 125 func testAccCheckRoute53ZoneAssociationExistsWithProvider(s *terraform.State, n string, zone *route53.HostedZone, provider *schema.Provider) error { 126 rs, ok := s.RootModule().Resources[n] 127 if !ok { 128 return fmt.Errorf("Not found: %s", n) 129 } 130 131 if rs.Primary.ID == "" { 132 return fmt.Errorf("No zone association ID is set") 133 } 134 135 zone_id, vpc_id := resourceAwsRoute53ZoneAssociationParseId(rs.Primary.ID) 136 137 conn := provider.Meta().(*AWSClient).r53conn 138 resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{Id: aws.String(zone_id)}) 139 if err != nil { 140 return fmt.Errorf("Hosted zone err: %v", err) 141 } 142 143 exists := false 144 for _, vpc := range resp.VPCs { 145 if vpc_id == *vpc.VPCId { 146 exists = true 147 } 148 } 149 if !exists { 150 return fmt.Errorf("Hosted zone association not found") 151 } 152 153 *zone = *resp.HostedZone 154 return nil 155 } 156 157 const testAccRoute53ZoneAssociationConfig = ` 158 resource "aws_vpc" "foo" { 159 cidr_block = "10.6.0.0/16" 160 enable_dns_hostnames = true 161 enable_dns_support = true 162 } 163 164 resource "aws_vpc" "bar" { 165 cidr_block = "10.7.0.0/16" 166 enable_dns_hostnames = true 167 enable_dns_support = true 168 } 169 170 resource "aws_route53_zone" "foo" { 171 name = "foo.com" 172 vpc_id = "${aws_vpc.foo.id}" 173 } 174 175 resource "aws_route53_zone_association" "foobar" { 176 zone_id = "${aws_route53_zone.foo.id}" 177 vpc_id = "${aws_vpc.bar.id}" 178 } 179 ` 180 181 const testAccRoute53ZoneAssociationRegionConfig = ` 182 provider "aws" { 183 alias = "west" 184 region = "us-west-2" 185 } 186 187 provider "aws" { 188 alias = "east" 189 region = "us-east-1" 190 } 191 192 resource "aws_vpc" "foo" { 193 provider = "aws.west" 194 cidr_block = "10.6.0.0/16" 195 enable_dns_hostnames = true 196 enable_dns_support = true 197 } 198 199 resource "aws_vpc" "bar" { 200 provider = "aws.east" 201 cidr_block = "10.7.0.0/16" 202 enable_dns_hostnames = true 203 enable_dns_support = true 204 } 205 206 resource "aws_route53_zone" "foo" { 207 provider = "aws.west" 208 name = "foo.com" 209 vpc_id = "${aws_vpc.foo.id}" 210 vpc_region = "us-west-2" 211 } 212 213 resource "aws_route53_zone_association" "foobar" { 214 provider = "aws.west" 215 zone_id = "${aws_route53_zone.foo.id}" 216 vpc_id = "${aws_vpc.bar.id}" 217 vpc_region = "us-east-1" 218 } 219 `