2023-01-09 04:05:25 -06:00
|
|
|
package differ
|
|
|
|
|
|
|
|
import (
|
2023-01-11 02:35:36 -06:00
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonformat/collections"
|
2023-01-10 10:24:48 -06:00
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonformat/computed"
|
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonformat/computed/renderers"
|
2023-01-09 04:05:25 -06:00
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonprovider"
|
2023-01-09 07:33:01 -06:00
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
2023-01-09 04:05:25 -06:00
|
|
|
)
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
func (change Change) ComputeDiffForBlock(block *jsonprovider.Block) computed.Diff {
|
|
|
|
if sensitive, ok := change.checkForSensitiveBlock(block); ok {
|
2023-01-09 09:49:35 -06:00
|
|
|
return sensitive
|
|
|
|
}
|
|
|
|
|
2023-01-11 02:04:26 -06:00
|
|
|
if unknown, ok := change.checkForUnknownBlock(block); ok {
|
|
|
|
return unknown
|
2023-01-09 09:49:35 -06:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
current := change.getDefaultActionForIteration()
|
2023-01-09 07:33:01 -06:00
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
blockValue := change.asMap()
|
2023-01-09 07:33:01 -06:00
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
attributes := make(map[string]computed.Diff)
|
2023-01-09 07:33:01 -06:00
|
|
|
for key, attr := range block.Attributes {
|
|
|
|
childValue := blockValue.getChild(key)
|
2023-01-12 10:59:07 -06:00
|
|
|
|
|
|
|
// Empty strings in blocks should be considered null for legacy reasons.
|
|
|
|
// The SDK doesn't support null strings yet, so we work around this now.
|
|
|
|
if before, ok := childValue.Before.(string); ok && len(before) == 0 {
|
|
|
|
childValue.Before = nil
|
|
|
|
}
|
|
|
|
if after, ok := childValue.After.(string); ok && len(after) == 0 {
|
|
|
|
childValue.After = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always treat changes to blocks as implicit.
|
|
|
|
childValue.BeforeExplicit = false
|
|
|
|
childValue.AfterExplicit = false
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
childChange := childValue.ComputeDiffForAttribute(attr)
|
|
|
|
if childChange.Action == plans.NoOp && childValue.Before == nil && childValue.After == nil {
|
2023-01-09 07:33:01 -06:00
|
|
|
// Don't record nil values at all in blocks.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes[key] = childChange
|
2023-01-11 02:35:36 -06:00
|
|
|
current = collections.CompareActions(current, childChange.Action)
|
2023-01-09 07:33:01 -06:00
|
|
|
}
|
|
|
|
|
2023-01-11 02:04:26 -06:00
|
|
|
blocks := renderers.Blocks{
|
2023-01-12 10:59:07 -06:00
|
|
|
ReplaceBlocks: make(map[string]bool),
|
|
|
|
BeforeSensitiveBlocks: make(map[string]bool),
|
|
|
|
AfterSensitiveBlocks: make(map[string]bool),
|
|
|
|
SingleBlocks: make(map[string]computed.Diff),
|
|
|
|
ListBlocks: make(map[string][]computed.Diff),
|
|
|
|
SetBlocks: make(map[string][]computed.Diff),
|
|
|
|
MapBlocks: make(map[string]map[string]computed.Diff),
|
2023-01-11 02:04:26 -06:00
|
|
|
}
|
|
|
|
|
2023-01-09 07:33:01 -06:00
|
|
|
for key, blockType := range block.BlockTypes {
|
|
|
|
childValue := blockValue.getChild(key)
|
2023-01-11 02:04:26 -06:00
|
|
|
|
2023-01-12 10:59:07 -06:00
|
|
|
beforeSensitive := childValue.isBeforeSensitive()
|
|
|
|
afterSensitive := childValue.isAfterSensitive()
|
|
|
|
forcesReplacement := childValue.ReplacePaths.ForcesReplacement()
|
|
|
|
|
2023-01-11 02:04:26 -06:00
|
|
|
switch NestingMode(blockType.NestingMode) {
|
|
|
|
case nestingModeSet:
|
|
|
|
diffs, action := childValue.computeBlockDiffsAsSet(blockType.Block)
|
2023-01-12 10:59:07 -06:00
|
|
|
if action == plans.NoOp && childValue.Before == nil && childValue.After == nil {
|
2023-01-11 02:04:26 -06:00
|
|
|
// Don't record nil values in blocks.
|
|
|
|
continue
|
|
|
|
}
|
2023-01-12 10:59:07 -06:00
|
|
|
blocks.AddAllSetBlock(key, diffs, forcesReplacement, beforeSensitive, afterSensitive)
|
2023-01-11 02:35:36 -06:00
|
|
|
current = collections.CompareActions(current, action)
|
2023-01-11 02:04:26 -06:00
|
|
|
case nestingModeList:
|
|
|
|
diffs, action := childValue.computeBlockDiffsAsList(blockType.Block)
|
2023-01-12 10:59:07 -06:00
|
|
|
if action == plans.NoOp && childValue.Before == nil && childValue.After == nil {
|
2023-01-11 02:04:26 -06:00
|
|
|
// Don't record nil values in blocks.
|
|
|
|
continue
|
|
|
|
}
|
2023-01-12 10:59:07 -06:00
|
|
|
blocks.AddAllListBlock(key, diffs, forcesReplacement, beforeSensitive, afterSensitive)
|
2023-01-11 02:35:36 -06:00
|
|
|
current = collections.CompareActions(current, action)
|
2023-01-11 02:04:26 -06:00
|
|
|
case nestingModeMap:
|
|
|
|
diffs, action := childValue.computeBlockDiffsAsMap(blockType.Block)
|
2023-01-12 10:59:07 -06:00
|
|
|
if action == plans.NoOp && childValue.Before == nil && childValue.After == nil {
|
2023-01-11 02:04:26 -06:00
|
|
|
// Don't record nil values in blocks.
|
|
|
|
continue
|
|
|
|
}
|
2023-01-12 10:59:07 -06:00
|
|
|
blocks.AddAllMapBlocks(key, diffs, forcesReplacement, beforeSensitive, afterSensitive)
|
2023-01-11 02:35:36 -06:00
|
|
|
current = collections.CompareActions(current, action)
|
2023-01-11 02:04:26 -06:00
|
|
|
case nestingModeSingle, nestingModeGroup:
|
|
|
|
diff := childValue.ComputeDiffForBlock(blockType.Block)
|
2023-01-12 10:59:07 -06:00
|
|
|
if diff.Action == plans.NoOp && childValue.Before == nil && childValue.After == nil {
|
2023-01-11 02:04:26 -06:00
|
|
|
// Don't record nil values in blocks.
|
|
|
|
continue
|
|
|
|
}
|
2023-01-12 10:59:07 -06:00
|
|
|
blocks.AddSingleBlock(key, diff, forcesReplacement, beforeSensitive, afterSensitive)
|
2023-01-11 02:35:36 -06:00
|
|
|
current = collections.CompareActions(current, diff.Action)
|
2023-01-11 02:04:26 -06:00
|
|
|
default:
|
|
|
|
panic("unrecognized nesting mode: " + blockType.NestingMode)
|
2023-01-09 07:33:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 03:20:24 -06:00
|
|
|
return computed.NewDiff(renderers.Block(attributes, blocks), current, change.ReplacePaths.ForcesReplacement())
|
2023-01-09 07:33:01 -06:00
|
|
|
}
|