2023-01-09 04:05:25 -06:00
|
|
|
package differ
|
|
|
|
|
|
|
|
import (
|
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-10 10:24:48 -06:00
|
|
|
if computed, ok := change.checkForUnknownBlock(block); ok {
|
2023-01-09 09:49:35 -06:00
|
|
|
return computed
|
|
|
|
}
|
|
|
|
|
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-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-10 10:24:48 -06:00
|
|
|
current = compareActions(current, childChange.Action)
|
2023-01-09 07:33:01 -06:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
blocks := make(map[string][]computed.Diff)
|
2023-01-09 07:33:01 -06:00
|
|
|
for key, blockType := range block.BlockTypes {
|
|
|
|
childValue := blockValue.getChild(key)
|
2023-01-10 10:24:48 -06:00
|
|
|
childChanges, next := childValue.computeDiffsForBlockType(blockType)
|
2023-01-09 07:33:01 -06:00
|
|
|
if next == plans.NoOp && childValue.Before == nil && childValue.After == nil {
|
|
|
|
// Don't record nil values at all in blocks.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
blocks[key] = childChanges
|
|
|
|
current = compareActions(current, next)
|
|
|
|
}
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
return computed.NewDiff(renderers.Block(attributes, blocks), current, change.replacePath())
|
2023-01-09 07:33:01 -06:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
func (change Change) computeDiffsForBlockType(blockType *jsonprovider.BlockType) ([]computed.Diff, plans.Action) {
|
2023-01-09 10:15:17 -06:00
|
|
|
switch NestingMode(blockType.NestingMode) {
|
|
|
|
case nestingModeSet:
|
2023-01-10 10:24:48 -06:00
|
|
|
return change.computeBlockDiffsAsSet(blockType.Block)
|
2023-01-09 10:15:17 -06:00
|
|
|
case nestingModeList:
|
2023-01-10 10:24:48 -06:00
|
|
|
return change.computeBlockDiffsAsList(blockType.Block)
|
2023-01-09 10:15:17 -06:00
|
|
|
case nestingModeMap:
|
2023-01-10 10:24:48 -06:00
|
|
|
return change.computeBlockDiffsAsMap(blockType.Block)
|
2023-01-09 10:15:17 -06:00
|
|
|
case nestingModeSingle, nestingModeGroup:
|
2023-01-10 10:24:48 -06:00
|
|
|
diff := change.ComputeDiffForBlock(blockType.Block)
|
|
|
|
return []computed.Diff{diff}, diff.Action
|
2023-01-09 07:33:01 -06:00
|
|
|
default:
|
|
|
|
panic("unrecognized nesting mode: " + blockType.NestingMode)
|
|
|
|
}
|
2023-01-09 04:05:25 -06:00
|
|
|
}
|