Skip to content

Commit 1c2591d

Browse files
committed
Add 'RequiresReplaceWO' plan modifier.
1 parent 8d24577 commit 1c2591d

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package stringplanmodifier
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
10+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
11+
"github.com/hashicorp/terraform-provider-aws/internal/framework/privatestate"
12+
)
13+
14+
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+
17+
return stringplanmodifier.RequiresReplaceIf(func(ctx context.Context, request planmodifier.StringRequest, response *stringplanmodifier.RequiresReplaceIfFuncResponse) {
18+
newValue := request.ConfigValue
19+
newValueExists := !newValue.IsNull()
20+
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+
}
27+
28+
if !newValueExists {
29+
if oldValueExists {
30+
response.RequiresReplace = true
31+
}
32+
return
33+
}
34+
35+
if !oldValueExists {
36+
response.RequiresReplace = true
37+
return
38+
}
39+
40+
equal, diags := woStore.EqualValue(ctx, newValue)
41+
response.Diagnostics.Append(diags...)
42+
if response.Diagnostics.HasError() {
43+
return
44+
}
45+
46+
if !equal {
47+
response.RequiresReplace = true
48+
}
49+
}, description, description)
50+
}

0 commit comments

Comments
 (0)