2018-06-20 01:03:59 +02:00
|
|
|
package plans
|
|
|
|
|
various: helpers for collecting necessary provider types
Since schemas are required to interpret provider, resource, and
provisioner attributes in configs, states, and plans, these helpers intend
to make it easier to gather up the the necessary provider types in order
to preload all of the needed schemas before beginning further processing.
Config.ProviderTypes returns directly the list of provider types, since
at this level further detail is not useful: we've not yet run the
provider allocation algorithm, and so the only thing we can reliably
extract here is provider types themselves.
State.ProviderAddrs and Plan.ProviderAddrs each return a list of
absolute provider addresses, which can then be turned into a list of
provider types using the new helper providers.AddressedTypesAbs.
Since we're already using configs.Config throughout core, this also
updates the terraform.LoadSchemas helper to use Config.ProviderTypes
to find the necessary providers, rather than implementing its own
discovery logic. states.State is not yet plumbed in, so we cannot yet
use State.ProviderAddrs to deal with the state but there's a TODO comment
to remind us to update that in a later commit when we swap out
terraform.State for states.State.
A later commit will probably refactor this further so that we can easily
obtain schema for the providers needed to interpret a plan too, but that
is deferred here because further work is required to make core work with
the new plan types first. At that point, terraform.LoadSchemas may become
providers.LoadSchemas with a different interface that just accepts lists
of provider and provisioner names that have been gathered by the caller
using these new helpers.
2018-06-22 02:39:27 +02:00
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
2021-05-17 21:00:50 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 21:17:09 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
2022-02-04 19:34:43 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/lang/globalref"
|
2021-05-17 21:43:35 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/states"
|
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
|
|
|
"github.com/zclconf/go-cty/cty"
|
various: helpers for collecting necessary provider types
Since schemas are required to interpret provider, resource, and
provisioner attributes in configs, states, and plans, these helpers intend
to make it easier to gather up the the necessary provider types in order
to preload all of the needed schemas before beginning further processing.
Config.ProviderTypes returns directly the list of provider types, since
at this level further detail is not useful: we've not yet run the
provider allocation algorithm, and so the only thing we can reliably
extract here is provider types themselves.
State.ProviderAddrs and Plan.ProviderAddrs each return a list of
absolute provider addresses, which can then be turned into a list of
provider types using the new helper providers.AddressedTypesAbs.
Since we're already using configs.Config throughout core, this also
updates the terraform.LoadSchemas helper to use Config.ProviderTypes
to find the necessary providers, rather than implementing its own
discovery logic. states.State is not yet plumbed in, so we cannot yet
use State.ProviderAddrs to deal with the state but there's a TODO comment
to remind us to update that in a later commit when we swap out
terraform.State for states.State.
A later commit will probably refactor this further so that we can easily
obtain schema for the providers needed to interpret a plan too, but that
is deferred here because further work is required to make core work with
the new plan types first. At that point, terraform.LoadSchemas may become
providers.LoadSchemas with a different interface that just accepts lists
of provider and provisioner names that have been gathered by the caller
using these new helpers.
2018-06-22 02:39:27 +02:00
|
|
|
)
|
|
|
|
|
2018-06-20 01:03:59 +02:00
|
|
|
// Plan is the top-level type representing a planned set of changes.
|
|
|
|
//
|
|
|
|
// A plan is a summary of the set of changes required to move from a current
|
|
|
|
// state to a goal state derived from configuration. The described changes
|
|
|
|
// are not applied directly, but contain an approximation of the final
|
|
|
|
// result that will be completed during apply by resolving any values that
|
|
|
|
// cannot be predicted.
|
|
|
|
//
|
2020-09-04 22:49:19 +02:00
|
|
|
// A plan must always be accompanied by the configuration it was built from,
|
|
|
|
// since the plan does not itself include all of the information required to
|
|
|
|
// make the changes indicated.
|
2018-06-20 01:03:59 +02:00
|
|
|
type Plan struct {
|
2021-04-06 02:05:57 +02:00
|
|
|
// Mode is the mode under which this plan was created.
|
|
|
|
//
|
|
|
|
// This is only recorded to allow for UI differences when presenting plans
|
|
|
|
// to the end-user, and so it must not be used to influence apply-time
|
|
|
|
// behavior. The actions during apply must be described entirely by
|
|
|
|
// the Changes field, regardless of how the plan was created.
|
2021-04-30 19:14:09 +02:00
|
|
|
UIMode Mode
|
2021-04-06 02:05:57 +02:00
|
|
|
|
2021-04-07 02:37:38 +02:00
|
|
|
VariableValues map[string]DynamicValue
|
|
|
|
Changes *Changes
|
2021-09-13 22:29:15 +02:00
|
|
|
DriftedResources []*ResourceInstanceChangeSrc
|
2021-04-07 02:37:38 +02:00
|
|
|
TargetAddrs []addrs.Targetable
|
|
|
|
ForceReplaceAddrs []addrs.AbsResourceInstance
|
|
|
|
Backend Backend
|
2021-05-05 00:59:58 +02:00
|
|
|
|
2022-02-04 19:34:43 +01:00
|
|
|
// RelevantAttributes is a set of resource addresses and attributes that are
|
|
|
|
// either directly affected by proposed changes or may have indirectly
|
|
|
|
// contributed to them via references in expressions.
|
2022-01-29 20:58:30 +01:00
|
|
|
//
|
|
|
|
// This is the result of a heuristic and is intended only as a hint to
|
|
|
|
// the UI layer in case it wants to emphasize or de-emphasize certain
|
|
|
|
// resources. Don't use this to drive any non-cosmetic behavior, especially
|
|
|
|
// including anything that would be subject to compatibility constraints.
|
2022-02-04 19:34:43 +01:00
|
|
|
RelevantAttributes []globalref.ResourceAttr
|
2022-01-29 20:58:30 +01:00
|
|
|
|
2021-05-05 00:59:58 +02:00
|
|
|
// PrevRunState and PriorState both describe the situation that the plan
|
|
|
|
// was derived from:
|
|
|
|
//
|
|
|
|
// PrevRunState is a representation of the outcome of the previous
|
|
|
|
// Terraform operation, without any updates from the remote system but
|
|
|
|
// potentially including some changes that resulted from state upgrade
|
|
|
|
// actions.
|
|
|
|
//
|
|
|
|
// PriorState is a representation of the current state of remote objects,
|
|
|
|
// which will differ from PrevRunState if the "refresh" step returned
|
|
|
|
// different data, which might reflect drift.
|
|
|
|
//
|
|
|
|
// PriorState is the main snapshot we use for actions during apply.
|
|
|
|
// PrevRunState is only here so that we can diff PriorState against it in
|
|
|
|
// order to report to the user any out-of-band changes we've detected.
|
|
|
|
PrevRunState *states.State
|
|
|
|
PriorState *states.State
|
2018-07-05 22:21:38 +02:00
|
|
|
}
|
|
|
|
|
2021-05-07 00:22:48 +02:00
|
|
|
// CanApply returns true if and only if the recieving plan includes content
|
|
|
|
// that would make sense to apply. If it returns false, the plan operation
|
|
|
|
// should indicate that there's nothing to do and Terraform should exit
|
|
|
|
// without prompting the user to confirm the changes.
|
|
|
|
//
|
|
|
|
// This function represents our main business logic for making the decision
|
|
|
|
// about whether a given plan represents meaningful "changes", and so its
|
|
|
|
// exact definition may change over time; the intent is just to centralize the
|
|
|
|
// rules for that rather than duplicating different versions of it at various
|
|
|
|
// locations in the UI code.
|
|
|
|
func (p *Plan) CanApply() bool {
|
|
|
|
switch {
|
|
|
|
case !p.Changes.Empty():
|
|
|
|
// "Empty" means that everything in the changes is a "NoOp", so if
|
|
|
|
// not empty then there's at least one non-NoOp change.
|
|
|
|
return true
|
2018-07-05 22:21:38 +02:00
|
|
|
|
2021-05-07 00:22:48 +02:00
|
|
|
case !p.PriorState.ManagedResourcesEqual(p.PrevRunState):
|
|
|
|
// If there are no changes planned but we detected some
|
|
|
|
// outside-Terraform changes while refreshing then we consider
|
|
|
|
// that applyable in isolation only if this was a refresh-only
|
|
|
|
// plan where we expect updating the state to include these
|
|
|
|
// changes was the intended goal.
|
|
|
|
//
|
|
|
|
// (We don't treat a "refresh only" plan as applyable in normal
|
|
|
|
// planning mode because historically the refresh result wasn't
|
|
|
|
// considered part of a plan at all, and so it would be
|
|
|
|
// a disruptive breaking change if refreshing alone suddenly
|
|
|
|
// became applyable in the normal case and an existing configuration
|
|
|
|
// was relying on ignore_changes in order to be convergent in spite
|
|
|
|
// of intentional out-of-band operations.)
|
|
|
|
return p.UIMode == RefreshOnlyMode
|
various: helpers for collecting necessary provider types
Since schemas are required to interpret provider, resource, and
provisioner attributes in configs, states, and plans, these helpers intend
to make it easier to gather up the the necessary provider types in order
to preload all of the needed schemas before beginning further processing.
Config.ProviderTypes returns directly the list of provider types, since
at this level further detail is not useful: we've not yet run the
provider allocation algorithm, and so the only thing we can reliably
extract here is provider types themselves.
State.ProviderAddrs and Plan.ProviderAddrs each return a list of
absolute provider addresses, which can then be turned into a list of
provider types using the new helper providers.AddressedTypesAbs.
Since we're already using configs.Config throughout core, this also
updates the terraform.LoadSchemas helper to use Config.ProviderTypes
to find the necessary providers, rather than implementing its own
discovery logic. states.State is not yet plumbed in, so we cannot yet
use State.ProviderAddrs to deal with the state but there's a TODO comment
to remind us to update that in a later commit when we swap out
terraform.State for states.State.
A later commit will probably refactor this further so that we can easily
obtain schema for the providers needed to interpret a plan too, but that
is deferred here because further work is required to make core work with
the new plan types first. At that point, terraform.LoadSchemas may become
providers.LoadSchemas with a different interface that just accepts lists
of provider and provisioner names that have been gathered by the caller
using these new helpers.
2018-06-22 02:39:27 +02:00
|
|
|
|
2021-05-07 00:22:48 +02:00
|
|
|
default:
|
|
|
|
// Otherwise, there are either no changes to apply or they are changes
|
|
|
|
// our cases above don't consider as worthy of applying in isolation.
|
|
|
|
return false
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
various: helpers for collecting necessary provider types
Since schemas are required to interpret provider, resource, and
provisioner attributes in configs, states, and plans, these helpers intend
to make it easier to gather up the the necessary provider types in order
to preload all of the needed schemas before beginning further processing.
Config.ProviderTypes returns directly the list of provider types, since
at this level further detail is not useful: we've not yet run the
provider allocation algorithm, and so the only thing we can reliably
extract here is provider types themselves.
State.ProviderAddrs and Plan.ProviderAddrs each return a list of
absolute provider addresses, which can then be turned into a list of
provider types using the new helper providers.AddressedTypesAbs.
Since we're already using configs.Config throughout core, this also
updates the terraform.LoadSchemas helper to use Config.ProviderTypes
to find the necessary providers, rather than implementing its own
discovery logic. states.State is not yet plumbed in, so we cannot yet
use State.ProviderAddrs to deal with the state but there's a TODO comment
to remind us to update that in a later commit when we swap out
terraform.State for states.State.
A later commit will probably refactor this further so that we can easily
obtain schema for the providers needed to interpret a plan too, but that
is deferred here because further work is required to make core work with
the new plan types first. At that point, terraform.LoadSchemas may become
providers.LoadSchemas with a different interface that just accepts lists
of provider and provisioner names that have been gathered by the caller
using these new helpers.
2018-06-22 02:39:27 +02:00
|
|
|
// ProviderAddrs returns a list of all of the provider configuration addresses
|
|
|
|
// referenced throughout the receiving plan.
|
|
|
|
//
|
|
|
|
// The result is de-duplicated so that each distinct address appears only once.
|
|
|
|
func (p *Plan) ProviderAddrs() []addrs.AbsProviderConfig {
|
|
|
|
if p == nil || p.Changes == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m := map[string]addrs.AbsProviderConfig{}
|
|
|
|
for _, rc := range p.Changes.Resources {
|
|
|
|
m[rc.ProviderAddr.String()] = rc.ProviderAddr
|
|
|
|
}
|
|
|
|
if len(m) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is mainly just so we'll get stable results for testing purposes.
|
|
|
|
keys := make([]string, 0, len(m))
|
|
|
|
for k := range m {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
ret := make([]addrs.AbsProviderConfig, len(keys))
|
|
|
|
for i, key := range keys {
|
|
|
|
ret[i] = m[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
2021-05-07 00:22:48 +02:00
|
|
|
|
|
|
|
// Backend represents the backend-related configuration and other data as it
|
|
|
|
// existed when a plan was created.
|
|
|
|
type Backend struct {
|
|
|
|
// Type is the type of backend that the plan will apply against.
|
|
|
|
Type string
|
|
|
|
|
|
|
|
// Config is the configuration of the backend, whose schema is decided by
|
|
|
|
// the backend Type.
|
|
|
|
Config DynamicValue
|
|
|
|
|
|
|
|
// Workspace is the name of the workspace that was active when the plan
|
|
|
|
// was created. It is illegal to apply a plan created for one workspace
|
|
|
|
// to the state of another workspace.
|
|
|
|
// (This constraint is already enforced by the statefile lineage mechanism,
|
|
|
|
// but storing this explicitly allows us to return a better error message
|
|
|
|
// in the situation where the user has the wrong workspace selected.)
|
|
|
|
Workspace string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBackend(typeName string, config cty.Value, configSchema *configschema.Block, workspaceName string) (*Backend, error) {
|
|
|
|
dv, err := NewDynamicValue(config, configSchema.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Backend{
|
|
|
|
Type: typeName,
|
|
|
|
Config: dv,
|
|
|
|
Workspace: workspaceName,
|
|
|
|
}, nil
|
|
|
|
}
|