2023-01-09 04:05:25 -06:00
|
|
|
package differ
|
|
|
|
|
|
|
|
import (
|
2023-01-09 04:24:01 -06:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
ctyjson "github.com/zclconf/go-cty/cty/json"
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonformat/computed"
|
|
|
|
|
2023-01-09 04:05:25 -06:00
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonprovider"
|
|
|
|
)
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
func (change Change) ComputeDiffForAttribute(attribute *jsonprovider.Attribute) computed.Diff {
|
2023-01-09 05:15:38 -06:00
|
|
|
if attribute.AttributeNestedType != nil {
|
2023-01-10 10:24:48 -06:00
|
|
|
return change.computeDiffForNestedAttribute(attribute.AttributeNestedType)
|
2023-01-09 04:40:47 -06:00
|
|
|
}
|
2023-02-07 02:14:14 -06:00
|
|
|
return change.ComputeDiffForType(unmarshalAttribute(attribute))
|
2023-01-09 05:15:38 -06:00
|
|
|
}
|
2023-01-09 04:40:47 -06:00
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
func (change Change) computeDiffForNestedAttribute(nested *jsonprovider.NestedType) computed.Diff {
|
|
|
|
if sensitive, ok := change.checkForSensitiveNestedAttribute(nested); ok {
|
2023-01-09 09:49:35 -06:00
|
|
|
return sensitive
|
|
|
|
}
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
if computed, ok := change.checkForUnknownNestedAttribute(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-01-10 10:24:48 -06:00
|
|
|
return change.computeAttributeDiffAsNestedObject(nested.Attributes)
|
2023-01-09 10:15:17 -06:00
|
|
|
case nestingModeMap:
|
2023-01-10 10:24:48 -06:00
|
|
|
return change.computeAttributeDiffAsNestedMap(nested.Attributes)
|
2023-01-09 10:15:17 -06:00
|
|
|
case nestingModeList:
|
2023-01-10 10:24:48 -06:00
|
|
|
return change.computeAttributeDiffAsNestedList(nested.Attributes)
|
2023-01-09 10:15:17 -06:00
|
|
|
case nestingModeSet:
|
2023-01-10 10:24:48 -06:00
|
|
|
return change.computeAttributeDiffAsNestedSet(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-02-07 02:14:14 -06:00
|
|
|
func (change Change) ComputeDiffForType(ctype cty.Type) computed.Diff {
|
2023-01-10 10:24:48 -06:00
|
|
|
if sensitive, ok := change.checkForSensitiveType(ctype); ok {
|
2023-01-09 09:49:35 -06:00
|
|
|
return sensitive
|
|
|
|
}
|
|
|
|
|
2023-01-10 10:24:48 -06:00
|
|
|
if computed, ok := change.checkForUnknownType(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-01-10 10:24:48 -06:00
|
|
|
return change.ComputeDiffForOutput()
|
2023-01-09 09:49:35 -06:00
|
|
|
case ctype.IsPrimitiveType():
|
2023-01-10 10:24:48 -06:00
|
|
|
return change.computeAttributeDiffAsPrimitive(ctype)
|
2023-01-09 09:49:35 -06:00
|
|
|
case ctype.IsObjectType():
|
2023-01-10 10:24:48 -06:00
|
|
|
return change.computeAttributeDiffAsObject(ctype.AttributeTypes())
|
2023-01-09 09:49:35 -06:00
|
|
|
case ctype.IsMapType():
|
2023-01-10 10:24:48 -06:00
|
|
|
return change.computeAttributeDiffAsMap(ctype.ElementType())
|
2023-01-09 09:49:35 -06:00
|
|
|
case ctype.IsListType():
|
2023-01-10 10:24:48 -06:00
|
|
|
return change.computeAttributeDiffAsList(ctype.ElementType())
|
2023-01-09 10:39:13 -06:00
|
|
|
case ctype.IsTupleType():
|
2023-01-10 10:24:48 -06:00
|
|
|
return change.computeAttributeDiffAsTuple(ctype.TupleElementTypes())
|
2023-01-09 09:49:35 -06:00
|
|
|
case ctype.IsSetType():
|
2023-01-10 10:24:48 -06:00
|
|
|
return change.computeAttributeDiffAsSet(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
|
|
|
}
|