2018-06-08 02:27:57 +02:00
|
|
|
package states
|
|
|
|
|
|
|
|
import (
|
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
|
|
|
"sort"
|
|
|
|
|
2018-06-08 02:27:57 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2020-03-26 20:04:48 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/getproviders"
|
2018-06-08 02:27:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// State is the top-level type of a Terraform state.
|
|
|
|
//
|
|
|
|
// A state should be mutated only via its accessor methods, to ensure that
|
|
|
|
// invariants are preserved.
|
|
|
|
//
|
|
|
|
// Access to State and the nested values within it is not concurrency-safe,
|
|
|
|
// so when accessing a State object concurrently it is the caller's
|
|
|
|
// responsibility to ensure that only one write is in progress at a time
|
2018-06-12 02:44:12 +02:00
|
|
|
// and that reads only occur when no write is in progress. The most common
|
|
|
|
// way to acheive this is to wrap the State in a SyncState and use the
|
|
|
|
// higher-level atomic operations supported by that type.
|
2018-06-08 02:27:57 +02:00
|
|
|
type State struct {
|
|
|
|
// Modules contains the state for each module. The keys in this map are
|
|
|
|
// an implementation detail and must not be used by outside callers.
|
|
|
|
Modules map[string]*Module
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewState constructs a minimal empty state, containing an empty root module.
|
|
|
|
func NewState() *State {
|
|
|
|
modules := map[string]*Module{}
|
|
|
|
modules[addrs.RootModuleInstance.String()] = NewModule(addrs.RootModuleInstance)
|
|
|
|
return &State{
|
|
|
|
Modules: modules,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// BuildState is a helper -- primarily intended for tests -- to build a state
|
|
|
|
// using imperative code against the StateSync type while still acting as
|
|
|
|
// an expression of type *State to assign into a containing struct.
|
|
|
|
func BuildState(cb func(*SyncState)) *State {
|
|
|
|
s := NewState()
|
|
|
|
cb(s.SyncWrapper())
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty returns true if there are no resources or populated output values
|
|
|
|
// in the receiver. In other words, if this state could be safely replaced
|
|
|
|
// with the return value of NewState and be functionally equivalent.
|
|
|
|
func (s *State) Empty() bool {
|
2018-09-30 18:29:51 +02:00
|
|
|
if s == nil {
|
|
|
|
return true
|
|
|
|
}
|
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
|
|
|
for _, ms := range s.Modules {
|
|
|
|
if len(ms.Resources) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(ms.OutputValues) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-06-08 02:27:57 +02:00
|
|
|
// Module returns the state for the module with the given address, or nil if
|
|
|
|
// the requested module is not tracked in the state.
|
|
|
|
func (s *State) Module(addr addrs.ModuleInstance) *Module {
|
2018-09-20 21:30:52 +02:00
|
|
|
if s == nil {
|
|
|
|
panic("State.Module on nil *State")
|
|
|
|
}
|
2018-06-08 02:27:57 +02:00
|
|
|
return s.Modules[addr.String()]
|
|
|
|
}
|
|
|
|
|
2020-03-13 18:05:37 +01:00
|
|
|
// ModuleInstances returns the set of Module states that matches the given path.
|
|
|
|
func (s *State) ModuleInstances(addr addrs.Module) []*Module {
|
|
|
|
var ms []*Module
|
|
|
|
for _, m := range s.Modules {
|
|
|
|
if m.Addr.Module().Equal(addr) {
|
|
|
|
ms = append(ms, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
2018-06-12 02:44:12 +02:00
|
|
|
// RemoveModule removes the module with the given address from the state,
|
|
|
|
// unless it is the root module. The root module cannot be deleted, and so
|
|
|
|
// this method will panic if that is attempted.
|
|
|
|
//
|
|
|
|
// Removing a module implicitly discards all of the resources, outputs and
|
|
|
|
// local values within it, and so this should usually be done only for empty
|
|
|
|
// modules. For callers accessing the state through a SyncState wrapper, modules
|
|
|
|
// are automatically pruned if they are empty after one of their contained
|
|
|
|
// elements is removed.
|
|
|
|
func (s *State) RemoveModule(addr addrs.ModuleInstance) {
|
|
|
|
if addr.IsRoot() {
|
2018-09-30 17:51:47 +02:00
|
|
|
panic("attempted to remove root module")
|
2018-06-12 02:44:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(s.Modules, addr.String())
|
|
|
|
}
|
|
|
|
|
2018-06-08 02:27:57 +02:00
|
|
|
// RootModule is a convenient alias for Module(addrs.RootModuleInstance).
|
|
|
|
func (s *State) RootModule() *Module {
|
2018-09-30 18:29:51 +02:00
|
|
|
if s == nil {
|
|
|
|
panic("RootModule called on nil State")
|
|
|
|
}
|
2018-06-08 02:27:57 +02:00
|
|
|
return s.Modules[addrs.RootModuleInstance.String()]
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnsureModule returns the state for the module with the given address,
|
|
|
|
// creating and adding a new one if necessary.
|
|
|
|
//
|
|
|
|
// Since this might modify the state to add a new instance, it is considered
|
|
|
|
// to be a write operation.
|
|
|
|
func (s *State) EnsureModule(addr addrs.ModuleInstance) *Module {
|
|
|
|
ms := s.Module(addr)
|
|
|
|
if ms == nil {
|
|
|
|
ms = NewModule(addr)
|
|
|
|
s.Modules[addr.String()] = ms
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// HasResources returns true if there is at least one resource (of any mode)
|
|
|
|
// present in the receiving state.
|
|
|
|
func (s *State) HasResources() bool {
|
|
|
|
if s == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, ms := range s.Modules {
|
|
|
|
if len(ms.Resources) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-06-08 02:27:57 +02:00
|
|
|
// Resource returns the state for the resource with the given address, or nil
|
|
|
|
// if no such resource is tracked in the state.
|
|
|
|
func (s *State) Resource(addr addrs.AbsResource) *Resource {
|
|
|
|
ms := s.Module(addr.Module)
|
|
|
|
if ms == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ms.Resource(addr.Resource)
|
|
|
|
}
|
|
|
|
|
2020-03-13 18:05:37 +01:00
|
|
|
// Resources returns the set of resources that match the given configuration path.
|
|
|
|
func (s *State) Resources(addr addrs.ConfigResource) []*Resource {
|
|
|
|
var ret []*Resource
|
|
|
|
for _, m := range s.ModuleInstances(addr.Module) {
|
|
|
|
r := m.Resource(addr.Resource)
|
|
|
|
if r != nil {
|
|
|
|
ret = append(ret, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-06-08 02:27:57 +02:00
|
|
|
// ResourceInstance returns the state for the resource instance with the given
|
|
|
|
// address, or nil if no such resource is tracked in the state.
|
|
|
|
func (s *State) ResourceInstance(addr addrs.AbsResourceInstance) *ResourceInstance {
|
2018-09-20 21:30:52 +02:00
|
|
|
if s == nil {
|
|
|
|
panic("State.ResourceInstance on nil *State")
|
|
|
|
}
|
2018-06-08 02:27:57 +02:00
|
|
|
ms := s.Module(addr.Module)
|
|
|
|
if ms == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ms.ResourceInstance(addr.Resource)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OutputValue returns the state for the output value with the given address,
|
|
|
|
// or nil if no such output value is tracked in the state.
|
|
|
|
func (s *State) OutputValue(addr addrs.AbsOutputValue) *OutputValue {
|
|
|
|
ms := s.Module(addr.Module)
|
|
|
|
if ms == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ms.OutputValues[addr.OutputValue.Name]
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalValue returns the value of the named local value with the given address,
|
|
|
|
// or cty.NilVal if no such value is tracked in the state.
|
|
|
|
func (s *State) LocalValue(addr addrs.AbsLocalValue) cty.Value {
|
|
|
|
ms := s.Module(addr.Module)
|
|
|
|
if ms == nil {
|
|
|
|
return cty.NilVal
|
|
|
|
}
|
|
|
|
return ms.LocalValues[addr.LocalValue.Name]
|
|
|
|
}
|
2018-06-12 02:44:12 +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 state.
|
|
|
|
//
|
|
|
|
// The result is de-duplicated so that each distinct address appears only once.
|
|
|
|
func (s *State) ProviderAddrs() []addrs.AbsProviderConfig {
|
|
|
|
if s == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m := map[string]addrs.AbsProviderConfig{}
|
|
|
|
for _, ms := range s.Modules {
|
|
|
|
for _, rc := range ms.Resources {
|
|
|
|
m[rc.ProviderConfig.String()] = rc.ProviderConfig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-26 20:04:48 +01:00
|
|
|
// ProviderRequirements returns a description of all of the providers that
|
|
|
|
// are required to work with the receiving state.
|
|
|
|
//
|
|
|
|
// Because the state does not track specific version information for providers,
|
|
|
|
// the requirements returned by this method will always be unconstrained.
|
|
|
|
// The result should usually be merged with a Requirements derived from the
|
|
|
|
// current configuration in order to apply some constraints.
|
|
|
|
func (s *State) ProviderRequirements() getproviders.Requirements {
|
|
|
|
configAddrs := s.ProviderAddrs()
|
|
|
|
ret := make(getproviders.Requirements, len(configAddrs))
|
|
|
|
for _, configAddr := range configAddrs {
|
|
|
|
ret[configAddr.Provider] = nil // unconstrained dependency
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-09-30 17:51:47 +02:00
|
|
|
// PruneResourceHusks is a specialized method that will remove any Resource
|
|
|
|
// objects that do not contain any instances, even if they have an EachMode.
|
|
|
|
//
|
|
|
|
// This should generally be used only after a "terraform destroy" operation,
|
|
|
|
// to finalize the cleanup of the state. It is not correct to use this after
|
|
|
|
// other operations because if a resource has "count = 0" or "for_each" over
|
|
|
|
// an empty collection then we want to retain it in the state so that references
|
|
|
|
// to it, particularly in "strange" contexts like "terraform console", can be
|
|
|
|
// properly resolved.
|
|
|
|
//
|
|
|
|
// This method MUST NOT be called concurrently with other readers and writers
|
|
|
|
// of the receiving state.
|
|
|
|
func (s *State) PruneResourceHusks() {
|
|
|
|
for _, m := range s.Modules {
|
|
|
|
m.PruneResourceHusks()
|
|
|
|
if len(m.Resources) == 0 && !m.Addr.IsRoot() {
|
|
|
|
s.RemoveModule(m.Addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-12 02:44:12 +02:00
|
|
|
// SyncWrapper returns a SyncState object wrapping the receiver.
|
|
|
|
func (s *State) SyncWrapper() *SyncState {
|
|
|
|
return &SyncState{
|
|
|
|
state: s,
|
|
|
|
}
|
|
|
|
}
|