github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/resource_aws_db_subnet_group_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/goamz/rds"
    10  )
    11  
    12  func TestAccAWSDBSubnetGroup(t *testing.T) {
    13  	var v rds.DBSubnetGroup
    14  
    15  	testCheck := func(*terraform.State) error {
    16  		return nil
    17  	}
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckDBSubnetGroupDestroy,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: testAccDBSubnetGroupConfig,
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckDBSubnetGroupExists(
    28  						"aws_db_subnet_group.foo", &v),
    29  					testCheck,
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func testAccCheckDBSubnetGroupDestroy(s *terraform.State) error {
    37  	conn := testAccProvider.Meta().(*AWSClient).rdsconn
    38  
    39  	for _, rs := range s.RootModule().Resources {
    40  		if rs.Type != "aws_db_subnet_group" {
    41  			continue
    42  		}
    43  
    44  		// Try to find the resource
    45  		resp, err := conn.DescribeDBSubnetGroups(&rds.DescribeDBSubnetGroups{rs.Primary.ID})
    46  		if err == nil {
    47  			if len(resp.DBSubnetGroups) > 0 {
    48  				return fmt.Errorf("still exist.")
    49  			}
    50  
    51  			return nil
    52  		}
    53  
    54  		// Verify the error is what we want
    55  		rdserr, ok := err.(*rds.Error)
    56  		if !ok {
    57  			return err
    58  		}
    59  		if rdserr.Code != "DBSubnetGroupNotFoundFault" {
    60  			return err
    61  		}
    62  	}
    63  
    64  	return nil
    65  }
    66  
    67  func testAccCheckDBSubnetGroupExists(n string, v *rds.DBSubnetGroup) 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).rdsconn
    79  		resp, err := conn.DescribeDBSubnetGroups(&rds.DescribeDBSubnetGroups{rs.Primary.ID})
    80  		if err != nil {
    81  			return err
    82  		}
    83  		if len(resp.DBSubnetGroups) == 0 {
    84  			return fmt.Errorf("DbSubnetGroup not found")
    85  		}
    86  
    87  		*v = resp.DBSubnetGroups[0]
    88  
    89  		return nil
    90  	}
    91  }
    92  
    93  const testAccDBSubnetGroupConfig = `
    94  resource "aws_vpc" "foo" {
    95  	cidr_block = "10.1.0.0/16"
    96  }
    97  
    98  resource "aws_subnet" "foo" {
    99  	cidr_block = "10.1.1.0/24"
   100  	availability_zone = "us-west-2a"
   101  	vpc_id = "${aws_vpc.foo.id}"
   102  }
   103  
   104  resource "aws_subnet" "bar" {
   105  	cidr_block = "10.1.2.0/24"
   106  	availability_zone = "us-west-2b"
   107  	vpc_id = "${aws_vpc.foo.id}"
   108  }
   109  
   110  resource "aws_db_subnet_group" "foo" {
   111  	name = "foo"
   112  	description = "foo description"
   113  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
   114  }
   115  `