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