|
| 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