github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/internal/states/state_equal.go (about) 1 package states 2 3 import ( 4 "reflect" 5 6 "github.com/hashicorp/terraform/internal/addrs" 7 ) 8 9 // Equal returns true if the receiver is functionally equivalent to other, 10 // including any ephemeral portions of the state that would not be included 11 // if the state were saved to files. 12 // 13 // To test only the persistent portions of two states for equality, instead 14 // use statefile.StatesMarshalEqual. 15 func (s *State) Equal(other *State) bool { 16 // For the moment this is sufficient, but we may need to do something 17 // more elaborate in future if we have any portions of state that require 18 // more sophisticated comparisons. 19 return reflect.DeepEqual(s, other) 20 } 21 22 // ManagedResourcesEqual returns true if all of the managed resources tracked 23 // in the reciever are functionally equivalent to the same tracked in the 24 // other given state. 25 // 26 // This is a more constrained version of Equal that disregards other 27 // differences, including but not limited to changes to data resources and 28 // changes to output values. 29 func (s *State) ManagedResourcesEqual(other *State) bool { 30 // First, some accommodations for situations where one of the objects is 31 // nil, for robustness since we sometimes use a nil state to represent 32 // a prior state being entirely absent. 33 if s == nil && other == nil { 34 return true 35 } 36 if s == nil { 37 return !other.HasManagedResourceInstanceObjects() 38 } 39 if other == nil { 40 return !s.HasManagedResourceInstanceObjects() 41 } 42 43 // If we get here then both states are non-nil. 44 45 // sameManagedResources tests that its second argument has all the 46 // resources that the first one does, so we'll call it twice with the 47 // arguments inverted to ensure that we'll also catch situations where 48 // the second has resources that the first does not. 49 return sameManagedResources(s, other) && sameManagedResources(other, s) 50 } 51 52 func sameManagedResources(s1, s2 *State) bool { 53 for _, ms := range s1.Modules { 54 for _, rs := range ms.Resources { 55 addr := rs.Addr 56 if addr.Resource.Mode != addrs.ManagedResourceMode { 57 continue 58 } 59 otherRS := s2.Resource(addr) 60 if !reflect.DeepEqual(rs, otherRS) { 61 return false 62 } 63 } 64 } 65 66 return true 67 68 }