github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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", "Test parameter group for 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  					resource.TestCheckResourceAttr(
    82  						"aws_elasticache_parameter_group.bar", "description", "Test parameter group for terraform"),
    83  				),
    84  			},
    85  		},
    86  	})
    87  }
    88  
    89  func testAccCheckAWSElasticacheParameterGroupDestroy(s *terraform.State) error {
    90  	conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
    91  
    92  	for _, rs := range s.RootModule().Resources {
    93  		if rs.Type != "aws_elasticache_parameter_group" {
    94  			continue
    95  		}
    96  
    97  		// Try to find the Group
    98  		resp, err := conn.DescribeCacheParameterGroups(
    99  			&elasticache.DescribeCacheParameterGroupsInput{
   100  				CacheParameterGroupName: aws.String(rs.Primary.ID),
   101  			})
   102  
   103  		if err == nil {
   104  			if len(resp.CacheParameterGroups) != 0 &&
   105  				*resp.CacheParameterGroups[0].CacheParameterGroupName == rs.Primary.ID {
   106  				return fmt.Errorf("Cache Parameter Group still exists")
   107  			}
   108  		}
   109  
   110  		// Verify the error
   111  		newerr, ok := err.(awserr.Error)
   112  		if !ok {
   113  			return err
   114  		}
   115  		if newerr.Code() != "InvalidCacheParameterGroup.NotFound" {
   116  			return err
   117  		}
   118  	}
   119  
   120  	return nil
   121  }
   122  
   123  func testAccCheckAWSElasticacheParameterGroupAttributes(v *elasticache.CacheParameterGroup) resource.TestCheckFunc {
   124  	return func(s *terraform.State) error {
   125  
   126  		if *v.CacheParameterGroupName != "parameter-group-test-terraform" {
   127  			return fmt.Errorf("bad name: %#v", v.CacheParameterGroupName)
   128  		}
   129  
   130  		if *v.CacheParameterGroupFamily != "redis2.8" {
   131  			return fmt.Errorf("bad family: %#v", v.CacheParameterGroupFamily)
   132  		}
   133  
   134  		if *v.Description != "Test parameter group for terraform" {
   135  			return fmt.Errorf("bad description: %#v", v.Description)
   136  		}
   137  
   138  		return nil
   139  	}
   140  }
   141  
   142  func testAccCheckAWSElasticacheParameterGroupExists(n string, v *elasticache.CacheParameterGroup) resource.TestCheckFunc {
   143  	return func(s *terraform.State) error {
   144  		rs, ok := s.RootModule().Resources[n]
   145  		if !ok {
   146  			return fmt.Errorf("Not found: %s", n)
   147  		}
   148  
   149  		if rs.Primary.ID == "" {
   150  			return fmt.Errorf("No Cache Parameter Group ID is set")
   151  		}
   152  
   153  		conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
   154  
   155  		opts := elasticache.DescribeCacheParameterGroupsInput{
   156  			CacheParameterGroupName: aws.String(rs.Primary.ID),
   157  		}
   158  
   159  		resp, err := conn.DescribeCacheParameterGroups(&opts)
   160  
   161  		if err != nil {
   162  			return err
   163  		}
   164  
   165  		if len(resp.CacheParameterGroups) != 1 ||
   166  			*resp.CacheParameterGroups[0].CacheParameterGroupName != rs.Primary.ID {
   167  			return fmt.Errorf("Cache Parameter Group not found")
   168  		}
   169  
   170  		*v = *resp.CacheParameterGroups[0]
   171  
   172  		return nil
   173  	}
   174  }
   175  
   176  const testAccAWSElasticacheParameterGroupConfig = `
   177  resource "aws_elasticache_parameter_group" "bar" {
   178  	name = "parameter-group-test-terraform"
   179  	family = "redis2.8"
   180  	description = "Test parameter group for terraform"
   181  	parameter {
   182  	  name = "appendonly"
   183  	  value = "yes"
   184  	}
   185  }
   186  `
   187  
   188  const testAccAWSElasticacheParameterGroupAddParametersConfig = `
   189  resource "aws_elasticache_parameter_group" "bar" {
   190  	name = "parameter-group-test-terraform"
   191  	family = "redis2.8"
   192  	description = "Test parameter group for terraform"
   193  	parameter {
   194  	  name = "appendonly"
   195  	  value = "yes"
   196  	}
   197  	parameter {
   198  	  name = "appendfsync"
   199  	  value = "always"
   200  	}
   201  }
   202  `
   203  
   204  const testAccAWSElasticacheParameterGroupOnlyConfig = `
   205  resource "aws_elasticache_parameter_group" "bar" {
   206  	name = "parameter-group-test-terraform"
   207  	family = "redis2.8"
   208  	description = "Test parameter group for terraform"
   209  }
   210  `