remote: Avoid a panic by re-ordering cases

This commit is contained in:
Armon Dadgar 2014-10-08 12:05:26 -07:00 committed by Mitchell Hashimoto
parent 220d496f9c
commit 7febe57ae6
1 changed files with 17 additions and 11 deletions

View File

@ -293,20 +293,18 @@ func RefreshState(conf *terraform.RemoteState) (StateChangeResult, error) {
return StateChangeInit, nil return StateChangeInit, nil
case remoteState == nil && localState != nil: case remoteState == nil && localState != nil:
fallthrough
case remoteState.Serial < localState.Serial:
// User should probably do a push, nothing to do // User should probably do a push, nothing to do
return StateChangeLocalNewer, nil return StateChangeLocalNewer, nil
case remoteState != nil && localState == nil: case remoteState != nil && localState == nil:
fallthrough goto PERSIST
case remoteState.Serial < localState.Serial:
// User should probably do a push, nothing to do
return StateChangeLocalNewer, nil
case remoteState.Serial > localState.Serial: case remoteState.Serial > localState.Serial:
// Update the local state from the remote state goto PERSIST
if err := Persist(bytes.NewReader(payload.State)); err != nil {
return StateChangeNoop,
fmt.Errorf("Failed to persist state: %v", err)
}
return StateChangeUpdateLocal, nil
case remoteState.Serial == localState.Serial: case remoteState.Serial == localState.Serial:
// Check for a hash collision on the local/remote state // Check for a hash collision on the local/remote state
@ -321,10 +319,18 @@ func RefreshState(conf *terraform.RemoteState) (StateChangeResult, error) {
// requires a manual reconciliation. // requires a manual reconciliation.
return StateChangeConflict, nil return StateChangeConflict, nil
} }
} default:
// We should not reach this point // We should not reach this point
panic("Unhandled remote update case") panic("Unhandled remote update case")
}
PERSIST:
// Update the local state from the remote state
if err := Persist(bytes.NewReader(payload.State)); err != nil {
return StateChangeNoop,
fmt.Errorf("Failed to persist state: %v", err)
}
return StateChangeUpdateLocal, nil
} }
// PushState is used to read the local state and // PushState is used to read the local state and