github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/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  		IDRefreshName: "aws_vpn_gateway.foo",
    37  		Providers:     testAccProviders,
    38  		CheckDestroy:  testAccCheckVpnGatewayDestroy,
    39  		Steps: []resource.TestStep{
    40  			resource.TestStep{
    41  				Config: testAccVpnGatewayConfig,
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testAccCheckVpnGatewayExists(
    44  						"aws_vpn_gateway.foo", &v),
    45  				),
    46  			},
    47  
    48  			resource.TestStep{
    49  				Config: testAccVpnGatewayConfigChangeVPC,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckVpnGatewayExists(
    52  						"aws_vpn_gateway.foo", &v2),
    53  					testNotEqual,
    54  				),
    55  			},
    56  		},
    57  	})
    58  }
    59  
    60  func TestAccAWSVpnGateway_reattach(t *testing.T) {
    61  	var v ec2.VpnGateway
    62  
    63  	genTestStateFunc := func(expectedState string) func(*terraform.State) error {
    64  		return func(*terraform.State) error {
    65  			if len(v.VpcAttachments) == 0 {
    66  				if expectedState != "detached" {
    67  					return fmt.Errorf("VPN gateway has no VPC attachments")
    68  				}
    69  			} else if len(v.VpcAttachments) == 1 {
    70  				if *v.VpcAttachments[0].State != expectedState {
    71  					return fmt.Errorf("Expected VPC gateway VPC attachment to be in '%s' state, but was not: %s", expectedState, v)
    72  				}
    73  			} else {
    74  				return fmt.Errorf("VPN gateway has unexpected number of VPC attachments(more than 1): %s", v)
    75  			}
    76  			return nil
    77  		}
    78  	}
    79  
    80  	resource.Test(t, resource.TestCase{
    81  		PreCheck:      func() { testAccPreCheck(t) },
    82  		IDRefreshName: "aws_vpn_gateway.foo",
    83  		Providers:     testAccProviders,
    84  		CheckDestroy:  testAccCheckVpnGatewayDestroy,
    85  		Steps: []resource.TestStep{
    86  			resource.TestStep{
    87  				Config: testAccVpnGatewayConfig,
    88  				Check: resource.ComposeTestCheckFunc(
    89  					testAccCheckVpnGatewayExists(
    90  						"aws_vpn_gateway.foo", &v),
    91  					genTestStateFunc("attached"),
    92  				),
    93  			},
    94  			resource.TestStep{
    95  				Config: testAccVpnGatewayConfigDetach,
    96  				Check: resource.ComposeTestCheckFunc(
    97  					testAccCheckVpnGatewayExists(
    98  						"aws_vpn_gateway.foo", &v),
    99  					genTestStateFunc("detached"),
   100  				),
   101  			},
   102  			resource.TestStep{
   103  				Config: testAccVpnGatewayConfig,
   104  				Check: resource.ComposeTestCheckFunc(
   105  					testAccCheckVpnGatewayExists(
   106  						"aws_vpn_gateway.foo", &v),
   107  					genTestStateFunc("attached"),
   108  				),
   109  			},
   110  		},
   111  	})
   112  }
   113  
   114  func TestAccAWSVpnGateway_delete(t *testing.T) {
   115  	var vpnGateway ec2.VpnGateway
   116  
   117  	testDeleted := func(r string) resource.TestCheckFunc {
   118  		return func(s *terraform.State) error {
   119  			_, ok := s.RootModule().Resources[r]
   120  			if ok {
   121  				return fmt.Errorf("VPN Gateway %q should have been deleted", r)
   122  			}
   123  			return nil
   124  		}
   125  	}
   126  
   127  	resource.Test(t, resource.TestCase{
   128  		PreCheck:      func() { testAccPreCheck(t) },
   129  		IDRefreshName: "aws_vpn_gateway.foo",
   130  		Providers:     testAccProviders,
   131  		CheckDestroy:  testAccCheckVpnGatewayDestroy,
   132  		Steps: []resource.TestStep{
   133  			resource.TestStep{
   134  				Config: testAccVpnGatewayConfig,
   135  				Check: resource.ComposeTestCheckFunc(
   136  					testAccCheckVpnGatewayExists("aws_vpn_gateway.foo", &vpnGateway)),
   137  			},
   138  			resource.TestStep{
   139  				Config: testAccNoVpnGatewayConfig,
   140  				Check:  resource.ComposeTestCheckFunc(testDeleted("aws_vpn_gateway.foo")),
   141  			},
   142  		},
   143  	})
   144  }
   145  
   146  func TestAccAWSVpnGateway_tags(t *testing.T) {
   147  	var v ec2.VpnGateway
   148  
   149  	resource.Test(t, resource.TestCase{
   150  		PreCheck:      func() { testAccPreCheck(t) },
   151  		IDRefreshName: "aws_vpn_gateway.foo",
   152  		Providers:     testAccProviders,
   153  		CheckDestroy:  testAccCheckVpnGatewayDestroy,
   154  		Steps: []resource.TestStep{
   155  			resource.TestStep{
   156  				Config: testAccCheckVpnGatewayConfigTags,
   157  				Check: resource.ComposeTestCheckFunc(
   158  					testAccCheckVpnGatewayExists("aws_vpn_gateway.foo", &v),
   159  					testAccCheckTags(&v.Tags, "foo", "bar"),
   160  				),
   161  			},
   162  
   163  			resource.TestStep{
   164  				Config: testAccCheckVpnGatewayConfigTagsUpdate,
   165  				Check: resource.ComposeTestCheckFunc(
   166  					testAccCheckVpnGatewayExists("aws_vpn_gateway.foo", &v),
   167  					testAccCheckTags(&v.Tags, "foo", ""),
   168  					testAccCheckTags(&v.Tags, "bar", "baz"),
   169  				),
   170  			},
   171  		},
   172  	})
   173  }
   174  
   175  func testAccCheckVpnGatewayDestroy(s *terraform.State) error {
   176  	ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
   177  
   178  	for _, rs := range s.RootModule().Resources {
   179  		if rs.Type != "aws_vpn_gateway" {
   180  			continue
   181  		}
   182  
   183  		// Try to find the resource
   184  		resp, err := ec2conn.DescribeVpnGateways(&ec2.DescribeVpnGatewaysInput{
   185  			VpnGatewayIds: []*string{aws.String(rs.Primary.ID)},
   186  		})
   187  		if err == nil {
   188  			var v *ec2.VpnGateway
   189  			for _, g := range resp.VpnGateways {
   190  				if *g.VpnGatewayId == rs.Primary.ID {
   191  					v = g
   192  				}
   193  			}
   194  
   195  			if v == nil {
   196  				// wasn't found
   197  				return nil
   198  			}
   199  
   200  			if *v.State != "deleted" {
   201  				return fmt.Errorf("Expected VpnGateway to be in deleted state, but was not: %s", v)
   202  			}
   203  			return nil
   204  		}
   205  
   206  		// Verify the error is what we want
   207  		ec2err, ok := err.(awserr.Error)
   208  		if !ok {
   209  			return err
   210  		}
   211  		if ec2err.Code() != "InvalidVpnGatewayID.NotFound" {
   212  			return err
   213  		}
   214  	}
   215  
   216  	return nil
   217  }
   218  
   219  func testAccCheckVpnGatewayExists(n string, ig *ec2.VpnGateway) resource.TestCheckFunc {
   220  	return func(s *terraform.State) error {
   221  		rs, ok := s.RootModule().Resources[n]
   222  		if !ok {
   223  			return fmt.Errorf("Not found: %s", n)
   224  		}
   225  
   226  		if rs.Primary.ID == "" {
   227  			return fmt.Errorf("No ID is set")
   228  		}
   229  
   230  		ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
   231  		resp, err := ec2conn.DescribeVpnGateways(&ec2.DescribeVpnGatewaysInput{
   232  			VpnGatewayIds: []*string{aws.String(rs.Primary.ID)},
   233  		})
   234  		if err != nil {
   235  			return err
   236  		}
   237  		if len(resp.VpnGateways) == 0 {
   238  			return fmt.Errorf("VPNGateway not found")
   239  		}
   240  
   241  		*ig = *resp.VpnGateways[0]
   242  
   243  		return nil
   244  	}
   245  }
   246  
   247  const testAccNoVpnGatewayConfig = `
   248  resource "aws_vpc" "foo" {
   249  	cidr_block = "10.1.0.0/16"
   250  }
   251  `
   252  
   253  const testAccVpnGatewayConfig = `
   254  resource "aws_vpc" "foo" {
   255  	cidr_block = "10.1.0.0/16"
   256  }
   257  
   258  resource "aws_vpn_gateway" "foo" {
   259  	vpc_id = "${aws_vpc.foo.id}"
   260  }
   261  `
   262  
   263  const testAccVpnGatewayConfigChangeVPC = `
   264  resource "aws_vpc" "bar" {
   265  	cidr_block = "10.2.0.0/16"
   266  }
   267  
   268  resource "aws_vpn_gateway" "foo" {
   269  	vpc_id = "${aws_vpc.bar.id}"
   270  }
   271  `
   272  
   273  const testAccVpnGatewayConfigDetach = `
   274  resource "aws_vpc" "foo" {
   275  	cidr_block = "10.1.0.0/16"
   276  }
   277  
   278  resource "aws_vpn_gateway" "foo" {
   279  	vpc_id = ""
   280  }
   281  `
   282  
   283  const testAccCheckVpnGatewayConfigTags = `
   284  resource "aws_vpc" "foo" {
   285  	cidr_block = "10.1.0.0/16"
   286  }
   287  
   288  resource "aws_vpn_gateway" "foo" {
   289  	vpc_id = "${aws_vpc.foo.id}"
   290  	tags {
   291  		foo = "bar"
   292  	}
   293  }
   294  `
   295  
   296  const testAccCheckVpnGatewayConfigTagsUpdate = `
   297  resource "aws_vpc" "foo" {
   298  	cidr_block = "10.1.0.0/16"
   299  }
   300  
   301  resource "aws_vpn_gateway" "foo" {
   302  	vpc_id = "${aws_vpc.foo.id}"
   303  	tags {
   304  		bar = "baz"
   305  	}
   306  }
   307  `