2024-02-08 04:48:59 -05:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 11:33:06 -04:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2023-01-09 05:05:25 -05:00
|
|
|
package differ
|
|
|
|
|
|
|
|
|
|
import (
|
2025-08-25 06:57:11 -04:00
|
|
|
"github.com/opentofu/opentofu/internal/command/jsonformat/computed/renderers"
|
2023-09-20 07:35:35 -04:00
|
|
|
"github.com/opentofu/opentofu/internal/command/jsonformat/structured"
|
2025-08-25 06:57:11 -04:00
|
|
|
"github.com/opentofu/opentofu/internal/plans"
|
2023-01-09 05:24:01 -05:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
ctyjson "github.com/zclconf/go-cty/cty/json"
|
|
|
|
|
|
2023-09-20 07:35:35 -04:00
|
|
|
"github.com/opentofu/opentofu/internal/command/jsonformat/computed"
|
2023-01-10 11:24:48 -05:00
|
|
|
|
2023-09-20 07:35:35 -04:00
|
|
|
"github.com/opentofu/opentofu/internal/command/jsonprovider"
|
2023-01-09 05:05:25 -05:00
|
|
|
)
|
|
|
|
|
|
2025-08-25 06:57:11 -04:00
|
|
|
// ComputeDiffForAttribute generates the diff for the change.
|
|
|
|
|
// It handles 3 specific cases:
|
|
|
|
|
// - When the attribute for which the change is generated is a nested object,
|
|
|
|
|
// it generates the diff for each attribute
|
|
|
|
|
// of the nested object.
|
|
|
|
|
// - If the attribute is write-only, due to the fact that its changes will always be null, we want
|
|
|
|
|
// to return a diff with the same action as the parent's.
|
|
|
|
|
// If we use change.CalculateAction(), then the action will always be NoOp because of the
|
|
|
|
|
// which will skip from showing this in the diff.
|
|
|
|
|
// - If none above, it tries to generate the diff by using the specific generator for the attr type.
|
|
|
|
|
func ComputeDiffForAttribute(change structured.Change, attribute *jsonprovider.Attribute, parentAction plans.Action) computed.Diff {
|
|
|
|
|
if attribute.WriteOnly {
|
|
|
|
|
return computeAttributeDiffAsWriteOnly(change, parentAction)
|
|
|
|
|
}
|
2023-01-09 06:15:38 -05:00
|
|
|
if attribute.AttributeNestedType != nil {
|
2023-04-21 03:51:55 -04:00
|
|
|
return computeDiffForNestedAttribute(change, attribute.AttributeNestedType)
|
2023-01-09 05:40:47 -05:00
|
|
|
}
|
2023-04-21 03:51:55 -04:00
|
|
|
return ComputeDiffForType(change, unmarshalAttribute(attribute))
|
2023-01-09 06:15:38 -05:00
|
|
|
}
|
2023-01-09 05:40:47 -05:00
|
|
|
|
2023-04-21 03:51:55 -04:00
|
|
|
func computeDiffForNestedAttribute(change structured.Change, nested *jsonprovider.NestedType) computed.Diff {
|
|
|
|
|
if sensitive, ok := checkForSensitiveNestedAttribute(change, nested); ok {
|
2023-01-09 10:49:35 -05:00
|
|
|
return sensitive
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 03:51:55 -04:00
|
|
|
if computed, ok := checkForUnknownNestedAttribute(change, nested); ok {
|
2023-01-09 10:49:35 -05:00
|
|
|
return computed
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 11:15:17 -05:00
|
|
|
switch NestingMode(nested.NestingMode) {
|
|
|
|
|
case nestingModeSingle, nestingModeGroup:
|
2023-04-21 03:51:55 -04:00
|
|
|
return computeAttributeDiffAsNestedObject(change, nested.Attributes)
|
2023-01-09 11:15:17 -05:00
|
|
|
case nestingModeMap:
|
2023-04-21 03:51:55 -04:00
|
|
|
return computeAttributeDiffAsNestedMap(change, nested.Attributes)
|
2023-01-09 11:15:17 -05:00
|
|
|
case nestingModeList:
|
2023-04-21 03:51:55 -04:00
|
|
|
return computeAttributeDiffAsNestedList(change, nested.Attributes)
|
2023-01-09 11:15:17 -05:00
|
|
|
case nestingModeSet:
|
2023-04-21 03:51:55 -04:00
|
|
|
return computeAttributeDiffAsNestedSet(change, nested.Attributes)
|
2023-01-09 06:15:38 -05:00
|
|
|
default:
|
2023-01-09 10:49:35 -05:00
|
|
|
panic("unrecognized nesting mode: " + nested.NestingMode)
|
2023-01-09 05:55:55 -05:00
|
|
|
}
|
2023-01-09 06:15:38 -05:00
|
|
|
}
|
2023-01-09 05:55:55 -05:00
|
|
|
|
2023-04-21 03:51:55 -04:00
|
|
|
func ComputeDiffForType(change structured.Change, ctype cty.Type) computed.Diff {
|
|
|
|
|
if sensitive, ok := checkForSensitiveType(change, ctype); ok {
|
2023-01-09 10:49:35 -05:00
|
|
|
return sensitive
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 03:51:55 -04:00
|
|
|
if computed, ok := checkForUnknownType(change, ctype); ok {
|
2023-01-09 10:49:35 -05:00
|
|
|
return computed
|
2023-01-09 08:45:35 -05:00
|
|
|
}
|
|
|
|
|
|
2023-01-09 05:24:01 -05:00
|
|
|
switch {
|
2023-01-09 10:49:35 -05:00
|
|
|
case ctype == cty.NilType, ctype == cty.DynamicPseudoType:
|
2023-01-09 14:08:08 -05: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 03:51:55 -04:00
|
|
|
return ComputeDiffForOutput(change)
|
2023-01-09 10:49:35 -05:00
|
|
|
case ctype.IsPrimitiveType():
|
2023-04-21 03:51:55 -04:00
|
|
|
return computeAttributeDiffAsPrimitive(change, ctype)
|
2023-01-09 10:49:35 -05:00
|
|
|
case ctype.IsObjectType():
|
2023-04-21 03:51:55 -04:00
|
|
|
return computeAttributeDiffAsObject(change, ctype.AttributeTypes())
|
2023-01-09 10:49:35 -05:00
|
|
|
case ctype.IsMapType():
|
2023-04-21 03:51:55 -04:00
|
|
|
return computeAttributeDiffAsMap(change, ctype.ElementType())
|
2023-01-09 10:49:35 -05:00
|
|
|
case ctype.IsListType():
|
2023-04-21 03:51:55 -04:00
|
|
|
return computeAttributeDiffAsList(change, ctype.ElementType())
|
2023-01-09 11:39:13 -05:00
|
|
|
case ctype.IsTupleType():
|
2023-04-21 03:51:55 -04:00
|
|
|
return computeAttributeDiffAsTuple(change, ctype.TupleElementTypes())
|
2023-01-09 10:49:35 -05:00
|
|
|
case ctype.IsSetType():
|
2023-04-21 03:51:55 -04:00
|
|
|
return computeAttributeDiffAsSet(change, ctype.ElementType())
|
2023-01-09 05:24:01 -05:00
|
|
|
default:
|
2023-01-09 10:49:35 -05:00
|
|
|
panic("unrecognized type: " + ctype.FriendlyName())
|
2023-01-09 05:24:01 -05: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 05:05:25 -05:00
|
|
|
}
|
2025-08-25 06:57:11 -04:00
|
|
|
|
|
|
|
|
func computeAttributeDiffAsWriteOnly(change structured.Change, parentAction plans.Action) computed.Diff {
|
2025-09-22 10:15:20 -04:00
|
|
|
// If the provider returned this write-only attribute in the require_replace list,
|
|
|
|
|
// we want to be sure that the action is not plans.NoOp.
|
|
|
|
|
// plans.Update might be the wrong one
|
|
|
|
|
// We want to do this to be sure that the user is informed about the fields that forces
|
|
|
|
|
// the replacement of the resource.
|
|
|
|
|
if parentAction == plans.NoOp && change.ReplacePaths.Matches() {
|
|
|
|
|
parentAction = plans.Update
|
|
|
|
|
}
|
2025-08-25 06:57:11 -04:00
|
|
|
return asDiffWithInheritedAction(change, parentAction, renderers.WriteOnly())
|
|
|
|
|
}
|