Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws_elasticache_parameter_group: reset ID if deleted manually #32669

Merged
merged 4 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/32669.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_elasticache_parameter_group: Remove from state on resource Read if deleted outside of Terraform
```
13 changes: 6 additions & 7 deletions internal/service/elasticache/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,26 +190,25 @@ func FindParameterGroupByName(ctx context.Context, conn *elasticache.ElastiCache
input := elasticache.DescribeCacheParameterGroupsInput{
CacheParameterGroupName: aws.String(name),
}
out, err := conn.DescribeCacheParameterGroupsWithContext(ctx, &input)

output, err := conn.DescribeCacheParameterGroupsWithContext(ctx, &input)

if tfawserr.ErrCodeEquals(err, elasticache.ErrCodeCacheParameterGroupNotFoundFault) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

switch count := len(out.CacheParameterGroups); count {
case 0:
if output == nil {
return nil, tfresource.NewEmptyResultError(input)
case 1:
return out.CacheParameterGroups[0], nil
default:
return nil, tfresource.NewTooManyResultsError(count, input)
}

return tfresource.AssertSinglePtrResult(output.CacheParameterGroups)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh TIL!

}

type redisParameterGroupFilter func(group *elasticache.CacheParameterGroup) bool
Expand Down
69 changes: 40 additions & 29 deletions internal/service/elasticache/parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,34 @@ func ResourceParameterGroup() *schema.Resource {
ReadWithoutTimeout: resourceParameterGroupRead,
UpdateWithoutTimeout: resourceParameterGroupUpdate,
DeleteWithoutTimeout: resourceParameterGroupDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"name": {
"arn": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Required: true,
StateFunc: func(val interface{}) string {
return strings.ToLower(val.(string))
},
Default: "Managed by Terraform",
},
"family": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "Managed by Terraform",
},
"arn": {
Type: schema.TypeString,
Computed: true,
Required: true,
StateFunc: func(val interface{}) string {
return strings.ToLower(val.(string))
},
},
"parameter": {
Type: schema.TypeSet,
Expand All @@ -81,6 +83,7 @@ func ResourceParameterGroup() *schema.Resource {
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
}
}
Expand All @@ -89,29 +92,29 @@ func resourceParameterGroupCreate(ctx context.Context, d *schema.ResourceData, m
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ElastiCacheConn(ctx)

input := elasticache.CreateCacheParameterGroupInput{
CacheParameterGroupName: aws.String(d.Get("name").(string)),
name := d.Get("name").(string)
input := &elasticache.CreateCacheParameterGroupInput{
CacheParameterGroupName: aws.String(name),
CacheParameterGroupFamily: aws.String(d.Get("family").(string)),
Description: aws.String(d.Get("description").(string)),
Tags: getTagsIn(ctx),
}

resp, err := conn.CreateCacheParameterGroupWithContext(ctx, &input)
output, err := conn.CreateCacheParameterGroupWithContext(ctx, input)

if input.Tags != nil && verify.ErrorISOUnsupported(conn.PartitionID, err) {
log.Printf("[WARN] failed creating ElastiCache Parameter Group with tags: %s. Trying create without tags.", err)

input.Tags = nil
resp, err = conn.CreateCacheParameterGroupWithContext(ctx, &input)
output, err = conn.CreateCacheParameterGroupWithContext(ctx, input)
}

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating ElastiCache Parameter Group: %s", err)
return sdkdiag.AppendErrorf(diags, "creating ElastiCache Parameter Group (%s): %s", name, err)
}

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

return append(diags, resourceParameterGroupUpdate(ctx, d, meta)...)
}
Expand All @@ -121,27 +124,35 @@ func resourceParameterGroupRead(ctx context.Context, d *schema.ResourceData, met
conn := meta.(*conns.AWSClient).ElastiCacheConn(ctx)

parameterGroup, err := FindParameterGroupByName(ctx, conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] ElastiCache Parameter Group (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
}

if err != nil {
return sdkdiag.AppendErrorf(diags, "unable to find ElastiCache Parameter Group (%s): %s", d.Id(), err)
return sdkdiag.AppendErrorf(diags, "reading ElastiCache Parameter Group (%s): %s", d.Id(), err)
}

d.Set("name", parameterGroup.CacheParameterGroupName)
d.Set("family", parameterGroup.CacheParameterGroupFamily)
d.Set("description", parameterGroup.Description)
d.Set("arn", parameterGroup.ARN)
d.Set("description", parameterGroup.Description)
d.Set("family", parameterGroup.CacheParameterGroupFamily)
d.Set("name", parameterGroup.CacheParameterGroupName)

// Only include user customized parameters as there's hundreds of system/default ones
describeParametersOpts := elasticache.DescribeCacheParametersInput{
// Only include user customized parameters as there's hundreds of system/default ones.
input := &elasticache.DescribeCacheParametersInput{
CacheParameterGroupName: aws.String(d.Id()),
Source: aws.String("user"),
}

describeParametersResp, err := conn.DescribeCacheParametersWithContext(ctx, &describeParametersOpts)
output, err := conn.DescribeCacheParametersWithContext(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading ElastiCache Parameter Group (%s): %s", d.Id(), err)
return sdkdiag.AppendErrorf(diags, "reading ElastiCache Parameter Group (%s) parameters: %s", d.Id(), err)
}

d.Set("parameter", FlattenParameters(describeParametersResp.Parameters))
d.Set("parameter", FlattenParameters(output.Parameters))

return diags
}
Expand Down