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