From c65bbc4f0ce58ca860da6e8365f11c86bd719561 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 24 Aug 2016 18:20:47 -0700 Subject: [PATCH] terraform: minor edits to reading state to prep for validation --- terraform/state.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/terraform/state.go b/terraform/state.go index 05d124e06..e3d3aacb3 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -1522,6 +1522,7 @@ func ReadState(src io.Reader) (*State, error) { return nil, fmt.Errorf("Decoding state file version failed: %v", err) } + var result *State switch versionIdentifier.Version { case 0: return nil, fmt.Errorf("State version 0 is not supported as JSON.") @@ -1543,7 +1544,7 @@ func ReadState(src io.Reader) (*State, error) { // increment the Serial whenever we upgrade state v3State.Serial++ - return v3State, nil + result = v3State case 2: v2State, err := ReadStateV2(jsonBytes) if err != nil { @@ -1555,18 +1556,25 @@ func ReadState(src io.Reader) (*State, error) { } v3State.Serial++ - return v3State, nil + result = v3State case 3: v3State, err := ReadStateV3(jsonBytes) if err != nil { return nil, err } - return v3State, nil + + result = v3State default: return nil, fmt.Errorf("Terraform %s does not support state version %d, please update.", SemVersion.String(), versionIdentifier.Version) } + // If we reached this place we must have a result set + if result == nil { + panic("resulting state in load not set, assertion failed") + } + + return result, nil } func ReadStateV1(jsonBytes []byte) (*stateV1, error) {