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