@@ -7,44 +7,59 @@ import (
7
7
"context"
8
8
9
9
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
10
- "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
11
10
"github.com/hashicorp/terraform-provider-aws/internal/framework/privatestate"
12
11
)
13
12
13
+ // RequiresReplaceWO returns a plan modifier that forces resource replacement
14
+ // if a write-only value changes.
14
15
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
+ }
16
20
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
+ }
20
24
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
+ }
27
28
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
+ }
34
32
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 ()
39
36
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
+ }
45
43
46
- if ! equal {
44
+ if ! newValueExists {
45
+ if oldValueExists {
47
46
response .RequiresReplace = true
48
47
}
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
+ }
50
65
}
0 commit comments