2018-07-21 02:15:03 +02:00
|
|
|
package plans
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-05-17 21:00:50 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2018-07-21 02:15:03 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ResourceInstanceChangeSrc is a not-yet-decoded ResourceInstanceChange.
|
|
|
|
// Pass the associated resource type's schema type to method Decode to
|
2020-05-13 10:14:58 +02:00
|
|
|
// obtain a ResourceInstanceChange.
|
2018-07-21 02:15:03 +02:00
|
|
|
type ResourceInstanceChangeSrc struct {
|
|
|
|
// Addr is the absolute address of the resource instance that the change
|
|
|
|
// will apply to.
|
|
|
|
Addr addrs.AbsResourceInstance
|
|
|
|
|
|
|
|
// DeposedKey is the identifier for a deposed object associated with the
|
|
|
|
// given instance, or states.NotDeposed if this change applies to the
|
|
|
|
// current object.
|
|
|
|
//
|
|
|
|
// A Replace change for a resource with create_before_destroy set will
|
|
|
|
// create a new DeposedKey temporarily during replacement. In that case,
|
|
|
|
// DeposedKey in the plan is always states.NotDeposed, representing that
|
|
|
|
// the current object is being replaced with the deposed.
|
|
|
|
DeposedKey states.DeposedKey
|
|
|
|
|
|
|
|
// Provider is the address of the provider configuration that was used
|
|
|
|
// to plan this change, and thus the configuration that must also be
|
|
|
|
// used to apply it.
|
|
|
|
ProviderAddr addrs.AbsProviderConfig
|
|
|
|
|
|
|
|
// ChangeSrc is an embedded description of the not-yet-decoded change.
|
|
|
|
ChangeSrc
|
2018-08-18 02:22:18 +02:00
|
|
|
|
2021-04-28 21:02:34 +02:00
|
|
|
// ActionReason is an optional extra indication of why we chose the
|
|
|
|
// action recorded in Change.Action for this particular resource instance.
|
|
|
|
//
|
|
|
|
// This is an approximate mechanism only for the purpose of explaining the
|
|
|
|
// plan to end-users in the UI and is not to be used for any
|
|
|
|
// decision-making during the apply step; if apply behavior needs to vary
|
|
|
|
// depending on the "action reason" then the information for that decision
|
|
|
|
// must be recorded more precisely elsewhere for that purpose.
|
|
|
|
//
|
|
|
|
// See the field of the same name in ResourceInstanceChange for more
|
|
|
|
// details.
|
|
|
|
ActionReason ResourceInstanceChangeActionReason
|
|
|
|
|
2018-08-25 01:13:50 +02:00
|
|
|
// RequiredReplace is a set of paths that caused the change action to be
|
|
|
|
// Replace rather than Update. Always nil if the change action is not
|
|
|
|
// Replace.
|
2018-08-29 20:25:09 +02:00
|
|
|
RequiredReplace cty.PathSet
|
2018-08-25 01:13:50 +02:00
|
|
|
|
2018-08-18 02:22:18 +02:00
|
|
|
// Private allows a provider to stash any extra data that is opaque to
|
|
|
|
// Terraform that relates to this change. Terraform will save this
|
|
|
|
// byte-for-byte and return it to the provider in the apply call.
|
|
|
|
Private []byte
|
2018-07-21 02:15:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decode unmarshals the raw representation of the instance object being
|
|
|
|
// changed. Pass the implied type of the corresponding resource type schema
|
|
|
|
// for correct operation.
|
|
|
|
func (rcs *ResourceInstanceChangeSrc) Decode(ty cty.Type) (*ResourceInstanceChange, error) {
|
|
|
|
change, err := rcs.ChangeSrc.Decode(ty)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ResourceInstanceChange{
|
2018-08-25 01:13:50 +02:00
|
|
|
Addr: rcs.Addr,
|
|
|
|
DeposedKey: rcs.DeposedKey,
|
|
|
|
ProviderAddr: rcs.ProviderAddr,
|
|
|
|
Change: *change,
|
2021-04-28 21:02:34 +02:00
|
|
|
ActionReason: rcs.ActionReason,
|
2018-08-25 01:13:50 +02:00
|
|
|
RequiredReplace: rcs.RequiredReplace,
|
|
|
|
Private: rcs.Private,
|
2018-07-21 02:15:03 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-08-27 23:33:08 +02:00
|
|
|
// DeepCopy creates a copy of the receiver where any pointers to nested mutable
|
|
|
|
// values are also copied, thus ensuring that future mutations of the receiver
|
|
|
|
// will not affect the copy.
|
|
|
|
//
|
|
|
|
// Some types used within a resource change are immutable by convention even
|
|
|
|
// though the Go language allows them to be mutated, such as the types from
|
|
|
|
// the addrs package. These are _not_ copied by this method, under the
|
|
|
|
// assumption that callers will behave themselves.
|
|
|
|
func (rcs *ResourceInstanceChangeSrc) DeepCopy() *ResourceInstanceChangeSrc {
|
|
|
|
if rcs == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ret := *rcs
|
|
|
|
|
2018-08-29 20:25:09 +02:00
|
|
|
ret.RequiredReplace = cty.NewPathSet(ret.RequiredReplace.List()...)
|
2018-08-27 23:33:08 +02:00
|
|
|
|
|
|
|
if len(ret.Private) != 0 {
|
|
|
|
private := make([]byte, len(ret.Private))
|
|
|
|
copy(private, ret.Private)
|
|
|
|
ret.Private = private
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.ChangeSrc.Before = ret.ChangeSrc.Before.Copy()
|
|
|
|
ret.ChangeSrc.After = ret.ChangeSrc.After.Copy()
|
|
|
|
|
|
|
|
return &ret
|
|
|
|
}
|
|
|
|
|
2018-09-11 01:26:55 +02:00
|
|
|
// OutputChangeSrc describes a change to an output value.
|
2018-07-21 02:15:03 +02:00
|
|
|
type OutputChangeSrc struct {
|
2018-09-11 01:26:55 +02:00
|
|
|
// Addr is the absolute address of the output value that the change
|
|
|
|
// will apply to.
|
|
|
|
Addr addrs.AbsOutputValue
|
|
|
|
|
2018-07-21 02:15:03 +02:00
|
|
|
// ChangeSrc is an embedded description of the not-yet-decoded change.
|
|
|
|
//
|
|
|
|
// For output value changes, the type constraint for the DynamicValue
|
|
|
|
// instances is always cty.DynamicPseudoType.
|
|
|
|
ChangeSrc
|
|
|
|
|
|
|
|
// Sensitive, if true, indicates that either the old or new value in the
|
|
|
|
// change is sensitive and so a rendered version of the plan in the UI
|
|
|
|
// should elide the actual values while still indicating the action of the
|
|
|
|
// change.
|
|
|
|
Sensitive bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode unmarshals the raw representation of the output value being
|
|
|
|
// changed.
|
|
|
|
func (ocs *OutputChangeSrc) Decode() (*OutputChange, error) {
|
|
|
|
change, err := ocs.ChangeSrc.Decode(cty.DynamicPseudoType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &OutputChange{
|
2018-09-11 01:26:55 +02:00
|
|
|
Addr: ocs.Addr,
|
2018-07-21 02:15:03 +02:00
|
|
|
Change: *change,
|
|
|
|
Sensitive: ocs.Sensitive,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-09-11 01:26:55 +02:00
|
|
|
// DeepCopy creates a copy of the receiver where any pointers to nested mutable
|
|
|
|
// values are also copied, thus ensuring that future mutations of the receiver
|
|
|
|
// will not affect the copy.
|
|
|
|
//
|
|
|
|
// Some types used within a resource change are immutable by convention even
|
|
|
|
// though the Go language allows them to be mutated, such as the types from
|
|
|
|
// the addrs package. These are _not_ copied by this method, under the
|
|
|
|
// assumption that callers will behave themselves.
|
|
|
|
func (ocs *OutputChangeSrc) DeepCopy() *OutputChangeSrc {
|
|
|
|
if ocs == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ret := *ocs
|
|
|
|
|
|
|
|
ret.ChangeSrc.Before = ret.ChangeSrc.Before.Copy()
|
|
|
|
ret.ChangeSrc.After = ret.ChangeSrc.After.Copy()
|
|
|
|
|
|
|
|
return &ret
|
|
|
|
}
|
|
|
|
|
2018-07-21 02:15:03 +02:00
|
|
|
// ChangeSrc is a not-yet-decoded Change.
|
|
|
|
type ChangeSrc struct {
|
|
|
|
// Action defines what kind of change is being made.
|
|
|
|
Action Action
|
|
|
|
|
|
|
|
// Before and After correspond to the fields of the same name in Change,
|
|
|
|
// but have not yet been decoded from the serialized value used for
|
|
|
|
// storage.
|
|
|
|
Before, After DynamicValue
|
2020-08-07 17:59:06 +02:00
|
|
|
|
2020-09-11 17:15:44 +02:00
|
|
|
// BeforeValMarks and AfterValMarks are stored path+mark combinations
|
|
|
|
// that might be discovered when encoding a change. Marks are removed
|
|
|
|
// to enable encoding (marked values cannot be marshalled), and so storing
|
|
|
|
// the path+mark combinations allow us to re-mark the value later
|
|
|
|
// when, for example, displaying the diff to the UI.
|
2020-09-03 21:42:58 +02:00
|
|
|
BeforeValMarks, AfterValMarks []cty.PathValueMarks
|
2018-07-21 02:15:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decode unmarshals the raw representations of the before and after values
|
|
|
|
// to produce a Change object. Pass the type constraint that the result must
|
|
|
|
// conform to.
|
|
|
|
//
|
|
|
|
// Where a ChangeSrc is embedded in some other struct, it's generally better
|
|
|
|
// to call the corresponding Decode method of that struct rather than working
|
|
|
|
// directly with its embedded Change.
|
|
|
|
func (cs *ChangeSrc) Decode(ty cty.Type) (*Change, error) {
|
2018-12-20 19:06:53 +01:00
|
|
|
var err error
|
|
|
|
before := cty.NullVal(ty)
|
|
|
|
after := cty.NullVal(ty)
|
|
|
|
|
|
|
|
if len(cs.Before) > 0 {
|
|
|
|
before, err = cs.Before.Decode(ty)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error decoding 'before' value: %s", err)
|
|
|
|
}
|
2018-07-21 02:15:03 +02:00
|
|
|
}
|
2018-12-20 19:06:53 +01:00
|
|
|
if len(cs.After) > 0 {
|
|
|
|
after, err = cs.After.Decode(ty)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error decoding 'after' value: %s", err)
|
|
|
|
}
|
2018-07-21 02:15:03 +02:00
|
|
|
}
|
2021-05-11 23:37:07 +02:00
|
|
|
|
2018-07-21 02:15:03 +02:00
|
|
|
return &Change{
|
|
|
|
Action: cs.Action,
|
2021-05-11 23:37:07 +02:00
|
|
|
Before: before.MarkWithPaths(cs.BeforeValMarks),
|
|
|
|
After: after.MarkWithPaths(cs.AfterValMarks),
|
2018-07-21 02:15:03 +02:00
|
|
|
}, nil
|
|
|
|
}
|