hashicorp / hashicorp/terraform-plugin-framework
Expose `AttributePath` function in `internal/fromtftypes/attribute_path.go`
- Dominant language
- Go
- Stars
- 384
- Forks
- 107
- Avg merge
- 3m
- Merged PRs (30d)
- 1
Description
### Module version
```
github.com/hashicorp/terraform-plugin-framework v1.14.1
```
### Use-cases
Support converting [tftypes.AttributePath](https://github.com/hashicorp/terraform-plugin-go/blob/main/tftypes/attribute_path.go) to [`path.Path`](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework@v1.14.1/path#Path).
### Attempted Solutions
Copy the code and modify the schema interface: [func AttributePath(ctx context.Context, tfType *tftypes.AttributePath, schema TPFSchema) (path.Path, diag.Diagnostics) {](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/3d0cf672f2ee65de8f3773770c62047a41c61809/internal/common/conversion/path_converter.go#L43).
Allthough this workaround works, we rather have access directly to the method instead of having to update a `THIRD PARTY NOTICES` file and maintain this extra file.
### Proposal
Expose the [func AttributePath(ctx context.Context, tfType *tftypes.AttributePath, schema fwschema.Schema) (path.Path, diag.Diagnostics) {](https://github.com/hashicorp/terraform-plugin-framework/blob/8ab70d039eaac0d4bfc32b56dcee6942aff18951/internal/fromtftypes/attribute_path.go#L17) or a simplified version that converts `tftypes.AttributePath` to `path.Path`.
### References
- Discussion: [Best practices for handling “known after apply” plan verbosity in TPF resources](https://discuss.hashicorp.com/t/best-practices-for-handling-known-after-apply-plan-verbosity-in-tpf-resources/73806)
### More Background Context
We are trying to reduce the verbosity users get in the plan diff.
For some attributes we cannot use the `UseStateForUnknown` plan modifier directly. We need extra logic to say: *Only use state when related attributes are unchanged*.
Therefore, in our plan modifier [logic](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/CLOUDP-308783_plan_modify_replication_specs_auto_scaling/internal/common/customplanmodifier/plan_modify_differ.go#L20) we use the `Diff` method on [`tfsdk.State.Raw`](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework@v1.14.1/tfsdk#State) to find changes between the state and plan.
```go
func NewPlanModifyDiffer(ctx context.Context, state *tfsdk.State, plan *tfsdk.Plan, diags *diag.Diagnostics, schema conversion.TPFSchema) *PlanModifyDiffer {
diffStatePlan, err := state.Raw.Diff(plan.Raw) // returns []tftypes.ValueDiff, error
```
The response from this diff is a [`[]tftypes.ValueDiff`](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-go@v0.26.0/tftypes#ValueDiff):
```go
type ValueDiff struct {
Path *AttributePath // need to convert this to path.Path
Value1 *Value
Value2 *Value
}
```
We are later using these diffs to find [`AttributeChanges`](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/CLOUDP-308783_plan_modify_replication_specs_auto_scaling/internal/common/customplanmodifier/plan_modify_differ.go#L123) which we *query* to find changed attributes and based on logic we replace the Unknown value with the state value ([example](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/CLOUDP-308783_plan_modify_replication_specs_auto_scaling/internal/service/advancedclustertpf/plan_modifier.go#L121)).
For any operation on the plan we want to use the `tfsdk.Plan` methods that requires a `path.Path` (2nd argument):
- `func (p Plan) GetAttribute(ctx context.Context, path path.Path, target interface{})`
- `func (p *Plan) SetAttribute(ctx context.Context, path path.Path, val interface{}) diag.Diagnostics`
Contributor guide
Assessment
This issue has not been assessed yet.