-
Notifications
You must be signed in to change notification settings - Fork 99
all: Add UseNonNullStateForUnknown plan modifier to assist with child nested attribute use-cases
#1242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
austinvalle
wants to merge
5
commits into
main
Choose a base branch
from
av/use-non-null-planmodifier
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
all: Add UseNonNullStateForUnknown plan modifier to assist with child nested attribute use-cases
#1242
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| kind: FEATURES | ||
| body: 'all: Added a new plan modifier for all types, `UseNonNullStateForUnknown` that preserves known, non-null, values for unconfigured attributes. | ||
| This can be used when it is known that an unconfigured value will remain the same after the attribute is updated to a non-null value.' | ||
| time: 2025-11-21T16:59:55.611735-05:00 | ||
| custom: | ||
| Issue: "1242" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| kind: NOTES | ||
| body: 'In `[email protected]`, the `UseStateForUnknown` plan modifier was updated to preserve null values from prior state | ||
| for unconfigured attributes. This updated version can cause plan inconsistency errors when used on child attributes of a nested attribute | ||
| that expect `UseStateForUnknown` to keep the child attributes on new nested objects as `<unknown>` (known after apply). | ||
|
|
||
| The new `UseNonNullStateForUnknown` plan modifier can now be used where child attributes are expecting this pre-1.15.1 behavior.' | ||
| time: 2025-11-21T17:01:17.961155-05:00 | ||
| custom: | ||
| Issue: "1197" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
resource/schema/boolplanmodifier/use_non_null_state_for_unknown.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package boolplanmodifier | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
| ) | ||
|
|
||
| // UseNonNullStateForUnknown returns a plan modifier that copies a known, non-null, prior state | ||
| // value into the planned value. Use this when it is known that an unconfigured value will remain the | ||
| // same after the attribute is updated to a non-null value. | ||
| // | ||
| // To prevent Terraform errors, the framework automatically sets unconfigured | ||
| // and Computed attributes to an unknown value "(known after apply)" on update. | ||
| // Using this plan modifier will instead display the non-null prior state value in the | ||
| // plan, unless a prior plan modifier adjusts the value. | ||
| // | ||
| // This plan modifier can be a useful alternative to [UseStateForUnknown] when the attribute is | ||
| // a child of a nested attribute that can be null after the resource is created. | ||
| func UseNonNullStateForUnknown() planmodifier.Bool { | ||
| return useNonNullStateForUnknown{} | ||
| } | ||
|
|
||
| type useNonNullStateForUnknown struct{} | ||
|
|
||
| func (m useNonNullStateForUnknown) Description(_ context.Context) string { | ||
| return "Once set to a non-null value, the value of this attribute in state will not change." | ||
| } | ||
|
|
||
| func (m useNonNullStateForUnknown) MarkdownDescription(_ context.Context) string { | ||
| return "Once set to a non-null value, the value of this attribute in state will not change." | ||
| } | ||
|
|
||
| func (m useNonNullStateForUnknown) PlanModifyBool(ctx context.Context, req planmodifier.BoolRequest, resp *planmodifier.BoolResponse) { | ||
| // Do nothing if the state value is null. | ||
| if req.StateValue.IsNull() { | ||
| return | ||
| } | ||
|
|
||
| // Do nothing if there is a known planned value. | ||
| if !req.PlanValue.IsUnknown() { | ||
| return | ||
| } | ||
|
|
||
| // Do nothing if there is an unknown configuration value, otherwise interpolation gets messed up. | ||
| if req.ConfigValue.IsUnknown() { | ||
| return | ||
| } | ||
|
|
||
| resp.PlanValue = req.StateValue | ||
| } |
166 changes: 166 additions & 0 deletions
166
resource/schema/boolplanmodifier/use_non_null_state_for_unknown_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package boolplanmodifier_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier" | ||
| "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
| "github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||
| "github.com/hashicorp/terraform-plugin-go/tftypes" | ||
| ) | ||
|
|
||
| func TestUseNonNullStateForUnknownModifierPlanModifyBool(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| testCases := map[string]struct { | ||
| request planmodifier.BoolRequest | ||
| expected *planmodifier.BoolResponse | ||
| }{ | ||
| "null-state": { | ||
| // when we first create the resource, the state value will be null, | ||
| // so use the unknown value | ||
| request: planmodifier.BoolRequest{ | ||
| State: tfsdk.State{ | ||
| Raw: tftypes.NewValue( | ||
| tftypes.Object{ | ||
| AttributeTypes: map[string]tftypes.Type{ | ||
| "attr": tftypes.Bool, | ||
| }, | ||
| }, | ||
| nil, | ||
| ), | ||
| }, | ||
| StateValue: types.BoolNull(), | ||
| PlanValue: types.BoolUnknown(), | ||
| ConfigValue: types.BoolNull(), | ||
| }, | ||
| expected: &planmodifier.BoolResponse{ | ||
| PlanValue: types.BoolUnknown(), | ||
| }, | ||
| }, | ||
| "known-plan": { | ||
| // this would really only happen if we had a plan | ||
| // modifier setting the value before this plan modifier | ||
| // got to it | ||
| // | ||
| // but we still want to preserve that value, in this | ||
| // case | ||
| request: planmodifier.BoolRequest{ | ||
| State: tfsdk.State{ | ||
| Raw: tftypes.NewValue( | ||
| tftypes.Object{ | ||
| AttributeTypes: map[string]tftypes.Type{ | ||
| "attr": tftypes.Bool, | ||
| }, | ||
| }, | ||
| map[string]tftypes.Value{ | ||
| "attr": tftypes.NewValue(tftypes.Bool, false), | ||
| }, | ||
| ), | ||
| }, | ||
| StateValue: types.BoolValue(false), | ||
| PlanValue: types.BoolValue(true), | ||
| ConfigValue: types.BoolNull(), | ||
| }, | ||
| expected: &planmodifier.BoolResponse{ | ||
| PlanValue: types.BoolValue(true), | ||
| }, | ||
| }, | ||
| "non-null-state-value-unknown-plan": { | ||
| // this is the situation we want to preserve the state in | ||
| request: planmodifier.BoolRequest{ | ||
| State: tfsdk.State{ | ||
| Raw: tftypes.NewValue( | ||
| tftypes.Object{ | ||
| AttributeTypes: map[string]tftypes.Type{ | ||
| "attr": tftypes.Bool, | ||
| }, | ||
| }, | ||
| map[string]tftypes.Value{ | ||
| "attr": tftypes.NewValue(tftypes.Bool, true), | ||
| }, | ||
| ), | ||
| }, | ||
| StateValue: types.BoolValue(true), | ||
| PlanValue: types.BoolUnknown(), | ||
| ConfigValue: types.BoolNull(), | ||
| }, | ||
| expected: &planmodifier.BoolResponse{ | ||
| PlanValue: types.BoolValue(true), | ||
| }, | ||
| }, | ||
| "null-state-value-unknown-plan": { | ||
| // Null state values should not be preserved | ||
| request: planmodifier.BoolRequest{ | ||
| State: tfsdk.State{ | ||
| Raw: tftypes.NewValue( | ||
| tftypes.Object{ | ||
| AttributeTypes: map[string]tftypes.Type{ | ||
| "attr": tftypes.Bool, | ||
| }, | ||
| }, | ||
| map[string]tftypes.Value{ | ||
| "attr": tftypes.NewValue(tftypes.Bool, nil), | ||
| }, | ||
| ), | ||
| }, | ||
| StateValue: types.BoolNull(), | ||
| PlanValue: types.BoolUnknown(), | ||
| ConfigValue: types.BoolNull(), | ||
| }, | ||
| expected: &planmodifier.BoolResponse{ | ||
| PlanValue: types.BoolUnknown(), | ||
| }, | ||
| }, | ||
| "unknown-config": { | ||
| // this is the situation in which a user is | ||
| // interpolating into a field. We want that to still | ||
| // show up as unknown, otherwise they'll get apply-time | ||
| // errors for changing the value even though we knew it | ||
| // was legitimately possible for it to change and the | ||
| // provider can't prevent this from happening | ||
| request: planmodifier.BoolRequest{ | ||
| State: tfsdk.State{ | ||
| Raw: tftypes.NewValue( | ||
| tftypes.Object{ | ||
| AttributeTypes: map[string]tftypes.Type{ | ||
| "attr": tftypes.Bool, | ||
| }, | ||
| }, | ||
| map[string]tftypes.Value{ | ||
| "attr": tftypes.NewValue(tftypes.Bool, true), | ||
| }, | ||
| ), | ||
| }, | ||
| StateValue: types.BoolValue(true), | ||
| PlanValue: types.BoolUnknown(), | ||
| ConfigValue: types.BoolUnknown(), | ||
| }, | ||
| expected: &planmodifier.BoolResponse{ | ||
| PlanValue: types.BoolUnknown(), | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for name, testCase := range testCases { | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| resp := &planmodifier.BoolResponse{ | ||
| PlanValue: testCase.request.PlanValue, | ||
| } | ||
|
|
||
| boolplanmodifier.UseNonNullStateForUnknown().PlanModifyBool(context.Background(), testCase.request, resp) | ||
|
|
||
| if diff := cmp.Diff(testCase.expected, resp); diff != "" { | ||
| t.Errorf("unexpected difference: %s", diff) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
resource/schema/dynamicplanmodifier/use_non_null_state_for_unknown.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package dynamicplanmodifier | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
| ) | ||
|
|
||
| // UseNonNullStateForUnknown returns a plan modifier that copies a known, non-null, prior state | ||
| // value into the planned value. Use this when it is known that an unconfigured value will remain the | ||
| // same after the attribute is updated to a non-null value. | ||
| // | ||
| // To prevent Terraform errors, the framework automatically sets unconfigured | ||
| // and Computed attributes to an unknown value "(known after apply)" on update. | ||
| // Using this plan modifier will instead display the non-null prior state value in the | ||
| // plan, unless a prior plan modifier adjusts the value. | ||
| // | ||
| // This plan modifier can be a useful alternative to [UseStateForUnknown] when the attribute is | ||
| // a child of a nested attribute that can be null after the resource is created. | ||
| func UseNonNullStateForUnknown() planmodifier.Dynamic { | ||
| return useNonNullStateForUnknown{} | ||
| } | ||
|
|
||
| type useNonNullStateForUnknown struct{} | ||
|
|
||
| func (m useNonNullStateForUnknown) Description(_ context.Context) string { | ||
| return "Once set to a non-null value, the value of this attribute in state will not change." | ||
| } | ||
|
|
||
| func (m useNonNullStateForUnknown) MarkdownDescription(_ context.Context) string { | ||
| return "Once set to a non-null value, the value of this attribute in state will not change." | ||
| } | ||
|
|
||
| func (m useNonNullStateForUnknown) PlanModifyDynamic(ctx context.Context, req planmodifier.DynamicRequest, resp *planmodifier.DynamicResponse) { | ||
| // Do nothing if the state value is null. | ||
| if req.StateValue.IsNull() { | ||
| return | ||
| } | ||
|
|
||
| // Do nothing if there is a known planned value. | ||
| if !req.PlanValue.IsUnknown() { | ||
| return | ||
| } | ||
|
|
||
| // Do nothing if there is an unknown configuration value, otherwise interpolation gets messed up. | ||
| if req.ConfigValue.IsUnknown() { | ||
| return | ||
| } | ||
|
|
||
| resp.PlanValue = req.StateValue | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also updated in the GH release (if we want to change the wording I can re-update 👍🏻 )
https://github.com/hashicorp/terraform-plugin-framework/releases/tag/v1.15.1
It felt weird to update the changelog to reference a future release, so I just mentioned the issue 🙂