github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/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  
    10  	"github.com/aws/aws-sdk-go/aws"
    11  	"github.com/aws/aws-sdk-go/aws/awserr"
    12  	"github.com/aws/aws-sdk-go/service/rds"
    13  )
    14  
    15  func TestAccAWSDBSubnetGroup_basic(t *testing.T) {
    16  	var v rds.DBSubnetGroup
    17  
    18  	testCheck := func(*terraform.State) error {
    19  		return nil
    20  	}
    21  
    22  	resource.Test(t, resource.TestCase{
    23  		PreCheck:     func() { testAccPreCheck(t) },
    24  		Providers:    testAccProviders,
    25  		CheckDestroy: testAccCheckDBSubnetGroupDestroy,
    26  		Steps: []resource.TestStep{
    27  			resource.TestStep{
    28  				Config: testAccDBSubnetGroupConfig,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckDBSubnetGroupExists(
    31  						"aws_db_subnet_group.foo", &v),
    32  					testCheck,
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func testAccCheckDBSubnetGroupDestroy(s *terraform.State) error {
    40  	conn := testAccProvider.Meta().(*AWSClient).rdsconn
    41  
    42  	for _, rs := range s.RootModule().Resources {
    43  		if rs.Type != "aws_db_subnet_group" {
    44  			continue
    45  		}
    46  
    47  		// Try to find the resource
    48  		resp, err := conn.DescribeDBSubnetGroups(
    49  			&rds.DescribeDBSubnetGroupsInput{DBSubnetGroupName: aws.String(rs.Primary.ID)})
    50  		if err == nil {
    51  			if len(resp.DBSubnetGroups) > 0 {
    52  				return fmt.Errorf("still exist.")
    53  			}
    54  
    55  			return nil
    56  		}
    57  
    58  		// Verify the error is what we want
    59  		rdserr, ok := err.(awserr.Error)
    60  		if !ok {
    61  			return err
    62  		}
    63  		if rdserr.Code() != "DBSubnetGroupNotFoundFault" {
    64  			return err
    65  		}
    66  	}
    67  
    68  	return nil
    69  }
    70  
    71  func testAccCheckDBSubnetGroupExists(n string, v *rds.DBSubnetGroup) resource.TestCheckFunc {
    72  	return func(s *terraform.State) error {
    73  		rs, ok := s.RootModule().Resources[n]
    74  		if !ok {
    75  			return fmt.Errorf("Not found: %s", n)
    76  		}
    77  
    78  		if rs.Primary.ID == "" {
    79  			return fmt.Errorf("No ID is set")
    80  		}
    81  
    82  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
    83  		resp, err := conn.DescribeDBSubnetGroups(
    84  			&rds.DescribeDBSubnetGroupsInput{DBSubnetGroupName: aws.String(rs.Primary.ID)})
    85  		if err != nil {
    86  			return err
    87  		}
    88  		if len(resp.DBSubnetGroups) == 0 {
    89  			return fmt.Errorf("DbSubnetGroup not found")
    90  		}
    91  
    92  		*v = *resp.DBSubnetGroups[0]
    93  
    94  		return nil
    95  	}
    96  }
    97  
    98  const testAccDBSubnetGroupConfig = `
    99  resource "aws_vpc" "foo" {
   100  	cidr_block = "10.1.0.0/16"
   101  }
   102  
   103  resource "aws_subnet" "foo" {
   104  	cidr_block = "10.1.1.0/24"
   105  	availability_zone = "us-west-2a"
   106  	vpc_id = "${aws_vpc.foo.id}"
   107  	tags {
   108  		Name = "tf-dbsubnet-test-1"
   109  	}
   110  }
   111  
   112  resource "aws_subnet" "bar" {
   113  	cidr_block = "10.1.2.0/24"
   114  	availability_zone = "us-west-2b"
   115  	vpc_id = "${aws_vpc.foo.id}"
   116  	tags {
   117  		Name = "tf-dbsubnet-test-2"
   118  	}
   119  }
   120  
   121  resource "aws_db_subnet_group" "foo" {
   122  	name = "FOO"
   123  	description = "foo description"
   124  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
   125  }
   126  `