github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_elasticache_parameter_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 TestAccAWSElasticacheParameterGroup_basic(t *testing.T) {
    16  	var v elasticache.CacheParameterGroup
    17  	rName := fmt.Sprintf("parameter-group-test-terraform-%d", acctest.RandInt())
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckAWSElasticacheParameterGroupDestroy,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: testAccAWSElasticacheParameterGroupConfig(rName),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckAWSElasticacheParameterGroupExists("aws_elasticache_parameter_group.bar", &v),
    28  					testAccCheckAWSElasticacheParameterGroupAttributes(&v, rName),
    29  					resource.TestCheckResourceAttr(
    30  						"aws_elasticache_parameter_group.bar", "name", rName),
    31  					resource.TestCheckResourceAttr(
    32  						"aws_elasticache_parameter_group.bar", "family", "redis2.8"),
    33  					resource.TestCheckResourceAttr(
    34  						"aws_elasticache_parameter_group.bar", "description", "Managed by Terraform"),
    35  					resource.TestCheckResourceAttr(
    36  						"aws_elasticache_parameter_group.bar", "parameter.283487565.name", "appendonly"),
    37  					resource.TestCheckResourceAttr(
    38  						"aws_elasticache_parameter_group.bar", "parameter.283487565.value", "yes"),
    39  				),
    40  			},
    41  			resource.TestStep{
    42  				Config: testAccAWSElasticacheParameterGroupAddParametersConfig(rName),
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckAWSElasticacheParameterGroupExists("aws_elasticache_parameter_group.bar", &v),
    45  					testAccCheckAWSElasticacheParameterGroupAttributes(&v, rName),
    46  					resource.TestCheckResourceAttr(
    47  						"aws_elasticache_parameter_group.bar", "name", rName),
    48  					resource.TestCheckResourceAttr(
    49  						"aws_elasticache_parameter_group.bar", "family", "redis2.8"),
    50  					resource.TestCheckResourceAttr(
    51  						"aws_elasticache_parameter_group.bar", "description", "Test parameter group for terraform"),
    52  					resource.TestCheckResourceAttr(
    53  						"aws_elasticache_parameter_group.bar", "parameter.283487565.name", "appendonly"),
    54  					resource.TestCheckResourceAttr(
    55  						"aws_elasticache_parameter_group.bar", "parameter.283487565.value", "yes"),
    56  					resource.TestCheckResourceAttr(
    57  						"aws_elasticache_parameter_group.bar", "parameter.2196914567.name", "appendfsync"),
    58  					resource.TestCheckResourceAttr(
    59  						"aws_elasticache_parameter_group.bar", "parameter.2196914567.value", "always"),
    60  				),
    61  			},
    62  		},
    63  	})
    64  }
    65  
    66  func TestAccAWSElasticacheParameterGroupOnly(t *testing.T) {
    67  	var v elasticache.CacheParameterGroup
    68  	rName := fmt.Sprintf("parameter-group-test-terraform-%d", acctest.RandInt())
    69  
    70  	resource.Test(t, resource.TestCase{
    71  		PreCheck:     func() { testAccPreCheck(t) },
    72  		Providers:    testAccProviders,
    73  		CheckDestroy: testAccCheckAWSElasticacheParameterGroupDestroy,
    74  		Steps: []resource.TestStep{
    75  			resource.TestStep{
    76  				Config: testAccAWSElasticacheParameterGroupOnlyConfig(rName),
    77  				Check: resource.ComposeTestCheckFunc(
    78  					testAccCheckAWSElasticacheParameterGroupExists("aws_elasticache_parameter_group.bar", &v),
    79  					testAccCheckAWSElasticacheParameterGroupAttributes(&v, rName),
    80  					resource.TestCheckResourceAttr(
    81  						"aws_elasticache_parameter_group.bar", "name", rName),
    82  					resource.TestCheckResourceAttr(
    83  						"aws_elasticache_parameter_group.bar", "family", "redis2.8"),
    84  				),
    85  			},
    86  		},
    87  	})
    88  }
    89  
    90  func testAccCheckAWSElasticacheParameterGroupDestroy(s *terraform.State) error {
    91  	conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
    92  
    93  	for _, rs := range s.RootModule().Resources {
    94  		if rs.Type != "aws_elasticache_parameter_group" {
    95  			continue
    96  		}
    97  
    98  		// Try to find the Group
    99  		resp, err := conn.DescribeCacheParameterGroups(
   100  			&elasticache.DescribeCacheParameterGroupsInput{
   101  				CacheParameterGroupName: aws.String(rs.Primary.ID),
   102  			})
   103  
   104  		if err == nil {
   105  			if len(resp.CacheParameterGroups) != 0 &&
   106  				*resp.CacheParameterGroups[0].CacheParameterGroupName == rs.Primary.ID {
   107  				return fmt.Errorf("Cache Parameter Group still exists")
   108  			}
   109  		}
   110  
   111  		// Verify the error
   112  		newerr, ok := err.(awserr.Error)
   113  		if !ok {
   114  			return err
   115  		}
   116  		if newerr.Code() != "CacheParameterGroupNotFound" {
   117  			return err
   118  		}
   119  	}
   120  
   121  	return nil
   122  }
   123  
   124  func testAccCheckAWSElasticacheParameterGroupAttributes(v *elasticache.CacheParameterGroup, rName string) resource.TestCheckFunc {
   125  	return func(s *terraform.State) error {
   126  
   127  		if *v.CacheParameterGroupName != rName {
   128  			return fmt.Errorf("bad name: %#v", v.CacheParameterGroupName)
   129  		}
   130  
   131  		if *v.CacheParameterGroupFamily != "redis2.8" {
   132  			return fmt.Errorf("bad family: %#v", v.CacheParameterGroupFamily)
   133  		}
   134  
   135  		return nil
   136  	}
   137  }
   138  
   139  func testAccCheckAWSElasticacheParameterGroupExists(n string, v *elasticache.CacheParameterGroup) resource.TestCheckFunc {
   140  	return func(s *terraform.State) error {
   141  		rs, ok := s.RootModule().Resources[n]
   142  		if !ok {
   143  			return fmt.Errorf("Not found: %s", n)
   144  		}
   145  
   146  		if rs.Primary.ID == "" {
   147  			return fmt.Errorf("No Cache Parameter Group ID is set")
   148  		}
   149  
   150  		conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
   151  
   152  		opts := elasticache.DescribeCacheParameterGroupsInput{
   153  			CacheParameterGroupName: aws.String(rs.Primary.ID),
   154  		}
   155  
   156  		resp, err := conn.DescribeCacheParameterGroups(&opts)
   157  
   158  		if err != nil {
   159  			return err
   160  		}
   161  
   162  		if len(resp.CacheParameterGroups) != 1 ||
   163  			*resp.CacheParameterGroups[0].CacheParameterGroupName != rs.Primary.ID {
   164  			return fmt.Errorf("Cache Parameter Group not found")
   165  		}
   166  
   167  		*v = *resp.CacheParameterGroups[0]
   168  
   169  		return nil
   170  	}
   171  }
   172  
   173  func testAccAWSElasticacheParameterGroupConfig(rName string) string {
   174  	return fmt.Sprintf(`
   175  resource "aws_elasticache_parameter_group" "bar" {
   176  	name = "%s"
   177  	family = "redis2.8"
   178  	parameter {
   179  	  name = "appendonly"
   180  	  value = "yes"
   181  	}
   182  }`, rName)
   183  }
   184  
   185  func testAccAWSElasticacheParameterGroupAddParametersConfig(rName string) string {
   186  	return fmt.Sprintf(`
   187  resource "aws_elasticache_parameter_group" "bar" {
   188  	name = "%s"
   189  	family = "redis2.8"
   190  	description = "Test parameter group for terraform"
   191  	parameter {
   192  	  name = "appendonly"
   193  	  value = "yes"
   194  	}
   195  	parameter {
   196  	  name = "appendfsync"
   197  	  value = "always"
   198  	}
   199  }`, rName)
   200  }
   201  
   202  func testAccAWSElasticacheParameterGroupOnlyConfig(rName string) string {
   203  	return fmt.Sprintf(`
   204  resource "aws_elasticache_parameter_group" "bar" {
   205  	name = "%s"
   206  	family = "redis2.8"
   207  	description = "Test parameter group for terraform"
   208  }`, rName)
   209  }