Return an error when there's no remote state

When refreshing remote state, indicate when no state file was found with
an ErrRemoteStateNotFound error. This prevents us from inadvertantly
getting a nil state into a terraform.State where we assume there's
always a root module.
This commit is contained in:
James Bardin 2016-07-01 18:37:38 -04:00
parent afccf62e3e
commit 24f6d3fe98
2 changed files with 28 additions and 6 deletions

View File

@ -41,3 +41,21 @@ func testClient(t *testing.T, c Client) {
t.Fatalf("bad: %#v", p) t.Fatalf("bad: %#v", p)
} }
} }
func TestRemoteClient_noPayload(t *testing.T) {
s := &State{
Client: nilClient{},
}
if err := s.RefreshState(); err != ErrRemoteStateNotFound {
t.Fatal("expected ErrRemoteStateNotFound, got", err)
}
}
// nilClient returns nil for everything
type nilClient struct{}
func (nilClient) Get() (*Payload, error) { return nil, nil }
func (c nilClient) Put([]byte) error { return nil }
func (c nilClient) Delete() error { return nil }

View File

@ -2,10 +2,13 @@ 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
@ -34,12 +37,13 @@ func (s *State) RefreshState() error {
return err return err
} }
var state *terraform.State if payload == nil {
if payload != nil { return ErrRemoteStateNotFound
state, err = terraform.ReadState(bytes.NewReader(payload.Data)) }
if err != nil {
return err state, err := terraform.ReadState(bytes.NewReader(payload.Data))
} if err != nil {
return err
} }
s.state = state s.state = state