Skip to content

Commit

Permalink
Revert #7464 and take allow an empty state
Browse files Browse the repository at this point in the history
The error for a nil remote state was showing up in other places. While
we could check for the error on all calls to Refresh(), this restores
the previous behavior, while preventing possible nil values in the
state.

The effect here is the same, because rather than the RefreshState
switching on

     cached != nil && durable == nil

it will switch on

    durable.Serial < cached.Serial

which sets the same value to RefreshResult().
  • Loading branch information
jbardin committed Jul 6, 2016
1 parent eb70dec commit 771fc03
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions command/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ func remoteState(

if refresh {
// Refresh the cache
// It's OK if we didn't find a remote state.
if err := cache.RefreshState(); err != nil {
return nil, errwrap.Wrapf(
"Error reloading remote state: {{err}}", err)
Expand Down
4 changes: 2 additions & 2 deletions state/remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func TestRemoteClient_noPayload(t *testing.T) {
s := &State{
Client: nilClient{},
}
if err := s.RefreshState(); err != ErrRemoteStateNotFound {
t.Fatal("expected ErrRemoteStateNotFound, got", err)
if err := s.RefreshState(); err != nil {
t.Fatal("error refreshing empty remote state")
}
}

Expand Down
12 changes: 8 additions & 4 deletions state/remote/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package remote

import (
"bytes"
"errors"

"github.com/hashicorp/terraform/terraform"
)

var ErrRemoteStateNotFound = errors.New("no remote state found")

// State implements the State interfaces in the state package to handle
// 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
Expand Down Expand Up @@ -38,7 +35,14 @@ func (s *State) RefreshState() error {
}

if payload == nil {
return ErrRemoteStateNotFound
// the response was empty, so treat it as an empty state
emptyState := terraform.NewState()
// this needs to be superceded by any other serial, incluing 0
emptyState.Serial = -1

s.state = emptyState
s.readState = emptyState
return nil
}

state, err := terraform.ReadState(bytes.NewReader(payload.Data))
Expand Down

0 comments on commit 771fc03

Please sign in to comment.