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