hashicorp / terraform-plugin-framework

A next-generation framework for building Terraform providers.
https://developer.hashicorp.com/terraform/plugin/framework
Mozilla Public License 2.0
304 stars 93 forks source link

tfsdk: Allow GetAttribute To Return Unknown Values Instead Of Null When Missing Due To Parent Being Unknown #186

Open bflad opened 3 years ago

bflad commented 3 years ago

Module version

v0.4.2

Use-cases

As part of fixing #150, the GetAttribute() method on Config, Plan, and State will skip tftypes.ErrInvalidStep errors and instead return null values when valid, but missing. A related case of this behavior that might be useful for functionality such as plan modifiers and validation is knowing whether the value is instead unknown, which can happen if the value is missing from Config, Plan, or State because a parent path value is marked as unknown.

Attempted Solutions

Providers can verify if any parent paths are unknown, when a null value is returned. For example in attribute plan modifiers, the entire Config, Plan, or State value is available.

Proposal

Have the GetAttribute() method on Config, Plan, and State automatically perform the parent path checks for unknown. If found, return unknown value for type, otherwise continue returning null value for type.

References

paddycarver commented 3 years ago

I think we may also just want to complicate the errors returned by tftypes.WalkAttributePath to be more explicit about why a step is invalid.

bflad commented 3 years ago

Created https://github.com/hashicorp/terraform-plugin-go/issues/113 upstream to noodle on that.

bflad commented 1 year ago

Another option here is limiting GetAttribute() and SetAttribute() to only allow a single AtName() path step for the path. That removes the possibility of getting an incorrect value state, since Terraform should always ensure that top-level attributes are always set correctly. This also would remove the current framework implementation complexity associated with switching to/from the terraform-plugin-go path system and transform data manipulation beyond that first "level".

Whether or not that means making that name parameter a string, path.PathStepAttributeName (confusing or verbose for provider code, I'm sure), or leaving it as path.Path is certainly debatable. We would not be able to change the existing tfsdk.Config, tfsdk.Plan, or tfsdk.State parameter implementation or limit its existing implementation due to 1.x compatibility promises, but it may be something to consider when thinking about #590 should new types be introduced.