github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_vpn_gateway_attachment_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/service/ec2"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSVpnGatewayAttachment_basic(t *testing.T) {
    14  	var vpc ec2.Vpc
    15  	var vgw ec2.VpnGateway
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:      func() { testAccPreCheck(t) },
    19  		IDRefreshName: "aws_vpn_gateway_attachment.test",
    20  		Providers:     testAccProviders,
    21  		CheckDestroy:  testAccCheckVpnGatewayAttachmentDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccVpnGatewayAttachmentConfig,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckVpcExists(
    27  						"aws_vpc.test",
    28  						&vpc),
    29  					testAccCheckVpnGatewayExists(
    30  						"aws_vpn_gateway.test",
    31  						&vgw),
    32  					testAccCheckVpnGatewayAttachmentExists(
    33  						"aws_vpn_gateway_attachment.test",
    34  						&vpc, &vgw),
    35  				),
    36  			},
    37  		},
    38  	})
    39  }
    40  
    41  func TestAccAWSVpnGatewayAttachment_deleted(t *testing.T) {
    42  	var vpc ec2.Vpc
    43  	var vgw ec2.VpnGateway
    44  
    45  	testDeleted := func(n string) resource.TestCheckFunc {
    46  		return func(s *terraform.State) error {
    47  			_, ok := s.RootModule().Resources[n]
    48  			if ok {
    49  				return fmt.Errorf("Expected VPN Gateway attachment resource %q to be deleted.", n)
    50  			}
    51  			return nil
    52  		}
    53  	}
    54  
    55  	resource.Test(t, resource.TestCase{
    56  		PreCheck:      func() { testAccPreCheck(t) },
    57  		IDRefreshName: "aws_vpn_gateway_attachment.test",
    58  		Providers:     testAccProviders,
    59  		CheckDestroy:  testAccCheckVpnGatewayAttachmentDestroy,
    60  		Steps: []resource.TestStep{
    61  			resource.TestStep{
    62  				Config: testAccVpnGatewayAttachmentConfig,
    63  				Check: resource.ComposeTestCheckFunc(
    64  					testAccCheckVpcExists(
    65  						"aws_vpc.test",
    66  						&vpc),
    67  					testAccCheckVpnGatewayExists(
    68  						"aws_vpn_gateway.test",
    69  						&vgw),
    70  					testAccCheckVpnGatewayAttachmentExists(
    71  						"aws_vpn_gateway_attachment.test",
    72  						&vpc, &vgw),
    73  				),
    74  			},
    75  			resource.TestStep{
    76  				Config: testAccNoVpnGatewayAttachmentConfig,
    77  				Check: resource.ComposeTestCheckFunc(
    78  					testDeleted("aws_vpn_gateway_attachment.test"),
    79  				),
    80  			},
    81  		},
    82  	})
    83  }
    84  
    85  func testAccCheckVpnGatewayAttachmentExists(n string, vpc *ec2.Vpc, vgw *ec2.VpnGateway) resource.TestCheckFunc {
    86  	return func(s *terraform.State) error {
    87  		rs, ok := s.RootModule().Resources[n]
    88  		if !ok {
    89  			return fmt.Errorf("Not found: %s", n)
    90  		}
    91  
    92  		if rs.Primary.ID == "" {
    93  			return fmt.Errorf("No ID is set")
    94  		}
    95  
    96  		vpcId := rs.Primary.Attributes["vpc_id"]
    97  		vgwId := rs.Primary.Attributes["vpn_gateway_id"]
    98  
    99  		if len(vgw.VpcAttachments) == 0 {
   100  			return fmt.Errorf("VPN Gateway %q has no attachments.", vgwId)
   101  		}
   102  
   103  		if *vgw.VpcAttachments[0].State != "attached" {
   104  			return fmt.Errorf("Expected VPN Gateway %q to be in attached state, but got: %q",
   105  				vgwId, *vgw.VpcAttachments[0].State)
   106  		}
   107  
   108  		if *vgw.VpcAttachments[0].VpcId != *vpc.VpcId {
   109  			return fmt.Errorf("Expected VPN Gateway %q to be attached to VPC %q, but got: %q",
   110  				vgwId, vpcId, *vgw.VpcAttachments[0].VpcId)
   111  		}
   112  
   113  		return nil
   114  	}
   115  }
   116  
   117  func testAccCheckVpnGatewayAttachmentDestroy(s *terraform.State) error {
   118  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
   119  
   120  	for _, rs := range s.RootModule().Resources {
   121  		if rs.Type != "aws_vpn_gateway_attachment" {
   122  			continue
   123  		}
   124  
   125  		vgwId := rs.Primary.Attributes["vpn_gateway_id"]
   126  
   127  		resp, err := conn.DescribeVpnGateways(&ec2.DescribeVpnGatewaysInput{
   128  			VpnGatewayIds: []*string{aws.String(vgwId)},
   129  		})
   130  		if err != nil {
   131  			return err
   132  		}
   133  
   134  		vgw := resp.VpnGateways[0]
   135  		if *vgw.VpcAttachments[0].State != "detached" {
   136  			return fmt.Errorf("Expected VPN Gateway %q to be in detached state, but got: %q",
   137  				vgwId, *vgw.VpcAttachments[0].State)
   138  		}
   139  	}
   140  
   141  	return nil
   142  }
   143  
   144  const testAccNoVpnGatewayAttachmentConfig = `
   145  resource "aws_vpc" "test" {
   146  	cidr_block = "10.0.0.0/16"
   147  }
   148  
   149  resource "aws_vpn_gateway" "test" { }
   150  `
   151  
   152  const testAccVpnGatewayAttachmentConfig = `
   153  resource "aws_vpc" "test" {
   154  	cidr_block = "10.0.0.0/16"
   155  }
   156  
   157  resource "aws_vpn_gateway" "test" { }
   158  
   159  resource "aws_vpn_gateway_attachment" "test" {
   160  	vpc_id = "${aws_vpc.test.id}"
   161  	vpn_gateway_id = "${aws_vpn_gateway.test.id}"
   162  }
   163  `