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:
parent
afccf62e3e
commit
24f6d3fe98
|
@ -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 }
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue