github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_customer_gateway_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "regexp" 6 "testing" 7 "time" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/aws/awserr" 11 "github.com/aws/aws-sdk-go/service/ec2" 12 13 "github.com/hashicorp/terraform/helper/acctest" 14 "github.com/hashicorp/terraform/helper/resource" 15 "github.com/hashicorp/terraform/terraform" 16 ) 17 18 func TestAccAWSCustomerGateway_basic(t *testing.T) { 19 var gateway ec2.CustomerGateway 20 rBgpAsn := acctest.RandIntRange(64512, 65534) 21 rInt := acctest.RandInt() 22 resource.Test(t, resource.TestCase{ 23 PreCheck: func() { testAccPreCheck(t) }, 24 IDRefreshName: "aws_customer_gateway.foo", 25 Providers: testAccProviders, 26 CheckDestroy: testAccCheckCustomerGatewayDestroy, 27 Steps: []resource.TestStep{ 28 { 29 Config: testAccCustomerGatewayConfig(rInt, rBgpAsn), 30 Check: resource.ComposeTestCheckFunc( 31 testAccCheckCustomerGateway("aws_customer_gateway.foo", &gateway), 32 ), 33 }, 34 { 35 Config: testAccCustomerGatewayConfigUpdateTags(rInt, rBgpAsn), 36 Check: resource.ComposeTestCheckFunc( 37 testAccCheckCustomerGateway("aws_customer_gateway.foo", &gateway), 38 ), 39 }, 40 { 41 Config: testAccCustomerGatewayConfigForceReplace(rInt, rBgpAsn), 42 Check: resource.ComposeTestCheckFunc( 43 testAccCheckCustomerGateway("aws_customer_gateway.foo", &gateway), 44 ), 45 }, 46 }, 47 }) 48 } 49 50 func TestAccAWSCustomerGateway_similarAlreadyExists(t *testing.T) { 51 var gateway ec2.CustomerGateway 52 rInt := acctest.RandInt() 53 rBgpAsn := acctest.RandIntRange(64512, 65534) 54 resource.Test(t, resource.TestCase{ 55 PreCheck: func() { testAccPreCheck(t) }, 56 IDRefreshName: "aws_customer_gateway.foo", 57 Providers: testAccProviders, 58 CheckDestroy: testAccCheckCustomerGatewayDestroy, 59 Steps: []resource.TestStep{ 60 { 61 Config: testAccCustomerGatewayConfig(rInt, rBgpAsn), 62 Check: resource.ComposeTestCheckFunc( 63 testAccCheckCustomerGateway("aws_customer_gateway.foo", &gateway), 64 ), 65 }, 66 { 67 Config: testAccCustomerGatewayConfigIdentical(rInt, rBgpAsn), 68 ExpectError: regexp.MustCompile("An existing customer gateway"), 69 }, 70 }, 71 }) 72 } 73 74 func TestAccAWSCustomerGateway_disappears(t *testing.T) { 75 rInt := acctest.RandInt() 76 rBgpAsn := acctest.RandIntRange(64512, 65534) 77 var gateway ec2.CustomerGateway 78 resource.Test(t, resource.TestCase{ 79 PreCheck: func() { testAccPreCheck(t) }, 80 Providers: testAccProviders, 81 CheckDestroy: testAccCheckCustomerGatewayDestroy, 82 Steps: []resource.TestStep{ 83 { 84 Config: testAccCustomerGatewayConfig(rInt, rBgpAsn), 85 Check: resource.ComposeTestCheckFunc( 86 testAccCheckCustomerGateway("aws_customer_gateway.foo", &gateway), 87 testAccAWSCustomerGatewayDisappears(&gateway), 88 ), 89 ExpectNonEmptyPlan: true, 90 }, 91 }, 92 }) 93 } 94 95 func testAccAWSCustomerGatewayDisappears(gateway *ec2.CustomerGateway) resource.TestCheckFunc { 96 return func(s *terraform.State) error { 97 conn := testAccProvider.Meta().(*AWSClient).ec2conn 98 opts := &ec2.DeleteCustomerGatewayInput{ 99 CustomerGatewayId: gateway.CustomerGatewayId, 100 } 101 if _, err := conn.DeleteCustomerGateway(opts); err != nil { 102 return err 103 } 104 return resource.Retry(40*time.Minute, func() *resource.RetryError { 105 opts := &ec2.DescribeCustomerGatewaysInput{ 106 CustomerGatewayIds: []*string{gateway.CustomerGatewayId}, 107 } 108 resp, err := conn.DescribeCustomerGateways(opts) 109 if err != nil { 110 cgw, ok := err.(awserr.Error) 111 if ok && cgw.Code() == "InvalidCustomerGatewayID.NotFound" { 112 return nil 113 } 114 return resource.NonRetryableError( 115 fmt.Errorf("Error retrieving Customer Gateway: %s", err)) 116 } 117 if *resp.CustomerGateways[0].State == "deleted" { 118 return nil 119 } 120 return resource.RetryableError(fmt.Errorf( 121 "Waiting for Customer Gateway: %v", gateway.CustomerGatewayId)) 122 }) 123 } 124 } 125 126 func testAccCheckCustomerGatewayDestroy(s *terraform.State) error { 127 conn := testAccProvider.Meta().(*AWSClient).ec2conn 128 129 for _, rs := range s.RootModule().Resources { 130 if rs.Type != "aws_customer_gatewah" { 131 continue 132 } 133 134 gatewayFilter := &ec2.Filter{ 135 Name: aws.String("customer-gateway-id"), 136 Values: []*string{aws.String(rs.Primary.ID)}, 137 } 138 139 resp, err := conn.DescribeCustomerGateways(&ec2.DescribeCustomerGatewaysInput{ 140 Filters: []*ec2.Filter{gatewayFilter}, 141 }) 142 143 if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidCustomerGatewayID.NotFound" { 144 continue 145 } 146 147 if err == nil { 148 if len(resp.CustomerGateways) > 0 { 149 return fmt.Errorf("Customer gateway still exists: %v", resp.CustomerGateways) 150 } 151 152 if *resp.CustomerGateways[0].State == "deleted" { 153 continue 154 } 155 } 156 157 return err 158 } 159 160 return nil 161 } 162 163 func testAccCheckCustomerGateway(gatewayResource string, cgw *ec2.CustomerGateway) resource.TestCheckFunc { 164 return func(s *terraform.State) error { 165 rs, ok := s.RootModule().Resources[gatewayResource] 166 if !ok { 167 return fmt.Errorf("Not found: %s", gatewayResource) 168 } 169 170 if rs.Primary.ID == "" { 171 return fmt.Errorf("No ID is set") 172 } 173 174 gateway, ok := s.RootModule().Resources[gatewayResource] 175 if !ok { 176 return fmt.Errorf("Not found: %s", gatewayResource) 177 } 178 179 ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn 180 gatewayFilter := &ec2.Filter{ 181 Name: aws.String("customer-gateway-id"), 182 Values: []*string{aws.String(gateway.Primary.ID)}, 183 } 184 185 resp, err := ec2conn.DescribeCustomerGateways(&ec2.DescribeCustomerGatewaysInput{ 186 Filters: []*ec2.Filter{gatewayFilter}, 187 }) 188 189 if err != nil { 190 return err 191 } 192 193 respGateway := resp.CustomerGateways[0] 194 *cgw = *respGateway 195 196 return nil 197 } 198 } 199 200 func testAccCustomerGatewayConfig(rInt, rBgpAsn int) string { 201 return fmt.Sprintf(` 202 resource "aws_customer_gateway" "foo" { 203 bgp_asn = %d 204 ip_address = "172.0.0.1" 205 type = "ipsec.1" 206 tags { 207 Name = "foo-gateway-%d" 208 } 209 } 210 `, rBgpAsn, rInt) 211 } 212 213 func testAccCustomerGatewayConfigIdentical(randInt, rBgpAsn int) string { 214 return fmt.Sprintf(` 215 resource "aws_customer_gateway" "foo" { 216 bgp_asn = %d 217 ip_address = "172.0.0.1" 218 type = "ipsec.1" 219 tags { 220 Name = "foo-gateway-%d" 221 } 222 } 223 resource "aws_customer_gateway" "identical" { 224 bgp_asn = %d 225 ip_address = "172.0.0.1" 226 type = "ipsec.1" 227 tags { 228 Name = "foo-gateway-identical-%d" 229 } 230 } 231 `, rBgpAsn, randInt, rBgpAsn, randInt) 232 } 233 234 // Add the Another: "tag" tag. 235 func testAccCustomerGatewayConfigUpdateTags(rInt, rBgpAsn int) string { 236 return fmt.Sprintf(` 237 resource "aws_customer_gateway" "foo" { 238 bgp_asn = %d 239 ip_address = "172.0.0.1" 240 type = "ipsec.1" 241 tags { 242 Name = "foo-gateway-%d" 243 Another = "tag" 244 } 245 } 246 `, rBgpAsn, rInt) 247 } 248 249 // Change the ip_address. 250 func testAccCustomerGatewayConfigForceReplace(rInt, rBgpAsn int) string { 251 return fmt.Sprintf(` 252 resource "aws_customer_gateway" "foo" { 253 bgp_asn = %d 254 ip_address = "172.10.10.1" 255 type = "ipsec.1" 256 tags { 257 Name = "foo-gateway-%d" 258 Another = "tag" 259 } 260 } 261 `, rBgpAsn, rInt) 262 }