Structured Plan Renderer: Remove attributes that do not match the relevant attributes filter (#32509)

* remove attributes that do not match the relevant attributes filter

* fix formatting

* fix renderer function, don't drop irrelevant attributes just mark them as no-ops

* fix imports
This commit is contained in:
Liam Cervante
2023-01-16 15:18:38 +01:00
committed by GitHub
parent 4fd8322802
commit e015b15f12
22 changed files with 1248 additions and 246 deletions

View File

@@ -5,20 +5,48 @@ import (
"github.com/hashicorp/terraform/internal/command/jsonformat/computed"
"github.com/hashicorp/terraform/internal/command/jsonformat/differ"
"github.com/hashicorp/terraform/internal/command/jsonformat/differ/attribute_path"
"github.com/hashicorp/terraform/internal/command/jsonplan"
"github.com/hashicorp/terraform/internal/plans"
)
func precomputeDiffs(plan Plan) diffs {
func precomputeDiffs(plan Plan, mode plans.Mode) diffs {
diffs := diffs{
outputs: make(map[string]computed.Diff),
}
for _, drift := range plan.ResourceDrift {
var relevantAttrs attribute_path.Matcher
if mode == plans.RefreshOnlyMode {
// For a refresh only plan, we show all the drift.
relevantAttrs = attribute_path.AlwaysMatcher()
} else {
matcher := attribute_path.Empty(true)
// Otherwise we only want to show the drift changes that are
// relevant.
for _, attr := range plan.RelevantAttributes {
if len(attr.Resource) == 0 || attr.Resource == drift.Address {
matcher = attribute_path.AppendSingle(matcher, attr.Attr)
}
}
if len(matcher.Paths) > 0 {
relevantAttrs = matcher
}
}
if relevantAttrs == nil {
// If we couldn't build a relevant attribute matcher, then we are
// not going to show anything for this drift.
continue
}
schema := plan.ProviderSchemas[drift.ProviderName].ResourceSchemas[drift.Type]
diffs.drift = append(diffs.drift, diff{
change: drift,
diff: differ.FromJsonChange(drift.Change).ComputeDiffForBlock(schema.Block),
diff: differ.FromJsonChange(drift.Change, relevantAttrs).ComputeDiffForBlock(schema.Block),
})
}
@@ -26,12 +54,12 @@ func precomputeDiffs(plan Plan) diffs {
schema := plan.ProviderSchemas[change.ProviderName].ResourceSchemas[change.Type]
diffs.changes = append(diffs.changes, diff{
change: change,
diff: differ.FromJsonChange(change.Change).ComputeDiffForBlock(schema.Block),
diff: differ.FromJsonChange(change.Change, attribute_path.AlwaysMatcher()).ComputeDiffForBlock(schema.Block),
})
}
for key, output := range plan.OutputChanges {
diffs.outputs[key] = differ.FromJsonChange(output).ComputeDiffForOutput()
diffs.outputs[key] = differ.FromJsonChange(output, attribute_path.AlwaysMatcher()).ComputeDiffForOutput()
}
less := func(drs []diff) func(i, j int) bool {