github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/builtin/providers/aws/resource_aws_vpn_gateway_test.go (about)

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