Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: fix terraform show with remote state #2371

Merged
merged 1 commit into from
Jun 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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