core: fix `terraform show` with remote state

fixes #1810
This commit is contained in:
Paul Hinze 2015-06-16 12:26:04 -05:00
parent e514e16215
commit cb5c2567b3
2 changed files with 45 additions and 7 deletions

View File

@ -6,7 +6,6 @@ import (
"os" "os"
"strings" "strings"
statelib "github.com/hashicorp/terraform/state"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
@ -66,16 +65,15 @@ func (c *ShowCommand) Run(args []string) int {
stateErr = err stateErr = err
} }
} }
} else { } else {
// We should use the default state if it exists. stateOpts := c.StateOpts()
stateStore := &statelib.LocalState{Path: DefaultStateFilename} stateOpts.RemoteCacheOnly = true
if err := stateStore.RefreshState(); err != nil { result, err := State(stateOpts)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error reading state: %s", err)) c.Ui.Error(fmt.Sprintf("Error reading state: %s", err))
return 1 return 1
} }
state = result.State.State()
state = stateStore.State()
if state == nil { if state == nil {
c.Ui.Output("No state.") c.Ui.Output("No state.")
return 0 return 0

View File

@ -4,6 +4,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/config/module"
@ -124,6 +125,45 @@ func TestShow_plan(t *testing.T) {
} }
} }
func TestShow_noArgsRemoteState(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
// Pretend like we have a local cache of remote state
remoteStatePath := filepath.Join(tmp, DefaultDataDir, DefaultStateFilename)
if err := os.MkdirAll(filepath.Dir(remoteStatePath), 0755); err != nil {
t.Fatalf("err: %s", err)
}
f, err := os.Create(remoteStatePath)
if err != nil {
t.Fatalf("err: %s", err)
}
err = terraform.WriteState(testState(), f)
f.Close()
if err != nil {
t.Fatalf("err: %s", err)
}
ui := new(cli.MockUi)
c := &ShowCommand{
Meta: Meta{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
},
}
args := []string{}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
}
expected := "test_instance.foo"
actual := ui.OutputWriter.String()
if !strings.Contains(actual, expected) {
t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
}
}
func TestShow_state(t *testing.T) { func TestShow_state(t *testing.T) {
originalState := testState() originalState := testState()
statePath := testStateFile(t, originalState) statePath := testStateFile(t, originalState)