2014-05-28 22:56:43 +02:00
package terraform
2014-06-05 16:27:01 +02:00
import (
2014-09-18 03:13:38 +02:00
"bufio"
2014-09-16 23:26:27 +02:00
"bytes"
2014-09-18 03:13:38 +02:00
"encoding/json"
2014-06-19 01:38:08 +02:00
"fmt"
2014-06-19 05:54:22 +02:00
"io"
2014-09-18 03:24:53 +02:00
"log"
2014-09-16 21:34:08 +02:00
"reflect"
2014-09-16 23:26:27 +02:00
"sort"
2016-01-07 21:43:43 +01:00
"strconv"
2014-09-23 00:53:18 +02:00
"strings"
2014-09-16 21:12:15 +02:00
2016-03-11 20:07:54 +01:00
"github.com/hashicorp/go-version"
2014-09-16 21:12:15 +02:00
"github.com/hashicorp/terraform/config"
2014-06-05 16:27:01 +02:00
)
2014-09-16 23:26:27 +02:00
const (
2014-10-02 22:45:34 +02:00
// StateVersion is the current version for our state file
StateVersion = 1
2014-09-16 23:26:27 +02:00
)
2014-09-16 02:11:36 +02:00
// rootModulePath is the path of the root module
var rootModulePath = [ ] string { "root" }
2014-05-28 22:56:43 +02:00
// State keeps track of a snapshot state-of-the-world that Terraform
// can use to keep track of what real world resources it is actually
2014-09-16 00:31:17 +02:00
// managing. This is the latest format as of Terraform 0.3
2014-05-28 22:56:43 +02:00
type State struct {
2014-09-16 00:31:17 +02:00
// Version is the protocol version. Currently only "1".
Version int ` json:"version" `
2014-07-22 19:09:11 +02:00
2016-03-11 20:07:54 +01:00
// TFVersion is the version of Terraform that wrote this state.
TFVersion string ` json:"terraform_version,omitempty" `
2014-09-16 00:31:17 +02:00
// Serial is incremented on any operation that modifies
// the State file. It is used to detect potentially conflicting
// updates.
Serial int64 ` json:"serial" `
2014-07-07 08:03:51 +02:00
2014-09-30 22:29:50 +02:00
// Remote is used to track the metadata required to
// pull and push state files from a remote storage endpoint.
Remote * RemoteState ` json:"remote,omitempty" `
2014-09-16 00:31:17 +02:00
// Modules contains all the modules in a breadth-first order
Modules [ ] * ModuleState ` json:"modules" `
2014-07-07 08:03:51 +02:00
}
2014-10-01 00:39:24 +02:00
// NewState is used to initialize a blank state
func NewState ( ) * State {
s := & State { }
s . init ( )
return s
}
2014-09-23 23:57:17 +02:00
// Children returns the ModuleStates that are direct children of
// the given path. If the path is "root", for example, then children
// returned might be "root.child", but not "root.child.grandchild".
func ( s * State ) Children ( path [ ] string ) [ ] * ModuleState {
// TODO: test
result := make ( [ ] * ModuleState , 0 )
for _ , m := range s . Modules {
if len ( m . Path ) != len ( path ) + 1 {
continue
}
if ! reflect . DeepEqual ( path , m . Path [ : len ( path ) ] ) {
continue
}
result = append ( result , m )
}
return result
}
2014-09-23 23:15:40 +02:00
// AddModule adds the module with the given path to the state.
//
// This should be the preferred method to add module states since it
// allows us to optimize lookups later as well as control sorting.
2014-09-23 23:20:26 +02:00
func ( s * State ) AddModule ( path [ ] string ) * ModuleState {
2014-09-23 23:15:40 +02:00
m := & ModuleState { Path : path }
m . init ( )
s . Modules = append ( s . Modules , m )
2014-10-11 21:47:06 +02:00
s . sort ( )
2014-09-23 23:15:40 +02:00
return m
}
2014-09-16 02:11:36 +02:00
// ModuleByPath is used to lookup the module state for the given path.
2015-09-11 20:56:20 +02:00
// This should be the preferred lookup mechanism as it allows for future
2014-09-16 02:11:36 +02:00
// lookup optimizations.
func ( s * State ) ModuleByPath ( path [ ] string ) * ModuleState {
2014-09-17 01:21:09 +02:00
if s == nil {
return nil
}
2014-09-16 02:11:36 +02:00
for _ , mod := range s . Modules {
2014-09-17 01:55:10 +02:00
if mod . Path == nil {
panic ( "missing module path" )
}
2014-09-16 21:34:08 +02:00
if reflect . DeepEqual ( mod . Path , path ) {
2014-09-16 02:11:36 +02:00
return mod
}
}
return nil
}
2015-01-27 19:13:50 +01:00
// ModuleOrphans returns all the module orphans in this state by
// returning their full paths. These paths can be used with ModuleByPath
// to return the actual state.
func ( s * State ) ModuleOrphans ( path [ ] string , c * config . Config ) [ ] [ ] string {
2015-07-19 23:14:05 +02:00
// direct keeps track of what direct children we have both in our config
// and in our state. childrenKeys keeps track of what isn't an orphan.
direct := make ( map [ string ] struct { } )
2015-01-27 19:13:50 +01:00
childrenKeys := make ( map [ string ] struct { } )
2015-02-12 02:47:30 +01:00
if c != nil {
for _ , m := range c . Modules {
childrenKeys [ m . Name ] = struct { } { }
2015-07-19 23:14:05 +02:00
direct [ m . Name ] = struct { } { }
2015-02-12 02:47:30 +01:00
}
2015-01-27 19:13:50 +01:00
}
2015-07-19 22:41:57 +02:00
// Go over the direct children and find any that aren't in our keys.
2015-01-27 19:13:50 +01:00
var orphans [ ] [ ] string
for _ , m := range s . Children ( path ) {
2015-07-19 22:41:57 +02:00
key := m . Path [ len ( m . Path ) - 1 ]
2015-01-27 19:13:50 +01:00
2015-07-19 22:41:57 +02:00
// Record that we found this key as a direct child. We use this
// later to find orphan nested modules.
direct [ key ] = struct { } { }
2015-07-19 23:14:05 +02:00
// If we have a direct child still in our config, it is not an orphan
if _ , ok := childrenKeys [ key ] ; ok {
continue
}
2015-01-27 19:13:50 +01:00
orphans = append ( orphans , m . Path )
}
2015-07-19 22:41:57 +02:00
// Find the orphans that are nested...
for _ , m := range s . Modules {
// We only want modules that are at least grandchildren
if len ( m . Path ) < len ( path ) + 2 {
continue
}
// If it isn't part of our tree, continue
if ! reflect . DeepEqual ( path , m . Path [ : len ( path ) ] ) {
continue
}
// If we have the direct child, then just skip it.
2015-07-20 17:41:24 +02:00
key := m . Path [ len ( path ) ]
2015-07-19 22:41:57 +02:00
if _ , ok := direct [ key ] ; ok {
continue
}
core: fix bug detecting deeply nested module orphans
Context:
As part of building up a Plan, Terraform needs to detect "orphaned"
resources--resources which are present in the state but not in the
config. This happens when config for those resources is removed by the
user, making it Terraform's responsibility to destroy them.
Both state and config are organized by Module into a logical tree, so
the process of finding orphans involves checking for orphaned Resources
in the current module and for orphaned Modules, which themselves will
have all their Resources marked as orphans.
Bug:
In #3114 a problem was exposed where, given a module tree that looked
like this:
```
root
|
+-- parent (empty, except for sub-modules)
|
+-- child1 (1 resource)
|
+-- child2 (1 resource)
```
If `parent` was removed, a bunch of error messages would occur during
the plan. The root cause of this was duplicate orphans appearing for the
resources in child1 and child2.
Fix:
This turned out to be a bug in orphaned module detection. When looking
for deeply nested orphaned modules, root.parent was getting added twice
as an orphaned module to the graph.
Here, we add an additional check to prevent a double add, which
addresses this scenario properly.
Fixes #3114 (the Provisioner side of it was fixed in #4877)
2016-02-05 19:19:10 +01:00
orphanPath := m . Path [ : len ( path ) + 1 ]
// Don't double-add if we've already added this orphan (which can happen if
// there are multiple nested sub-modules that get orphaned together).
alreadyAdded := false
for _ , o := range orphans {
if reflect . DeepEqual ( o , orphanPath ) {
alreadyAdded = true
break
}
}
if alreadyAdded {
continue
}
2015-07-19 22:41:57 +02:00
// Add this orphan
core: fix bug detecting deeply nested module orphans
Context:
As part of building up a Plan, Terraform needs to detect "orphaned"
resources--resources which are present in the state but not in the
config. This happens when config for those resources is removed by the
user, making it Terraform's responsibility to destroy them.
Both state and config are organized by Module into a logical tree, so
the process of finding orphans involves checking for orphaned Resources
in the current module and for orphaned Modules, which themselves will
have all their Resources marked as orphans.
Bug:
In #3114 a problem was exposed where, given a module tree that looked
like this:
```
root
|
+-- parent (empty, except for sub-modules)
|
+-- child1 (1 resource)
|
+-- child2 (1 resource)
```
If `parent` was removed, a bunch of error messages would occur during
the plan. The root cause of this was duplicate orphans appearing for the
resources in child1 and child2.
Fix:
This turned out to be a bug in orphaned module detection. When looking
for deeply nested orphaned modules, root.parent was getting added twice
as an orphaned module to the graph.
Here, we add an additional check to prevent a double add, which
addresses this scenario properly.
Fixes #3114 (the Provisioner side of it was fixed in #4877)
2016-02-05 19:19:10 +01:00
orphans = append ( orphans , orphanPath )
2015-07-19 22:41:57 +02:00
}
2015-01-27 19:13:50 +01:00
return orphans
}
2015-02-22 19:35:26 +01:00
// Empty returns true if the state is empty.
func ( s * State ) Empty ( ) bool {
if s == nil {
return true
}
return len ( s . Modules ) == 0
}
2015-02-22 19:29:42 +01:00
// IsRemote returns true if State represents a state that exists and is
// remote.
func ( s * State ) IsRemote ( ) bool {
if s == nil {
return false
}
if s . Remote == nil {
return false
}
if s . Remote . Type == "" {
return false
}
return true
}
2016-03-30 18:29:20 +02:00
// Remove removes the item in the state at the given address, returning
// any errors that may have occurred.
//
// If the address references a module state or resource, it will delete
// all children as well. To check what will be deleted, use a StateFilter
// first.
func ( s * State ) Remove ( addr ... string ) error {
// Filter out what we need to delete
filter := & StateFilter { State : s }
results , err := filter . Filter ( addr ... )
if err != nil {
return err
}
// If we have no results, just exit early, we're not going to do anything.
// While what happens below is fairly fast, this is an important early
// exit since the prune below might modify the state more and we don't
// want to modify the state if we don't have to.
if len ( results ) == 0 {
return nil
}
// Go through each result and grab what we need
removed := make ( map [ interface { } ] struct { } )
for _ , r := range results {
// Convert the path to our own type
path := append ( [ ] string { "root" } , r . Path ... )
// If we removed this already, then ignore
if _ , ok := removed [ r . Value ] ; ok {
continue
}
// If we removed the parent already, then ignore
if r . Parent != nil {
if _ , ok := removed [ r . Parent . Value ] ; ok {
continue
}
}
// Add this to the removed list
removed [ r . Value ] = struct { } { }
switch v := r . Value . ( type ) {
case * ModuleState :
s . removeModule ( path , v )
case * ResourceState :
s . removeResource ( path , v )
case * InstanceState :
s . removeInstance ( path , r . Parent . Value . ( * ResourceState ) , v )
default :
return fmt . Errorf ( "unknown type to delete: %T" , r . Value )
}
}
// Prune since the removal functions often do the bare minimum to
// remove a thing and may leave around dangling empty modules, resources,
// etc. Prune will clean that all up.
s . prune ( )
return nil
}
func ( s * State ) removeModule ( path [ ] string , v * ModuleState ) {
for i , m := range s . Modules {
if m == v {
s . Modules , s . Modules [ len ( s . Modules ) - 1 ] = append ( s . Modules [ : i ] , s . Modules [ i + 1 : ] ... ) , nil
return
}
}
}
func ( s * State ) removeResource ( path [ ] string , v * ResourceState ) {
// Get the module this resource lives in. If it doesn't exist, we're done.
mod := s . ModuleByPath ( path )
if mod == nil {
return
}
// Find this resource. This is a O(N) lookup when if we had the key
// it could be O(1) but even with thousands of resources this shouldn't
// matter right now. We can easily up performance here when the time comes.
for k , r := range mod . Resources {
if r == v {
// Found it
delete ( mod . Resources , k )
return
}
}
}
func ( s * State ) removeInstance ( path [ ] string , r * ResourceState , v * InstanceState ) {
// Go through the resource and find the instance that matches this
// (if any) and remove it.
// Check primary
if r . Primary == v {
r . Primary = nil
return
}
// Check lists
lists := [ ] [ ] * InstanceState { r . Tainted , r . Deposed }
for _ , is := range lists {
for i , instance := range is {
if instance == v {
// Found it, remove it
is , is [ len ( is ) - 1 ] = append ( is [ : i ] , is [ i + 1 : ] ... ) , nil
// Done
return
}
}
}
}
2014-09-16 02:11:36 +02:00
// RootModule returns the ModuleState for the root module
func ( s * State ) RootModule ( ) * ModuleState {
2014-09-17 00:09:40 +02:00
root := s . ModuleByPath ( rootModulePath )
if root == nil {
panic ( "missing root module" )
}
return root
2014-09-16 02:11:36 +02:00
}
2015-02-20 22:39:49 +01:00
// Equal tests if one state is equal to another.
func ( s * State ) Equal ( other * State ) bool {
2015-02-24 06:26:33 +01:00
// If one is nil, we do a direct check
if s == nil || other == nil {
return s == other
}
2015-02-20 22:39:49 +01:00
// If the versions are different, they're certainly not equal
if s . Version != other . Version {
return false
}
// If any of the modules are not equal, then this state isn't equal
2015-02-24 06:43:54 +01:00
if len ( s . Modules ) != len ( other . Modules ) {
return false
}
2015-02-20 22:39:49 +01:00
for _ , m := range s . Modules {
// This isn't very optimal currently but works.
otherM := other . ModuleByPath ( m . Path )
if otherM == nil {
return false
}
// If they're not equal, then we're not equal!
if ! m . Equal ( otherM ) {
return false
}
}
return true
}
2015-02-24 06:32:27 +01:00
// DeepCopy performs a deep copy of the state structure and returns
// a new structure.
func ( s * State ) DeepCopy ( ) * State {
if s == nil {
return nil
}
n := & State {
2016-03-11 20:07:54 +01:00
Version : s . Version ,
TFVersion : s . TFVersion ,
Serial : s . Serial ,
Modules : make ( [ ] * ModuleState , 0 , len ( s . Modules ) ) ,
2015-02-24 06:32:27 +01:00
}
for _ , mod := range s . Modules {
n . Modules = append ( n . Modules , mod . deepcopy ( ) )
}
if s . Remote != nil {
n . Remote = s . Remote . deepcopy ( )
}
return n
}
2015-02-24 06:26:33 +01:00
// IncrementSerialMaybe increments the serial number of this state
// if it different from the other state.
func ( s * State ) IncrementSerialMaybe ( other * State ) {
2015-03-25 23:38:24 +01:00
if s == nil {
return
}
if other == nil {
return
}
if s . Serial > other . Serial {
return
}
2016-03-11 20:07:54 +01:00
if other . TFVersion != s . TFVersion || ! s . Equal ( other ) {
2015-03-25 23:40:53 +01:00
if other . Serial > s . Serial {
s . Serial = other . Serial
}
2015-02-24 06:26:33 +01:00
s . Serial ++
}
}
2016-03-11 20:07:54 +01:00
// FromFutureTerraform checks if this state was written by a Terraform
// version from the future.
func ( s * State ) FromFutureTerraform ( ) bool {
// No TF version means it is certainly from the past
if s . TFVersion == "" {
return false
}
v := version . Must ( version . NewVersion ( s . TFVersion ) )
return SemVersion . LessThan ( v )
}
2014-09-16 23:26:27 +02:00
func ( s * State ) init ( ) {
if s . Version == 0 {
2014-10-02 22:45:34 +02:00
s . Version = StateVersion
2014-09-16 23:26:27 +02:00
}
if len ( s . Modules ) == 0 {
2014-09-17 00:09:40 +02:00
root := & ModuleState {
Path : rootModulePath ,
}
2014-09-16 23:26:27 +02:00
root . init ( )
s . Modules = [ ] * ModuleState { root }
}
}
2014-09-16 02:06:32 +02:00
// prune is used to remove any resources that are no longer required
func ( s * State ) prune ( ) {
2014-09-20 01:15:52 +02:00
if s == nil {
return
}
2014-09-16 21:34:08 +02:00
for _ , mod := range s . Modules {
2014-09-16 02:06:32 +02:00
mod . prune ( )
}
2014-09-30 22:29:50 +02:00
if s . Remote != nil && s . Remote . Empty ( ) {
s . Remote = nil
}
2014-09-16 02:06:32 +02:00
}
2014-10-11 21:47:06 +02:00
// sort sorts the modules
func ( s * State ) sort ( ) {
sort . Sort ( moduleStateSort ( s . Modules ) )
2015-02-17 20:47:53 +01:00
// Allow modules to be sorted
for _ , m := range s . Modules {
m . sort ( )
}
2014-10-11 21:47:06 +02:00
}
2014-09-16 21:34:08 +02:00
func ( s * State ) GoString ( ) string {
return fmt . Sprintf ( "*%#v" , * s )
}
2014-09-16 23:26:27 +02:00
func ( s * State ) String ( ) string {
2015-02-26 19:29:23 +01:00
if s == nil {
return "<nil>"
}
2014-09-16 23:26:27 +02:00
var buf bytes . Buffer
2014-09-23 00:53:18 +02:00
for _ , m := range s . Modules {
mStr := m . String ( )
2014-09-16 23:26:27 +02:00
2014-09-23 00:53:18 +02:00
// If we're the root module, we just write the output directly.
if reflect . DeepEqual ( m . Path , rootModulePath ) {
2014-09-23 23:20:26 +02:00
buf . WriteString ( mStr + "\n" )
2014-09-23 00:53:18 +02:00
continue
2014-09-16 23:26:27 +02:00
}
2014-09-23 01:48:18 +02:00
buf . WriteString ( fmt . Sprintf ( "module.%s:\n" , strings . Join ( m . Path [ 1 : ] , "." ) ) )
2014-09-16 23:26:27 +02:00
2014-09-23 00:53:18 +02:00
s := bufio . NewScanner ( strings . NewReader ( mStr ) )
for s . Scan ( ) {
2015-02-20 18:40:41 +01:00
text := s . Text ( )
if text != "" {
text = " " + text
}
buf . WriteString ( fmt . Sprintf ( "%s\n" , text ) )
2014-09-16 23:26:27 +02:00
}
}
2014-09-23 00:53:18 +02:00
return strings . TrimSpace ( buf . String ( ) )
2014-09-16 23:26:27 +02:00
}
2014-09-30 22:29:50 +02:00
// RemoteState is used to track the information about a remote
// state store that we push/pull state to.
type RemoteState struct {
2014-12-04 03:59:23 +01:00
// Type controls the client we use for the remote state
Type string ` json:"type" `
2014-09-30 22:29:50 +02:00
2014-12-04 03:59:23 +01:00
// Config is used to store arbitrary configuration that
// is type specific
Config map [ string ] string ` json:"config" `
2014-09-30 22:29:50 +02:00
}
func ( r * RemoteState ) deepcopy ( ) * RemoteState {
2014-12-04 03:59:23 +01:00
confCopy := make ( map [ string ] string , len ( r . Config ) )
for k , v := range r . Config {
confCopy [ k ] = v
}
2014-09-30 22:29:50 +02:00
return & RemoteState {
2014-12-04 03:59:23 +01:00
Type : r . Type ,
Config : confCopy ,
2014-09-30 22:29:50 +02:00
}
}
func ( r * RemoteState ) Empty ( ) bool {
2015-02-22 19:35:26 +01:00
return r == nil || r . Type == ""
2014-09-30 22:29:50 +02:00
}
2014-10-01 00:39:24 +02:00
func ( r * RemoteState ) Equals ( other * RemoteState ) bool {
2014-12-04 03:59:23 +01:00
if r . Type != other . Type {
2014-10-01 00:39:24 +02:00
return false
}
2014-12-04 03:59:23 +01:00
if len ( r . Config ) != len ( other . Config ) {
2014-10-01 00:39:24 +02:00
return false
}
2014-12-04 03:59:23 +01:00
for k , v := range r . Config {
if other . Config [ k ] != v {
return false
}
2014-10-01 00:39:24 +02:00
}
return true
}
2015-03-27 00:55:18 +01:00
func ( r * RemoteState ) GoString ( ) string {
return fmt . Sprintf ( "*%#v" , * r )
}
2014-09-16 00:31:17 +02:00
// ModuleState is used to track all the state relevant to a single
// module. Previous to Terraform 0.3, all state belonged to the "root"
// module.
type ModuleState struct {
// Path is the import path from the root module. Modules imports are
// always disjoint, so the path represents amodule tree
Path [ ] string ` json:"path" `
// Outputs declared by the module and maintained for each module
// even though only the root module technically needs to be kept.
// This allows operators to inspect values at the boundaries.
Outputs map [ string ] string ` json:"outputs" `
// Resources is a mapping of the logically named resource to
// the state of the resource. Each resource may actually have
// N instances underneath, although a user only needs to think
// about the 1:1 case.
Resources map [ string ] * ResourceState ` json:"resources" `
2014-11-22 00:34:23 +01:00
// Dependencies are a list of things that this module relies on
// existing to remain intact. For example: an module may depend
// on a VPC ID given by an aws_vpc resource.
//
// Terraform uses this information to build valid destruction
// orders and to warn the user if they're destroying a module that
// another resource depends on.
//
// Things can be put into this list that may not be managed by
// Terraform. If Terraform doesn't find a matching ID in the
// overall state, then it assumes it isn't managed and doesn't
// worry about it.
Dependencies [ ] string ` json:"depends_on,omitempty" `
2014-07-13 20:07:31 +02:00
}
2015-02-20 22:39:49 +01:00
// Equal tests whether one module state is equal to another.
func ( m * ModuleState ) Equal ( other * ModuleState ) bool {
// Paths must be equal
if ! reflect . DeepEqual ( m . Path , other . Path ) {
return false
}
// Outputs must be equal
if len ( m . Outputs ) != len ( other . Outputs ) {
return false
}
for k , v := range m . Outputs {
if other . Outputs [ k ] != v {
return false
}
}
// Dependencies must be equal. This sorts these in place but
// this shouldn't cause any problems.
sort . Strings ( m . Dependencies )
sort . Strings ( other . Dependencies )
if len ( m . Dependencies ) != len ( other . Dependencies ) {
return false
}
for i , d := range m . Dependencies {
if other . Dependencies [ i ] != d {
return false
}
}
// Resources must be equal
if len ( m . Resources ) != len ( other . Resources ) {
return false
}
for k , r := range m . Resources {
otherR , ok := other . Resources [ k ]
if ! ok {
return false
}
if ! r . Equal ( otherR ) {
return false
}
}
return true
}
2014-09-26 04:25:10 +02:00
// IsRoot says whether or not this module diff is for the root module.
func ( m * ModuleState ) IsRoot ( ) bool {
return reflect . DeepEqual ( m . Path , rootModulePath )
}
2014-09-16 21:34:08 +02:00
// Orphans returns a list of keys of resources that are in the State
// but aren't present in the configuration itself. Hence, these keys
// represent the state of resources that are orphans.
func ( m * ModuleState ) Orphans ( c * config . Config ) [ ] string {
keys := make ( map [ string ] struct { } )
for k , _ := range m . Resources {
keys [ k ] = struct { } { }
}
2015-02-12 02:47:30 +01:00
if c != nil {
for _ , r := range c . Resources {
delete ( keys , r . Id ( ) )
2014-09-16 21:34:08 +02:00
2015-02-12 02:47:30 +01:00
for k , _ := range keys {
if strings . HasPrefix ( k , r . Id ( ) + "." ) {
delete ( keys , k )
}
2014-10-02 20:34:08 +02:00
}
2014-09-16 21:34:08 +02:00
}
}
result := make ( [ ] string , 0 , len ( keys ) )
for k , _ := range keys {
result = append ( result , k )
}
return result
}
2014-10-02 19:42:58 +02:00
// View returns a view with the given resource prefix.
func ( m * ModuleState ) View ( id string ) * ModuleState {
if m == nil {
return m
}
r := m . deepcopy ( )
for k , _ := range r . Resources {
if id == k || strings . HasPrefix ( k , id + "." ) {
continue
}
delete ( r . Resources , k )
}
return r
}
2014-09-16 21:12:15 +02:00
func ( m * ModuleState ) init ( ) {
if m . Outputs == nil {
2014-09-16 21:34:08 +02:00
m . Outputs = make ( map [ string ] string )
2014-09-16 21:12:15 +02:00
}
if m . Resources == nil {
m . Resources = make ( map [ string ] * ResourceState )
}
}
2014-09-16 01:08:13 +02:00
func ( m * ModuleState ) deepcopy ( ) * ModuleState {
2014-09-16 21:34:08 +02:00
if m == nil {
return nil
}
2014-09-16 01:08:13 +02:00
n := & ModuleState {
Path : make ( [ ] string , len ( m . Path ) ) ,
Outputs : make ( map [ string ] string , len ( m . Outputs ) ) ,
Resources : make ( map [ string ] * ResourceState , len ( m . Resources ) ) ,
}
copy ( n . Path , m . Path )
for k , v := range m . Outputs {
n . Outputs [ k ] = v
}
for k , v := range m . Resources {
n . Resources [ k ] = v . deepcopy ( )
}
return n
}
2014-09-16 02:06:32 +02:00
// prune is used to remove any resources that are no longer required
func ( m * ModuleState ) prune ( ) {
for k , v := range m . Resources {
v . prune ( )
2015-02-14 01:27:23 +01:00
2015-03-04 19:15:53 +01:00
if ( v . Primary == nil || v . Primary . ID == "" ) && len ( v . Tainted ) == 0 && len ( v . Deposed ) == 0 {
2014-09-16 02:06:32 +02:00
delete ( m . Resources , k )
}
}
2015-02-12 07:53:22 +01:00
for k , v := range m . Outputs {
if v == config . UnknownVariableValue {
delete ( m . Outputs , k )
}
}
2014-09-16 02:06:32 +02:00
}
2015-02-17 20:47:53 +01:00
func ( m * ModuleState ) sort ( ) {
for _ , v := range m . Resources {
v . sort ( )
}
}
2014-09-16 21:34:08 +02:00
func ( m * ModuleState ) GoString ( ) string {
return fmt . Sprintf ( "*%#v" , * m )
}
2014-09-23 00:53:18 +02:00
func ( m * ModuleState ) String ( ) string {
2014-11-25 04:18:52 +01:00
var buf bytes . Buffer
2014-09-23 00:53:18 +02:00
if len ( m . Resources ) == 0 {
2014-11-25 04:18:52 +01:00
buf . WriteString ( "<no state>" )
2014-09-23 00:53:18 +02:00
}
names := make ( [ ] string , 0 , len ( m . Resources ) )
for name , _ := range m . Resources {
names = append ( names , name )
}
sort . Strings ( names )
for _ , k := range names {
rs := m . Resources [ k ]
var id string
if rs . Primary != nil {
id = rs . Primary . ID
}
if id == "" {
id = "<not created>"
}
taintStr := ""
if len ( rs . Tainted ) > 0 {
taintStr = fmt . Sprintf ( " (%d tainted)" , len ( rs . Tainted ) )
}
2015-02-27 18:38:17 +01:00
deposedStr := ""
2015-03-04 19:15:53 +01:00
if len ( rs . Deposed ) > 0 {
deposedStr = fmt . Sprintf ( " (%d deposed)" , len ( rs . Deposed ) )
2015-02-27 18:38:17 +01:00
}
buf . WriteString ( fmt . Sprintf ( "%s:%s%s\n" , k , taintStr , deposedStr ) )
2014-09-23 00:53:18 +02:00
buf . WriteString ( fmt . Sprintf ( " ID = %s\n" , id ) )
2015-03-23 23:36:53 +01:00
if rs . Provider != "" {
buf . WriteString ( fmt . Sprintf ( " provider = %s\n" , rs . Provider ) )
}
2014-09-23 00:53:18 +02:00
var attributes map [ string ] string
if rs . Primary != nil {
attributes = rs . Primary . Attributes
}
attrKeys := make ( [ ] string , 0 , len ( attributes ) )
for ak , _ := range attributes {
if ak == "id" {
continue
}
attrKeys = append ( attrKeys , ak )
}
sort . Strings ( attrKeys )
for _ , ak := range attrKeys {
av := attributes [ ak ]
buf . WriteString ( fmt . Sprintf ( " %s = %s\n" , ak , av ) )
}
for idx , t := range rs . Tainted {
buf . WriteString ( fmt . Sprintf ( " Tainted ID %d = %s\n" , idx + 1 , t . ID ) )
}
2015-03-04 19:15:53 +01:00
for idx , t := range rs . Deposed {
buf . WriteString ( fmt . Sprintf ( " Deposed ID %d = %s\n" , idx + 1 , t . ID ) )
2015-02-27 18:38:17 +01:00
}
2014-09-23 00:53:18 +02:00
if len ( rs . Dependencies ) > 0 {
buf . WriteString ( fmt . Sprintf ( "\n Dependencies:\n" ) )
for _ , dep := range rs . Dependencies {
buf . WriteString ( fmt . Sprintf ( " %s\n" , dep ) )
}
}
}
if len ( m . Outputs ) > 0 {
buf . WriteString ( "\nOutputs:\n\n" )
ks := make ( [ ] string , 0 , len ( m . Outputs ) )
for k , _ := range m . Outputs {
ks = append ( ks , k )
}
sort . Strings ( ks )
for _ , k := range ks {
v := m . Outputs [ k ]
buf . WriteString ( fmt . Sprintf ( "%s = %s\n" , k , v ) )
}
}
return buf . String ( )
}
2016-01-07 21:43:43 +01:00
// ResourceStateKey is a structured representation of the key used for the
// ModuleState.Resources mapping
type ResourceStateKey struct {
Name string
Type string
Index int
}
// Equal determines whether two ResourceStateKeys are the same
func ( rsk * ResourceStateKey ) Equal ( other * ResourceStateKey ) bool {
if rsk == nil || other == nil {
return false
}
if rsk . Type != other . Type {
return false
}
if rsk . Name != other . Name {
return false
}
if rsk . Index != other . Index {
return false
}
return true
}
func ( rsk * ResourceStateKey ) String ( ) string {
if rsk == nil {
return ""
}
if rsk . Index == - 1 {
return fmt . Sprintf ( "%s.%s" , rsk . Type , rsk . Name )
}
return fmt . Sprintf ( "%s.%s.%d" , rsk . Type , rsk . Name , rsk . Index )
}
// ParseResourceStateKey accepts a key in the format used by
// ModuleState.Resources and returns a resource name and resource index. In the
// state, a resource has the format "type.name.index" or "type.name". In the
// latter case, the index is returned as -1.
func ParseResourceStateKey ( k string ) ( * ResourceStateKey , error ) {
parts := strings . Split ( k , "." )
if len ( parts ) < 2 || len ( parts ) > 3 {
return nil , fmt . Errorf ( "Malformed resource state key: %s" , k )
}
rsk := & ResourceStateKey {
Type : parts [ 0 ] ,
Name : parts [ 1 ] ,
Index : - 1 ,
}
if len ( parts ) == 3 {
index , err := strconv . Atoi ( parts [ 2 ] )
if err != nil {
return nil , fmt . Errorf ( "Malformed resource state key index: %s" , k )
}
rsk . Index = index
}
return rsk , nil
}
2014-09-16 00:31:17 +02:00
// ResourceState holds the state of a resource that is used so that
// a provider can find and manage an existing resource as well as for
// storing attributes that are used to populate variables of child
// resources.
//
// Attributes has attributes about the created resource that are
// queryable in interpolation: "${type.id.attr}"
//
// Extra is just extra data that a provider can return that we store
// for later, but is not exposed in any way to the user.
//
type ResourceState struct {
// This is filled in and managed by Terraform, and is the resource
// type itself such as "mycloud_instance". If a resource provider sets
// this value, it won't be persisted.
Type string ` json:"type" `
2014-06-24 23:58:57 +02:00
2014-09-16 00:31:17 +02:00
// Dependencies are a list of things that this resource relies on
// existing to remain intact. For example: an AWS instance might
// depend on a subnet (which itself might depend on a VPC, and so
// on).
//
// Terraform uses this information to build valid destruction
// orders and to warn the user if they're destroying a resource that
// another resource depends on.
//
// Things can be put into this list that may not be managed by
// Terraform. If Terraform doesn't find a matching ID in the
// overall state, then it assumes it isn't managed and doesn't
// worry about it.
Dependencies [ ] string ` json:"depends_on,omitempty" `
2014-09-16 02:49:31 +02:00
// Primary is the current active instance for this resource.
// It can be replaced but only after a successful creation.
// This is the instances on which providers will act.
Primary * InstanceState ` json:"primary" `
// Tainted is used to track any underlying instances that
// have been created but are in a bad or unknown state and
// need to be cleaned up subsequently. In the
// standard case, there is only at most a single instance.
2014-09-16 00:31:17 +02:00
// However, in pathological cases, it is possible for the number
2014-09-16 02:49:31 +02:00
// of instances to accumulate.
Tainted [ ] * InstanceState ` json:"tainted,omitempty" `
2015-02-27 18:38:17 +01:00
// Deposed is used in the mechanics of CreateBeforeDestroy: the existing
// Primary is Deposed to get it out of the way for the replacement Primary to
// be created by Apply. If the replacement Primary creates successfully, the
// Deposed instance is cleaned up. If there were problems creating the
2015-03-05 01:01:46 +01:00
// replacement, the instance remains in the Deposed list so it can be
// destroyed in a future run. Functionally, Deposed instances are very
// similar to Tainted instances in that Terraform is only tracking them in
// order to remember to destroy them.
2015-03-04 19:15:53 +01:00
Deposed [ ] * InstanceState ` json:"deposed,omitempty" `
2015-03-23 23:36:53 +01:00
// Provider is used when a resource is connected to a provider with an alias.
// If this string is empty, the resource is connected to the default provider,
// e.g. "aws_instance" goes with the "aws" provider.
// If the resource block contained a "provider" key, that value will be set here.
Provider string ` json:"provider,omitempty" `
2014-09-16 02:30:18 +02:00
}
2015-02-20 22:39:49 +01:00
// Equal tests whether two ResourceStates are equal.
func ( s * ResourceState ) Equal ( other * ResourceState ) bool {
if s . Type != other . Type {
return false
}
2015-03-23 23:36:53 +01:00
if s . Provider != other . Provider {
return false
}
2015-02-20 22:39:49 +01:00
// Dependencies must be equal
sort . Strings ( s . Dependencies )
sort . Strings ( other . Dependencies )
if len ( s . Dependencies ) != len ( other . Dependencies ) {
return false
}
for i , d := range s . Dependencies {
if other . Dependencies [ i ] != d {
return false
}
}
// States must be equal
if ! s . Primary . Equal ( other . Primary ) {
return false
}
// Tainted
taints := make ( map [ string ] * InstanceState )
for _ , t := range other . Tainted {
if t == nil {
continue
}
taints [ t . ID ] = t
}
for _ , t := range s . Tainted {
if t == nil {
continue
}
otherT , ok := taints [ t . ID ]
if ! ok {
return false
}
delete ( taints , t . ID )
if ! t . Equal ( otherT ) {
return false
}
}
// This means that we have stuff in other tainted that we don't
// have, so it is not equal.
if len ( taints ) > 0 {
return false
}
return true
}
2015-02-26 18:58:56 +01:00
// Taint takes the primary state and marks it as tainted. If there is no
// primary state, this does nothing.
func ( r * ResourceState ) Taint ( ) {
// If there is no primary, nothing to do
if r . Primary == nil {
return
}
// Shuffle to the end of the taint list and set primary to nil
r . Tainted = append ( r . Tainted , r . Primary )
r . Primary = nil
}
2016-03-08 21:37:34 +01:00
// Untaint takes a tainted InstanceState and marks it as primary.
// The index argument is used to select a single InstanceState from the
// array of Tainted when there are more than one. If index is -1, the
// first Tainted InstanceState will be untainted iff there is only one
// Tainted InstanceState. Index must be >= 0 to specify an InstanceState
// when Tainted has more than one member.
func ( r * ResourceState ) Untaint ( index int ) error {
if len ( r . Tainted ) == 0 {
return fmt . Errorf ( "Nothing to untaint." )
}
if r . Primary != nil {
return fmt . Errorf ( "Resource has a primary instance in the state that would be overwritten by untainting. If you want to restore a tainted resource to primary, taint the existing primary instance first." )
}
if index == - 1 && len ( r . Tainted ) > 1 {
return fmt . Errorf ( "There are %d tainted instances for this resource, please specify an index to select which one to untaint." , len ( r . Tainted ) )
}
if index == - 1 {
index = 0
}
if index >= len ( r . Tainted ) {
return fmt . Errorf ( "There are %d tainted instances for this resource, the index specified (%d) is out of range." , len ( r . Tainted ) , index )
}
// Perform the untaint
r . Primary = r . Tainted [ index ]
r . Tainted = append ( r . Tainted [ : index ] , r . Tainted [ index + 1 : ] ... )
return nil
}
2014-09-16 21:12:15 +02:00
func ( r * ResourceState ) init ( ) {
2014-09-16 21:34:08 +02:00
if r . Primary == nil {
r . Primary = & InstanceState { }
2014-09-16 21:12:15 +02:00
}
2014-09-17 01:21:09 +02:00
r . Primary . init ( )
2014-09-16 21:12:15 +02:00
}
2014-09-16 01:08:13 +02:00
func ( r * ResourceState ) deepcopy ( ) * ResourceState {
2014-09-16 21:34:08 +02:00
if r == nil {
return nil
}
2015-02-26 18:58:56 +01:00
2014-09-16 01:08:13 +02:00
n := & ResourceState {
Type : r . Type ,
2015-02-26 18:58:56 +01:00
Dependencies : nil ,
2014-09-16 21:34:08 +02:00
Primary : r . Primary . deepcopy ( ) ,
2015-02-26 18:58:56 +01:00
Tainted : nil ,
2015-03-23 23:36:53 +01:00
Provider : r . Provider ,
2014-09-16 01:08:13 +02:00
}
2015-02-26 18:58:56 +01:00
if r . Dependencies != nil {
n . Dependencies = make ( [ ] string , len ( r . Dependencies ) )
copy ( n . Dependencies , r . Dependencies )
2014-09-16 01:08:13 +02:00
}
2015-02-26 18:58:56 +01:00
if r . Tainted != nil {
n . Tainted = make ( [ ] * InstanceState , 0 , len ( r . Tainted ) )
for _ , inst := range r . Tainted {
n . Tainted = append ( n . Tainted , inst . deepcopy ( ) )
}
}
2015-02-27 18:38:17 +01:00
if r . Deposed != nil {
2015-03-04 19:15:53 +01:00
n . Deposed = make ( [ ] * InstanceState , 0 , len ( r . Deposed ) )
for _ , inst := range r . Deposed {
n . Deposed = append ( n . Deposed , inst . deepcopy ( ) )
}
2015-02-27 18:38:17 +01:00
}
2015-02-26 18:58:56 +01:00
2014-09-16 01:08:13 +02:00
return n
}
2014-09-16 02:06:32 +02:00
// prune is used to remove any instances that are no longer required
func ( r * ResourceState ) prune ( ) {
2014-09-16 21:34:08 +02:00
n := len ( r . Tainted )
2014-09-16 02:06:32 +02:00
for i := 0 ; i < n ; i ++ {
2014-09-16 21:34:08 +02:00
inst := r . Tainted [ i ]
2015-02-12 23:46:22 +01:00
if inst == nil || inst . ID == "" {
2014-09-16 21:34:08 +02:00
copy ( r . Tainted [ i : ] , r . Tainted [ i + 1 : ] )
r . Tainted [ n - 1 ] = nil
2014-09-16 02:06:32 +02:00
n --
2015-02-14 01:27:23 +01:00
i --
2014-09-16 02:06:32 +02:00
}
}
2015-02-14 01:27:23 +01:00
2014-09-16 21:34:08 +02:00
r . Tainted = r . Tainted [ : n ]
2015-02-27 18:38:17 +01:00
2015-03-04 19:15:53 +01:00
n = len ( r . Deposed )
for i := 0 ; i < n ; i ++ {
inst := r . Deposed [ i ]
if inst == nil || inst . ID == "" {
copy ( r . Deposed [ i : ] , r . Deposed [ i + 1 : ] )
r . Deposed [ n - 1 ] = nil
n --
i --
}
2015-02-27 18:38:17 +01:00
}
2015-03-04 19:15:53 +01:00
r . Deposed = r . Deposed [ : n ]
2014-09-16 21:34:08 +02:00
}
2015-02-17 20:47:53 +01:00
func ( r * ResourceState ) sort ( ) {
sort . Strings ( r . Dependencies )
}
2014-09-16 21:34:08 +02:00
func ( s * ResourceState ) GoString ( ) string {
return fmt . Sprintf ( "*%#v" , * s )
2014-09-16 02:06:32 +02:00
}
2014-09-20 06:28:13 +02:00
func ( s * ResourceState ) String ( ) string {
var buf bytes . Buffer
buf . WriteString ( fmt . Sprintf ( "Type = %s" , s . Type ) )
return buf . String ( )
}
2014-09-16 00:31:17 +02:00
// InstanceState is used to track the unique state information belonging
// to a given instance.
type InstanceState struct {
// A unique ID for this resource. This is opaque to Terraform
// and is only meant as a lookup mechanism for the providers.
ID string ` json:"id" `
2014-07-05 00:36:28 +02:00
2014-09-16 00:31:17 +02:00
// Attributes are basic information about the resource. Any keys here
// are accessible in variable format within Terraform configurations:
// ${resourcetype.name.attribute}.
Attributes map [ string ] string ` json:"attributes,omitempty" `
2014-07-10 21:01:26 +02:00
2014-09-16 00:31:17 +02:00
// Ephemeral is used to store any state associated with this instance
// that is necessary for the Terraform run to complete, but is not
// persisted to a state file.
Ephemeral EphemeralState ` json:"-" `
2015-03-06 02:04:04 +01:00
// Meta is a simple K/V map that is persisted to the State but otherwise
// ignored by Terraform core. It's meant to be used for accounting by
// external client code.
Meta map [ string ] string ` json:"meta,omitempty" `
2014-07-10 21:01:26 +02:00
}
2014-09-16 21:12:15 +02:00
func ( i * InstanceState ) init ( ) {
if i . Attributes == nil {
i . Attributes = make ( map [ string ] string )
}
2015-03-19 01:07:29 +01:00
if i . Meta == nil {
i . Meta = make ( map [ string ] string )
}
2014-09-16 21:12:15 +02:00
i . Ephemeral . init ( )
}
2014-09-16 01:08:13 +02:00
func ( i * InstanceState ) deepcopy ( ) * InstanceState {
2014-09-16 21:34:08 +02:00
if i == nil {
return nil
}
2014-09-16 01:08:13 +02:00
n := & InstanceState {
2014-09-20 01:15:52 +02:00
ID : i . ID ,
Ephemeral : * i . Ephemeral . deepcopy ( ) ,
2014-09-16 01:08:13 +02:00
}
2014-09-20 01:15:52 +02:00
if i . Attributes != nil {
n . Attributes = make ( map [ string ] string , len ( i . Attributes ) )
for k , v := range i . Attributes {
n . Attributes [ k ] = v
}
2014-09-16 01:08:13 +02:00
}
2015-03-19 01:07:29 +01:00
if i . Meta != nil {
n . Meta = make ( map [ string ] string , len ( i . Meta ) )
for k , v := range i . Meta {
n . Meta [ k ] = v
}
}
2014-09-16 01:08:13 +02:00
return n
}
2015-03-26 18:11:26 +01:00
func ( s * InstanceState ) Empty ( ) bool {
return s == nil || s . ID == ""
}
2015-02-20 22:39:49 +01:00
func ( s * InstanceState ) Equal ( other * InstanceState ) bool {
// Short circuit some nil checks
if s == nil || other == nil {
return s == other
}
// IDs must be equal
if s . ID != other . ID {
return false
}
// Attributes must be equal
if len ( s . Attributes ) != len ( other . Attributes ) {
return false
}
for k , v := range s . Attributes {
otherV , ok := other . Attributes [ k ]
if ! ok {
return false
}
if v != otherV {
return false
}
}
2015-10-20 19:28:12 +02:00
// Meta must be equal
if len ( s . Meta ) != len ( other . Meta ) {
return false
}
for k , v := range s . Meta {
otherV , ok := other . Meta [ k ]
if ! ok {
return false
}
if v != otherV {
return false
}
}
2015-02-20 22:39:49 +01:00
return true
}
2014-09-17 01:20:11 +02:00
// MergeDiff takes a ResourceDiff and merges the attributes into
// this resource state in order to generate a new state. This new
// state can be used to provide updated attribute lookups for
// variable interpolation.
//
// If the diff attribute requires computing the value, and hence
// won't be available until apply, the value is replaced with the
// computeID.
2014-09-18 01:33:24 +02:00
func ( s * InstanceState ) MergeDiff ( d * InstanceDiff ) * InstanceState {
2014-09-18 02:18:03 +02:00
result := s . deepcopy ( )
if result == nil {
result = new ( InstanceState )
2014-09-17 01:20:11 +02:00
}
2014-09-20 01:24:17 +02:00
result . init ( )
2014-09-17 01:20:11 +02:00
if s != nil {
for k , v := range s . Attributes {
result . Attributes [ k ] = v
}
}
if d != nil {
for k , diff := range d . Attributes {
if diff . NewRemoved {
delete ( result . Attributes , k )
continue
}
if diff . NewComputed {
result . Attributes [ k ] = config . UnknownVariableValue
continue
}
result . Attributes [ k ] = diff . New
}
}
2014-09-18 02:18:03 +02:00
return result
2014-09-17 01:20:11 +02:00
}
2014-09-16 21:34:08 +02:00
func ( i * InstanceState ) GoString ( ) string {
return fmt . Sprintf ( "*%#v" , * i )
}
2014-09-18 19:40:23 +02:00
func ( i * InstanceState ) String ( ) string {
var buf bytes . Buffer
2014-09-20 06:28:13 +02:00
if i == nil || i . ID == "" {
2014-09-18 19:40:23 +02:00
return "<not created>"
}
buf . WriteString ( fmt . Sprintf ( "ID = %s\n" , i . ID ) )
attributes := i . Attributes
attrKeys := make ( [ ] string , 0 , len ( attributes ) )
for ak , _ := range attributes {
if ak == "id" {
continue
}
attrKeys = append ( attrKeys , ak )
}
sort . Strings ( attrKeys )
for _ , ak := range attrKeys {
av := attributes [ ak ]
buf . WriteString ( fmt . Sprintf ( "%s = %s\n" , ak , av ) )
}
return buf . String ( )
}
2014-09-16 00:31:17 +02:00
// EphemeralState is used for transient state that is only kept in-memory
type EphemeralState struct {
// ConnInfo is used for the providers to export information which is
// used to connect to the resource for provisioning. For example,
// this could contain SSH or WinRM credentials.
ConnInfo map [ string ] string ` json:"-" `
2014-07-10 21:01:26 +02:00
}
2014-09-16 02:49:31 +02:00
func ( e * EphemeralState ) init ( ) {
if e . ConnInfo == nil {
e . ConnInfo = make ( map [ string ] string )
}
}
2014-09-16 01:08:13 +02:00
func ( e * EphemeralState ) deepcopy ( ) * EphemeralState {
2014-09-16 21:34:08 +02:00
if e == nil {
return nil
}
2014-09-20 01:15:52 +02:00
n := & EphemeralState { }
if e . ConnInfo != nil {
n . ConnInfo = make ( map [ string ] string , len ( e . ConnInfo ) )
for k , v := range e . ConnInfo {
n . ConnInfo [ k ] = v
}
2014-09-16 01:08:13 +02:00
}
return n
}
2014-06-19 05:54:22 +02:00
// ReadState reads a state structure out of a reader in the format that
// was written by WriteState.
func ReadState ( src io . Reader ) ( * State , error ) {
2014-09-18 03:13:38 +02:00
buf := bufio . NewReader ( src )
// Check if this is a V1 format
start , err := buf . Peek ( len ( stateFormatMagic ) )
if err != nil {
return nil , fmt . Errorf ( "Failed to check for magic bytes: %v" , err )
}
if string ( start ) == stateFormatMagic {
// Read the old state
2014-09-18 03:24:53 +02:00
old , err := ReadStateV1 ( buf )
2014-09-18 03:13:38 +02:00
if err != nil {
return nil , err
}
2014-09-18 03:24:53 +02:00
return upgradeV1State ( old )
2014-09-18 03:13:38 +02:00
}
// Otherwise, must be V2
dec := json . NewDecoder ( buf )
state := & State { }
if err := dec . Decode ( state ) ; err != nil {
return nil , fmt . Errorf ( "Decoding state file failed: %v" , err )
}
2014-09-18 22:43:26 +02:00
// Check the version, this to ensure we don't read a future
// version that we don't understand
2014-10-02 22:45:34 +02:00
if state . Version > StateVersion {
2014-09-18 22:43:26 +02:00
return nil , fmt . Errorf ( "State version %d not supported, please update." ,
state . Version )
}
2014-10-11 21:47:06 +02:00
2016-03-11 20:07:54 +01:00
// Make sure the version is semantic
if state . TFVersion != "" {
if _ , err := version . NewVersion ( state . TFVersion ) ; err != nil {
return nil , fmt . Errorf (
"State contains invalid version: %s\n\n" +
"Terraform validates the version format prior to writing it. This\n" +
"means that this is invalid of the state becoming corrupted through\n" +
"some external means. Please manually modify the Terraform version\n" +
"field to be a proper semantic version." ,
state . TFVersion )
}
}
2014-10-11 21:47:06 +02:00
// Sort it
state . sort ( )
2014-09-18 03:13:38 +02:00
return state , nil
2014-06-19 05:54:22 +02:00
}
// WriteState writes a state somewhere in a binary format.
func WriteState ( d * State , dst io . Writer ) error {
2014-10-11 21:47:06 +02:00
// Make sure it is sorted
d . sort ( )
2014-09-18 22:38:36 +02:00
// Ensure the version is set
2014-10-02 22:45:34 +02:00
d . Version = StateVersion
2014-09-18 22:38:36 +02:00
2016-03-11 20:07:54 +01:00
// If the TFVersion is set, verify it. We used to just set the version
// here, but this isn't safe since it changes the MD5 sum on some remote
// state storage backends such as Atlas. We now leave it be if needed.
if d . TFVersion != "" {
if _ , err := version . NewVersion ( d . TFVersion ) ; err != nil {
return fmt . Errorf (
"Error writing state, invalid version: %s\n\n" +
"The Terraform version when writing the state must be a semantic\n" +
"version." ,
d . TFVersion )
}
}
2014-10-13 07:56:34 +02:00
// Encode the data in a human-friendly way
data , err := json . MarshalIndent ( d , "" , " " )
if err != nil {
return fmt . Errorf ( "Failed to encode state: %s" , err )
}
// We append a newline to the data because MarshalIndent doesn't
data = append ( data , '\n' )
// Write the data out to the dst
if _ , err := io . Copy ( dst , bytes . NewReader ( data ) ) ; err != nil {
2014-09-18 03:13:38 +02:00
return fmt . Errorf ( "Failed to write state: %v" , err )
}
2014-10-13 07:56:34 +02:00
2014-09-16 21:34:08 +02:00
return nil
2014-06-19 05:54:22 +02:00
}
2014-09-18 03:24:53 +02:00
// upgradeV1State is used to upgrade a V1 state representation
// into a proper State representation.
func upgradeV1State ( old * StateV1 ) ( * State , error ) {
s := & State { }
s . init ( )
// Old format had no modules, so we migrate everything
// directly into the root module.
root := s . RootModule ( )
// Copy the outputs
root . Outputs = old . Outputs
// Upgrade the resources
for id , rs := range old . Resources {
newRs := & ResourceState {
Type : rs . Type ,
}
root . Resources [ id ] = newRs
// Migrate to an instance state
instance := & InstanceState {
ID : rs . ID ,
Attributes : rs . Attributes ,
}
// Check if this is the primary or tainted instance
if _ , ok := old . Tainted [ id ] ; ok {
newRs . Tainted = append ( newRs . Tainted , instance )
} else {
newRs . Primary = instance
}
// Warn if the resource uses Extra, as there is
// no upgrade path for this! Now totally deprecated.
if len ( rs . Extra ) > 0 {
2014-09-22 07:36:34 +02:00
log . Printf (
"[WARN] Resource %s uses deprecated attribute " +
"storage, state file upgrade may be incomplete." ,
rs . ID ,
)
2014-09-18 03:24:53 +02:00
}
}
return s , nil
}
2014-10-11 21:47:06 +02:00
// moduleStateSort implements sort.Interface to sort module states
type moduleStateSort [ ] * ModuleState
func ( s moduleStateSort ) Len ( ) int {
return len ( s )
}
func ( s moduleStateSort ) Less ( i , j int ) bool {
a := s [ i ]
b := s [ j ]
// If the lengths are different, then the shorter one always wins
if len ( a . Path ) != len ( b . Path ) {
return len ( a . Path ) < len ( b . Path )
}
2015-10-20 21:33:28 +02:00
// Otherwise, compare lexically
return strings . Join ( a . Path , "." ) < strings . Join ( b . Path , "." )
2014-10-11 21:47:06 +02:00
}
func ( s moduleStateSort ) Swap ( i , j int ) {
s [ i ] , s [ j ] = s [ j ] , s [ i ]
}