terraform: support backends in the state

This commit is contained in:
Mitchell Hashimoto 2017-01-18 20:50:57 -08:00
parent ad7b063262
commit 523801dcc2
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
5 changed files with 57 additions and 0 deletions

View File

@ -313,6 +313,13 @@ func (c *Context) ShadowError() error {
return c.shadowErr
}
// State returns a copy of the current state associated with this context.
//
// This cannot safely be called in parallel with any other Context function.
func (c *Context) State() *State {
return c.state.DeepCopy()
}
// Interpolater returns an Interpolater built on a copy of the state
// that can be used to test interpolation values.
func (c *Context) Interpolater() *Interpolater {

View File

@ -20,6 +20,10 @@ func init() {
// Plan represents a single Terraform execution plan, which contains
// all the information necessary to make an infrastructure change.
//
// A plan has to contain basically the entire state of the world
// necessary to make a change: the state, diff, config, backend config, etc.
// This is so that it can run alone without any other data.
type Plan struct {
Diff *Diff
Module *module.Tree
@ -27,6 +31,9 @@ type Plan struct {
Vars map[string]interface{}
Targets []string
// Backend is the backend that this plan should use and store data with.
Backend *BackendState
once sync.Once
}

View File

@ -79,6 +79,11 @@ type State struct {
// pull and push state files from a remote storage endpoint.
Remote *RemoteState `json:"remote,omitempty"`
// Backend tracks the configuration for the backend in use with
// this state. This is used to track any changes in the backend
// configuration.
Backend *BackendState `json:"backend,omitempty"`
// Modules contains all the modules in a breadth-first order
Modules []*ModuleState `json:"modules"`
@ -779,6 +784,22 @@ func (s *State) String() string {
return strings.TrimSpace(buf.String())
}
// BackendState stores the configuration to connect to a remote backend.
type BackendState struct {
Type string `json:"type"` // Backend type
Config map[string]interface{} `json:"config"` // Backend raw config
// Hash is the hash code to uniquely identify the original source
// configuration. We use this to detect when there is a change in
// configuration even when "type" isn't changed.
Hash uint64 `json:"hash"`
}
// Empty returns true if BackendState has no state.
func (s *BackendState) Empty() bool {
return s == nil || s.Type == ""
}
// RemoteState is used to track the information about a remote
// state store that we push/pull state to.
type RemoteState struct {

View File

@ -256,6 +256,9 @@ func TestStateDeepCopy(t *testing.T) {
cases := []struct {
State *State
}{
// Nil
{nil},
// Version
{
&State{Version: 5},

19
terraform/testing.go Normal file
View File

@ -0,0 +1,19 @@
package terraform
import (
"os"
"testing"
)
// TestStateFile writes the given state to the path.
func TestStateFile(t *testing.T, path string, state *State) {
f, err := os.Create(path)
if err != nil {
t.Fatalf("err: %s", err)
}
defer f.Close()
if err := WriteState(state, f); err != nil {
t.Fatalf("err: %s", err)
}
}