github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/jsonformat/diff.go (about) 1 package jsonformat 2 3 import ( 4 "github.com/hashicorp/terraform/internal/command/jsonformat/computed" 5 "github.com/hashicorp/terraform/internal/command/jsonformat/differ" 6 "github.com/hashicorp/terraform/internal/command/jsonformat/differ/attribute_path" 7 "github.com/hashicorp/terraform/internal/command/jsonplan" 8 "github.com/hashicorp/terraform/internal/plans" 9 ) 10 11 func precomputeDiffs(plan Plan, mode plans.Mode) diffs { 12 diffs := diffs{ 13 outputs: make(map[string]computed.Diff), 14 } 15 16 for _, drift := range plan.ResourceDrift { 17 18 var relevantAttrs attribute_path.Matcher 19 if mode == plans.RefreshOnlyMode { 20 // For a refresh only plan, we show all the drift. 21 relevantAttrs = attribute_path.AlwaysMatcher() 22 } else { 23 matcher := attribute_path.Empty(true) 24 25 // Otherwise we only want to show the drift changes that are 26 // relevant. 27 for _, attr := range plan.RelevantAttributes { 28 if len(attr.Resource) == 0 || attr.Resource == drift.Address { 29 matcher = attribute_path.AppendSingle(matcher, attr.Attr) 30 } 31 } 32 33 if len(matcher.Paths) > 0 { 34 relevantAttrs = matcher 35 } 36 } 37 38 if relevantAttrs == nil { 39 // If we couldn't build a relevant attribute matcher, then we are 40 // not going to show anything for this drift. 41 continue 42 } 43 44 schema := plan.getSchema(drift) 45 diffs.drift = append(diffs.drift, diff{ 46 change: drift, 47 diff: differ.FromJsonChange(drift.Change, relevantAttrs).ComputeDiffForBlock(schema.Block), 48 }) 49 } 50 51 for _, change := range plan.ResourceChanges { 52 schema := plan.getSchema(change) 53 diffs.changes = append(diffs.changes, diff{ 54 change: change, 55 diff: differ.FromJsonChange(change.Change, attribute_path.AlwaysMatcher()).ComputeDiffForBlock(schema.Block), 56 }) 57 } 58 59 for key, output := range plan.OutputChanges { 60 diffs.outputs[key] = differ.FromJsonChange(output, attribute_path.AlwaysMatcher()).ComputeDiffForOutput() 61 } 62 63 return diffs 64 } 65 66 type diffs struct { 67 drift []diff 68 changes []diff 69 outputs map[string]computed.Diff 70 } 71 72 func (d diffs) Empty() bool { 73 for _, change := range d.changes { 74 if change.diff.Action != plans.NoOp || change.Moved() { 75 return false 76 } 77 } 78 79 for _, output := range d.outputs { 80 if output.Action != plans.NoOp { 81 return false 82 } 83 } 84 85 return true 86 } 87 88 type diff struct { 89 change jsonplan.ResourceChange 90 diff computed.Diff 91 } 92 93 func (d diff) Moved() bool { 94 return len(d.change.PreviousAddress) > 0 && d.change.PreviousAddress != d.change.Address 95 }