2018-06-20 01:03:59 +02:00
|
|
|
package plans
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
"github.com/hashicorp/terraform/states"
|
2018-07-21 02:15:03 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2018-06-20 01:03:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Changes describes various actions that Terraform will attempt to take if
|
|
|
|
// the corresponding plan is applied.
|
|
|
|
//
|
|
|
|
// A Changes object can be rendered into a visual diff (by the caller, using
|
|
|
|
// code in another package) for display to the user.
|
|
|
|
type Changes struct {
|
2018-07-21 02:15:03 +02:00
|
|
|
Resources []*ResourceInstanceChangeSrc
|
|
|
|
RootOutputs map[string]*OutputChangeSrc
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewChanges returns a valid Changes object that describes no changes.
|
|
|
|
func NewChanges() *Changes {
|
|
|
|
return &Changes{
|
|
|
|
RootOutputs: make(map[string]*OutputChangeSrc),
|
|
|
|
}
|
2018-06-20 01:03:59 +02:00
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
func (c *Changes) Empty() bool {
|
|
|
|
return (len(c.Resources) + len(c.RootOutputs)) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceInstance returns the planned change for the current object of the
|
|
|
|
// resource instance of the given address, if any. Returns nil if no change is
|
|
|
|
// planned.
|
|
|
|
func (c *Changes) ResourceInstance(addr addrs.AbsResourceInstance) *ResourceInstanceChangeSrc {
|
|
|
|
addrStr := addr.String()
|
|
|
|
for _, rc := range c.Resources {
|
|
|
|
if rc.Addr.String() == addrStr && rc.DeposedKey == states.NotDeposed {
|
|
|
|
return rc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceInstanceDeposed returns the plan change of a deposed object of
|
|
|
|
// the resource instance of the given address, if any. Returns nil if no change
|
|
|
|
// is planned.
|
|
|
|
func (c *Changes) ResourceInstanceDeposed(addr addrs.AbsResourceInstance, key states.DeposedKey) *ResourceInstanceChangeSrc {
|
|
|
|
addrStr := addr.String()
|
|
|
|
for _, rc := range c.Resources {
|
|
|
|
if rc.Addr.String() == addrStr && rc.DeposedKey == key {
|
|
|
|
return rc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-27 23:33:08 +02:00
|
|
|
// SyncWrapper returns a wrapper object around the receiver that can be used
|
|
|
|
// to make certain changes to the receiver in a concurrency-safe way, as long
|
|
|
|
// as all callers share the same wrapper object.
|
|
|
|
func (c *Changes) SyncWrapper() *ChangesSync {
|
|
|
|
return &ChangesSync{
|
|
|
|
changes: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 01:03:59 +02:00
|
|
|
// ResourceInstanceChange describes a change to a particular resource instance
|
|
|
|
// object.
|
|
|
|
type ResourceInstanceChange 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
|
|
|
|
|
2018-06-22 00:26:37 +02:00
|
|
|
// 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
|
|
|
|
|
2018-06-20 01:03:59 +02:00
|
|
|
// Change is an embedded description of the change.
|
|
|
|
Change
|
2018-08-18 02:22:18 +02:00
|
|
|
|
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.
|
|
|
|
//
|
|
|
|
// This is retained only for UI-plan-rendering purposes and so it does not
|
|
|
|
// currently survive a round-trip through a saved plan file.
|
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-06-20 01:03:59 +02:00
|
|
|
}
|
|
|
|
|
2018-07-21 02:15:03 +02:00
|
|
|
// Encode produces a variant of the reciever that has its change values
|
|
|
|
// serialized so it can be written to a plan file. Pass the implied type of the
|
|
|
|
// corresponding resource type schema for correct operation.
|
|
|
|
func (rc *ResourceInstanceChange) Encode(ty cty.Type) (*ResourceInstanceChangeSrc, error) {
|
|
|
|
cs, err := rc.Change.Encode(ty)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ResourceInstanceChangeSrc{
|
2018-08-25 01:13:50 +02:00
|
|
|
Addr: rc.Addr,
|
|
|
|
DeposedKey: rc.DeposedKey,
|
|
|
|
ProviderAddr: rc.ProviderAddr,
|
|
|
|
ChangeSrc: *cs,
|
|
|
|
RequiredReplace: rc.RequiredReplace,
|
|
|
|
Private: rc.Private,
|
2018-07-21 02:15:03 +02:00
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
2018-06-20 01:03:59 +02:00
|
|
|
// OutputChange describes a change to an output value.
|
|
|
|
type OutputChange struct {
|
|
|
|
// Change is an embedded description of the change.
|
|
|
|
//
|
|
|
|
// For output value changes, the type constraint for the DynamicValue
|
|
|
|
// instances is always cty.DynamicPseudoType.
|
|
|
|
Change
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-07-21 02:15:03 +02:00
|
|
|
// Encode produces a variant of the reciever that has its change values
|
|
|
|
// serialized so it can be written to a plan file.
|
|
|
|
func (oc *OutputChange) Encode() (*OutputChangeSrc, error) {
|
|
|
|
cs, err := oc.Change.Encode(cty.DynamicPseudoType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &OutputChangeSrc{
|
|
|
|
ChangeSrc: *cs,
|
|
|
|
Sensitive: oc.Sensitive,
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
2018-06-20 01:03:59 +02:00
|
|
|
// Change describes a single change with a given action.
|
|
|
|
type Change struct {
|
|
|
|
// Action defines what kind of change is being made.
|
|
|
|
Action Action
|
|
|
|
|
|
|
|
// Interpretation of Before and After depend on Action:
|
|
|
|
//
|
|
|
|
// NoOp Before and After are the same, unchanged value
|
|
|
|
// Create Before is nil, and After is the expected value after create.
|
|
|
|
// Read Before is any prior value (nil if no prior), and After is the
|
|
|
|
// value that was or will be read.
|
|
|
|
// Update Before is the value prior to update, and After is the expected
|
|
|
|
// value after update.
|
|
|
|
// Replace As with Update.
|
|
|
|
// Delete Before is the value prior to delete, and After is always nil.
|
|
|
|
//
|
|
|
|
// Unknown values may appear anywhere within the Before and After values,
|
|
|
|
// either as the values themselves or as nested elements within known
|
|
|
|
// collections/structures.
|
2018-07-21 02:15:03 +02:00
|
|
|
Before, After cty.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode produces a variant of the reciever that has its change values
|
|
|
|
// serialized so it can be written to a plan file. Pass the type constraint
|
|
|
|
// that the values are expected to conform to; to properly decode the values
|
|
|
|
// later an identical type constraint must be provided at that time.
|
|
|
|
//
|
|
|
|
// Where a Change is embedded in some other struct, it's generally better
|
|
|
|
// to call the corresponding Encode method of that struct rather than working
|
|
|
|
// directly with its embedded Change.
|
|
|
|
func (c *Change) Encode(ty cty.Type) (*ChangeSrc, error) {
|
|
|
|
beforeDV, err := NewDynamicValue(c.Before, ty)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
afterDV, err := NewDynamicValue(c.After, ty)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ChangeSrc{
|
|
|
|
Action: c.Action,
|
|
|
|
Before: beforeDV,
|
|
|
|
After: afterDV,
|
|
|
|
}, nil
|
2018-06-20 01:03:59 +02:00
|
|
|
}
|