Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 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
41 changes: 37 additions & 4 deletions internal/service/elasticache/serverless_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package elasticache
import (
"context"
"fmt"
"strconv"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
Expand Down Expand Up @@ -90,18 +92,23 @@ func (r *serverlessCacheResource) Schema(ctx context.Context, request resource.S
names.AttrEngine: schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
stringplanmodifier.RequiresReplaceIf(
func(ctx context.Context, req planmodifier.StringRequest, resp *stringplanmodifier.RequiresReplaceIfFuncResponse) {
// In-place updates are only supported for redis -> valkey
// In-place update support for redis -> valkey
if req.StateValue.Equal(types.StringValue(engineRedis)) && req.PlanValue.Equal(types.StringValue(engineValkey)) {
return
}
// In-place updates support for valkey -> redis
if req.StateValue.Equal(types.StringValue(engineValkey)) && req.PlanValue.Equal(types.StringValue(engineRedis)) {
return
}

// Any other change will force a replacement
resp.RequiresReplace = true
},
"Engine modifications other than redis to valkey require a replacement",
"Engine modifications other than redis to valkey require a replacement",
"Engine modifications other than redis to valkey or valkey to redis require a replacement",
"Engine modifications other than redis to valkey or valkey to redis require a replacement",
),
},
},
Expand All @@ -120,7 +127,33 @@ func (r *serverlessCacheResource) Schema(ctx context.Context, request resource.S
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
stringplanmodifier.RequiresReplace(),
stringplanmodifier.RequiresReplaceIf(
func(ctx context.Context, req planmodifier.StringRequest, resp *stringplanmodifier.RequiresReplaceIfFuncResponse) {
var engineVal types.String
req.Config.GetAttribute(ctx, path.Root(names.AttrEngine), &engineVal)

stateFloatVal, err := strconv.ParseFloat(req.StateValue.ValueString(), 64)
if err != nil {
response.Diagnostics.AddError("incorrect major_engine_version format", err.Error())
return
}

planFloatVal, err := strconv.ParseFloat(req.PlanValue.ValueString(), 64)
if err != nil {
response.Diagnostics.AddError("incorrect major_engine_version format", err.Error())
return
}

if stateFloatVal < planFloatVal && engineVal.Equal(types.StringValue(engineValkey)) {
return
}

// Any other change will force a replacement
resp.RequiresReplace = true
},
"major_engine_version downgrade is not supported for valkey",
"major_engine_version downgrade is not supported for valkey",
),
},
},
names.AttrName: schema.StringAttribute{
Expand Down
2 changes: 1 addition & 1 deletion internal/service/elasticache/serverless_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ func TestAccElastiCacheServerlessCache_engine(t *testing.T) {
),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionDestroyBeforeCreate),
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
},
},
},
Expand Down