Skip to content

internal/fwserver: Log detected plan value differences before unknown marking #630

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

Merged
merged 3 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/630.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:note
New `DEBUG` level `Detected value change between proposed new state and prior state` log messages with the offending attribute path are now emitted when proposed new state value differences would cause the framework to automatically mark all unconfigured `Computed` attributes as unknown during planning. These can be used to troubleshoot potential resource implementation issues, or framework and Terraform plan logic bugs.
```

```release-note:enhancement
internal/fwserver: Added `DEBUG` logging to aid troubleshooting unexpected plans with unknown values
```
44 changes: 43 additions & 1 deletion internal/fwserver/server_planresourcechange.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
Expand Down Expand Up @@ -155,7 +156,48 @@ func (s *Server) PlanResourceChange(ctx context.Context, req *PlanResourceChange
// We only do this if there's a plan to modify; otherwise, it
// represents a resource being deleted and there's no point.
if !resp.PlannedState.Raw.IsNull() && !resp.PlannedState.Raw.Equal(req.PriorState.Raw) {
logging.FrameworkTrace(ctx, "Marking Computed null Config values as unknown in Plan")
// Loop through top level attributes/blocks to individually emit logs
// for value changes. This is helpful for troubleshooting unexpected
// plan outputs and only needs to be done for resource update plans.
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/627
if !req.PriorState.Raw.IsNull() {
var allPaths, changedPaths path.Paths

for attrName := range resp.PlannedState.Schema.GetAttributes() {
allPaths.Append(path.Root(attrName))
}

for blockName := range resp.PlannedState.Schema.GetBlocks() {
allPaths.Append(path.Root(blockName))
}

for _, p := range allPaths {
var plannedState, priorState attr.Value

// This logging is best effort and any errors should not be
// returned to practitioners.
_ = resp.PlannedState.GetAttribute(ctx, p, &plannedState)
_ = req.PriorState.GetAttribute(ctx, p, &priorState)

if plannedState.Equal(priorState) {
continue
}

changedPaths.Append(p)
}

// Colocate these log entries to not intermix with GetAttribute logging
for _, p := range changedPaths {
logging.FrameworkDebug(ctx,
"Detected value change between proposed new state and prior state",
map[string]any{
logging.KeyAttributePath: p.String(),
},
)
}
}

logging.FrameworkDebug(ctx, "Marking Computed attributes with null configuration values as unknown (known after apply) in the plan to prevent potential Terraform errors")

modifiedPlan, err := tftypes.Transform(resp.PlannedState.Raw, MarkComputedNilsAsUnknown(ctx, req.Config.Raw, req.ResourceSchema))

Expand Down