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
internal/fwserver: Skip automatic object state conversion and nested attributes plan modification if object is null or unknown #995
Merged
Conversation
This file contains 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
…attributes plan modification if object is null or unknown Reference: #993 By data definition, a null or unknown object has no underlying data where nested attributes should run plan modification. By Terraform data consistency rules, a null or unknown object should be preserved as-is (unless it is an unknown value being refined into a more known value), however this was not the case where a collection-based nested attribute had a null or unknown object, such as this example configuration: ``` resource "examplecloud_thing" "example" { nested_map = { examplekey = null # this was previously problematic } } ``` Previously, a null or unknown object would be errantly transformed by the framework plan modification logic into a known object with null/unknown attributes. This could be seen in human-readable Terraform plans, such as (`{}` below): ``` # examplecloud_thing.example will be created + resource "examplecloud_thing" "example" { + nested_map = { + "examplekey" = {}, } } ``` Following new unit test creation, failures could be seen from the PlanResourceChange RPC: ``` --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip (0.00s) --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-list-nested-optional-and-computed-unknown-element (0.00s) /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference: &fwserver.PlanResourceChangeResponse{ Diagnostics: nil, PlannedPrivate: &{Provider: &{data: {}}}, PlannedState: &tfsdk.State{ - Raw: s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attrib`..., + Raw: s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`, Schema: schema.Schema{Attributes: {"list_nested_attribute": schema.ListNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}}, }, RequiresReplace: s"[]", } --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-list-nested-optional-null-element (0.00s) /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference: &fwserver.PlanResourceChangeResponse{ Diagnostics: nil, PlannedPrivate: &{Provider: &{data: {}}}, PlannedState: &tfsdk.State{ - Raw: s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attrib`..., + Raw: s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<null>>>`, Schema: schema.Schema{Attributes: {"list_nested_attribute": schema.ListNestedAttribute{NestedObject: {...}, Optional: true}}}, }, RequiresReplace: s"[]", } --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-list-nested-optional-and-computed-null-element (0.00s) /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference: &fwserver.PlanResourceChangeResponse{ Diagnostics: nil, PlannedPrivate: &{Provider: &{data: {}}}, PlannedState: &tfsdk.State{ - Raw: s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attrib`..., + Raw: s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`, Schema: schema.Schema{Attributes: {"list_nested_attribute": schema.ListNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}}, }, RequiresReplace: s"[]", } --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-map-nested-optional-and-computed-null-element (0.00s) /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference: &fwserver.PlanResourceChangeResponse{ Diagnostics: nil, PlannedPrivate: &{Provider: &{data: {}}}, PlannedState: &tfsdk.State{ - Raw: s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<"string_att`..., + Raw: s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<unknown>>>`, Schema: schema.Schema{Attributes: {"map_nested_attribute": schema.MapNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}}, }, RequiresReplace: s"[]", } --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-map-nested-optional-unknown-element (0.00s) /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference: &fwserver.PlanResourceChangeResponse{ Diagnostics: nil, PlannedPrivate: &{Provider: &{data: {}}}, PlannedState: &tfsdk.State{ - Raw: s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<"string_att`..., + Raw: s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<unknown>>>`, Schema: schema.Schema{Attributes: {"map_nested_attribute": schema.MapNestedAttribute{NestedObject: {...}, Optional: true}}}, }, RequiresReplace: s"[]", } --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-set-nested-optional-unknown-element (0.00s) /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference: &fwserver.PlanResourceChangeResponse{ Diagnostics: nil, PlannedPrivate: &{Provider: &{data: {}}}, PlannedState: &tfsdk.State{ - Raw: s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attribute"`..., + Raw: s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`, Schema: schema.Schema{Attributes: {"set_nested_attribute": schema.SetNestedAttribute{NestedObject: {...}, Optional: true}}}, }, RequiresReplace: s"[]", } --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-set-nested-optional-null-element (0.00s) /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference: &fwserver.PlanResourceChangeResponse{ Diagnostics: nil, PlannedPrivate: &{Provider: &{data: {}}}, PlannedState: &tfsdk.State{ - Raw: s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attribute"`..., + Raw: s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<null>>>`, Schema: schema.Schema{Attributes: {"set_nested_attribute": schema.SetNestedAttribute{NestedObject: {...}, Optional: true}}}, }, RequiresReplace: s"[]", } --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-set-nested-optional-and-computed-null-element (0.00s) /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference: &fwserver.PlanResourceChangeResponse{ Diagnostics: nil, PlannedPrivate: &{Provider: &{data: {}}}, PlannedState: &tfsdk.State{ - Raw: s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attribute"`..., + Raw: s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`, Schema: schema.Schema{Attributes: {"set_nested_attribute": schema.SetNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}}, }, RequiresReplace: s"[]", } --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-map-nested-optional-null-element (0.00s) /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference: &fwserver.PlanResourceChangeResponse{ Diagnostics: nil, PlannedPrivate: &{Provider: &{data: {}}}, PlannedState: &tfsdk.State{ - Raw: s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<"string_att`..., + Raw: s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<null>>>`, Schema: schema.Schema{Attributes: {"map_nested_attribute": schema.MapNestedAttribute{NestedObject: {...}, Optional: true}}}, }, RequiresReplace: s"[]", } --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-set-nested-optional-and-computed-unknown-element (0.00s) /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference: &fwserver.PlanResourceChangeResponse{ Diagnostics: nil, PlannedPrivate: &{Provider: &{data: {}}}, PlannedState: &tfsdk.State{ - Raw: s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attribute"`..., + Raw: s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`, Schema: schema.Schema{Attributes: {"set_nested_attribute": schema.SetNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}}, }, RequiresReplace: s"[]", } --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-map-nested-optional-and-computed-unknown-element (0.00s) /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference: &fwserver.PlanResourceChangeResponse{ Diagnostics: nil, PlannedPrivate: &{Provider: &{data: {}}}, PlannedState: &tfsdk.State{ - Raw: s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<"string_att`..., + Raw: s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<unknown>>>`, Schema: schema.Schema{Attributes: {"map_nested_attribute": schema.MapNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}}, }, RequiresReplace: s"[]", } --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-list-nested-optional-unknown-element (0.00s) /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference: &fwserver.PlanResourceChangeResponse{ Diagnostics: nil, PlannedPrivate: &{Provider: &{data: {}}}, PlannedState: &tfsdk.State{ - Raw: s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attrib`..., + Raw: s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`, Schema: schema.Schema{Attributes: {"list_nested_attribute": schema.ListNestedAttribute{NestedObject: {...}, Optional: true}}}, }, RequiresReplace: s"[]", } ``` ``` --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip (0.00s) --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-list-nested-optional-and-computed-null-element (0.00s) /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference: &fwserver.PlanResourceChangeResponse{ Diagnostics: nil, PlannedPrivate: &{Provider: &{data: {}}}, PlannedState: &tfsdk.State{ - Raw: s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<null>>>`, + Raw: s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`, Schema: schema.Schema{Attributes: {"list_nested_attribute": schema.ListNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}}, }, RequiresReplace: s"[]", } --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-set-nested-optional-and-computed-null-element (0.00s) /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference: &fwserver.PlanResourceChangeResponse{ Diagnostics: nil, PlannedPrivate: &{Provider: &{data: {}}}, PlannedState: &tfsdk.State{ - Raw: s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<null>>>`, + Raw: s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`, Schema: schema.Schema{Attributes: {"set_nested_attribute": schema.SetNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}}, }, RequiresReplace: s"[]", } --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-map-nested-optional-and-computed-null-element (0.00s) /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference: &fwserver.PlanResourceChangeResponse{ Diagnostics: nil, PlannedPrivate: &{Provider: &{data: {}}}, PlannedState: &tfsdk.State{ - Raw: s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<null>>>`, + Raw: s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<unknown>>>`, Schema: schema.Schema{Attributes: {"map_nested_attribute": schema.MapNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}}, }, RequiresReplace: s"[]", } ``` The plan modification logic was updated to return a null/unknown object early, but only after the object plan modifiers have run. This ensures that provider developers still have an escape hatch to run logic before the framework skips the nested attributes. If provider developers need to override this logic change, object-level plan modifiers can convert a null/unknown object into a known object will all null/unknown attributes, which then the framework will run nested attribute plan modification on those attributes. If this is a common use case, a feature request can be submitted to consider including this type of object plan modifier in the framework.
bendbennett
approved these changes
Apr 30, 2024
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.
LGTM 🚀
Thank you for adding such thorough unit test coverage.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #993
By data definition, a null or unknown object has no underlying data where nested attributes should run plan modification. By Terraform data consistency rules, a null or unknown object should be preserved as-is (unless it is an unknown value being refined into a more known value), however this was not the case where a collection-based nested attribute had a null or unknown object, such as this example configuration:
Previously, a null or unknown object would be errantly transformed by the framework plan modification logic into a known object with null/unknown attributes. This could be seen in human-readable Terraform plans, such as (
{}
below):Following new unit test creation, failures could be seen from the PlanResourceChange RPC:
The plan modification logic was updated to return a null/unknown object early, but only after the object plan modifiers have run. This ensures that provider developers still have an escape hatch to run logic before the framework skips the nested attributes.
If provider developers need to override this logic change, object-level plan modifiers can convert a null/unknown object into a known object will all null/unknown attributes, which then the framework will run nested attribute plan modification on those attributes. If this is a common use case, a feature request can be submitted to consider including this type of object plan modifier in the framework.