github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_vpn_gateway_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  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSVpnGateway_basic(t *testing.T) {
    15  	var v, v2 ec2.VpnGateway
    16  
    17  	testNotEqual := func(*terraform.State) error {
    18  		if len(v.VpcAttachments) == 0 {
    19  			return fmt.Errorf("VPN gateway A is not attached")
    20  		}
    21  		if len(v2.VpcAttachments) == 0 {
    22  			return fmt.Errorf("VPN gateway B is not attached")
    23  		}
    24  
    25  		id1 := v.VpcAttachments[0].VpcId
    26  		id2 := v2.VpcAttachments[0].VpcId
    27  		if id1 == id2 {
    28  			return fmt.Errorf("Both attachment IDs are the same")
    29  		}
    30  
    31  		return nil
    32  	}
    33  
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccCheckVpnGatewayDestroy,
    38  		Steps: []resource.TestStep{
    39  			resource.TestStep{
    40  				Config: testAccVpnGatewayConfig,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckVpnGatewayExists(
    43  						"aws_vpn_gateway.foo", &v),
    44  				),
    45  			},
    46  
    47  			resource.TestStep{
    48  				Config: testAccVpnGatewayConfigChangeVPC,
    49  				Check: resource.ComposeTestCheckFunc(
    50  					testAccCheckVpnGatewayExists(
    51  						"aws_vpn_gateway.foo", &v2),
    52  					testNotEqual,
    53  				),
    54  			},
    55  		},
    56  	})
    57  }
    58  
    59  func TestAccAWSVpnGateway_delete(t *testing.T) {
    60  	var vpnGateway ec2.VpnGateway
    61  
    62  	testDeleted := func(r string) resource.TestCheckFunc {
    63  		return func(s *terraform.State) error {
    64  			_, ok := s.RootModule().Resources[r]
    65  			if ok {
    66  				return fmt.Errorf("VPN Gateway %q should have been deleted", r)
    67  			}
    68  			return nil
    69  		}
    70  	}
    71  
    72  	resource.Test(t, resource.TestCase{
    73  		PreCheck:     func() { testAccPreCheck(t) },
    74  		Providers:    testAccProviders,
    75  		CheckDestroy: testAccCheckVpnGatewayDestroy,
    76  		Steps: []resource.TestStep{
    77  			resource.TestStep{
    78  				Config: testAccVpnGatewayConfig,
    79  				Check: resource.ComposeTestCheckFunc(
    80  					testAccCheckVpnGatewayExists("aws_vpn_gateway.foo", &vpnGateway)),
    81  			},
    82  			resource.TestStep{
    83  				Config: testAccNoVpnGatewayConfig,
    84  				Check:  resource.ComposeTestCheckFunc(testDeleted("aws_vpn_gateway.foo")),
    85  			},
    86  		},
    87  	})
    88  }
    89  
    90  func TestAccAWSVpnGateway_tags(t *testing.T) {
    91  	var v ec2.VpnGateway
    92  
    93  	resource.Test(t, resource.TestCase{
    94  		PreCheck:     func() { testAccPreCheck(t) },
    95  		Providers:    testAccProviders,
    96  		CheckDestroy: testAccCheckVpnGatewayDestroy,
    97  		Steps: []resource.TestStep{
    98  			resource.TestStep{
    99  				Config: testAccCheckVpnGatewayConfigTags,
   100  				Check: resource.ComposeTestCheckFunc(
   101  					testAccCheckVpnGatewayExists("aws_vpn_gateway.foo", &v),
   102  					testAccCheckTags(&v.Tags, "foo", "bar"),
   103  				),
   104  			},
   105  
   106  			resource.TestStep{
   107  				Config: testAccCheckVpnGatewayConfigTagsUpdate,
   108  				Check: resource.ComposeTestCheckFunc(
   109  					testAccCheckVpnGatewayExists("aws_vpn_gateway.foo", &v),
   110  					testAccCheckTags(&v.Tags, "foo", ""),
   111  					testAccCheckTags(&v.Tags, "bar", "baz"),
   112  				),
   113  			},
   114  		},
   115  	})
   116  }
   117  
   118  func testAccCheckVpnGatewayDestroy(s *terraform.State) error {
   119  	ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
   120  
   121  	for _, rs := range s.RootModule().Resources {
   122  		if rs.Type != "aws_vpn_gateway" {
   123  			continue
   124  		}
   125  
   126  		// Try to find the resource
   127  		resp, err := ec2conn.DescribeVpnGateways(&ec2.DescribeVpnGatewaysInput{
   128  			VpnGatewayIds: []*string{aws.String(rs.Primary.ID)},
   129  		})
   130  		if err == nil {
   131  			if len(resp.VpnGateways) > 0 {
   132  				return fmt.Errorf("still exists")
   133  			}
   134  
   135  			return nil
   136  		}
   137  
   138  		// Verify the error is what we want
   139  		ec2err, ok := err.(awserr.Error)
   140  		if !ok {
   141  			return err
   142  		}
   143  		if ec2err.Code() != "InvalidVpnGatewayID.NotFound" {
   144  			return err
   145  		}
   146  	}
   147  
   148  	return nil
   149  }
   150  
   151  func testAccCheckVpnGatewayExists(n string, ig *ec2.VpnGateway) resource.TestCheckFunc {
   152  	return func(s *terraform.State) error {
   153  		rs, ok := s.RootModule().Resources[n]
   154  		if !ok {
   155  			return fmt.Errorf("Not found: %s", n)
   156  		}
   157  
   158  		if rs.Primary.ID == "" {
   159  			return fmt.Errorf("No ID is set")
   160  		}
   161  
   162  		ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
   163  		resp, err := ec2conn.DescribeVpnGateways(&ec2.DescribeVpnGatewaysInput{
   164  			VpnGatewayIds: []*string{aws.String(rs.Primary.ID)},
   165  		})
   166  		if err != nil {
   167  			return err
   168  		}
   169  		if len(resp.VpnGateways) == 0 {
   170  			return fmt.Errorf("VPNGateway not found")
   171  		}
   172  
   173  		*ig = *resp.VpnGateways[0]
   174  
   175  		return nil
   176  	}
   177  }
   178  
   179  const testAccNoVpnGatewayConfig = `
   180  resource "aws_vpc" "foo" {
   181  	cidr_block = "10.1.0.0/16"
   182  }
   183  `
   184  
   185  const testAccVpnGatewayConfig = `
   186  resource "aws_vpc" "foo" {
   187  	cidr_block = "10.1.0.0/16"
   188  }
   189  
   190  resource "aws_vpn_gateway" "foo" {
   191  	vpc_id = "${aws_vpc.foo.id}"
   192  }
   193  `
   194  
   195  const testAccVpnGatewayConfigChangeVPC = `
   196  resource "aws_vpc" "bar" {
   197  	cidr_block = "10.2.0.0/16"
   198  }
   199  
   200  resource "aws_vpn_gateway" "foo" {
   201  	vpc_id = "${aws_vpc.bar.id}"
   202  }
   203  `
   204  
   205  const testAccCheckVpnGatewayConfigTags = `
   206  resource "aws_vpc" "foo" {
   207  	cidr_block = "10.1.0.0/16"
   208  }
   209  
   210  resource "aws_vpn_gateway" "foo" {
   211  	vpc_id = "${aws_vpc.foo.id}"
   212  	tags {
   213  		foo = "bar"
   214  	}
   215  }
   216  `
   217  
   218  const testAccCheckVpnGatewayConfigTagsUpdate = `
   219  resource "aws_vpc" "foo" {
   220  	cidr_block = "10.1.0.0/16"
   221  }
   222  
   223  resource "aws_vpn_gateway" "foo" {
   224  	vpc_id = "${aws_vpc.foo.id}"
   225  	tags {
   226  		bar = "baz"
   227  	}
   228  }
   229  `