github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_nat_gateway_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/ec2"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSNatGateway_basic(t *testing.T) {
    16  	var natGateway ec2.NatGateway
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:      func() { testAccPreCheck(t) },
    20  		IDRefreshName: "aws_nat_gateway.gateway",
    21  		Providers:     testAccProviders,
    22  		CheckDestroy:  testAccCheckNatGatewayDestroy,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: testAccNatGatewayConfig,
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckNatGatewayExists("aws_nat_gateway.gateway", &natGateway),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func testAccCheckNatGatewayDestroy(s *terraform.State) error {
    35  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    36  
    37  	for _, rs := range s.RootModule().Resources {
    38  		if rs.Type != "aws_nat_gateway" {
    39  			continue
    40  		}
    41  
    42  		// Try to find the resource
    43  		resp, err := conn.DescribeNatGateways(&ec2.DescribeNatGatewaysInput{
    44  			NatGatewayIds: []*string{aws.String(rs.Primary.ID)},
    45  		})
    46  		if err == nil {
    47  			status := map[string]bool{
    48  				"deleted":  true,
    49  				"deleting": true,
    50  				"failed":   true,
    51  			}
    52  			if _, ok := status[strings.ToLower(*resp.NatGateways[0].State)]; len(resp.NatGateways) > 0 && !ok {
    53  				return fmt.Errorf("still exists")
    54  			}
    55  
    56  			return nil
    57  		}
    58  
    59  		// Verify the error is what we want
    60  		ec2err, ok := err.(awserr.Error)
    61  		if !ok {
    62  			return err
    63  		}
    64  		if ec2err.Code() != "NatGatewayNotFound" {
    65  			return err
    66  		}
    67  	}
    68  
    69  	return nil
    70  }
    71  
    72  func testAccCheckNatGatewayExists(n string, ng *ec2.NatGateway) resource.TestCheckFunc {
    73  	return func(s *terraform.State) error {
    74  		rs, ok := s.RootModule().Resources[n]
    75  		if !ok {
    76  			return fmt.Errorf("Not found: %s", n)
    77  		}
    78  
    79  		if rs.Primary.ID == "" {
    80  			return fmt.Errorf("No ID is set")
    81  		}
    82  
    83  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    84  		resp, err := conn.DescribeNatGateways(&ec2.DescribeNatGatewaysInput{
    85  			NatGatewayIds: []*string{aws.String(rs.Primary.ID)},
    86  		})
    87  		if err != nil {
    88  			return err
    89  		}
    90  		if len(resp.NatGateways) == 0 {
    91  			return fmt.Errorf("NatGateway not found")
    92  		}
    93  
    94  		*ng = *resp.NatGateways[0]
    95  
    96  		return nil
    97  	}
    98  }
    99  
   100  const testAccNatGatewayConfig = `
   101  resource "aws_vpc" "vpc" {
   102      cidr_block = "10.0.0.0/16"
   103  }
   104  
   105  resource "aws_subnet" "private" {
   106      vpc_id = "${aws_vpc.vpc.id}"
   107      cidr_block = "10.0.1.0/24"
   108      map_public_ip_on_launch = false
   109  }
   110  
   111  resource "aws_subnet" "public" {
   112      vpc_id = "${aws_vpc.vpc.id}"
   113      cidr_block = "10.0.2.0/24"
   114      map_public_ip_on_launch = true
   115  }
   116  
   117  resource "aws_internet_gateway" "gw" {
   118      vpc_id = "${aws_vpc.vpc.id}"
   119  }
   120  
   121  resource "aws_eip" "nat_gateway" {
   122      vpc = true
   123  }
   124  
   125  // Actual SUT
   126  resource "aws_nat_gateway" "gateway" {
   127      allocation_id = "${aws_eip.nat_gateway.id}"
   128      subnet_id = "${aws_subnet.public.id}"
   129  
   130      depends_on = ["aws_internet_gateway.gw"]
   131  }
   132  
   133  resource "aws_route_table" "private" {
   134      vpc_id = "${aws_vpc.vpc.id}"
   135  
   136      route {
   137          cidr_block = "0.0.0.0/0"
   138          nat_gateway_id = "${aws_nat_gateway.gateway.id}"
   139      }
   140  }
   141  
   142  resource "aws_route_table_association" "private" {
   143      subnet_id = "${aws_subnet.private.id}"
   144      route_table_id = "${aws_route_table.private.id}"
   145  }
   146  
   147  resource "aws_route_table" "public" {
   148      vpc_id = "${aws_vpc.vpc.id}"
   149  
   150      route {
   151          cidr_block = "0.0.0.0/0"
   152          gateway_id = "${aws_internet_gateway.gw.id}"
   153      }
   154  }
   155  
   156  resource "aws_route_table_association" "public" {
   157      subnet_id = "${aws_subnet.public.id}"
   158      route_table_id = "${aws_route_table.public.id}"
   159  }
   160  `