Skip to content

Commit 5c33d3d

Browse files
committed
Get 'RequiresReplaceWO' working.
1 parent 7ca6181 commit 5c33d3d

File tree

1 file changed

+43
-28
lines changed

1 file changed

+43
-28
lines changed

internal/framework/planmodifiers/stringplanmodifier/requires_replace_wo.go

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,59 @@ import (
77
"context"
88

99
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
10-
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1110
"github.com/hashicorp/terraform-provider-aws/internal/framework/privatestate"
1211
)
1312

13+
// RequiresReplaceWO returns a plan modifier that forces resource replacement
14+
// if a write-only value changes.
1415
func RequiresReplaceWO(privateStateKey string) planmodifier.String {
15-
description := "If the value of this write-only attribute changes, Terraform will destroy and recreate the resource."
16+
return requiresReplaceWO{
17+
privateStateKey: privateStateKey,
18+
}
19+
}
1620

17-
return stringplanmodifier.RequiresReplaceIf(func(ctx context.Context, request planmodifier.StringRequest, response *stringplanmodifier.RequiresReplaceIfFuncResponse) {
18-
newValue := request.ConfigValue
19-
newValueExists := !newValue.IsNull()
21+
type requiresReplaceWO struct {
22+
privateStateKey string
23+
}
2024

21-
woStore := privatestate.NewWriteOnlyValueStore(request.Private, privateStateKey)
22-
oldValueExists, diags := woStore.HasValue(ctx)
23-
response.Diagnostics.Append(diags...)
24-
if response.Diagnostics.HasError() {
25-
return
26-
}
25+
func (m requiresReplaceWO) Description(ctx context.Context) string {
26+
return m.MarkdownDescription(ctx)
27+
}
2728

28-
if !newValueExists {
29-
if oldValueExists {
30-
response.RequiresReplace = true
31-
}
32-
return
33-
}
29+
func (m requiresReplaceWO) MarkdownDescription(context.Context) string {
30+
return "If the value of this write-only attribute changes, Terraform will destroy and recreate the resource."
31+
}
3432

35-
if !oldValueExists {
36-
response.RequiresReplace = true
37-
return
38-
}
33+
func (m requiresReplaceWO) PlanModifyString(ctx context.Context, request planmodifier.StringRequest, response *planmodifier.StringResponse) {
34+
newValue := request.ConfigValue
35+
newValueExists := !newValue.IsNull()
3936

40-
equal, diags := woStore.EqualValue(ctx, newValue)
41-
response.Diagnostics.Append(diags...)
42-
if response.Diagnostics.HasError() {
43-
return
44-
}
37+
woStore := privatestate.NewWriteOnlyValueStore(request.Private, m.privateStateKey)
38+
oldValueExists, diags := woStore.HasValue(ctx)
39+
response.Diagnostics.Append(diags...)
40+
if response.Diagnostics.HasError() {
41+
return
42+
}
4543

46-
if !equal {
44+
if !newValueExists {
45+
if oldValueExists {
4746
response.RequiresReplace = true
4847
}
49-
}, description, description)
48+
return
49+
}
50+
51+
if !oldValueExists {
52+
response.RequiresReplace = true
53+
return
54+
}
55+
56+
equal, diags := woStore.EqualValue(ctx, newValue)
57+
response.Diagnostics.Append(diags...)
58+
if response.Diagnostics.HasError() {
59+
return
60+
}
61+
62+
if !equal {
63+
response.RequiresReplace = true
64+
}
5065
}

0 commit comments

Comments
 (0)