2024-02-08 03:48:59 -06:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 10:33:06 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-01-09 04:05:25 -06:00
|
|
|
package differ
|
|
|
|
|
|
|
|
import (
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/command/jsonformat/structured"
|
2023-01-09 04:24:01 -06:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
ctyjson "github.com/zclconf/go-cty/cty/json"
|
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/command/jsonformat/computed"
|
2023-01-10 10:24:48 -06:00
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/command/jsonprovider"
|
2023-01-09 04:05:25 -06:00
|
|
|
)
|
|
|
|
|
2023-04-21 02:51:55 -05:00
|
|
|
func ComputeDiffForAttribute(change structured.Change, attribute *jsonprovider.Attribute) computed.Diff {
|
2023-01-09 05:15:38 -06:00
|
|
|
if attribute.AttributeNestedType != nil {
|
2023-04-21 02:51:55 -05:00
|
|
|
return computeDiffForNestedAttribute(change, attribute.AttributeNestedType)
|
2023-01-09 04:40:47 -06:00
|
|
|
}
|
2023-04-21 02:51:55 -05:00
|
|
|
return ComputeDiffForType(change, unmarshalAttribute(attribute))
|
2023-01-09 05:15:38 -06:00
|
|
|
}
|
2023-01-09 04:40:47 -06:00
|
|
|
|
2023-04-21 02:51:55 -05:00
|
|
|
func computeDiffForNestedAttribute(change structured.Change, nested *jsonprovider.NestedType) computed.Diff {
|
|
|
|
if sensitive, ok := checkForSensitiveNestedAttribute(change, nested); ok {
|
2023-01-09 09:49:35 -06:00
|
|
|
return sensitive
|
|
|
|
}
|
|
|
|
|
2023-04-21 02:51:55 -05:00
|
|
|
if computed, ok := checkForUnknownNestedAttribute(change, nested); ok {
|
2023-01-09 09:49:35 -06:00
|
|
|
return computed
|
|
|
|
}
|
|
|
|
|
2023-01-09 10:15:17 -06:00
|
|
|
switch NestingMode(nested.NestingMode) {
|
|
|
|
case nestingModeSingle, nestingModeGroup:
|
2023-04-21 02:51:55 -05:00
|
|
|
return computeAttributeDiffAsNestedObject(change, nested.Attributes)
|
2023-01-09 10:15:17 -06:00
|
|
|
case nestingModeMap:
|
2023-04-21 02:51:55 -05:00
|
|
|
return computeAttributeDiffAsNestedMap(change, nested.Attributes)
|
2023-01-09 10:15:17 -06:00
|
|
|
case nestingModeList:
|
2023-04-21 02:51:55 -05:00
|
|
|
return computeAttributeDiffAsNestedList(change, nested.Attributes)
|
2023-01-09 10:15:17 -06:00
|
|
|
case nestingModeSet:
|
2023-04-21 02:51:55 -05:00
|
|
|
return computeAttributeDiffAsNestedSet(change, nested.Attributes)
|
2023-01-09 05:15:38 -06:00
|
|
|
default:
|
2023-01-09 09:49:35 -06:00
|
|
|
panic("unrecognized nesting mode: " + nested.NestingMode)
|
2023-01-09 04:55:55 -06:00
|
|
|
}
|
2023-01-09 05:15:38 -06:00
|
|
|
}
|
2023-01-09 04:55:55 -06:00
|
|
|
|
2023-04-21 02:51:55 -05:00
|
|
|
func ComputeDiffForType(change structured.Change, ctype cty.Type) computed.Diff {
|
|
|
|
if sensitive, ok := checkForSensitiveType(change, ctype); ok {
|
2023-01-09 09:49:35 -06:00
|
|
|
return sensitive
|
|
|
|
}
|
|
|
|
|
2023-04-21 02:51:55 -05:00
|
|
|
if computed, ok := checkForUnknownType(change, ctype); ok {
|
2023-01-09 09:49:35 -06:00
|
|
|
return computed
|
2023-01-09 07:45:35 -06:00
|
|
|
}
|
|
|
|
|
2023-01-09 04:24:01 -06:00
|
|
|
switch {
|
2023-01-09 09:49:35 -06:00
|
|
|
case ctype == cty.NilType, ctype == cty.DynamicPseudoType:
|
2023-01-09 13:08:08 -06:00
|
|
|
// Forward nil or dynamic types over to be processed as outputs.
|
|
|
|
// There is nothing particularly special about the way outputs are
|
|
|
|
// processed that make this unsafe, we could just as easily call this
|
|
|
|
// function computeChangeForDynamicValues(), but external callers will
|
|
|
|
// only be in this situation when processing outputs so this function
|
|
|
|
// is named for their benefit.
|
2023-04-21 02:51:55 -05:00
|
|
|
return ComputeDiffForOutput(change)
|
2023-01-09 09:49:35 -06:00
|
|
|
case ctype.IsPrimitiveType():
|
2023-04-21 02:51:55 -05:00
|
|
|
return computeAttributeDiffAsPrimitive(change, ctype)
|
2023-01-09 09:49:35 -06:00
|
|
|
case ctype.IsObjectType():
|
2023-04-21 02:51:55 -05:00
|
|
|
return computeAttributeDiffAsObject(change, ctype.AttributeTypes())
|
2023-01-09 09:49:35 -06:00
|
|
|
case ctype.IsMapType():
|
2023-04-21 02:51:55 -05:00
|
|
|
return computeAttributeDiffAsMap(change, ctype.ElementType())
|
2023-01-09 09:49:35 -06:00
|
|
|
case ctype.IsListType():
|
2023-04-21 02:51:55 -05:00
|
|
|
return computeAttributeDiffAsList(change, ctype.ElementType())
|
2023-01-09 10:39:13 -06:00
|
|
|
case ctype.IsTupleType():
|
2023-04-21 02:51:55 -05:00
|
|
|
return computeAttributeDiffAsTuple(change, ctype.TupleElementTypes())
|
2023-01-09 09:49:35 -06:00
|
|
|
case ctype.IsSetType():
|
2023-04-21 02:51:55 -05:00
|
|
|
return computeAttributeDiffAsSet(change, ctype.ElementType())
|
2023-01-09 04:24:01 -06:00
|
|
|
default:
|
2023-01-09 09:49:35 -06:00
|
|
|
panic("unrecognized type: " + ctype.FriendlyName())
|
2023-01-09 04:24:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalAttribute(attribute *jsonprovider.Attribute) cty.Type {
|
|
|
|
ctyType, err := ctyjson.UnmarshalType(attribute.AttributeType)
|
|
|
|
if err != nil {
|
|
|
|
panic("could not unmarshal attribute type: " + err.Error())
|
|
|
|
}
|
|
|
|
return ctyType
|
2023-01-09 04:05:25 -06:00
|
|
|
}
|