command: Refresh remote state when loading
This commit is contained in:
parent
dab47b0d48
commit
ba01e27026
|
@ -186,6 +186,26 @@ func (m *Meta) loadState() (*terraform.State, error) {
|
||||||
// Set the state if enabled
|
// Set the state if enabled
|
||||||
var state *terraform.State
|
var state *terraform.State
|
||||||
if localCache != nil {
|
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
|
state = localCache
|
||||||
m.useRemoteState = true
|
m.useRemoteState = true
|
||||||
}
|
}
|
||||||
|
|
|
@ -267,6 +267,11 @@ func TestMeta_loadState_remote(t *testing.T) {
|
||||||
|
|
||||||
s := terraform.NewState()
|
s := terraform.NewState()
|
||||||
s.Serial = 1000
|
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 {
|
if err := remote.PersistState(s); err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -228,6 +228,10 @@ func ReadLocalState() (*terraform.State, []byte, error) {
|
||||||
// the configuration for the remote endpoint, and update
|
// the configuration for the remote endpoint, and update
|
||||||
// the local state if necessary.
|
// the local state if necessary.
|
||||||
func RefreshState(conf *terraform.RemoteState) (StateChangeResult, error) {
|
func RefreshState(conf *terraform.RemoteState) (StateChangeResult, error) {
|
||||||
|
if conf == nil {
|
||||||
|
return StateChangeNoop, fmt.Errorf("Missing remote server configuration")
|
||||||
|
}
|
||||||
|
|
||||||
// Read the state from the server
|
// Read the state from the server
|
||||||
client := &remoteStateClient{conf: conf}
|
client := &remoteStateClient{conf: conf}
|
||||||
payload, err := client.GetState()
|
payload, err := client.GetState()
|
||||||
|
|
Loading…
Reference in New Issue