github.com/ggriffiths/terraform@v0.9.0-beta1.0.20170222213024-79c4935604cb/terraform/state.go (about)

     1  package terraform
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"encoding/json"
     7  	"errors"
     8  	"fmt"
     9  	"io"
    10  	"io/ioutil"
    11  	"log"
    12  	"reflect"
    13  	"regexp"
    14  	"sort"
    15  	"strconv"
    16  	"strings"
    17  	"sync"
    18  
    19  	"github.com/hashicorp/go-multierror"
    20  	"github.com/hashicorp/go-version"
    21  	"github.com/hashicorp/terraform/config"
    22  	"github.com/mitchellh/copystructure"
    23  	"github.com/satori/go.uuid"
    24  )
    25  
    26  const (
    27  	// StateVersion is the current version for our state file
    28  	StateVersion = 3
    29  )
    30  
    31  // rootModulePath is the path of the root module
    32  var rootModulePath = []string{"root"}
    33  
    34  // normalizeModulePath takes a raw module path and returns a path that
    35  // has the rootModulePath prepended to it. If I could go back in time I
    36  // would've never had a rootModulePath (empty path would be root). We can
    37  // still fix this but thats a big refactor that my branch doesn't make sense
    38  // for. Instead, this function normalizes paths.
    39  func normalizeModulePath(p []string) []string {
    40  	k := len(rootModulePath)
    41  
    42  	// If we already have a root module prefix, we're done
    43  	if len(p) >= len(rootModulePath) {
    44  		if reflect.DeepEqual(p[:k], rootModulePath) {
    45  			return p
    46  		}
    47  	}
    48  
    49  	// None? Prefix it
    50  	result := make([]string, len(rootModulePath)+len(p))
    51  	copy(result, rootModulePath)
    52  	copy(result[k:], p)
    53  	return result
    54  }
    55  
    56  // State keeps track of a snapshot state-of-the-world that Terraform
    57  // can use to keep track of what real world resources it is actually
    58  // managing.
    59  type State struct {
    60  	// Version is the state file protocol version.
    61  	Version int `json:"version"`
    62  
    63  	// TFVersion is the version of Terraform that wrote this state.
    64  	TFVersion string `json:"terraform_version,omitempty"`
    65  
    66  	// Serial is incremented on any operation that modifies
    67  	// the State file. It is used to detect potentially conflicting
    68  	// updates.
    69  	Serial int64 `json:"serial"`
    70  
    71  	// Lineage is set when a new, blank state is created and then
    72  	// never updated. This allows us to determine whether the serials
    73  	// of two states can be meaningfully compared.
    74  	// Apart from the guarantee that collisions between two lineages
    75  	// are very unlikely, this value is opaque and external callers
    76  	// should only compare lineage strings byte-for-byte for equality.
    77  	Lineage string `json:"lineage"`
    78  
    79  	// Remote is used to track the metadata required to
    80  	// pull and push state files from a remote storage endpoint.
    81  	Remote *RemoteState `json:"remote,omitempty"`
    82  
    83  	// Backend tracks the configuration for the backend in use with
    84  	// this state. This is used to track any changes in the backend
    85  	// configuration.
    86  	Backend *BackendState `json:"backend,omitempty"`
    87  
    88  	// Modules contains all the modules in a breadth-first order
    89  	Modules []*ModuleState `json:"modules"`
    90  
    91  	mu sync.Mutex
    92  }
    93  
    94  func (s *State) Lock()   { s.mu.Lock() }
    95  func (s *State) Unlock() { s.mu.Unlock() }
    96  
    97  // NewState is used to initialize a blank state
    98  func NewState() *State {
    99  	s := &State{}
   100  	s.init()
   101  	return s
   102  }
   103  
   104  // Children returns the ModuleStates that are direct children of
   105  // the given path. If the path is "root", for example, then children
   106  // returned might be "root.child", but not "root.child.grandchild".
   107  func (s *State) Children(path []string) []*ModuleState {
   108  	s.Lock()
   109  	defer s.Unlock()
   110  	// TODO: test
   111  
   112  	return s.children(path)
   113  }
   114  
   115  func (s *State) children(path []string) []*ModuleState {
   116  	result := make([]*ModuleState, 0)
   117  	for _, m := range s.Modules {
   118  		if m == nil {
   119  			continue
   120  		}
   121  
   122  		if len(m.Path) != len(path)+1 {
   123  			continue
   124  		}
   125  		if !reflect.DeepEqual(path, m.Path[:len(path)]) {
   126  			continue
   127  		}
   128  
   129  		result = append(result, m)
   130  	}
   131  
   132  	return result
   133  }
   134  
   135  // AddModule adds the module with the given path to the state.
   136  //
   137  // This should be the preferred method to add module states since it
   138  // allows us to optimize lookups later as well as control sorting.
   139  func (s *State) AddModule(path []string) *ModuleState {
   140  	s.Lock()
   141  	defer s.Unlock()
   142  
   143  	return s.addModule(path)
   144  }
   145  
   146  func (s *State) addModule(path []string) *ModuleState {
   147  	// check if the module exists first
   148  	m := s.moduleByPath(path)
   149  	if m != nil {
   150  		return m
   151  	}
   152  
   153  	m = &ModuleState{Path: path}
   154  	m.init()
   155  	s.Modules = append(s.Modules, m)
   156  	s.sort()
   157  	return m
   158  }
   159  
   160  // ModuleByPath is used to lookup the module state for the given path.
   161  // This should be the preferred lookup mechanism as it allows for future
   162  // lookup optimizations.
   163  func (s *State) ModuleByPath(path []string) *ModuleState {
   164  	if s == nil {
   165  		return nil
   166  	}
   167  	s.Lock()
   168  	defer s.Unlock()
   169  
   170  	return s.moduleByPath(path)
   171  }
   172  
   173  func (s *State) moduleByPath(path []string) *ModuleState {
   174  	for _, mod := range s.Modules {
   175  		if mod == nil {
   176  			continue
   177  		}
   178  		if mod.Path == nil {
   179  			panic("missing module path")
   180  		}
   181  		if reflect.DeepEqual(mod.Path, path) {
   182  			return mod
   183  		}
   184  	}
   185  	return nil
   186  }
   187  
   188  // ModuleOrphans returns all the module orphans in this state by
   189  // returning their full paths. These paths can be used with ModuleByPath
   190  // to return the actual state.
   191  func (s *State) ModuleOrphans(path []string, c *config.Config) [][]string {
   192  	s.Lock()
   193  	defer s.Unlock()
   194  
   195  	return s.moduleOrphans(path, c)
   196  
   197  }
   198  
   199  func (s *State) moduleOrphans(path []string, c *config.Config) [][]string {
   200  	// direct keeps track of what direct children we have both in our config
   201  	// and in our state. childrenKeys keeps track of what isn't an orphan.
   202  	direct := make(map[string]struct{})
   203  	childrenKeys := make(map[string]struct{})
   204  	if c != nil {
   205  		for _, m := range c.Modules {
   206  			childrenKeys[m.Name] = struct{}{}
   207  			direct[m.Name] = struct{}{}
   208  		}
   209  	}
   210  
   211  	// Go over the direct children and find any that aren't in our keys.
   212  	var orphans [][]string
   213  	for _, m := range s.children(path) {
   214  		key := m.Path[len(m.Path)-1]
   215  
   216  		// Record that we found this key as a direct child. We use this
   217  		// later to find orphan nested modules.
   218  		direct[key] = struct{}{}
   219  
   220  		// If we have a direct child still in our config, it is not an orphan
   221  		if _, ok := childrenKeys[key]; ok {
   222  			continue
   223  		}
   224  
   225  		orphans = append(orphans, m.Path)
   226  	}
   227  
   228  	// Find the orphans that are nested...
   229  	for _, m := range s.Modules {
   230  		if m == nil {
   231  			continue
   232  		}
   233  
   234  		// We only want modules that are at least grandchildren
   235  		if len(m.Path) < len(path)+2 {
   236  			continue
   237  		}
   238  
   239  		// If it isn't part of our tree, continue
   240  		if !reflect.DeepEqual(path, m.Path[:len(path)]) {
   241  			continue
   242  		}
   243  
   244  		// If we have the direct child, then just skip it.
   245  		key := m.Path[len(path)]
   246  		if _, ok := direct[key]; ok {
   247  			continue
   248  		}
   249  
   250  		orphanPath := m.Path[:len(path)+1]
   251  
   252  		// Don't double-add if we've already added this orphan (which can happen if
   253  		// there are multiple nested sub-modules that get orphaned together).
   254  		alreadyAdded := false
   255  		for _, o := range orphans {
   256  			if reflect.DeepEqual(o, orphanPath) {
   257  				alreadyAdded = true
   258  				break
   259  			}
   260  		}
   261  		if alreadyAdded {
   262  			continue
   263  		}
   264  
   265  		// Add this orphan
   266  		orphans = append(orphans, orphanPath)
   267  	}
   268  
   269  	return orphans
   270  }
   271  
   272  // Empty returns true if the state is empty.
   273  func (s *State) Empty() bool {
   274  	if s == nil {
   275  		return true
   276  	}
   277  	s.Lock()
   278  	defer s.Unlock()
   279  
   280  	return len(s.Modules) == 0
   281  }
   282  
   283  // HasResources returns true if the state contains any resources.
   284  //
   285  // This is similar to !s.Empty, but returns true also in the case where the
   286  // state has modules but all of them are devoid of resources.
   287  func (s *State) HasResources() bool {
   288  	if s.Empty() {
   289  		return false
   290  	}
   291  
   292  	for _, mod := range s.Modules {
   293  		if len(mod.Resources) > 0 {
   294  			return true
   295  		}
   296  	}
   297  
   298  	return false
   299  }
   300  
   301  // IsRemote returns true if State represents a state that exists and is
   302  // remote.
   303  func (s *State) IsRemote() bool {
   304  	if s == nil {
   305  		return false
   306  	}
   307  	s.Lock()
   308  	defer s.Unlock()
   309  
   310  	if s.Remote == nil {
   311  		return false
   312  	}
   313  	if s.Remote.Type == "" {
   314  		return false
   315  	}
   316  
   317  	return true
   318  }
   319  
   320  // Validate validates the integrity of this state file.
   321  //
   322  // Certain properties of the statefile are expected by Terraform in order
   323  // to behave properly. The core of Terraform will assume that once it
   324  // receives a State structure that it has been validated. This validation
   325  // check should be called to ensure that.
   326  //
   327  // If this returns an error, then the user should be notified. The error
   328  // response will include detailed information on the nature of the error.
   329  func (s *State) Validate() error {
   330  	s.Lock()
   331  	defer s.Unlock()
   332  
   333  	var result error
   334  
   335  	// !!!! FOR DEVELOPERS !!!!
   336  	//
   337  	// Any errors returned from this Validate function will BLOCK TERRAFORM
   338  	// from loading a state file. Therefore, this should only contain checks
   339  	// that are only resolvable through manual intervention.
   340  	//
   341  	// !!!! FOR DEVELOPERS !!!!
   342  
   343  	// Make sure there are no duplicate module states. We open a new
   344  	// block here so we can use basic variable names and future validations
   345  	// can do the same.
   346  	{
   347  		found := make(map[string]struct{})
   348  		for _, ms := range s.Modules {
   349  			if ms == nil {
   350  				continue
   351  			}
   352  
   353  			key := strings.Join(ms.Path, ".")
   354  			if _, ok := found[key]; ok {
   355  				result = multierror.Append(result, fmt.Errorf(
   356  					strings.TrimSpace(stateValidateErrMultiModule), key))
   357  				continue
   358  			}
   359  
   360  			found[key] = struct{}{}
   361  		}
   362  	}
   363  
   364  	return result
   365  }
   366  
   367  // Remove removes the item in the state at the given address, returning
   368  // any errors that may have occurred.
   369  //
   370  // If the address references a module state or resource, it will delete
   371  // all children as well. To check what will be deleted, use a StateFilter
   372  // first.
   373  func (s *State) Remove(addr ...string) error {
   374  	s.Lock()
   375  	defer s.Unlock()
   376  
   377  	// Filter out what we need to delete
   378  	filter := &StateFilter{State: s}
   379  	results, err := filter.Filter(addr...)
   380  	if err != nil {
   381  		return err
   382  	}
   383  
   384  	// If we have no results, just exit early, we're not going to do anything.
   385  	// While what happens below is fairly fast, this is an important early
   386  	// exit since the prune below might modify the state more and we don't
   387  	// want to modify the state if we don't have to.
   388  	if len(results) == 0 {
   389  		return nil
   390  	}
   391  
   392  	// Go through each result and grab what we need
   393  	removed := make(map[interface{}]struct{})
   394  	for _, r := range results {
   395  		// Convert the path to our own type
   396  		path := append([]string{"root"}, r.Path...)
   397  
   398  		// If we removed this already, then ignore
   399  		if _, ok := removed[r.Value]; ok {
   400  			continue
   401  		}
   402  
   403  		// If we removed the parent already, then ignore
   404  		if r.Parent != nil {
   405  			if _, ok := removed[r.Parent.Value]; ok {
   406  				continue
   407  			}
   408  		}
   409  
   410  		// Add this to the removed list
   411  		removed[r.Value] = struct{}{}
   412  
   413  		switch v := r.Value.(type) {
   414  		case *ModuleState:
   415  			s.removeModule(path, v)
   416  		case *ResourceState:
   417  			s.removeResource(path, v)
   418  		case *InstanceState:
   419  			s.removeInstance(path, r.Parent.Value.(*ResourceState), v)
   420  		default:
   421  			return fmt.Errorf("unknown type to delete: %T", r.Value)
   422  		}
   423  	}
   424  
   425  	// Prune since the removal functions often do the bare minimum to
   426  	// remove a thing and may leave around dangling empty modules, resources,
   427  	// etc. Prune will clean that all up.
   428  	s.prune()
   429  
   430  	return nil
   431  }
   432  
   433  func (s *State) removeModule(path []string, v *ModuleState) {
   434  	for i, m := range s.Modules {
   435  		if m == v {
   436  			s.Modules, s.Modules[len(s.Modules)-1] = append(s.Modules[:i], s.Modules[i+1:]...), nil
   437  			return
   438  		}
   439  	}
   440  }
   441  
   442  func (s *State) removeResource(path []string, v *ResourceState) {
   443  	// Get the module this resource lives in. If it doesn't exist, we're done.
   444  	mod := s.moduleByPath(path)
   445  	if mod == nil {
   446  		return
   447  	}
   448  
   449  	// Find this resource. This is a O(N) lookup when if we had the key
   450  	// it could be O(1) but even with thousands of resources this shouldn't
   451  	// matter right now. We can easily up performance here when the time comes.
   452  	for k, r := range mod.Resources {
   453  		if r == v {
   454  			// Found it
   455  			delete(mod.Resources, k)
   456  			return
   457  		}
   458  	}
   459  }
   460  
   461  func (s *State) removeInstance(path []string, r *ResourceState, v *InstanceState) {
   462  	// Go through the resource and find the instance that matches this
   463  	// (if any) and remove it.
   464  
   465  	// Check primary
   466  	if r.Primary == v {
   467  		r.Primary = nil
   468  		return
   469  	}
   470  
   471  	// Check lists
   472  	lists := [][]*InstanceState{r.Deposed}
   473  	for _, is := range lists {
   474  		for i, instance := range is {
   475  			if instance == v {
   476  				// Found it, remove it
   477  				is, is[len(is)-1] = append(is[:i], is[i+1:]...), nil
   478  
   479  				// Done
   480  				return
   481  			}
   482  		}
   483  	}
   484  }
   485  
   486  // RootModule returns the ModuleState for the root module
   487  func (s *State) RootModule() *ModuleState {
   488  	root := s.ModuleByPath(rootModulePath)
   489  	if root == nil {
   490  		panic("missing root module")
   491  	}
   492  	return root
   493  }
   494  
   495  // Equal tests if one state is equal to another.
   496  func (s *State) Equal(other *State) bool {
   497  	// If one is nil, we do a direct check
   498  	if s == nil || other == nil {
   499  		return s == other
   500  	}
   501  
   502  	s.Lock()
   503  	defer s.Unlock()
   504  	return s.equal(other)
   505  }
   506  
   507  func (s *State) equal(other *State) bool {
   508  	if s == nil || other == nil {
   509  		return s == other
   510  	}
   511  
   512  	// If the versions are different, they're certainly not equal
   513  	if s.Version != other.Version {
   514  		return false
   515  	}
   516  
   517  	// If any of the modules are not equal, then this state isn't equal
   518  	if len(s.Modules) != len(other.Modules) {
   519  		return false
   520  	}
   521  	for _, m := range s.Modules {
   522  		// This isn't very optimal currently but works.
   523  		otherM := other.moduleByPath(m.Path)
   524  		if otherM == nil {
   525  			return false
   526  		}
   527  
   528  		// If they're not equal, then we're not equal!
   529  		if !m.Equal(otherM) {
   530  			return false
   531  		}
   532  	}
   533  
   534  	return true
   535  }
   536  
   537  type StateAgeComparison int
   538  
   539  const (
   540  	StateAgeEqual         StateAgeComparison = 0
   541  	StateAgeReceiverNewer StateAgeComparison = 1
   542  	StateAgeReceiverOlder StateAgeComparison = -1
   543  )
   544  
   545  // CompareAges compares one state with another for which is "older".
   546  //
   547  // This is a simple check using the state's serial, and is thus only as
   548  // reliable as the serial itself. In the normal case, only one state
   549  // exists for a given combination of lineage/serial, but Terraform
   550  // does not guarantee this and so the result of this method should be
   551  // used with care.
   552  //
   553  // Returns an integer that is negative if the receiver is older than
   554  // the argument, positive if the converse, and zero if they are equal.
   555  // An error is returned if the two states are not of the same lineage,
   556  // in which case the integer returned has no meaning.
   557  func (s *State) CompareAges(other *State) (StateAgeComparison, error) {
   558  	// nil states are "older" than actual states
   559  	switch {
   560  	case s != nil && other == nil:
   561  		return StateAgeReceiverNewer, nil
   562  	case s == nil && other != nil:
   563  		return StateAgeReceiverOlder, nil
   564  	case s == nil && other == nil:
   565  		return StateAgeEqual, nil
   566  	}
   567  
   568  	if !s.SameLineage(other) {
   569  		return StateAgeEqual, fmt.Errorf(
   570  			"can't compare two states of differing lineage",
   571  		)
   572  	}
   573  
   574  	s.Lock()
   575  	defer s.Unlock()
   576  
   577  	switch {
   578  	case s.Serial < other.Serial:
   579  		return StateAgeReceiverOlder, nil
   580  	case s.Serial > other.Serial:
   581  		return StateAgeReceiverNewer, nil
   582  	default:
   583  		return StateAgeEqual, nil
   584  	}
   585  }
   586  
   587  // SameLineage returns true only if the state given in argument belongs
   588  // to the same "lineage" of states as the reciever.
   589  func (s *State) SameLineage(other *State) bool {
   590  	s.Lock()
   591  	defer s.Unlock()
   592  
   593  	// If one of the states has no lineage then it is assumed to predate
   594  	// this concept, and so we'll accept it as belonging to any lineage
   595  	// so that a lineage string can be assigned to newer versions
   596  	// without breaking compatibility with older versions.
   597  	if s.Lineage == "" || other.Lineage == "" {
   598  		return true
   599  	}
   600  
   601  	return s.Lineage == other.Lineage
   602  }
   603  
   604  // DeepCopy performs a deep copy of the state structure and returns
   605  // a new structure.
   606  func (s *State) DeepCopy() *State {
   607  	copy, err := copystructure.Config{Lock: true}.Copy(s)
   608  	if err != nil {
   609  		panic(err)
   610  	}
   611  
   612  	return copy.(*State)
   613  }
   614  
   615  // IncrementSerialMaybe increments the serial number of this state
   616  // if it different from the other state.
   617  func (s *State) IncrementSerialMaybe(other *State) {
   618  	if s == nil {
   619  		return
   620  	}
   621  	if other == nil {
   622  		return
   623  	}
   624  	s.Lock()
   625  	defer s.Unlock()
   626  
   627  	if s.Serial > other.Serial {
   628  		return
   629  	}
   630  	if other.TFVersion != s.TFVersion || !s.equal(other) {
   631  		if other.Serial > s.Serial {
   632  			s.Serial = other.Serial
   633  		}
   634  
   635  		s.Serial++
   636  	}
   637  }
   638  
   639  // FromFutureTerraform checks if this state was written by a Terraform
   640  // version from the future.
   641  func (s *State) FromFutureTerraform() bool {
   642  	s.Lock()
   643  	defer s.Unlock()
   644  
   645  	// No TF version means it is certainly from the past
   646  	if s.TFVersion == "" {
   647  		return false
   648  	}
   649  
   650  	v := version.Must(version.NewVersion(s.TFVersion))
   651  	return SemVersion.LessThan(v)
   652  }
   653  
   654  func (s *State) Init() {
   655  	s.Lock()
   656  	defer s.Unlock()
   657  	s.init()
   658  }
   659  
   660  func (s *State) init() {
   661  	if s.Version == 0 {
   662  		s.Version = StateVersion
   663  	}
   664  	if s.moduleByPath(rootModulePath) == nil {
   665  		s.addModule(rootModulePath)
   666  	}
   667  	s.ensureHasLineage()
   668  
   669  	for _, mod := range s.Modules {
   670  		if mod != nil {
   671  			mod.init()
   672  		}
   673  	}
   674  
   675  	if s.Remote != nil {
   676  		s.Remote.init()
   677  	}
   678  
   679  }
   680  
   681  func (s *State) EnsureHasLineage() {
   682  	s.Lock()
   683  	defer s.Unlock()
   684  
   685  	s.ensureHasLineage()
   686  }
   687  
   688  func (s *State) ensureHasLineage() {
   689  	if s.Lineage == "" {
   690  		s.Lineage = uuid.NewV4().String()
   691  		log.Printf("[DEBUG] New state was assigned lineage %q\n", s.Lineage)
   692  	} else {
   693  		log.Printf("[TRACE] Preserving existing state lineage %q\n", s.Lineage)
   694  	}
   695  }
   696  
   697  // AddModuleState insert this module state and override any existing ModuleState
   698  func (s *State) AddModuleState(mod *ModuleState) {
   699  	mod.init()
   700  	s.Lock()
   701  	defer s.Unlock()
   702  
   703  	s.addModuleState(mod)
   704  }
   705  
   706  func (s *State) addModuleState(mod *ModuleState) {
   707  	for i, m := range s.Modules {
   708  		if reflect.DeepEqual(m.Path, mod.Path) {
   709  			s.Modules[i] = mod
   710  			return
   711  		}
   712  	}
   713  
   714  	s.Modules = append(s.Modules, mod)
   715  	s.sort()
   716  }
   717  
   718  // prune is used to remove any resources that are no longer required
   719  func (s *State) prune() {
   720  	if s == nil {
   721  		return
   722  	}
   723  
   724  	// Filter out empty modules.
   725  	// A module is always assumed to have a path, and it's length isn't always
   726  	// bounds checked later on. Modules may be "emptied" during destroy, but we
   727  	// never want to store those in the state.
   728  	for i := 0; i < len(s.Modules); i++ {
   729  		if s.Modules[i] == nil || len(s.Modules[i].Path) == 0 {
   730  			s.Modules = append(s.Modules[:i], s.Modules[i+1:]...)
   731  			i--
   732  		}
   733  	}
   734  
   735  	for _, mod := range s.Modules {
   736  		mod.prune()
   737  	}
   738  	if s.Remote != nil && s.Remote.Empty() {
   739  		s.Remote = nil
   740  	}
   741  }
   742  
   743  // sort sorts the modules
   744  func (s *State) sort() {
   745  	sort.Sort(moduleStateSort(s.Modules))
   746  
   747  	// Allow modules to be sorted
   748  	for _, m := range s.Modules {
   749  		if m != nil {
   750  			m.sort()
   751  		}
   752  	}
   753  }
   754  
   755  func (s *State) String() string {
   756  	if s == nil {
   757  		return "<nil>"
   758  	}
   759  	s.Lock()
   760  	defer s.Unlock()
   761  
   762  	var buf bytes.Buffer
   763  	for _, m := range s.Modules {
   764  		mStr := m.String()
   765  
   766  		// If we're the root module, we just write the output directly.
   767  		if reflect.DeepEqual(m.Path, rootModulePath) {
   768  			buf.WriteString(mStr + "\n")
   769  			continue
   770  		}
   771  
   772  		buf.WriteString(fmt.Sprintf("module.%s:\n", strings.Join(m.Path[1:], ".")))
   773  
   774  		s := bufio.NewScanner(strings.NewReader(mStr))
   775  		for s.Scan() {
   776  			text := s.Text()
   777  			if text != "" {
   778  				text = "  " + text
   779  			}
   780  
   781  			buf.WriteString(fmt.Sprintf("%s\n", text))
   782  		}
   783  	}
   784  
   785  	return strings.TrimSpace(buf.String())
   786  }
   787  
   788  // BackendState stores the configuration to connect to a remote backend.
   789  type BackendState struct {
   790  	Type   string                 `json:"type"`   // Backend type
   791  	Config map[string]interface{} `json:"config"` // Backend raw config
   792  
   793  	// Hash is the hash code to uniquely identify the original source
   794  	// configuration. We use this to detect when there is a change in
   795  	// configuration even when "type" isn't changed.
   796  	Hash uint64 `json:"hash"`
   797  }
   798  
   799  // Empty returns true if BackendState has no state.
   800  func (s *BackendState) Empty() bool {
   801  	return s == nil || s.Type == ""
   802  }
   803  
   804  // RemoteState is used to track the information about a remote
   805  // state store that we push/pull state to.
   806  type RemoteState struct {
   807  	// Type controls the client we use for the remote state
   808  	Type string `json:"type"`
   809  
   810  	// Config is used to store arbitrary configuration that
   811  	// is type specific
   812  	Config map[string]string `json:"config"`
   813  
   814  	mu sync.Mutex
   815  }
   816  
   817  func (s *RemoteState) Lock()   { s.mu.Lock() }
   818  func (s *RemoteState) Unlock() { s.mu.Unlock() }
   819  
   820  func (r *RemoteState) init() {
   821  	r.Lock()
   822  	defer r.Unlock()
   823  
   824  	if r.Config == nil {
   825  		r.Config = make(map[string]string)
   826  	}
   827  }
   828  
   829  func (r *RemoteState) deepcopy() *RemoteState {
   830  	r.Lock()
   831  	defer r.Unlock()
   832  
   833  	confCopy := make(map[string]string, len(r.Config))
   834  	for k, v := range r.Config {
   835  		confCopy[k] = v
   836  	}
   837  	return &RemoteState{
   838  		Type:   r.Type,
   839  		Config: confCopy,
   840  	}
   841  }
   842  
   843  func (r *RemoteState) Empty() bool {
   844  	if r == nil {
   845  		return true
   846  	}
   847  	r.Lock()
   848  	defer r.Unlock()
   849  
   850  	return r.Type == ""
   851  }
   852  
   853  func (r *RemoteState) Equals(other *RemoteState) bool {
   854  	r.Lock()
   855  	defer r.Unlock()
   856  
   857  	if r.Type != other.Type {
   858  		return false
   859  	}
   860  	if len(r.Config) != len(other.Config) {
   861  		return false
   862  	}
   863  	for k, v := range r.Config {
   864  		if other.Config[k] != v {
   865  			return false
   866  		}
   867  	}
   868  	return true
   869  }
   870  
   871  // OutputState is used to track the state relevant to a single output.
   872  type OutputState struct {
   873  	// Sensitive describes whether the output is considered sensitive,
   874  	// which may lead to masking the value on screen in some cases.
   875  	Sensitive bool `json:"sensitive"`
   876  	// Type describes the structure of Value. Valid values are "string",
   877  	// "map" and "list"
   878  	Type string `json:"type"`
   879  	// Value contains the value of the output, in the structure described
   880  	// by the Type field.
   881  	Value interface{} `json:"value"`
   882  
   883  	mu sync.Mutex
   884  }
   885  
   886  func (s *OutputState) Lock()   { s.mu.Lock() }
   887  func (s *OutputState) Unlock() { s.mu.Unlock() }
   888  
   889  func (s *OutputState) String() string {
   890  	return fmt.Sprintf("%#v", s.Value)
   891  }
   892  
   893  // Equal compares two OutputState structures for equality. nil values are
   894  // considered equal.
   895  func (s *OutputState) Equal(other *OutputState) bool {
   896  	if s == nil && other == nil {
   897  		return true
   898  	}
   899  
   900  	if s == nil || other == nil {
   901  		return false
   902  	}
   903  	s.Lock()
   904  	defer s.Unlock()
   905  
   906  	if s.Type != other.Type {
   907  		return false
   908  	}
   909  
   910  	if s.Sensitive != other.Sensitive {
   911  		return false
   912  	}
   913  
   914  	if !reflect.DeepEqual(s.Value, other.Value) {
   915  		return false
   916  	}
   917  
   918  	return true
   919  }
   920  
   921  func (s *OutputState) deepcopy() *OutputState {
   922  	if s == nil {
   923  		return nil
   924  	}
   925  
   926  	stateCopy, err := copystructure.Config{Lock: true}.Copy(s)
   927  	if err != nil {
   928  		panic(fmt.Errorf("Error copying output value: %s", err))
   929  	}
   930  
   931  	return stateCopy.(*OutputState)
   932  }
   933  
   934  // ModuleState is used to track all the state relevant to a single
   935  // module. Previous to Terraform 0.3, all state belonged to the "root"
   936  // module.
   937  type ModuleState struct {
   938  	// Path is the import path from the root module. Modules imports are
   939  	// always disjoint, so the path represents amodule tree
   940  	Path []string `json:"path"`
   941  
   942  	// Outputs declared by the module and maintained for each module
   943  	// even though only the root module technically needs to be kept.
   944  	// This allows operators to inspect values at the boundaries.
   945  	Outputs map[string]*OutputState `json:"outputs"`
   946  
   947  	// Resources is a mapping of the logically named resource to
   948  	// the state of the resource. Each resource may actually have
   949  	// N instances underneath, although a user only needs to think
   950  	// about the 1:1 case.
   951  	Resources map[string]*ResourceState `json:"resources"`
   952  
   953  	// Dependencies are a list of things that this module relies on
   954  	// existing to remain intact. For example: an module may depend
   955  	// on a VPC ID given by an aws_vpc resource.
   956  	//
   957  	// Terraform uses this information to build valid destruction
   958  	// orders and to warn the user if they're destroying a module that
   959  	// another resource depends on.
   960  	//
   961  	// Things can be put into this list that may not be managed by
   962  	// Terraform. If Terraform doesn't find a matching ID in the
   963  	// overall state, then it assumes it isn't managed and doesn't
   964  	// worry about it.
   965  	Dependencies []string `json:"depends_on"`
   966  
   967  	mu sync.Mutex
   968  }
   969  
   970  func (s *ModuleState) Lock()   { s.mu.Lock() }
   971  func (s *ModuleState) Unlock() { s.mu.Unlock() }
   972  
   973  // Equal tests whether one module state is equal to another.
   974  func (m *ModuleState) Equal(other *ModuleState) bool {
   975  	m.Lock()
   976  	defer m.Unlock()
   977  
   978  	// Paths must be equal
   979  	if !reflect.DeepEqual(m.Path, other.Path) {
   980  		return false
   981  	}
   982  
   983  	// Outputs must be equal
   984  	if len(m.Outputs) != len(other.Outputs) {
   985  		return false
   986  	}
   987  	for k, v := range m.Outputs {
   988  		if !other.Outputs[k].Equal(v) {
   989  			return false
   990  		}
   991  	}
   992  
   993  	// Dependencies must be equal. This sorts these in place but
   994  	// this shouldn't cause any problems.
   995  	sort.Strings(m.Dependencies)
   996  	sort.Strings(other.Dependencies)
   997  	if len(m.Dependencies) != len(other.Dependencies) {
   998  		return false
   999  	}
  1000  	for i, d := range m.Dependencies {
  1001  		if other.Dependencies[i] != d {
  1002  			return false
  1003  		}
  1004  	}
  1005  
  1006  	// Resources must be equal
  1007  	if len(m.Resources) != len(other.Resources) {
  1008  		return false
  1009  	}
  1010  	for k, r := range m.Resources {
  1011  		otherR, ok := other.Resources[k]
  1012  		if !ok {
  1013  			return false
  1014  		}
  1015  
  1016  		if !r.Equal(otherR) {
  1017  			return false
  1018  		}
  1019  	}
  1020  
  1021  	return true
  1022  }
  1023  
  1024  // IsRoot says whether or not this module diff is for the root module.
  1025  func (m *ModuleState) IsRoot() bool {
  1026  	m.Lock()
  1027  	defer m.Unlock()
  1028  	return reflect.DeepEqual(m.Path, rootModulePath)
  1029  }
  1030  
  1031  // IsDescendent returns true if other is a descendent of this module.
  1032  func (m *ModuleState) IsDescendent(other *ModuleState) bool {
  1033  	m.Lock()
  1034  	defer m.Unlock()
  1035  
  1036  	i := len(m.Path)
  1037  	return len(other.Path) > i && reflect.DeepEqual(other.Path[:i], m.Path)
  1038  }
  1039  
  1040  // Orphans returns a list of keys of resources that are in the State
  1041  // but aren't present in the configuration itself. Hence, these keys
  1042  // represent the state of resources that are orphans.
  1043  func (m *ModuleState) Orphans(c *config.Config) []string {
  1044  	m.Lock()
  1045  	defer m.Unlock()
  1046  
  1047  	keys := make(map[string]struct{})
  1048  	for k, _ := range m.Resources {
  1049  		keys[k] = struct{}{}
  1050  	}
  1051  
  1052  	if c != nil {
  1053  		for _, r := range c.Resources {
  1054  			delete(keys, r.Id())
  1055  
  1056  			for k, _ := range keys {
  1057  				if strings.HasPrefix(k, r.Id()+".") {
  1058  					delete(keys, k)
  1059  				}
  1060  			}
  1061  		}
  1062  	}
  1063  
  1064  	result := make([]string, 0, len(keys))
  1065  	for k, _ := range keys {
  1066  		result = append(result, k)
  1067  	}
  1068  
  1069  	return result
  1070  }
  1071  
  1072  // View returns a view with the given resource prefix.
  1073  func (m *ModuleState) View(id string) *ModuleState {
  1074  	if m == nil {
  1075  		return m
  1076  	}
  1077  
  1078  	r := m.deepcopy()
  1079  	for k, _ := range r.Resources {
  1080  		if id == k || strings.HasPrefix(k, id+".") {
  1081  			continue
  1082  		}
  1083  
  1084  		delete(r.Resources, k)
  1085  	}
  1086  
  1087  	return r
  1088  }
  1089  
  1090  func (m *ModuleState) init() {
  1091  	m.Lock()
  1092  	defer m.Unlock()
  1093  
  1094  	if m.Path == nil {
  1095  		m.Path = []string{}
  1096  	}
  1097  	if m.Outputs == nil {
  1098  		m.Outputs = make(map[string]*OutputState)
  1099  	}
  1100  	if m.Resources == nil {
  1101  		m.Resources = make(map[string]*ResourceState)
  1102  	}
  1103  
  1104  	if m.Dependencies == nil {
  1105  		m.Dependencies = make([]string, 0)
  1106  	}
  1107  
  1108  	for _, rs := range m.Resources {
  1109  		rs.init()
  1110  	}
  1111  }
  1112  
  1113  func (m *ModuleState) deepcopy() *ModuleState {
  1114  	if m == nil {
  1115  		return nil
  1116  	}
  1117  
  1118  	stateCopy, err := copystructure.Config{Lock: true}.Copy(m)
  1119  	if err != nil {
  1120  		panic(err)
  1121  	}
  1122  
  1123  	return stateCopy.(*ModuleState)
  1124  }
  1125  
  1126  // prune is used to remove any resources that are no longer required
  1127  func (m *ModuleState) prune() {
  1128  	m.Lock()
  1129  	defer m.Unlock()
  1130  
  1131  	for k, v := range m.Resources {
  1132  		if v == nil || (v.Primary == nil || v.Primary.ID == "") && len(v.Deposed) == 0 {
  1133  			delete(m.Resources, k)
  1134  			continue
  1135  		}
  1136  
  1137  		v.prune()
  1138  	}
  1139  
  1140  	for k, v := range m.Outputs {
  1141  		if v.Value == config.UnknownVariableValue {
  1142  			delete(m.Outputs, k)
  1143  		}
  1144  	}
  1145  }
  1146  
  1147  func (m *ModuleState) sort() {
  1148  	for _, v := range m.Resources {
  1149  		v.sort()
  1150  	}
  1151  }
  1152  
  1153  func (m *ModuleState) String() string {
  1154  	m.Lock()
  1155  	defer m.Unlock()
  1156  
  1157  	var buf bytes.Buffer
  1158  
  1159  	if len(m.Resources) == 0 {
  1160  		buf.WriteString("<no state>")
  1161  	}
  1162  
  1163  	names := make([]string, 0, len(m.Resources))
  1164  	for name, _ := range m.Resources {
  1165  		names = append(names, name)
  1166  	}
  1167  	sort.Strings(names)
  1168  
  1169  	for _, k := range names {
  1170  		rs := m.Resources[k]
  1171  		var id string
  1172  		if rs.Primary != nil {
  1173  			id = rs.Primary.ID
  1174  		}
  1175  		if id == "" {
  1176  			id = "<not created>"
  1177  		}
  1178  
  1179  		taintStr := ""
  1180  		if rs.Primary.Tainted {
  1181  			taintStr = " (tainted)"
  1182  		}
  1183  
  1184  		deposedStr := ""
  1185  		if len(rs.Deposed) > 0 {
  1186  			deposedStr = fmt.Sprintf(" (%d deposed)", len(rs.Deposed))
  1187  		}
  1188  
  1189  		buf.WriteString(fmt.Sprintf("%s:%s%s\n", k, taintStr, deposedStr))
  1190  		buf.WriteString(fmt.Sprintf("  ID = %s\n", id))
  1191  		if rs.Provider != "" {
  1192  			buf.WriteString(fmt.Sprintf("  provider = %s\n", rs.Provider))
  1193  		}
  1194  
  1195  		var attributes map[string]string
  1196  		if rs.Primary != nil {
  1197  			attributes = rs.Primary.Attributes
  1198  		}
  1199  		attrKeys := make([]string, 0, len(attributes))
  1200  		for ak, _ := range attributes {
  1201  			if ak == "id" {
  1202  				continue
  1203  			}
  1204  
  1205  			attrKeys = append(attrKeys, ak)
  1206  		}
  1207  		sort.Strings(attrKeys)
  1208  
  1209  		for _, ak := range attrKeys {
  1210  			av := attributes[ak]
  1211  			buf.WriteString(fmt.Sprintf("  %s = %s\n", ak, av))
  1212  		}
  1213  
  1214  		for idx, t := range rs.Deposed {
  1215  			taintStr := ""
  1216  			if t.Tainted {
  1217  				taintStr = " (tainted)"
  1218  			}
  1219  			buf.WriteString(fmt.Sprintf("  Deposed ID %d = %s%s\n", idx+1, t.ID, taintStr))
  1220  		}
  1221  
  1222  		if len(rs.Dependencies) > 0 {
  1223  			buf.WriteString(fmt.Sprintf("\n  Dependencies:\n"))
  1224  			for _, dep := range rs.Dependencies {
  1225  				buf.WriteString(fmt.Sprintf("    %s\n", dep))
  1226  			}
  1227  		}
  1228  	}
  1229  
  1230  	if len(m.Outputs) > 0 {
  1231  		buf.WriteString("\nOutputs:\n\n")
  1232  
  1233  		ks := make([]string, 0, len(m.Outputs))
  1234  		for k, _ := range m.Outputs {
  1235  			ks = append(ks, k)
  1236  		}
  1237  		sort.Strings(ks)
  1238  
  1239  		for _, k := range ks {
  1240  			v := m.Outputs[k]
  1241  			switch vTyped := v.Value.(type) {
  1242  			case string:
  1243  				buf.WriteString(fmt.Sprintf("%s = %s\n", k, vTyped))
  1244  			case []interface{}:
  1245  				buf.WriteString(fmt.Sprintf("%s = %s\n", k, vTyped))
  1246  			case map[string]interface{}:
  1247  				var mapKeys []string
  1248  				for key, _ := range vTyped {
  1249  					mapKeys = append(mapKeys, key)
  1250  				}
  1251  				sort.Strings(mapKeys)
  1252  
  1253  				var mapBuf bytes.Buffer
  1254  				mapBuf.WriteString("{")
  1255  				for _, key := range mapKeys {
  1256  					mapBuf.WriteString(fmt.Sprintf("%s:%s ", key, vTyped[key]))
  1257  				}
  1258  				mapBuf.WriteString("}")
  1259  
  1260  				buf.WriteString(fmt.Sprintf("%s = %s\n", k, mapBuf.String()))
  1261  			}
  1262  		}
  1263  	}
  1264  
  1265  	return buf.String()
  1266  }
  1267  
  1268  // ResourceStateKey is a structured representation of the key used for the
  1269  // ModuleState.Resources mapping
  1270  type ResourceStateKey struct {
  1271  	Name  string
  1272  	Type  string
  1273  	Mode  config.ResourceMode
  1274  	Index int
  1275  }
  1276  
  1277  // Equal determines whether two ResourceStateKeys are the same
  1278  func (rsk *ResourceStateKey) Equal(other *ResourceStateKey) bool {
  1279  	if rsk == nil || other == nil {
  1280  		return false
  1281  	}
  1282  	if rsk.Mode != other.Mode {
  1283  		return false
  1284  	}
  1285  	if rsk.Type != other.Type {
  1286  		return false
  1287  	}
  1288  	if rsk.Name != other.Name {
  1289  		return false
  1290  	}
  1291  	if rsk.Index != other.Index {
  1292  		return false
  1293  	}
  1294  	return true
  1295  }
  1296  
  1297  func (rsk *ResourceStateKey) String() string {
  1298  	if rsk == nil {
  1299  		return ""
  1300  	}
  1301  	var prefix string
  1302  	switch rsk.Mode {
  1303  	case config.ManagedResourceMode:
  1304  		prefix = ""
  1305  	case config.DataResourceMode:
  1306  		prefix = "data."
  1307  	default:
  1308  		panic(fmt.Errorf("unknown resource mode %s", rsk.Mode))
  1309  	}
  1310  	if rsk.Index == -1 {
  1311  		return fmt.Sprintf("%s%s.%s", prefix, rsk.Type, rsk.Name)
  1312  	}
  1313  	return fmt.Sprintf("%s%s.%s.%d", prefix, rsk.Type, rsk.Name, rsk.Index)
  1314  }
  1315  
  1316  // ParseResourceStateKey accepts a key in the format used by
  1317  // ModuleState.Resources and returns a resource name and resource index. In the
  1318  // state, a resource has the format "type.name.index" or "type.name". In the
  1319  // latter case, the index is returned as -1.
  1320  func ParseResourceStateKey(k string) (*ResourceStateKey, error) {
  1321  	parts := strings.Split(k, ".")
  1322  	mode := config.ManagedResourceMode
  1323  	if len(parts) > 0 && parts[0] == "data" {
  1324  		mode = config.DataResourceMode
  1325  		// Don't need the constant "data" prefix for parsing
  1326  		// now that we've figured out the mode.
  1327  		parts = parts[1:]
  1328  	}
  1329  	if len(parts) < 2 || len(parts) > 3 {
  1330  		return nil, fmt.Errorf("Malformed resource state key: %s", k)
  1331  	}
  1332  	rsk := &ResourceStateKey{
  1333  		Mode:  mode,
  1334  		Type:  parts[0],
  1335  		Name:  parts[1],
  1336  		Index: -1,
  1337  	}
  1338  	if len(parts) == 3 {
  1339  		index, err := strconv.Atoi(parts[2])
  1340  		if err != nil {
  1341  			return nil, fmt.Errorf("Malformed resource state key index: %s", k)
  1342  		}
  1343  		rsk.Index = index
  1344  	}
  1345  	return rsk, nil
  1346  }
  1347  
  1348  // ResourceState holds the state of a resource that is used so that
  1349  // a provider can find and manage an existing resource as well as for
  1350  // storing attributes that are used to populate variables of child
  1351  // resources.
  1352  //
  1353  // Attributes has attributes about the created resource that are
  1354  // queryable in interpolation: "${type.id.attr}"
  1355  //
  1356  // Extra is just extra data that a provider can return that we store
  1357  // for later, but is not exposed in any way to the user.
  1358  //
  1359  type ResourceState struct {
  1360  	// This is filled in and managed by Terraform, and is the resource
  1361  	// type itself such as "mycloud_instance". If a resource provider sets
  1362  	// this value, it won't be persisted.
  1363  	Type string `json:"type"`
  1364  
  1365  	// Dependencies are a list of things that this resource relies on
  1366  	// existing to remain intact. For example: an AWS instance might
  1367  	// depend on a subnet (which itself might depend on a VPC, and so
  1368  	// on).
  1369  	//
  1370  	// Terraform uses this information to build valid destruction
  1371  	// orders and to warn the user if they're destroying a resource that
  1372  	// another resource depends on.
  1373  	//
  1374  	// Things can be put into this list that may not be managed by
  1375  	// Terraform. If Terraform doesn't find a matching ID in the
  1376  	// overall state, then it assumes it isn't managed and doesn't
  1377  	// worry about it.
  1378  	Dependencies []string `json:"depends_on"`
  1379  
  1380  	// Primary is the current active instance for this resource.
  1381  	// It can be replaced but only after a successful creation.
  1382  	// This is the instances on which providers will act.
  1383  	Primary *InstanceState `json:"primary"`
  1384  
  1385  	// Deposed is used in the mechanics of CreateBeforeDestroy: the existing
  1386  	// Primary is Deposed to get it out of the way for the replacement Primary to
  1387  	// be created by Apply. If the replacement Primary creates successfully, the
  1388  	// Deposed instance is cleaned up.
  1389  	//
  1390  	// If there were problems creating the replacement Primary, the Deposed
  1391  	// instance and the (now tainted) replacement Primary will be swapped so the
  1392  	// tainted replacement will be cleaned up instead.
  1393  	//
  1394  	// An instance will remain in the Deposed list until it is successfully
  1395  	// destroyed and purged.
  1396  	Deposed []*InstanceState `json:"deposed"`
  1397  
  1398  	// Provider is used when a resource is connected to a provider with an alias.
  1399  	// If this string is empty, the resource is connected to the default provider,
  1400  	// e.g. "aws_instance" goes with the "aws" provider.
  1401  	// If the resource block contained a "provider" key, that value will be set here.
  1402  	Provider string `json:"provider"`
  1403  
  1404  	mu sync.Mutex
  1405  }
  1406  
  1407  func (s *ResourceState) Lock()   { s.mu.Lock() }
  1408  func (s *ResourceState) Unlock() { s.mu.Unlock() }
  1409  
  1410  // Equal tests whether two ResourceStates are equal.
  1411  func (s *ResourceState) Equal(other *ResourceState) bool {
  1412  	s.Lock()
  1413  	defer s.Unlock()
  1414  
  1415  	if s.Type != other.Type {
  1416  		return false
  1417  	}
  1418  
  1419  	if s.Provider != other.Provider {
  1420  		return false
  1421  	}
  1422  
  1423  	// Dependencies must be equal
  1424  	sort.Strings(s.Dependencies)
  1425  	sort.Strings(other.Dependencies)
  1426  	if len(s.Dependencies) != len(other.Dependencies) {
  1427  		return false
  1428  	}
  1429  	for i, d := range s.Dependencies {
  1430  		if other.Dependencies[i] != d {
  1431  			return false
  1432  		}
  1433  	}
  1434  
  1435  	// States must be equal
  1436  	if !s.Primary.Equal(other.Primary) {
  1437  		return false
  1438  	}
  1439  
  1440  	return true
  1441  }
  1442  
  1443  // Taint marks a resource as tainted.
  1444  func (s *ResourceState) Taint() {
  1445  	s.Lock()
  1446  	defer s.Unlock()
  1447  
  1448  	if s.Primary != nil {
  1449  		s.Primary.Tainted = true
  1450  	}
  1451  }
  1452  
  1453  // Untaint unmarks a resource as tainted.
  1454  func (s *ResourceState) Untaint() {
  1455  	s.Lock()
  1456  	defer s.Unlock()
  1457  
  1458  	if s.Primary != nil {
  1459  		s.Primary.Tainted = false
  1460  	}
  1461  }
  1462  
  1463  func (s *ResourceState) init() {
  1464  	s.Lock()
  1465  	defer s.Unlock()
  1466  
  1467  	if s.Primary == nil {
  1468  		s.Primary = &InstanceState{}
  1469  	}
  1470  	s.Primary.init()
  1471  
  1472  	if s.Dependencies == nil {
  1473  		s.Dependencies = []string{}
  1474  	}
  1475  
  1476  	if s.Deposed == nil {
  1477  		s.Deposed = make([]*InstanceState, 0)
  1478  	}
  1479  }
  1480  
  1481  func (s *ResourceState) deepcopy() *ResourceState {
  1482  	copy, err := copystructure.Config{Lock: true}.Copy(s)
  1483  	if err != nil {
  1484  		panic(err)
  1485  	}
  1486  
  1487  	return copy.(*ResourceState)
  1488  }
  1489  
  1490  // prune is used to remove any instances that are no longer required
  1491  func (s *ResourceState) prune() {
  1492  	s.Lock()
  1493  	defer s.Unlock()
  1494  
  1495  	n := len(s.Deposed)
  1496  	for i := 0; i < n; i++ {
  1497  		inst := s.Deposed[i]
  1498  		if inst == nil || inst.ID == "" {
  1499  			copy(s.Deposed[i:], s.Deposed[i+1:])
  1500  			s.Deposed[n-1] = nil
  1501  			n--
  1502  			i--
  1503  		}
  1504  	}
  1505  
  1506  	s.Deposed = s.Deposed[:n]
  1507  }
  1508  
  1509  func (s *ResourceState) sort() {
  1510  	s.Lock()
  1511  	defer s.Unlock()
  1512  
  1513  	sort.Strings(s.Dependencies)
  1514  }
  1515  
  1516  func (s *ResourceState) String() string {
  1517  	s.Lock()
  1518  	defer s.Unlock()
  1519  
  1520  	var buf bytes.Buffer
  1521  	buf.WriteString(fmt.Sprintf("Type = %s", s.Type))
  1522  	return buf.String()
  1523  }
  1524  
  1525  // InstanceState is used to track the unique state information belonging
  1526  // to a given instance.
  1527  type InstanceState struct {
  1528  	// A unique ID for this resource. This is opaque to Terraform
  1529  	// and is only meant as a lookup mechanism for the providers.
  1530  	ID string `json:"id"`
  1531  
  1532  	// Attributes are basic information about the resource. Any keys here
  1533  	// are accessible in variable format within Terraform configurations:
  1534  	// ${resourcetype.name.attribute}.
  1535  	Attributes map[string]string `json:"attributes"`
  1536  
  1537  	// Ephemeral is used to store any state associated with this instance
  1538  	// that is necessary for the Terraform run to complete, but is not
  1539  	// persisted to a state file.
  1540  	Ephemeral EphemeralState `json:"-"`
  1541  
  1542  	// Meta is a simple K/V map that is persisted to the State but otherwise
  1543  	// ignored by Terraform core. It's meant to be used for accounting by
  1544  	// external client code.
  1545  	Meta map[string]string `json:"meta"`
  1546  
  1547  	// Tainted is used to mark a resource for recreation.
  1548  	Tainted bool `json:"tainted"`
  1549  
  1550  	mu sync.Mutex
  1551  }
  1552  
  1553  func (s *InstanceState) Lock()   { s.mu.Lock() }
  1554  func (s *InstanceState) Unlock() { s.mu.Unlock() }
  1555  
  1556  func (s *InstanceState) init() {
  1557  	s.Lock()
  1558  	defer s.Unlock()
  1559  
  1560  	if s.Attributes == nil {
  1561  		s.Attributes = make(map[string]string)
  1562  	}
  1563  	if s.Meta == nil {
  1564  		s.Meta = make(map[string]string)
  1565  	}
  1566  	s.Ephemeral.init()
  1567  }
  1568  
  1569  // Copy all the Fields from another InstanceState
  1570  func (s *InstanceState) Set(from *InstanceState) {
  1571  	s.Lock()
  1572  	defer s.Unlock()
  1573  
  1574  	from.Lock()
  1575  	defer from.Unlock()
  1576  
  1577  	s.ID = from.ID
  1578  	s.Attributes = from.Attributes
  1579  	s.Ephemeral = from.Ephemeral
  1580  	s.Meta = from.Meta
  1581  	s.Tainted = from.Tainted
  1582  }
  1583  
  1584  func (s *InstanceState) DeepCopy() *InstanceState {
  1585  	copy, err := copystructure.Config{Lock: true}.Copy(s)
  1586  	if err != nil {
  1587  		panic(err)
  1588  	}
  1589  
  1590  	return copy.(*InstanceState)
  1591  }
  1592  
  1593  func (s *InstanceState) Empty() bool {
  1594  	if s == nil {
  1595  		return true
  1596  	}
  1597  	s.Lock()
  1598  	defer s.Unlock()
  1599  
  1600  	return s.ID == ""
  1601  }
  1602  
  1603  func (s *InstanceState) Equal(other *InstanceState) bool {
  1604  	// Short circuit some nil checks
  1605  	if s == nil || other == nil {
  1606  		return s == other
  1607  	}
  1608  	s.Lock()
  1609  	defer s.Unlock()
  1610  
  1611  	// IDs must be equal
  1612  	if s.ID != other.ID {
  1613  		return false
  1614  	}
  1615  
  1616  	// Attributes must be equal
  1617  	if len(s.Attributes) != len(other.Attributes) {
  1618  		return false
  1619  	}
  1620  	for k, v := range s.Attributes {
  1621  		otherV, ok := other.Attributes[k]
  1622  		if !ok {
  1623  			return false
  1624  		}
  1625  
  1626  		if v != otherV {
  1627  			return false
  1628  		}
  1629  	}
  1630  
  1631  	// Meta must be equal
  1632  	if len(s.Meta) != len(other.Meta) {
  1633  		return false
  1634  	}
  1635  	for k, v := range s.Meta {
  1636  		otherV, ok := other.Meta[k]
  1637  		if !ok {
  1638  			return false
  1639  		}
  1640  
  1641  		if v != otherV {
  1642  			return false
  1643  		}
  1644  	}
  1645  
  1646  	if s.Tainted != other.Tainted {
  1647  		return false
  1648  	}
  1649  
  1650  	return true
  1651  }
  1652  
  1653  // MergeDiff takes a ResourceDiff and merges the attributes into
  1654  // this resource state in order to generate a new state. This new
  1655  // state can be used to provide updated attribute lookups for
  1656  // variable interpolation.
  1657  //
  1658  // If the diff attribute requires computing the value, and hence
  1659  // won't be available until apply, the value is replaced with the
  1660  // computeID.
  1661  func (s *InstanceState) MergeDiff(d *InstanceDiff) *InstanceState {
  1662  	result := s.DeepCopy()
  1663  	if result == nil {
  1664  		result = new(InstanceState)
  1665  	}
  1666  	result.init()
  1667  
  1668  	if s != nil {
  1669  		s.Lock()
  1670  		defer s.Unlock()
  1671  		for k, v := range s.Attributes {
  1672  			result.Attributes[k] = v
  1673  		}
  1674  	}
  1675  	if d != nil {
  1676  		for k, diff := range d.CopyAttributes() {
  1677  			if diff.NewRemoved {
  1678  				delete(result.Attributes, k)
  1679  				continue
  1680  			}
  1681  			if diff.NewComputed {
  1682  				result.Attributes[k] = config.UnknownVariableValue
  1683  				continue
  1684  			}
  1685  
  1686  			result.Attributes[k] = diff.New
  1687  		}
  1688  	}
  1689  
  1690  	// Remove any now empty array, maps or sets because a parent structure
  1691  	// won't include these entries in the count value.
  1692  	isCount := regexp.MustCompile(`\.[%#]$`).MatchString
  1693  	var deleted []string
  1694  
  1695  	for k, v := range result.Attributes {
  1696  		if isCount(k) && v == "0" {
  1697  			delete(result.Attributes, k)
  1698  			deleted = append(deleted, k)
  1699  		}
  1700  	}
  1701  
  1702  	for _, k := range deleted {
  1703  		// Sanity check for invalid structures.
  1704  		// If we removed the primary count key, there should have been no
  1705  		// other keys left with this prefix.
  1706  
  1707  		// this must have a "#" or "%" which we need to remove
  1708  		base := k[:len(k)-1]
  1709  		for k, _ := range result.Attributes {
  1710  			if strings.HasPrefix(k, base) {
  1711  				panic(fmt.Sprintf("empty structure %q has entry %q", base, k))
  1712  			}
  1713  		}
  1714  	}
  1715  
  1716  	return result
  1717  }
  1718  
  1719  func (s *InstanceState) String() string {
  1720  	s.Lock()
  1721  	defer s.Unlock()
  1722  
  1723  	var buf bytes.Buffer
  1724  
  1725  	if s == nil || s.ID == "" {
  1726  		return "<not created>"
  1727  	}
  1728  
  1729  	buf.WriteString(fmt.Sprintf("ID = %s\n", s.ID))
  1730  
  1731  	attributes := s.Attributes
  1732  	attrKeys := make([]string, 0, len(attributes))
  1733  	for ak, _ := range attributes {
  1734  		if ak == "id" {
  1735  			continue
  1736  		}
  1737  
  1738  		attrKeys = append(attrKeys, ak)
  1739  	}
  1740  	sort.Strings(attrKeys)
  1741  
  1742  	for _, ak := range attrKeys {
  1743  		av := attributes[ak]
  1744  		buf.WriteString(fmt.Sprintf("%s = %s\n", ak, av))
  1745  	}
  1746  
  1747  	buf.WriteString(fmt.Sprintf("Tainted = %t\n", s.Tainted))
  1748  
  1749  	return buf.String()
  1750  }
  1751  
  1752  // EphemeralState is used for transient state that is only kept in-memory
  1753  type EphemeralState struct {
  1754  	// ConnInfo is used for the providers to export information which is
  1755  	// used to connect to the resource for provisioning. For example,
  1756  	// this could contain SSH or WinRM credentials.
  1757  	ConnInfo map[string]string `json:"-"`
  1758  
  1759  	// Type is used to specify the resource type for this instance. This is only
  1760  	// required for import operations (as documented). If the documentation
  1761  	// doesn't state that you need to set this, then don't worry about
  1762  	// setting it.
  1763  	Type string `json:"-"`
  1764  }
  1765  
  1766  func (e *EphemeralState) init() {
  1767  	if e.ConnInfo == nil {
  1768  		e.ConnInfo = make(map[string]string)
  1769  	}
  1770  }
  1771  
  1772  func (e *EphemeralState) DeepCopy() *EphemeralState {
  1773  	copy, err := copystructure.Config{Lock: true}.Copy(e)
  1774  	if err != nil {
  1775  		panic(err)
  1776  	}
  1777  
  1778  	return copy.(*EphemeralState)
  1779  }
  1780  
  1781  type jsonStateVersionIdentifier struct {
  1782  	Version int `json:"version"`
  1783  }
  1784  
  1785  // Check if this is a V0 format - the magic bytes at the start of the file
  1786  // should be "tfstate" if so. We no longer support upgrading this type of
  1787  // state but return an error message explaining to a user how they can
  1788  // upgrade via the 0.6.x series.
  1789  func testForV0State(buf *bufio.Reader) error {
  1790  	start, err := buf.Peek(len("tfstate"))
  1791  	if err != nil {
  1792  		return fmt.Errorf("Failed to check for magic bytes: %v", err)
  1793  	}
  1794  	if string(start) == "tfstate" {
  1795  		return fmt.Errorf("Terraform 0.7 no longer supports upgrading the binary state\n" +
  1796  			"format which was used prior to Terraform 0.3. Please upgrade\n" +
  1797  			"this state file using Terraform 0.6.16 prior to using it with\n" +
  1798  			"Terraform 0.7.")
  1799  	}
  1800  
  1801  	return nil
  1802  }
  1803  
  1804  // ErrNoState is returned by ReadState when the io.Reader contains no data
  1805  var ErrNoState = errors.New("no state")
  1806  
  1807  // ReadState reads a state structure out of a reader in the format that
  1808  // was written by WriteState.
  1809  func ReadState(src io.Reader) (*State, error) {
  1810  	buf := bufio.NewReader(src)
  1811  	if _, err := buf.Peek(1); err != nil {
  1812  		// the error is either io.EOF or "invalid argument", and both are from
  1813  		// an empty state.
  1814  		return nil, ErrNoState
  1815  	}
  1816  
  1817  	if err := testForV0State(buf); err != nil {
  1818  		return nil, err
  1819  	}
  1820  
  1821  	// If we are JSON we buffer the whole thing in memory so we can read it twice.
  1822  	// This is suboptimal, but will work for now.
  1823  	jsonBytes, err := ioutil.ReadAll(buf)
  1824  	if err != nil {
  1825  		return nil, fmt.Errorf("Reading state file failed: %v", err)
  1826  	}
  1827  
  1828  	versionIdentifier := &jsonStateVersionIdentifier{}
  1829  	if err := json.Unmarshal(jsonBytes, versionIdentifier); err != nil {
  1830  		return nil, fmt.Errorf("Decoding state file version failed: %v", err)
  1831  	}
  1832  
  1833  	var result *State
  1834  	switch versionIdentifier.Version {
  1835  	case 0:
  1836  		return nil, fmt.Errorf("State version 0 is not supported as JSON.")
  1837  	case 1:
  1838  		v1State, err := ReadStateV1(jsonBytes)
  1839  		if err != nil {
  1840  			return nil, err
  1841  		}
  1842  
  1843  		v2State, err := upgradeStateV1ToV2(v1State)
  1844  		if err != nil {
  1845  			return nil, err
  1846  		}
  1847  
  1848  		v3State, err := upgradeStateV2ToV3(v2State)
  1849  		if err != nil {
  1850  			return nil, err
  1851  		}
  1852  
  1853  		// increment the Serial whenever we upgrade state
  1854  		v3State.Serial++
  1855  		result = v3State
  1856  	case 2:
  1857  		v2State, err := ReadStateV2(jsonBytes)
  1858  		if err != nil {
  1859  			return nil, err
  1860  		}
  1861  		v3State, err := upgradeStateV2ToV3(v2State)
  1862  		if err != nil {
  1863  			return nil, err
  1864  		}
  1865  
  1866  		v3State.Serial++
  1867  		result = v3State
  1868  	case 3:
  1869  		v3State, err := ReadStateV3(jsonBytes)
  1870  		if err != nil {
  1871  			return nil, err
  1872  		}
  1873  
  1874  		result = v3State
  1875  	default:
  1876  		return nil, fmt.Errorf("Terraform %s does not support state version %d, please update.",
  1877  			SemVersion.String(), versionIdentifier.Version)
  1878  	}
  1879  
  1880  	// If we reached this place we must have a result set
  1881  	if result == nil {
  1882  		panic("resulting state in load not set, assertion failed")
  1883  	}
  1884  
  1885  	// Prune the state when read it. Its possible to write unpruned states or
  1886  	// for a user to make a state unpruned (nil-ing a module state for example).
  1887  	result.prune()
  1888  
  1889  	// Validate the state file is valid
  1890  	if err := result.Validate(); err != nil {
  1891  		return nil, err
  1892  	}
  1893  
  1894  	return result, nil
  1895  }
  1896  
  1897  func ReadStateV1(jsonBytes []byte) (*stateV1, error) {
  1898  	v1State := &stateV1{}
  1899  	if err := json.Unmarshal(jsonBytes, v1State); err != nil {
  1900  		return nil, fmt.Errorf("Decoding state file failed: %v", err)
  1901  	}
  1902  
  1903  	if v1State.Version != 1 {
  1904  		return nil, fmt.Errorf("Decoded state version did not match the decoder selection: "+
  1905  			"read %d, expected 1", v1State.Version)
  1906  	}
  1907  
  1908  	return v1State, nil
  1909  }
  1910  
  1911  func ReadStateV2(jsonBytes []byte) (*State, error) {
  1912  	state := &State{}
  1913  	if err := json.Unmarshal(jsonBytes, state); err != nil {
  1914  		return nil, fmt.Errorf("Decoding state file failed: %v", err)
  1915  	}
  1916  
  1917  	// Check the version, this to ensure we don't read a future
  1918  	// version that we don't understand
  1919  	if state.Version > StateVersion {
  1920  		return nil, fmt.Errorf("Terraform %s does not support state version %d, please update.",
  1921  			SemVersion.String(), state.Version)
  1922  	}
  1923  
  1924  	// Make sure the version is semantic
  1925  	if state.TFVersion != "" {
  1926  		if _, err := version.NewVersion(state.TFVersion); err != nil {
  1927  			return nil, fmt.Errorf(
  1928  				"State contains invalid version: %s\n\n"+
  1929  					"Terraform validates the version format prior to writing it. This\n"+
  1930  					"means that this is invalid of the state becoming corrupted through\n"+
  1931  					"some external means. Please manually modify the Terraform version\n"+
  1932  					"field to be a proper semantic version.",
  1933  				state.TFVersion)
  1934  		}
  1935  	}
  1936  
  1937  	// Sort it
  1938  	state.sort()
  1939  
  1940  	// catch any unitialized fields in the state
  1941  	state.init()
  1942  
  1943  	return state, nil
  1944  }
  1945  
  1946  func ReadStateV3(jsonBytes []byte) (*State, error) {
  1947  	state := &State{}
  1948  	if err := json.Unmarshal(jsonBytes, state); err != nil {
  1949  		return nil, fmt.Errorf("Decoding state file failed: %v", err)
  1950  	}
  1951  
  1952  	// Check the version, this to ensure we don't read a future
  1953  	// version that we don't understand
  1954  	if state.Version > StateVersion {
  1955  		return nil, fmt.Errorf("Terraform %s does not support state version %d, please update.",
  1956  			SemVersion.String(), state.Version)
  1957  	}
  1958  
  1959  	// Make sure the version is semantic
  1960  	if state.TFVersion != "" {
  1961  		if _, err := version.NewVersion(state.TFVersion); err != nil {
  1962  			return nil, fmt.Errorf(
  1963  				"State contains invalid version: %s\n\n"+
  1964  					"Terraform validates the version format prior to writing it. This\n"+
  1965  					"means that this is invalid of the state becoming corrupted through\n"+
  1966  					"some external means. Please manually modify the Terraform version\n"+
  1967  					"field to be a proper semantic version.",
  1968  				state.TFVersion)
  1969  		}
  1970  	}
  1971  
  1972  	// Sort it
  1973  	state.sort()
  1974  
  1975  	// catch any unitialized fields in the state
  1976  	state.init()
  1977  
  1978  	// Now we write the state back out to detect any changes in normaliztion.
  1979  	// If our state is now written out differently, bump the serial number to
  1980  	// prevent conflicts.
  1981  	var buf bytes.Buffer
  1982  	err := WriteState(state, &buf)
  1983  	if err != nil {
  1984  		return nil, err
  1985  	}
  1986  
  1987  	if !bytes.Equal(jsonBytes, buf.Bytes()) {
  1988  		log.Println("[INFO] state modified during read or write. incrementing serial number")
  1989  		state.Serial++
  1990  	}
  1991  
  1992  	return state, nil
  1993  }
  1994  
  1995  // WriteState writes a state somewhere in a binary format.
  1996  func WriteState(d *State, dst io.Writer) error {
  1997  	// Make sure it is sorted
  1998  	d.sort()
  1999  
  2000  	// make sure we have no uninitialized fields
  2001  	d.init()
  2002  
  2003  	// Ensure the version is set
  2004  	d.Version = StateVersion
  2005  
  2006  	// If the TFVersion is set, verify it. We used to just set the version
  2007  	// here, but this isn't safe since it changes the MD5 sum on some remote
  2008  	// state storage backends such as Atlas. We now leave it be if needed.
  2009  	if d.TFVersion != "" {
  2010  		if _, err := version.NewVersion(d.TFVersion); err != nil {
  2011  			return fmt.Errorf(
  2012  				"Error writing state, invalid version: %s\n\n"+
  2013  					"The Terraform version when writing the state must be a semantic\n"+
  2014  					"version.",
  2015  				d.TFVersion)
  2016  		}
  2017  	}
  2018  
  2019  	// Encode the data in a human-friendly way
  2020  	data, err := json.MarshalIndent(d, "", "    ")
  2021  	if err != nil {
  2022  		return fmt.Errorf("Failed to encode state: %s", err)
  2023  	}
  2024  
  2025  	// We append a newline to the data because MarshalIndent doesn't
  2026  	data = append(data, '\n')
  2027  
  2028  	// Write the data out to the dst
  2029  	if _, err := io.Copy(dst, bytes.NewReader(data)); err != nil {
  2030  		return fmt.Errorf("Failed to write state: %v", err)
  2031  	}
  2032  
  2033  	return nil
  2034  }
  2035  
  2036  // moduleStateSort implements sort.Interface to sort module states
  2037  type moduleStateSort []*ModuleState
  2038  
  2039  func (s moduleStateSort) Len() int {
  2040  	return len(s)
  2041  }
  2042  
  2043  func (s moduleStateSort) Less(i, j int) bool {
  2044  	a := s[i]
  2045  	b := s[j]
  2046  
  2047  	// If either is nil, then the nil one is "less" than
  2048  	if a == nil || b == nil {
  2049  		return a == nil
  2050  	}
  2051  
  2052  	// If the lengths are different, then the shorter one always wins
  2053  	if len(a.Path) != len(b.Path) {
  2054  		return len(a.Path) < len(b.Path)
  2055  	}
  2056  
  2057  	// Otherwise, compare lexically
  2058  	return strings.Join(a.Path, ".") < strings.Join(b.Path, ".")
  2059  }
  2060  
  2061  func (s moduleStateSort) Swap(i, j int) {
  2062  	s[i], s[j] = s[j], s[i]
  2063  }
  2064  
  2065  const stateValidateErrMultiModule = `
  2066  Multiple modules with the same path: %s
  2067  
  2068  This means that there are multiple entries in the "modules" field
  2069  in your state file that point to the same module. This will cause Terraform
  2070  to behave in unexpected and error prone ways and is invalid. Please back up
  2071  and modify your state file manually to resolve this.
  2072  `