command: Refresh remote state when loading

This commit is contained in:
Armon Dadgar 2014-10-11 18:57:12 -07:00 committed by Mitchell Hashimoto
parent dab47b0d48
commit ba01e27026
3 changed files with 29 additions and 0 deletions

View File

@ -186,6 +186,26 @@ func (m *Meta) loadState() (*terraform.State, error) {
// Set the state if enabled
var state *terraform.State
if localCache != nil {
// Refresh the state
log.Printf("[INFO] Refreshing local state...")
changes, err := remote.RefreshState(localCache.Remote)
if err != nil {
return nil, fmt.Errorf("Failed to refresh state: %v", err)
}
switch changes {
case remote.StateChangeNoop:
case remote.StateChangeInit:
case remote.StateChangeLocalNewer:
case remote.StateChangeUpdateLocal:
// Reload the state since we've udpated
localCache, _, err = remote.ReadLocalState()
if err != nil {
return nil, fmt.Errorf("Error loading state: %s", err)
}
default:
return nil, fmt.Errorf("%s", changes)
}
state = localCache
m.useRemoteState = true
}

View File

@ -267,6 +267,11 @@ func TestMeta_loadState_remote(t *testing.T) {
s := terraform.NewState()
s.Serial = 1000
conf, srv := testRemoteState(t, s, 200)
s.Remote = conf
defer srv.Close()
s.Serial = 500
if err := remote.PersistState(s); err != nil {
t.Fatalf("err: %v", err)
}

View File

@ -228,6 +228,10 @@ func ReadLocalState() (*terraform.State, []byte, error) {
// the configuration for the remote endpoint, and update
// the local state if necessary.
func RefreshState(conf *terraform.RemoteState) (StateChangeResult, error) {
if conf == nil {
return StateChangeNoop, fmt.Errorf("Missing remote server configuration")
}
// Read the state from the server
client := &remoteStateClient{conf: conf}
payload, err := client.GetState()