Merge pull request #7875 from hashicorp/b-outputs-by-module

core: Fix -module for terraform output command
This commit is contained in:
James Nugent 2016-07-29 16:40:24 -05:00 committed by GitHub
commit 9034196baf
5 changed files with 57 additions and 7 deletions

View File

@ -251,7 +251,7 @@ func (c *ApplyCommand) Run(args []string) int {
} }
if !c.Destroy { 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)) c.Ui.Output(c.Colorize().Color(outputs))
} }
} }
@ -377,12 +377,12 @@ Options:
return strings.TrimSpace(helpText) 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 { if state == nil {
return "" return ""
} }
outputs := state.RootModule().Outputs outputs := state.ModuleByPath(modPath).Outputs
outputBuf := new(bytes.Buffer) outputBuf := new(bytes.Buffer)
if len(outputs) > 0 { if len(outputs) > 0 {
schemaMap := make(map[string]*config.Output) schemaMap := make(map[string]*config.Output)

View File

@ -88,7 +88,7 @@ func (c *OutputCommand) Run(args []string) int {
c.Ui.Output(string(jsonOutputs)) c.Ui.Output(string(jsonOutputs))
return 0 return 0
} else { } else {
c.Ui.Output(outputsAsString(state, nil, false)) c.Ui.Output(outputsAsString(state, modPath, nil, false))
return 0 return 0
} }
} }

View File

@ -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) { func TestOutput_nestedListAndMap(t *testing.T) {
originalState := &terraform.State{ originalState := &terraform.State{
Modules: []*terraform.ModuleState{ Modules: []*terraform.ModuleState{

View File

@ -5,6 +5,8 @@ import (
"log" "log"
"os" "os"
"strings" "strings"
"github.com/hashicorp/terraform/terraform"
) )
// RefreshCommand is a cli.Command implementation that refreshes the state // RefreshCommand is a cli.Command implementation that refreshes the state
@ -109,7 +111,7 @@ func (c *RefreshCommand) Run(args []string) int {
return 1 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)) c.Ui.Output(c.Colorize().Color(outputs))
} }

View File

@ -14,10 +14,9 @@ import (
"strings" "strings"
"github.com/hashicorp/go-version" "github.com/hashicorp/go-version"
"github.com/satori/go.uuid"
"github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/config"
"github.com/mitchellh/copystructure" "github.com/mitchellh/copystructure"
"github.com/satori/go.uuid"
) )
const ( const (