Skip to content

Commit

Permalink
Merge pull request #2371 from hashicorp/b-fix-tf-show-with-remote-state
Browse files Browse the repository at this point in the history
core: fix `terraform show` with remote state
  • Loading branch information
phinze committed Jun 23, 2015
2 parents d36223a + cb5c256 commit 74fb179
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
12 changes: 5 additions & 7 deletions command/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"strings"

statelib "github.com/hashicorp/terraform/state"
"github.com/hashicorp/terraform/terraform"
)

Expand Down Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions command/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

"github.com/hashicorp/terraform/config/module"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 74fb179

Please sign in to comment.