2023-05-02 11:33:06 -04:00
|
|
|
// Copyright IBM Corp. 2014, 2026
|
2023-08-10 18:43:27 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-05-02 11:33:06 -04:00
|
|
|
|
2023-01-09 06:41:24 -05:00
|
|
|
package differ
|
|
|
|
|
|
|
|
|
|
import (
|
2023-01-09 08:06:38 -05:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
2023-01-11 03:35:36 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonformat/collections"
|
2023-01-10 11:24:48 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonformat/computed"
|
|
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonformat/computed/renderers"
|
2023-04-21 03:51:55 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonformat/structured"
|
2023-01-09 06:41:24 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonprovider"
|
2023-01-09 08:33:01 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
2023-01-09 06:41:24 -05:00
|
|
|
)
|
|
|
|
|
|
2023-04-21 03:51:55 -04:00
|
|
|
func computeAttributeDiffAsMap(change structured.Change, elementType cty.Type) computed.Diff {
|
|
|
|
|
mapValue := change.AsMap()
|
2023-04-24 04:28:21 -04:00
|
|
|
elements, current := collections.TransformMap(mapValue.Before, mapValue.After, mapValue.AllKeys(), func(key string) computed.Diff {
|
2023-04-21 03:51:55 -04:00
|
|
|
value := mapValue.GetChild(key)
|
2023-01-16 09:18:38 -05:00
|
|
|
if !value.RelevantAttributes.MatchesPartial() {
|
|
|
|
|
// Mark non-relevant attributes as unchanged.
|
|
|
|
|
value = value.AsNoOp()
|
|
|
|
|
}
|
2023-04-21 03:51:55 -04:00
|
|
|
return ComputeDiffForType(value, elementType)
|
2023-01-09 06:41:24 -05:00
|
|
|
})
|
2023-01-16 09:18:38 -05:00
|
|
|
return computed.NewDiff(renderers.Map(elements), current, change.ReplacePaths.Matches())
|
2023-01-09 06:41:24 -05:00
|
|
|
}
|
|
|
|
|
|
2023-04-21 03:51:55 -04:00
|
|
|
func computeAttributeDiffAsNestedMap(change structured.Change, attributes map[string]*jsonprovider.Attribute) computed.Diff {
|
|
|
|
|
mapValue := change.AsMap()
|
2023-04-24 04:28:21 -04:00
|
|
|
elements, current := collections.TransformMap(mapValue.Before, mapValue.After, mapValue.ExplicitKeys(), func(key string) computed.Diff {
|
2023-04-21 03:51:55 -04:00
|
|
|
value := mapValue.GetChild(key)
|
2023-01-16 09:18:38 -05:00
|
|
|
if !value.RelevantAttributes.MatchesPartial() {
|
|
|
|
|
// Mark non-relevant attributes as unchanged.
|
|
|
|
|
value = value.AsNoOp()
|
|
|
|
|
}
|
2023-04-21 03:51:55 -04:00
|
|
|
return computeDiffForNestedAttribute(value, &jsonprovider.NestedType{
|
2023-01-09 10:49:35 -05:00
|
|
|
Attributes: attributes,
|
|
|
|
|
NestingMode: "single",
|
|
|
|
|
})
|
2023-01-09 06:41:24 -05:00
|
|
|
})
|
2023-01-16 09:18:38 -05:00
|
|
|
return computed.NewDiff(renderers.NestedMap(elements), current, change.ReplacePaths.Matches())
|
2023-01-09 06:41:24 -05:00
|
|
|
}
|
|
|
|
|
|
2023-04-21 03:51:55 -04:00
|
|
|
func computeBlockDiffsAsMap(change structured.Change, block *jsonprovider.Block) (map[string]computed.Diff, plans.Action) {
|
|
|
|
|
mapValue := change.AsMap()
|
2023-04-24 04:28:21 -04:00
|
|
|
return collections.TransformMap(mapValue.Before, mapValue.After, mapValue.ExplicitKeys(), func(key string) computed.Diff {
|
2023-04-21 03:51:55 -04:00
|
|
|
value := mapValue.GetChild(key)
|
2023-01-16 09:18:38 -05:00
|
|
|
if !value.RelevantAttributes.MatchesPartial() {
|
|
|
|
|
// Mark non-relevant attributes as unchanged.
|
|
|
|
|
value = value.AsNoOp()
|
|
|
|
|
}
|
2023-04-21 03:51:55 -04:00
|
|
|
return ComputeDiffForBlock(value, block)
|
2023-01-11 03:35:36 -05:00
|
|
|
})
|
2023-01-09 06:41:24 -05:00
|
|
|
}
|