From 0e4e94a86f883b762dc39033cf02bbcadb63dbb3 Mon Sep 17 00:00:00 2001 From: James Nugent Date: Fri, 29 Jul 2016 14:13:50 -0500 Subject: [PATCH] core: Fix -module for terraform output command The behaviour whereby outputs for a particular nested module can be output was broken by the changes for lists and maps. This commit restores the previous behaviour by passing the module path into the outputsAsString function. We also add a new test of this since the code path for indivdual output vs all outputs for a module has diverged. --- command/apply.go | 6 +++--- command/output.go | 2 +- command/output_test.go | 49 ++++++++++++++++++++++++++++++++++++++++++ command/refresh.go | 4 +++- terraform/state.go | 3 +-- 5 files changed, 57 insertions(+), 7 deletions(-) diff --git a/command/apply.go b/command/apply.go index 83ae29a7f..659161aee 100644 --- a/command/apply.go +++ b/command/apply.go @@ -251,7 +251,7 @@ func (c *ApplyCommand) Run(args []string) int { } if !c.Destroy { - if outputs := outputsAsString(state, ctx.Module().Config().Outputs, true); outputs != "" { + if outputs := outputsAsString(state, terraform.RootModulePath, ctx.Module().Config().Outputs, true); outputs != "" { c.Ui.Output(c.Colorize().Color(outputs)) } } @@ -377,12 +377,12 @@ Options: return strings.TrimSpace(helpText) } -func outputsAsString(state *terraform.State, schema []*config.Output, includeHeader bool) string { +func outputsAsString(state *terraform.State, modPath []string, schema []*config.Output, includeHeader bool) string { if state == nil { return "" } - outputs := state.RootModule().Outputs + outputs := state.ModuleByPath(modPath).Outputs outputBuf := new(bytes.Buffer) if len(outputs) > 0 { schemaMap := make(map[string]*config.Output) diff --git a/command/output.go b/command/output.go index 9054dfb4d..a1cf7bf4e 100644 --- a/command/output.go +++ b/command/output.go @@ -88,7 +88,7 @@ func (c *OutputCommand) Run(args []string) int { c.Ui.Output(string(jsonOutputs)) return 0 } else { - c.Ui.Output(outputsAsString(state, nil, false)) + c.Ui.Output(outputsAsString(state, modPath, nil, false)) return 0 } } diff --git a/command/output_test.go b/command/output_test.go index 1487d41cb..01f5c034e 100644 --- a/command/output_test.go +++ b/command/output_test.go @@ -100,6 +100,55 @@ func TestModuleOutput(t *testing.T) { } } +func TestModuleOutputs(t *testing.T) { + originalState := &terraform.State{ + Modules: []*terraform.ModuleState{ + { + Path: []string{"root"}, + Outputs: map[string]*terraform.OutputState{ + "foo": { + Value: "bar", + Type: "string", + }, + }, + }, + { + Path: []string{"root", "my_module"}, + Outputs: map[string]*terraform.OutputState{ + "blah": { + Value: "tastatur", + Type: "string", + }, + }, + }, + }, + } + + statePath := testStateFile(t, originalState) + + ui := new(cli.MockUi) + c := &OutputCommand{ + Meta: Meta{ + ContextOpts: testCtxConfig(testProvider()), + Ui: ui, + }, + } + + args := []string{ + "-state", statePath, + "-module", "my_module", + } + + if code := c.Run(args); code != 0 { + t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) + } + + actual := strings.TrimSpace(ui.OutputWriter.String()) + if actual != "blah = tastatur" { + t.Fatalf("bad: %#v", actual) + } +} + func TestOutput_nestedListAndMap(t *testing.T) { originalState := &terraform.State{ Modules: []*terraform.ModuleState{ diff --git a/command/refresh.go b/command/refresh.go index 0c41bcbe4..0f9206276 100644 --- a/command/refresh.go +++ b/command/refresh.go @@ -5,6 +5,8 @@ import ( "log" "os" "strings" + + "github.com/hashicorp/terraform/terraform" ) // RefreshCommand is a cli.Command implementation that refreshes the state @@ -109,7 +111,7 @@ func (c *RefreshCommand) Run(args []string) int { return 1 } - if outputs := outputsAsString(newState, ctx.Module().Config().Outputs, true); outputs != "" { + if outputs := outputsAsString(newState, terraform.RootModulePath, ctx.Module().Config().Outputs, true); outputs != "" { c.Ui.Output(c.Colorize().Color(outputs)) } diff --git a/terraform/state.go b/terraform/state.go index a8d3dac89..55f977c72 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -14,10 +14,9 @@ import ( "strings" "github.com/hashicorp/go-version" - "github.com/satori/go.uuid" - "github.com/hashicorp/terraform/config" "github.com/mitchellh/copystructure" + "github.com/satori/go.uuid" ) const (