github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_vpn_connection_route_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/ec2" 10 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSVpnConnectionRoute_basic(t *testing.T) { 17 rBgpAsn := acctest.RandIntRange(64512, 65534) 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccAwsVpnConnectionRouteDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: testAccAwsVpnConnectionRouteConfig(rBgpAsn), 25 Check: resource.ComposeTestCheckFunc( 26 testAccAwsVpnConnectionRoute( 27 "aws_vpn_gateway.vpn_gateway", 28 "aws_customer_gateway.customer_gateway", 29 "aws_vpn_connection.vpn_connection", 30 "aws_vpn_connection_route.foo", 31 ), 32 ), 33 }, 34 resource.TestStep{ 35 Config: testAccAwsVpnConnectionRouteConfigUpdate(rBgpAsn), 36 Check: resource.ComposeTestCheckFunc( 37 testAccAwsVpnConnectionRoute( 38 "aws_vpn_gateway.vpn_gateway", 39 "aws_customer_gateway.customer_gateway", 40 "aws_vpn_connection.vpn_connection", 41 "aws_vpn_connection_route.foo", 42 ), 43 ), 44 }, 45 }, 46 }) 47 } 48 49 func testAccAwsVpnConnectionRouteDestroy(s *terraform.State) error { 50 conn := testAccProvider.Meta().(*AWSClient).ec2conn 51 for _, rs := range s.RootModule().Resources { 52 if rs.Type != "aws_vpn_connection_route" { 53 continue 54 } 55 56 cidrBlock, vpnConnectionId := resourceAwsVpnConnectionRouteParseId(rs.Primary.ID) 57 58 routeFilters := []*ec2.Filter{ 59 &ec2.Filter{ 60 Name: aws.String("route.destination-cidr-block"), 61 Values: []*string{aws.String(cidrBlock)}, 62 }, 63 &ec2.Filter{ 64 Name: aws.String("vpn-connection-id"), 65 Values: []*string{aws.String(vpnConnectionId)}, 66 }, 67 } 68 69 resp, err := conn.DescribeVpnConnections(&ec2.DescribeVpnConnectionsInput{ 70 Filters: routeFilters, 71 }) 72 if err != nil { 73 if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVpnConnectionID.NotFound" { 74 // not found, all good 75 return nil 76 } 77 return err 78 } 79 80 var vpnc *ec2.VpnConnection 81 if resp != nil { 82 // range over the connections and isolate the one we created 83 for _, v := range resp.VpnConnections { 84 if *v.VpnConnectionId == vpnConnectionId { 85 vpnc = v 86 } 87 } 88 89 if vpnc == nil { 90 // vpn connection not found, so that's good... 91 return nil 92 } 93 94 if vpnc.State != nil && *vpnc.State == "deleted" { 95 return nil 96 } 97 } 98 99 } 100 return fmt.Errorf("Fall through error, Check Destroy criteria not met") 101 } 102 103 func testAccAwsVpnConnectionRoute( 104 vpnGatewayResource string, 105 customerGatewayResource string, 106 vpnConnectionResource string, 107 vpnConnectionRouteResource string) resource.TestCheckFunc { 108 return func(s *terraform.State) error { 109 rs, ok := s.RootModule().Resources[vpnConnectionRouteResource] 110 if !ok { 111 return fmt.Errorf("Not found: %s", vpnConnectionRouteResource) 112 } 113 114 if rs.Primary.ID == "" { 115 return fmt.Errorf("No ID is set") 116 } 117 route, ok := s.RootModule().Resources[vpnConnectionRouteResource] 118 if !ok { 119 return fmt.Errorf("Not found: %s", vpnConnectionRouteResource) 120 } 121 122 cidrBlock, vpnConnectionId := resourceAwsVpnConnectionRouteParseId(route.Primary.ID) 123 124 routeFilters := []*ec2.Filter{ 125 &ec2.Filter{ 126 Name: aws.String("route.destination-cidr-block"), 127 Values: []*string{aws.String(cidrBlock)}, 128 }, 129 &ec2.Filter{ 130 Name: aws.String("vpn-connection-id"), 131 Values: []*string{aws.String(vpnConnectionId)}, 132 }, 133 } 134 135 ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn 136 137 _, err := ec2conn.DescribeVpnConnections(&ec2.DescribeVpnConnectionsInput{ 138 Filters: routeFilters, 139 }) 140 if err != nil { 141 return err 142 } 143 144 return nil 145 } 146 } 147 148 func testAccAwsVpnConnectionRouteConfig(rBgpAsn int) string { 149 return fmt.Sprintf(` 150 resource "aws_vpn_gateway" "vpn_gateway" { 151 tags { 152 Name = "vpn_gateway" 153 } 154 } 155 156 resource "aws_customer_gateway" "customer_gateway" { 157 bgp_asn = %d 158 ip_address = "182.0.0.1" 159 type = "ipsec.1" 160 } 161 162 resource "aws_vpn_connection" "vpn_connection" { 163 vpn_gateway_id = "${aws_vpn_gateway.vpn_gateway.id}" 164 customer_gateway_id = "${aws_customer_gateway.customer_gateway.id}" 165 type = "ipsec.1" 166 static_routes_only = true 167 } 168 169 resource "aws_vpn_connection_route" "foo" { 170 destination_cidr_block = "172.168.10.0/24" 171 vpn_connection_id = "${aws_vpn_connection.vpn_connection.id}" 172 } 173 `, rBgpAsn) 174 } 175 176 // Change destination_cidr_block 177 func testAccAwsVpnConnectionRouteConfigUpdate(rBgpAsn int) string { 178 return fmt.Sprintf(` 179 resource "aws_vpn_gateway" "vpn_gateway" { 180 tags { 181 Name = "vpn_gateway" 182 } 183 } 184 185 resource "aws_customer_gateway" "customer_gateway" { 186 bgp_asn = %d 187 ip_address = "182.0.0.1" 188 type = "ipsec.1" 189 } 190 191 resource "aws_vpn_connection" "vpn_connection" { 192 vpn_gateway_id = "${aws_vpn_gateway.vpn_gateway.id}" 193 customer_gateway_id = "${aws_customer_gateway.customer_gateway.id}" 194 type = "ipsec.1" 195 static_routes_only = true 196 } 197 198 resource "aws_vpn_connection_route" "foo" { 199 destination_cidr_block = "172.168.20.0/24" 200 vpn_connection_id = "${aws_vpn_connection.vpn_connection.id}" 201 } 202 `, rBgpAsn) 203 }