2023-01-09 05:41:24 -06:00
|
|
|
package differ
|
|
|
|
|
|
|
|
import (
|
2023-01-09 07:06:38 -06:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
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 05:41:24 -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 05:41:24 -06:00
|
|
|
)
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
func (change Change) computeAttributeDiffAsMap(elementType cty.Type) computed.Diff {
|
2023-01-11 02:35:36 -06:00
|
|
|
mapValue := change.asMap()
|
2023-04-14 02:56:32 -05:00
|
|
|
|
|
|
|
// The jsonplan package will have stripped out unknowns from our after value
|
|
|
|
// so we're going to add them back in here.
|
|
|
|
//
|
|
|
|
// This only affects attributes and not nested attributes or blocks, so we
|
|
|
|
// only perform this fix in this function and not the equivalent map
|
|
|
|
// functions for nested attributes and blocks.
|
|
|
|
|
|
|
|
// There is actually a difference between a null map and an empty map for
|
|
|
|
// purposes of calculating a delete, create, or update operation.
|
|
|
|
|
|
|
|
var after map[string]interface{}
|
|
|
|
if mapValue.After != nil {
|
|
|
|
after = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range mapValue.After {
|
|
|
|
after[key] = value
|
|
|
|
}
|
|
|
|
for key := range mapValue.Unknown {
|
|
|
|
if _, ok := after[key]; ok {
|
|
|
|
// Then this unknown value was in after, this probably means it has
|
|
|
|
// a child that is unknown rather than being unknown itself. As
|
|
|
|
// such, we'll skip over it. Note, it doesn't particularly matter if
|
|
|
|
// an element is in both places - it's just important we actually
|
|
|
|
// do cover all the elements. We want a complete union and therefore
|
|
|
|
// duplicates are no cause for concern as long as we dedupe here.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
after[key] = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
elements, current := collections.TransformMap(mapValue.Before, after, func(key string) computed.Diff {
|
2023-01-16 08:18:38 -06:00
|
|
|
value := mapValue.getChild(key)
|
|
|
|
if !value.RelevantAttributes.MatchesPartial() {
|
|
|
|
// Mark non-relevant attributes as unchanged.
|
|
|
|
value = value.AsNoOp()
|
|
|
|
}
|
2023-02-07 02:14:14 -06:00
|
|
|
return value.ComputeDiffForType(elementType)
|
2023-01-09 05:41:24 -06:00
|
|
|
})
|
2023-01-16 08:18:38 -06:00
|
|
|
return computed.NewDiff(renderers.Map(elements), current, change.ReplacePaths.Matches())
|
2023-01-09 05:41:24 -06:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
func (change Change) computeAttributeDiffAsNestedMap(attributes map[string]*jsonprovider.Attribute) computed.Diff {
|
2023-01-11 02:35:36 -06:00
|
|
|
mapValue := change.asMap()
|
|
|
|
elements, current := collections.TransformMap(mapValue.Before, mapValue.After, func(key string) computed.Diff {
|
2023-01-16 08:18:38 -06:00
|
|
|
value := mapValue.getChild(key)
|
|
|
|
if !value.RelevantAttributes.MatchesPartial() {
|
|
|
|
// Mark non-relevant attributes as unchanged.
|
|
|
|
value = value.AsNoOp()
|
|
|
|
}
|
|
|
|
return value.computeDiffForNestedAttribute(&jsonprovider.NestedType{
|
2023-01-09 09:49:35 -06:00
|
|
|
Attributes: attributes,
|
|
|
|
NestingMode: "single",
|
|
|
|
})
|
2023-01-09 05:41:24 -06:00
|
|
|
})
|
2023-01-16 08:18:38 -06:00
|
|
|
return computed.NewDiff(renderers.NestedMap(elements), current, change.ReplacePaths.Matches())
|
2023-01-09 05:41:24 -06:00
|
|
|
}
|
|
|
|
|
2023-01-11 02:04:26 -06:00
|
|
|
func (change Change) computeBlockDiffsAsMap(block *jsonprovider.Block) (map[string]computed.Diff, plans.Action) {
|
2023-01-10 10:24:48 -06:00
|
|
|
mapValue := change.asMap()
|
2023-01-11 02:35:36 -06:00
|
|
|
return collections.TransformMap(mapValue.Before, mapValue.After, func(key string) computed.Diff {
|
2023-01-16 08:18:38 -06:00
|
|
|
value := mapValue.getChild(key)
|
|
|
|
if !value.RelevantAttributes.MatchesPartial() {
|
|
|
|
// Mark non-relevant attributes as unchanged.
|
|
|
|
value = value.AsNoOp()
|
|
|
|
}
|
|
|
|
return value.ComputeDiffForBlock(block)
|
2023-01-11 02:35:36 -06:00
|
|
|
})
|
2023-01-09 05:41:24 -06:00
|
|
|
}
|