6 "github.com/zclconf/go-cty/cty"
8 "github.com/hashicorp/terraform/addrs"
11 // State is the top-level type of a Terraform state.
13 // A state should be mutated only via its accessor methods, to ensure that
14 // invariants are preserved.
16 // Access to State and the nested values within it is not concurrency-safe,
17 // so when accessing a State object concurrently it is the caller's
18 // responsibility to ensure that only one write is in progress at a time
19 // and that reads only occur when no write is in progress. The most common
20 // way to acheive this is to wrap the State in a SyncState and use the
21 // higher-level atomic operations supported by that type.
23 // Modules contains the state for each module. The keys in this map are
24 // an implementation detail and must not be used by outside callers.
25 Modules map[string]*Module
28 // NewState constructs a minimal empty state, containing an empty root module.
29 func NewState() *State {
30 modules := map[string]*Module{}
31 modules[addrs.RootModuleInstance.String()] = NewModule(addrs.RootModuleInstance)
37 // BuildState is a helper -- primarily intended for tests -- to build a state
38 // using imperative code against the StateSync type while still acting as
39 // an expression of type *State to assign into a containing struct.
40 func BuildState(cb func(*SyncState)) *State {
46 // Empty returns true if there are no resources or populated output values
47 // in the receiver. In other words, if this state could be safely replaced
48 // with the return value of NewState and be functionally equivalent.
49 func (s *State) Empty() bool {
53 for _, ms := range s.Modules {
54 if len(ms.Resources) != 0 {
57 if len(ms.OutputValues) != 0 {
64 // Module returns the state for the module with the given address, or nil if
65 // the requested module is not tracked in the state.
66 func (s *State) Module(addr addrs.ModuleInstance) *Module {
68 panic("State.Module on nil *State")
70 return s.Modules[addr.String()]
73 // RemoveModule removes the module with the given address from the state,
74 // unless it is the root module. The root module cannot be deleted, and so
75 // this method will panic if that is attempted.
77 // Removing a module implicitly discards all of the resources, outputs and
78 // local values within it, and so this should usually be done only for empty
79 // modules. For callers accessing the state through a SyncState wrapper, modules
80 // are automatically pruned if they are empty after one of their contained
81 // elements is removed.
82 func (s *State) RemoveModule(addr addrs.ModuleInstance) {
84 panic("attempted to remove root module")
87 delete(s.Modules, addr.String())
90 // RootModule is a convenient alias for Module(addrs.RootModuleInstance).
91 func (s *State) RootModule() *Module {
93 panic("RootModule called on nil State")
95 return s.Modules[addrs.RootModuleInstance.String()]
98 // EnsureModule returns the state for the module with the given address,
99 // creating and adding a new one if necessary.
101 // Since this might modify the state to add a new instance, it is considered
102 // to be a write operation.
103 func (s *State) EnsureModule(addr addrs.ModuleInstance) *Module {
107 s.Modules[addr.String()] = ms
112 // HasResources returns true if there is at least one resource (of any mode)
113 // present in the receiving state.
114 func (s *State) HasResources() bool {
118 for _, ms := range s.Modules {
119 if len(ms.Resources) > 0 {
126 // Resource returns the state for the resource with the given address, or nil
127 // if no such resource is tracked in the state.
128 func (s *State) Resource(addr addrs.AbsResource) *Resource {
129 ms := s.Module(addr.Module)
133 return ms.Resource(addr.Resource)
136 // ResourceInstance returns the state for the resource instance with the given
137 // address, or nil if no such resource is tracked in the state.
138 func (s *State) ResourceInstance(addr addrs.AbsResourceInstance) *ResourceInstance {
140 panic("State.ResourceInstance on nil *State")
142 ms := s.Module(addr.Module)
146 return ms.ResourceInstance(addr.Resource)
149 // OutputValue returns the state for the output value with the given address,
150 // or nil if no such output value is tracked in the state.
151 func (s *State) OutputValue(addr addrs.AbsOutputValue) *OutputValue {
152 ms := s.Module(addr.Module)
156 return ms.OutputValues[addr.OutputValue.Name]
159 // LocalValue returns the value of the named local value with the given address,
160 // or cty.NilVal if no such value is tracked in the state.
161 func (s *State) LocalValue(addr addrs.AbsLocalValue) cty.Value {
162 ms := s.Module(addr.Module)
166 return ms.LocalValues[addr.LocalValue.Name]
169 // ProviderAddrs returns a list of all of the provider configuration addresses
170 // referenced throughout the receiving state.
172 // The result is de-duplicated so that each distinct address appears only once.
173 func (s *State) ProviderAddrs() []addrs.AbsProviderConfig {
178 m := map[string]addrs.AbsProviderConfig{}
179 for _, ms := range s.Modules {
180 for _, rc := range ms.Resources {
181 m[rc.ProviderConfig.String()] = rc.ProviderConfig
188 // This is mainly just so we'll get stable results for testing purposes.
189 keys := make([]string, 0, len(m))
191 keys = append(keys, k)
195 ret := make([]addrs.AbsProviderConfig, len(keys))
196 for i, key := range keys {
203 // PruneResourceHusks is a specialized method that will remove any Resource
204 // objects that do not contain any instances, even if they have an EachMode.
206 // This should generally be used only after a "terraform destroy" operation,
207 // to finalize the cleanup of the state. It is not correct to use this after
208 // other operations because if a resource has "count = 0" or "for_each" over
209 // an empty collection then we want to retain it in the state so that references
210 // to it, particularly in "strange" contexts like "terraform console", can be
211 // properly resolved.
213 // This method MUST NOT be called concurrently with other readers and writers
214 // of the receiving state.
215 func (s *State) PruneResourceHusks() {
216 for _, m := range s.Modules {
217 m.PruneResourceHusks()
218 if len(m.Resources) == 0 && !m.Addr.IsRoot() {
219 s.RemoveModule(m.Addr)
224 // SyncWrapper returns a SyncState object wrapping the receiver.
225 func (s *State) SyncWrapper() *SyncState {