Merge pull request #2371 from hashicorp/b-fix-tf-show-with-remote-state

core: fix `terraform show` with remote state
This commit is contained in:
Paul Hinze 2015-06-23 15:01:10 -05:00
commit 74fb179127
2 changed files with 45 additions and 7 deletions

View File

@ -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

View File

@ -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)