Skip to content

Commit

Permalink
adding tags to aws_elasticache_parameter_group and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pall-valmundsson committed May 27, 2021
1 parent 8a4d82b commit 3ebccc1
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
40 changes: 40 additions & 0 deletions aws/resource_aws_elasticache_parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/hashcode"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsElasticacheParameterGroup() *schema.Resource {
Expand Down Expand Up @@ -45,6 +46,10 @@ func resourceAwsElasticacheParameterGroup() *schema.Resource {
ForceNew: true,
Default: "Managed by Terraform",
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"parameter": {
Type: schema.TypeSet,
Optional: true,
Expand All @@ -62,17 +67,23 @@ func resourceAwsElasticacheParameterGroup() *schema.Resource {
},
Set: resourceAwsElasticacheParameterHash,
},
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
},
CustomizeDiff: SetTagsDiff,
}
}

func resourceAwsElasticacheParameterGroupCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticacheconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(keyvaluetags.New(d.Get("tags").(map[string]interface{})))

createOpts := elasticache.CreateCacheParameterGroupInput{
CacheParameterGroupName: aws.String(d.Get("name").(string)),
CacheParameterGroupFamily: aws.String(d.Get("family").(string)),
Description: aws.String(d.Get("description").(string)),
Tags: tags.IgnoreAws().ElasticacheTags(),
}

log.Printf("[DEBUG] Create ElastiCache Parameter Group: %#v", createOpts)
Expand All @@ -82,13 +93,16 @@ func resourceAwsElasticacheParameterGroupCreate(d *schema.ResourceData, meta int
}

d.SetId(aws.StringValue(resp.CacheParameterGroup.CacheParameterGroupName))
d.Set("arn", aws.StringValue(resp.CacheParameterGroup.ARN))
log.Printf("[INFO] ElastiCache Parameter Group ID: %s", d.Id())

return resourceAwsElasticacheParameterGroupUpdate(d, meta)
}

func resourceAwsElasticacheParameterGroupRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticacheconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

describeOpts := elasticache.DescribeCacheParameterGroupsInput{
CacheParameterGroupName: aws.String(d.Id()),
Expand All @@ -107,6 +121,24 @@ func resourceAwsElasticacheParameterGroupRead(d *schema.ResourceData, meta inter
d.Set("name", describeResp.CacheParameterGroups[0].CacheParameterGroupName)
d.Set("family", describeResp.CacheParameterGroups[0].CacheParameterGroupFamily)
d.Set("description", describeResp.CacheParameterGroups[0].Description)
d.Set("arn", describeResp.CacheParameterGroups[0].ARN)

tags, err := keyvaluetags.ElasticacheListTags(conn, aws.StringValue(describeResp.CacheParameterGroups[0].ARN))

if err != nil {
return fmt.Errorf("error listing tags for ElastiCache Parameter Group (%s): %w", d.Id(), err)
}

tags = tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return fmt.Errorf("error setting tags_all: %w", err)
}

// Only include user customized parameters as there's hundreds of system/default ones
describeParametersOpts := elasticache.DescribeCacheParametersInput{
Expand All @@ -127,6 +159,14 @@ func resourceAwsElasticacheParameterGroupRead(d *schema.ResourceData, meta inter
func resourceAwsElasticacheParameterGroupUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticacheconn

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")

if err := keyvaluetags.ElasticacheUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating ElastiCache Parameter Group (%s) tags: %w", d.Get("arn").(string), err)
}
}

if d.HasChange("parameter") {
o, n := d.GetChange("parameter")
toRemove, toAdd := elastiCacheParameterChanges(o, n)
Expand Down
72 changes: 72 additions & 0 deletions aws/resource_aws_elasticache_parameter_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func TestAccAWSElasticacheParameterGroup_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "description", "Managed by Terraform"),
resource.TestCheckResourceAttr(resourceName, "family", "redis2.8"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
Expand Down Expand Up @@ -413,6 +414,50 @@ func TestAccAWSElasticacheParameterGroup_Description(t *testing.T) {
})
}

func TestAccAWSElasticacheParameterGroup_Tags(t *testing.T) {
var cacheParameterGroup1 elasticache.CacheParameterGroup
resourceName := "aws_elasticache_parameter_group.test"
rName := fmt.Sprintf("parameter-group-test-terraform-%d", acctest.RandInt())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, elasticache.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSElasticacheParameterGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSElasticacheParameterGroupConfigTags1(rName, "redis2.8", "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheParameterGroupExists(resourceName, &cacheParameterGroup1),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
Config: testAccAWSElasticacheParameterGroupConfigTags2(rName, "redis2.8", "key1", "updatedvalue1", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheParameterGroupExists(resourceName, &cacheParameterGroup1),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "updatedvalue1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccAWSElasticacheParameterGroupConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheParameterGroupExists(resourceName, &cacheParameterGroup1),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckAWSElasticacheParameterGroupDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).elasticacheconn

Expand Down Expand Up @@ -545,6 +590,33 @@ resource "aws_elasticache_parameter_group" "test" {
`, family, rName, parameterName1, parameterValue1, parameterName2, parameterValue2)
}

func testAccAWSElasticacheParameterGroupConfigTags1(rName, family, tagName1, tagValue1 string) string {
return fmt.Sprintf(`
resource "aws_elasticache_parameter_group" "test" {
family = %[1]q
name = %[2]q
tags = {
%[3]s = %[4]q
}
}
`, family, rName, tagName1, tagValue1)
}

func testAccAWSElasticacheParameterGroupConfigTags2(rName, family, tagName1, tagValue1, tagName2, tagValue2 string) string {
return fmt.Sprintf(`
resource "aws_elasticache_parameter_group" "test" {
family = %[1]q
name = %[2]q
tags = {
%[3]s = %[4]q
%[5]s = %[6]q
}
}
`, family, rName, tagName1, tagValue1, tagName2, tagValue2)
}

func TestFlattenElasticacheParameters(t *testing.T) {
cases := []struct {
Input []*elasticache.Parameter
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/elasticache_parameter_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The following arguments are supported:
* `family` - (Required) The family of the ElastiCache parameter group.
* `description` - (Optional) The description of the ElastiCache parameter group. Defaults to "Managed by Terraform".
* `parameter` - (Optional) A list of ElastiCache parameters to apply.
* `tags` - (Optional) Key-value mapping of resource tags. If configured with a provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

Parameter blocks support the following:

Expand All @@ -50,6 +51,8 @@ Parameter blocks support the following:
In addition to all arguments above, the following attributes are exported:

* `id` - The ElastiCache parameter group name.
* `arn` - The AWS ARN associated with the parameter group.
* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block).


## Import
Expand Down

0 comments on commit 3ebccc1

Please sign in to comment.