Skip to content

Commit

Permalink
Rename to UseStateForUnknown.
Browse files Browse the repository at this point in the history
  • Loading branch information
paddycarver committed Nov 3, 2021
1 parent 8b91fc3 commit e746c9e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .changelog/204.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
```release-note:feature
Added `tfsdk.PreserveState()` as a built-in plan modifier, which will automatically replace an unknown value in the plan with the value from the state. This mimics the behavior of computed and optional+computed values in Terraform Plugin SDK versions 1 and 2. Provider developers will likely want to use it for "write-once" attributes that never change once they're set in state.
Added `tfsdk.UseStateForUnknown()` as a built-in plan modifier, which will automatically replace an unknown value in the plan with the value from the state. This mimics the behavior of computed and optional+computed values in Terraform Plugin SDK versions 1 and 2. Provider developers will likely want to use it for "write-once" attributes that never change once they're set in state.
```
18 changes: 9 additions & 9 deletions tfsdk/attribute_plan_modification.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,24 @@ func (r RequiresReplaceIfModifier) MarkdownDescription(ctx context.Context) stri
return r.markdownDescription
}

// PreserveState returns a PreserveStateModifier.
func PreserveState() AttributePlanModifier {
return PreserveStateModifier{}
// UseStateForUnknown returns a UseStateForUnknownModifier.
func UseStateForUnknown() AttributePlanModifier {
return UseStateForUnknownModifier{}
}

// PreserveStateModifier is an AttributePlanModifier that copies the prior state
// UseStateForUnknownModifier is an AttributePlanModifier that copies the prior state
// value for an attribute into that attribute's plan, if that state is non-null.
//
// Computed attributes without the PreserveState attribute plan modifier will
// Computed attributes without the UseStateForUnknown attribute plan modifier will
// have their value set to Unknown in the plan, so their value always will be
// displayed as "(known after apply)" in the CLI plan output.
// If this plan modifier is used, the prior state value will be displayed in
// the plan instead unless a prior plan modifier adjusts the value.
type PreserveStateModifier struct{}
type UseStateForUnknownModifier struct{}

// Modify copies the attribute's prior state to the attribute plan if the prior
// state value is not null.
func (r PreserveStateModifier) Modify(ctx context.Context, req ModifyAttributePlanRequest, resp *ModifyAttributePlanResponse) {
func (r UseStateForUnknownModifier) Modify(ctx context.Context, req ModifyAttributePlanRequest, resp *ModifyAttributePlanResponse) {
if req.AttributeState == nil || resp.AttributePlan == nil || req.AttributeConfig == nil {
return
}
Expand Down Expand Up @@ -189,12 +189,12 @@ func (r PreserveStateModifier) Modify(ctx context.Context, req ModifyAttributePl
}

// Description returns a human-readable description of the plan modifier.
func (r PreserveStateModifier) Description(ctx context.Context) string {
func (r UseStateForUnknownModifier) Description(ctx context.Context) string {
return "Once set, the value of this attribute in state will not change."
}

// MarkdownDescription returns a markdown description of the plan modifier.
func (r PreserveStateModifier) MarkdownDescription(ctx context.Context) string {
func (r UseStateForUnknownModifier) MarkdownDescription(ctx context.Context) string {
return "Once set, the value of this attribute in state will not change."
}

Expand Down
4 changes: 2 additions & 2 deletions tfsdk/attribute_plan_modification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

func TestPreserveStateModifier(t *testing.T) {
func TestUseStateForUnknownModifier(t *testing.T) {
t.Parallel()

type testCase struct {
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestPreserveStateModifier(t *testing.T) {
resp := &ModifyAttributePlanResponse{
AttributePlan: req.AttributePlan,
}
modifier := PreserveState()
modifier := UseStateForUnknown()

modifier.Modify(context.Background(), req, resp)
if resp.Diagnostics.HasError() {
Expand Down

0 comments on commit e746c9e

Please sign in to comment.