github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/builtin/providers/aws/resource_aws_elasticache_security_group_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/elasticache"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSElasticacheSecurityGroup_basic(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckAWSElasticacheSecurityGroupDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAWSElasticacheSecurityGroupConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"),
    24  				),
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  func testAccCheckAWSElasticacheSecurityGroupDestroy(s *terraform.State) error {
    31  	conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
    32  
    33  	for _, rs := range s.RootModule().Resources {
    34  		if rs.Type != "aws_elasticache_security_group" {
    35  			continue
    36  		}
    37  		res, err := conn.DescribeCacheSecurityGroups(&elasticache.DescribeCacheSecurityGroupsInput{
    38  			CacheSecurityGroupName: aws.String(rs.Primary.ID),
    39  		})
    40  		if awserr, ok := err.(awserr.Error); ok && awserr.Code() == "CacheSecurityGroupNotFound" {
    41  			continue
    42  		}
    43  
    44  		if len(res.CacheSecurityGroups) > 0 {
    45  			return fmt.Errorf("cache security group still exists")
    46  		}
    47  		return err
    48  	}
    49  	return nil
    50  }
    51  
    52  func testAccCheckAWSElasticacheSecurityGroupExists(n string) resource.TestCheckFunc {
    53  	return func(s *terraform.State) error {
    54  		rs, ok := s.RootModule().Resources[n]
    55  		if !ok {
    56  			return fmt.Errorf("Not found: %s", n)
    57  		}
    58  
    59  		if rs.Primary.ID == "" {
    60  			return fmt.Errorf("No cache security group ID is set")
    61  		}
    62  
    63  		conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
    64  		_, err := conn.DescribeCacheSecurityGroups(&elasticache.DescribeCacheSecurityGroupsInput{
    65  			CacheSecurityGroupName: aws.String(rs.Primary.ID),
    66  		})
    67  		if err != nil {
    68  			return fmt.Errorf("CacheSecurityGroup error: %v", err)
    69  		}
    70  		return nil
    71  	}
    72  }
    73  
    74  var testAccAWSElasticacheSecurityGroupConfig = fmt.Sprintf(`
    75  provider "aws" {
    76    region = "us-east-1"
    77  }
    78  resource "aws_security_group" "bar" {
    79      name = "tf-test-security-group-%03d"
    80      description = "tf-test-security-group-descr"
    81      ingress {
    82          from_port = -1
    83          to_port = -1
    84          protocol = "icmp"
    85          cidr_blocks = ["0.0.0.0/0"]
    86      }
    87  }
    88  
    89  resource "aws_elasticache_security_group" "bar" {
    90      name = "tf-test-security-group-%03d"
    91      description = "tf-test-security-group-descr"
    92      security_group_names = ["${aws_security_group.bar.name}"]
    93  }
    94  `, genRandInt(), genRandInt())