github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/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 if len(resp.NatGateways) > 0 && strings.ToLower(*resp.NatGateways[0].State) != "deleted" { 48 return fmt.Errorf("still exists") 49 } 50 51 return nil 52 } 53 54 // Verify the error is what we want 55 ec2err, ok := err.(awserr.Error) 56 if !ok { 57 return err 58 } 59 if ec2err.Code() != "NatGatewayNotFound" { 60 return err 61 } 62 } 63 64 return nil 65 } 66 67 func testAccCheckNatGatewayExists(n string, ng *ec2.NatGateway) resource.TestCheckFunc { 68 return func(s *terraform.State) error { 69 rs, ok := s.RootModule().Resources[n] 70 if !ok { 71 return fmt.Errorf("Not found: %s", n) 72 } 73 74 if rs.Primary.ID == "" { 75 return fmt.Errorf("No ID is set") 76 } 77 78 conn := testAccProvider.Meta().(*AWSClient).ec2conn 79 resp, err := conn.DescribeNatGateways(&ec2.DescribeNatGatewaysInput{ 80 NatGatewayIds: []*string{aws.String(rs.Primary.ID)}, 81 }) 82 if err != nil { 83 return err 84 } 85 if len(resp.NatGateways) == 0 { 86 return fmt.Errorf("NatGateway not found") 87 } 88 89 *ng = *resp.NatGateways[0] 90 91 return nil 92 } 93 } 94 95 const testAccNatGatewayConfig = ` 96 resource "aws_vpc" "vpc" { 97 cidr_block = "10.0.0.0/16" 98 } 99 100 resource "aws_subnet" "private" { 101 vpc_id = "${aws_vpc.vpc.id}" 102 cidr_block = "10.0.1.0/24" 103 map_public_ip_on_launch = false 104 } 105 106 resource "aws_subnet" "public" { 107 vpc_id = "${aws_vpc.vpc.id}" 108 cidr_block = "10.0.2.0/24" 109 map_public_ip_on_launch = true 110 } 111 112 resource "aws_internet_gateway" "gw" { 113 vpc_id = "${aws_vpc.vpc.id}" 114 } 115 116 resource "aws_eip" "nat_gateway" { 117 vpc = true 118 } 119 120 // Actual SUT 121 resource "aws_nat_gateway" "gateway" { 122 allocation_id = "${aws_eip.nat_gateway.id}" 123 subnet_id = "${aws_subnet.public.id}" 124 125 depends_on = ["aws_internet_gateway.gw"] 126 } 127 128 resource "aws_route_table" "private" { 129 vpc_id = "${aws_vpc.vpc.id}" 130 131 route { 132 cidr_block = "0.0.0.0/0" 133 nat_gateway_id = "${aws_nat_gateway.gateway.id}" 134 } 135 } 136 137 resource "aws_route_table_association" "private" { 138 subnet_id = "${aws_subnet.private.id}" 139 route_table_id = "${aws_route_table.private.id}" 140 } 141 142 resource "aws_route_table" "public" { 143 vpc_id = "${aws_vpc.vpc.id}" 144 145 route { 146 cidr_block = "0.0.0.0/0" 147 gateway_id = "${aws_internet_gateway.gw.id}" 148 } 149 } 150 151 resource "aws_route_table_association" "public" { 152 subnet_id = "${aws_subnet.public.id}" 153 route_table_id = "${aws_route_table.public.id}" 154 } 155 `