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