diff --git a/state/inmem.go b/state/inmem.go new file mode 100644 index 000000000..82385a6df --- /dev/null +++ b/state/inmem.go @@ -0,0 +1,27 @@ +package state + +import ( + "github.com/hashicorp/terraform/terraform" +) + +// InmemState is an in-memory state storage. +type InmemState struct { + state *terraform.State +} + +func (s *InmemState) State() *terraform.State { + return s.state +} + +func (s *InmemState) RefreshState() error { + return nil +} + +func (s *InmemState) WriteState(state *terraform.State) error { + s.state = state + return nil +} + +func (s *InmemState) PersistState() error { + return nil +} diff --git a/state/inmem_test.go b/state/inmem_test.go new file mode 100644 index 000000000..885127122 --- /dev/null +++ b/state/inmem_test.go @@ -0,0 +1,16 @@ +package state + +import ( + "testing" +) + +func TestInmemState(t *testing.T) { + TestState(t, &InmemState{state: TestStateInitial()}) +} + +func TestInmemState_impl(t *testing.T) { + var _ StateReader = new(InmemState) + var _ StateWriter = new(InmemState) + var _ StatePersister = new(InmemState) + var _ StateRefresher = new(InmemState) +} diff --git a/state/local.go b/state/local.go index 355b9b53c..1840cae1a 100644 --- a/state/local.go +++ b/state/local.go @@ -2,6 +2,7 @@ package state import ( "os" + "path/filepath" "github.com/hashicorp/terraform/terraform" ) @@ -49,6 +50,11 @@ func (s *LocalState) WriteState(state *terraform.State) error { return err } + // Create all the directories + if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { + return err + } + f, err := os.Create(path) if err != nil { return err diff --git a/state/testing.go b/state/testing.go index 6b3f6b4ef..3ada81e40 100644 --- a/state/testing.go +++ b/state/testing.go @@ -26,11 +26,12 @@ func TestState(t *testing.T, s interface{}) { // current will track our current state current := TestStateInitial() - current.Serial++ // Check that the initial state is correct - if !reflect.DeepEqual(reader.State(), current) { - t.Fatalf("not initial: %#v\n\n%#v", reader.State(), current) + state := reader.State() + current.Serial = state.Serial + if !reflect.DeepEqual(state, current) { + t.Fatalf("not initial: %#v\n\n%#v", state, current) } // Write a new state and verify that we have it