From cb5c2567b30d48adc152195b4fcd70ee6ad594f9 Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Tue, 16 Jun 2015 12:26:04 -0500 Subject: [PATCH] core: fix `terraform show` with remote state fixes #1810 --- command/show.go | 12 +++++------- command/show_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/command/show.go b/command/show.go index cf7b6cfa6451..624ee659db63 100644 --- a/command/show.go +++ b/command/show.go @@ -6,7 +6,6 @@ import ( "os" "strings" - statelib "github.com/hashicorp/terraform/state" "github.com/hashicorp/terraform/terraform" ) @@ -66,16 +65,15 @@ func (c *ShowCommand) Run(args []string) int { stateErr = err } } - } else { - // We should use the default state if it exists. - stateStore := &statelib.LocalState{Path: DefaultStateFilename} - if err := stateStore.RefreshState(); err != nil { + stateOpts := c.StateOpts() + stateOpts.RemoteCacheOnly = true + result, err := State(stateOpts) + if err != nil { c.Ui.Error(fmt.Sprintf("Error reading state: %s", err)) return 1 } - - state = stateStore.State() + state = result.State.State() if state == nil { c.Ui.Output("No state.") return 0 diff --git a/command/show_test.go b/command/show_test.go index 245b24def1dd..eb73ebe2f441 100644 --- a/command/show_test.go +++ b/command/show_test.go @@ -4,6 +4,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" "testing" "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) { originalState := testState() statePath := testStateFile(t, originalState)