github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_egress_only_internet_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/service/ec2"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSEgressOnlyInternetGateway_basic(t *testing.T) {
    14  	var igw ec2.EgressOnlyInternetGateway
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckAWSEgressOnlyInternetGatewayDestroy,
    19  		Steps: []resource.TestStep{
    20  			{
    21  				Config: testAccAWSEgressOnlyInternetGatewayConfig_basic,
    22  				Check: resource.ComposeAggregateTestCheckFunc(
    23  					testAccCheckAWSEgressOnlyInternetGatewayExists("aws_egress_only_internet_gateway.foo", &igw),
    24  				),
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  func testAccCheckAWSEgressOnlyInternetGatewayDestroy(s *terraform.State) error {
    31  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    32  
    33  	for _, rs := range s.RootModule().Resources {
    34  		if rs.Type != "aws_egress_only_internet_gateway" {
    35  			continue
    36  		}
    37  
    38  		describe, err := conn.DescribeEgressOnlyInternetGateways(&ec2.DescribeEgressOnlyInternetGatewaysInput{
    39  			EgressOnlyInternetGatewayIds: []*string{aws.String(rs.Primary.ID)},
    40  		})
    41  
    42  		if err == nil {
    43  			if len(describe.EgressOnlyInternetGateways) != 0 &&
    44  				*describe.EgressOnlyInternetGateways[0].EgressOnlyInternetGatewayId == rs.Primary.ID {
    45  				return fmt.Errorf("Egress Only Internet Gateway %q still exists", rs.Primary.ID)
    46  			}
    47  		}
    48  
    49  		return nil
    50  	}
    51  
    52  	return nil
    53  }
    54  
    55  func testAccCheckAWSEgressOnlyInternetGatewayExists(n string, igw *ec2.EgressOnlyInternetGateway) resource.TestCheckFunc {
    56  	return func(s *terraform.State) error {
    57  		rs, ok := s.RootModule().Resources[n]
    58  		if !ok {
    59  			return fmt.Errorf("Not found: %s", n)
    60  		}
    61  
    62  		if rs.Primary.ID == "" {
    63  			return fmt.Errorf("No Egress Only IGW ID is set")
    64  		}
    65  
    66  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    67  		resp, err := conn.DescribeEgressOnlyInternetGateways(&ec2.DescribeEgressOnlyInternetGatewaysInput{
    68  			EgressOnlyInternetGatewayIds: []*string{aws.String(rs.Primary.ID)},
    69  		})
    70  		if err != nil {
    71  			return err
    72  		}
    73  		if len(resp.EgressOnlyInternetGateways) == 0 {
    74  			return fmt.Errorf("Egress Only IGW not found")
    75  		}
    76  
    77  		*igw = *resp.EgressOnlyInternetGateways[0]
    78  
    79  		return nil
    80  	}
    81  }
    82  
    83  const testAccAWSEgressOnlyInternetGatewayConfig_basic = `
    84  resource "aws_vpc" "foo" {
    85  	cidr_block = "10.1.0.0/16"
    86  	assign_generated_ipv6_cidr_block = true
    87  }
    88  
    89  resource "aws_egress_only_internet_gateway" "foo" {
    90    	vpc_id = "${aws_vpc.foo.id}"
    91  }
    92  `