Revert #7464 and allow an empty state
Revert back to using a nil state. The external usage of the state shoudl always check the Empty() method.
This commit is contained in:
parent
eb70dec2ec
commit
3622bfddd6
|
@ -2,6 +2,9 @@ package remote
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/state"
|
"github.com/hashicorp/terraform/state"
|
||||||
|
@ -46,8 +49,8 @@ func TestRemoteClient_noPayload(t *testing.T) {
|
||||||
s := &State{
|
s := &State{
|
||||||
Client: nilClient{},
|
Client: nilClient{},
|
||||||
}
|
}
|
||||||
if err := s.RefreshState(); err != ErrRemoteStateNotFound {
|
if err := s.RefreshState(); err != nil {
|
||||||
t.Fatal("expected ErrRemoteStateNotFound, got", err)
|
t.Fatal("error refreshing empty remote state")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,3 +62,72 @@ func (nilClient) Get() (*Payload, error) { return nil, nil }
|
||||||
func (c nilClient) Put([]byte) error { return nil }
|
func (c nilClient) Put([]byte) error { return nil }
|
||||||
|
|
||||||
func (c nilClient) Delete() error { return nil }
|
func (c nilClient) Delete() error { return nil }
|
||||||
|
|
||||||
|
// ensure that remote state can be properly initialized
|
||||||
|
func TestRemoteClient_stateInit(t *testing.T) {
|
||||||
|
localStateFile, err := ioutil.TempFile("", "tf")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// we need to remove the temp files so we recognize there's no local or
|
||||||
|
// remote state.
|
||||||
|
localStateFile.Close()
|
||||||
|
os.Remove(localStateFile.Name())
|
||||||
|
//defer os.Remove(localStateFile.Name())
|
||||||
|
fmt.Println("LOCAL:", localStateFile.Name())
|
||||||
|
|
||||||
|
local := &state.LocalState{
|
||||||
|
Path: localStateFile.Name(),
|
||||||
|
}
|
||||||
|
if err := local.RefreshState(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
localState := local.State()
|
||||||
|
|
||||||
|
fmt.Println("localState.Empty():", localState.Empty())
|
||||||
|
|
||||||
|
remoteStateFile, err := ioutil.TempFile("", "tf")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
remoteStateFile.Close()
|
||||||
|
os.Remove(remoteStateFile.Name())
|
||||||
|
//defer os.Remove(remoteStateFile.Name()
|
||||||
|
fmt.Println("LOCAL:", localStateFile.Name())
|
||||||
|
fmt.Println("REMOTE:", remoteStateFile.Name())
|
||||||
|
|
||||||
|
remoteClient := &FileClient{
|
||||||
|
Path: remoteStateFile.Name(),
|
||||||
|
}
|
||||||
|
|
||||||
|
durable := &State{
|
||||||
|
Client: remoteClient,
|
||||||
|
}
|
||||||
|
|
||||||
|
cache := &state.CacheState{
|
||||||
|
Cache: local,
|
||||||
|
Durable: durable,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := cache.RefreshState(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch cache.RefreshResult() {
|
||||||
|
|
||||||
|
// we should be "refreshing" the remote state to initialize it
|
||||||
|
case state.CacheRefreshLocalNewer:
|
||||||
|
// Write our local state out to the durable storage to start.
|
||||||
|
if err := cache.WriteState(localState); err != nil {
|
||||||
|
t.Fatal("Error preparing remote state:", err)
|
||||||
|
}
|
||||||
|
if err := cache.PersistState(); err != nil {
|
||||||
|
t.Fatal("Error preparing remote state:", err)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
|
||||||
|
t.Fatal("unexpected refresh result:", cache.RefreshResult())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -2,13 +2,10 @@ package remote
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrRemoteStateNotFound = errors.New("no remote state found")
|
|
||||||
|
|
||||||
// State implements the State interfaces in the state package to handle
|
// State implements the State interfaces in the state package to handle
|
||||||
// reading and writing the remote state. This State on its own does no
|
// reading and writing the remote state. This State on its own does no
|
||||||
// local caching so every persist will go to the remote storage and local
|
// local caching so every persist will go to the remote storage and local
|
||||||
|
@ -37,8 +34,9 @@ func (s *State) RefreshState() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// no remote state is OK
|
||||||
if payload == nil {
|
if payload == nil {
|
||||||
return ErrRemoteStateNotFound
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
state, err := terraform.ReadState(bytes.NewReader(payload.Data))
|
state, err := terraform.ReadState(bytes.NewReader(payload.Data))
|
||||||
|
|
Loading…
Reference in New Issue