2023-01-09 04:05:25 -06:00
|
|
|
package differ
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonformat/change"
|
|
|
|
"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-09 07:33:01 -06:00
|
|
|
func (v Value) computeChangeForBlock(block *jsonprovider.Block) change.Change {
|
|
|
|
current := v.getDefaultActionForIteration()
|
|
|
|
|
|
|
|
blockValue := v.asMap()
|
|
|
|
|
|
|
|
attributes := make(map[string]change.Change)
|
|
|
|
for key, attr := range block.Attributes {
|
|
|
|
childValue := blockValue.getChild(key)
|
|
|
|
childChange := childValue.ComputeChange(attr)
|
|
|
|
if childChange.GetAction() == plans.NoOp && childValue.Before == nil && childValue.After == nil {
|
|
|
|
// Don't record nil values at all in blocks.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes[key] = childChange
|
|
|
|
current = compareActions(current, childChange.GetAction())
|
|
|
|
}
|
|
|
|
|
|
|
|
blocks := make(map[string][]change.Change)
|
|
|
|
for key, blockType := range block.BlockTypes {
|
|
|
|
childValue := blockValue.getChild(key)
|
|
|
|
childChanges, next := childValue.computeChangesForBlockType(blockType)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return change.New(change.Block(attributes, blocks), current, v.replacePath())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Value) computeChangesForBlockType(blockType *jsonprovider.BlockType) ([]change.Change, plans.Action) {
|
|
|
|
switch blockType.NestingMode {
|
|
|
|
case "set":
|
|
|
|
return v.computeBlockChangesAsSet(blockType.Block)
|
|
|
|
case "list":
|
|
|
|
return v.computeBlockChangesAsList(blockType.Block)
|
|
|
|
case "map":
|
|
|
|
return v.computeBlockChangesAsMap(blockType.Block)
|
|
|
|
case "single", "group":
|
|
|
|
ch := v.ComputeChange(blockType.Block)
|
|
|
|
return []change.Change{ch}, ch.GetAction()
|
|
|
|
default:
|
|
|
|
panic("unrecognized nesting mode: " + blockType.NestingMode)
|
|
|
|
}
|
2023-01-09 04:05:25 -06:00
|
|
|
}
|