Merge pull request #7464 from hashicorp/jbardin/GH-7455

core: Return an error when there's no remote state
This commit is contained in:
James Bardin 2016-07-05 08:59:22 -04:00 committed by GitHub
commit be2469a05c
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,13 +37,14 @@ 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)) }
state, err := terraform.ReadState(bytes.NewReader(payload.Data))
if err != nil { if err != nil {
return err return err
} }
}
s.state = state s.state = state
s.readState = state s.readState = state