terraform/states/state_equal.go

69 lines
2.1 KiB
Go
Raw Normal View History

states: New package with modern models for Terraform state Our previous state models in the "terraform" package had a few limitations that are addressed here: - Instance attributes were stored as map[string]string with dot-separated keys representing traversals through a data structure. Now that we have a full type system, it's preferable to store it as a real data structure. - The existing state structures skipped over the "resource" concept and went straight to resource instance, requiring heuristics to decide whether a particular resource should appear as a single object or as a list of objects when used in configuration expressions. - Related to the previous point, the state models also used incorrect terminology where "ResourceState" was really a resource instance state and "InstanceState" was really the state of a particular remote object associated with an instance. These new models use the correct names for each of these, introducing the idea of a "ResourceInstanceObject" as the local record of a remote object associated with an instance. This is a first pass at fleshing out a new model for state. Undoubtedly there will be further iterations of this as we work on integrating these new models into the "terraform" package. These new model types no longer serve double-duty as a description of the JSON state file format, since they are for in-memory use only. A subsequent commit will introduce a separate package that deals with persisting state to files and reloading those files later.
2018-06-08 02:27:57 +02:00
package states
import (
"reflect"
"github.com/hashicorp/terraform/internal/addrs"
states: New package with modern models for Terraform state Our previous state models in the "terraform" package had a few limitations that are addressed here: - Instance attributes were stored as map[string]string with dot-separated keys representing traversals through a data structure. Now that we have a full type system, it's preferable to store it as a real data structure. - The existing state structures skipped over the "resource" concept and went straight to resource instance, requiring heuristics to decide whether a particular resource should appear as a single object or as a list of objects when used in configuration expressions. - Related to the previous point, the state models also used incorrect terminology where "ResourceState" was really a resource instance state and "InstanceState" was really the state of a particular remote object associated with an instance. These new models use the correct names for each of these, introducing the idea of a "ResourceInstanceObject" as the local record of a remote object associated with an instance. This is a first pass at fleshing out a new model for state. Undoubtedly there will be further iterations of this as we work on integrating these new models into the "terraform" package. These new model types no longer serve double-duty as a description of the JSON state file format, since they are for in-memory use only. A subsequent commit will introduce a separate package that deals with persisting state to files and reloading those files later.
2018-06-08 02:27:57 +02:00
)
// Equal returns true if the receiver is functionally equivalent to other,
// including any ephemeral portions of the state that would not be included
// if the state were saved to files.
//
// To test only the persistent portions of two states for equality, instead
// use statefile.StatesMarshalEqual.
func (s *State) Equal(other *State) bool {
// For the moment this is sufficient, but we may need to do something
// more elaborate in future if we have any portions of state that require
// more sophisticated comparisons.
return reflect.DeepEqual(s, other)
}
// ManagedResourcesEqual returns true if all of the managed resources tracked
// in the reciever are functionally equivalent to the same tracked in the
// other given state.
//
// This is a more constrained version of Equal that disregards other
// differences, including but not limited to changes to data resources and
// changes to output values.
func (s *State) ManagedResourcesEqual(other *State) bool {
// First, some accommodations for situations where one of the objects is
// nil, for robustness since we sometimes use a nil state to represent
// a prior state being entirely absent.
if s == nil && other == nil {
return true
}
if s == nil {
return !other.HasResources()
}
if other == nil {
return !s.HasResources()
}
// If we get here then both states are non-nil.
// sameManagedResources tests that its second argument has all the
// resources that the first one does, so we'll call it twice with the
// arguments inverted to ensure that we'll also catch situations where
// the second has resources that the first does not.
return sameManagedResources(s, other) && sameManagedResources(other, s)
}
func sameManagedResources(s1, s2 *State) bool {
for _, ms := range s1.Modules {
for _, rs := range ms.Resources {
addr := rs.Addr
if addr.Resource.Mode != addrs.ManagedResourceMode {
continue
}
otherRS := s2.Resource(addr)
if !reflect.DeepEqual(rs, otherRS) {
return false
}
}
}
return true
}