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