github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/builtin/providers/aws/resource_aws_subnet_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/aws-sdk-go/aws"
     8  	"github.com/hashicorp/aws-sdk-go/gen/ec2"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSSubnet(t *testing.T) {
    14  	var v ec2.Subnet
    15  
    16  	testCheck := func(*terraform.State) error {
    17  		if *v.CIDRBlock != "10.1.1.0/24" {
    18  			return fmt.Errorf("bad cidr: %s", *v.CIDRBlock)
    19  		}
    20  
    21  		if *v.MapPublicIPOnLaunch != true {
    22  			return fmt.Errorf("bad MapPublicIpOnLaunch: %t", *v.MapPublicIPOnLaunch)
    23  		}
    24  
    25  		return nil
    26  	}
    27  
    28  	resource.Test(t, resource.TestCase{
    29  		PreCheck:     func() { testAccPreCheck(t) },
    30  		Providers:    testAccProviders,
    31  		CheckDestroy: testAccCheckSubnetDestroy,
    32  		Steps: []resource.TestStep{
    33  			resource.TestStep{
    34  				Config: testAccSubnetConfig,
    35  				Check: resource.ComposeTestCheckFunc(
    36  					testAccCheckSubnetExists(
    37  						"aws_subnet.foo", &v),
    38  					testCheck,
    39  				),
    40  			},
    41  		},
    42  	})
    43  }
    44  
    45  func testAccCheckSubnetDestroy(s *terraform.State) error {
    46  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    47  
    48  	for _, rs := range s.RootModule().Resources {
    49  		if rs.Type != "aws_subnet" {
    50  			continue
    51  		}
    52  
    53  		// Try to find the resource
    54  		resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsRequest{
    55  			SubnetIDs: []string{rs.Primary.ID},
    56  		})
    57  		if err == nil {
    58  			if len(resp.Subnets) > 0 {
    59  				return fmt.Errorf("still exist.")
    60  			}
    61  
    62  			return nil
    63  		}
    64  
    65  		// Verify the error is what we want
    66  		ec2err, ok := err.(aws.APIError)
    67  		if !ok {
    68  			return err
    69  		}
    70  		if ec2err.Code != "InvalidSubnetID.NotFound" {
    71  			return err
    72  		}
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  func testAccCheckSubnetExists(n string, v *ec2.Subnet) resource.TestCheckFunc {
    79  	return func(s *terraform.State) error {
    80  		rs, ok := s.RootModule().Resources[n]
    81  		if !ok {
    82  			return fmt.Errorf("Not found: %s", n)
    83  		}
    84  
    85  		if rs.Primary.ID == "" {
    86  			return fmt.Errorf("No ID is set")
    87  		}
    88  
    89  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    90  		resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsRequest{
    91  			SubnetIDs: []string{rs.Primary.ID},
    92  		})
    93  		if err != nil {
    94  			return err
    95  		}
    96  		if len(resp.Subnets) == 0 {
    97  			return fmt.Errorf("Subnet not found")
    98  		}
    99  
   100  		*v = resp.Subnets[0]
   101  
   102  		return nil
   103  	}
   104  }
   105  
   106  const testAccSubnetConfig = `
   107  resource "aws_vpc" "foo" {
   108  	cidr_block = "10.1.0.0/16"
   109  }
   110  
   111  resource "aws_subnet" "foo" {
   112  	cidr_block = "10.1.1.0/24"
   113  	vpc_id = "${aws_vpc.foo.id}"
   114  	map_public_ip_on_launch = true
   115  }
   116  `