github.com/armen/terraform@v0.5.2-0.20150529052519-caa8117a08f1/builtin/providers/aws/resource_aws_elasticache_subnet_group_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/awslabs/aws-sdk-go/aws"
     8  	"github.com/awslabs/aws-sdk-go/service/elasticache"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSElasticacheSubnetGroup(t *testing.T) {
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckAWSElasticacheSubnetGroupDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccAWSElasticacheSubnetGroupConfig,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckAWSElasticacheSubnetGroupExists("aws_elasticache_subnet_group.bar"),
    23  				),
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  func testAccCheckAWSElasticacheSubnetGroupDestroy(s *terraform.State) error {
    30  	conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
    31  
    32  	for _, rs := range s.RootModule().Resources {
    33  		if rs.Type != "aws_elasticache_subnet_group" {
    34  			continue
    35  		}
    36  		res, err := conn.DescribeCacheSubnetGroups(&elasticache.DescribeCacheSubnetGroupsInput{
    37  			CacheSubnetGroupName: aws.String(rs.Primary.ID),
    38  		})
    39  		if err != nil {
    40  			return err
    41  		}
    42  		if len(res.CacheSubnetGroups) > 0 {
    43  			return fmt.Errorf("still exist.")
    44  		}
    45  	}
    46  	return nil
    47  }
    48  
    49  func testAccCheckAWSElasticacheSubnetGroupExists(n string) resource.TestCheckFunc {
    50  	return func(s *terraform.State) error {
    51  		rs, ok := s.RootModule().Resources[n]
    52  		if !ok {
    53  			return fmt.Errorf("Not found: %s", n)
    54  		}
    55  
    56  		if rs.Primary.ID == "" {
    57  			return fmt.Errorf("No cache subnet group ID is set")
    58  		}
    59  
    60  		conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
    61  		_, err := conn.DescribeCacheSubnetGroups(&elasticache.DescribeCacheSubnetGroupsInput{
    62  			CacheSubnetGroupName: aws.String(rs.Primary.ID),
    63  		})
    64  		if err != nil {
    65  			return fmt.Errorf("CacheSubnetGroup error: %v", err)
    66  		}
    67  		return nil
    68  	}
    69  }
    70  
    71  var testAccAWSElasticacheSubnetGroupConfig = fmt.Sprintf(`
    72  resource "aws_vpc" "foo" {
    73      cidr_block = "192.168.0.0/16"
    74      tags {
    75              Name = "tf-test"
    76      }
    77  }
    78  
    79  resource "aws_subnet" "foo" {
    80      vpc_id = "${aws_vpc.foo.id}"
    81      cidr_block = "192.168.0.0/20"
    82      availability_zone = "us-west-2a"
    83      tags {
    84              Name = "tf-test"
    85      }
    86  }
    87  
    88  resource "aws_elasticache_subnet_group" "bar" {
    89      name = "tf-test-cache-subnet-%03d"
    90      description = "tf-test-cache-subnet-group-descr"
    91      subnet_ids = ["${aws_subnet.foo.id}"]
    92  }
    93  `, genRandInt())