Skip to content

Commit

Permalink
Merge pull request #2102 from Fodoj/add-module-outputs
Browse files Browse the repository at this point in the history
Add module outputs
  • Loading branch information
phinze committed Jun 3, 2015
2 parents 61c6817 + b945d1e commit e91705a
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
26 changes: 24 additions & 2 deletions command/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ type OutputCommand struct {
func (c *OutputCommand) Run(args []string) int {
args = c.Meta.process(args, false)

var module string
cmdFlags := flag.NewFlagSet("output", flag.ContinueOnError)
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
cmdFlags.StringVar(&module, "module", "", "module")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }

if err := cmdFlags.Parse(args); err != nil {
return 1
}
Expand All @@ -38,15 +41,34 @@ func (c *OutputCommand) Run(args []string) int {
return 1
}

if module == "" {
module = "root"
} else {
module = "root." + module
}

// Get the proper module we want to get outputs for
modPath := strings.Split(module, ".")

state := stateStore.State()
if state.Empty() || len(state.RootModule().Outputs) == 0 {
mod := state.ModuleByPath(modPath)

if mod == nil {
c.Ui.Error(fmt.Sprintf(
"The module %s could not be found. There is nothing to taint.",
module))
return 1
}

if state.Empty() || len(mod.Outputs) == 0 {
c.Ui.Error(fmt.Sprintf(
"The state file has no outputs defined. Define an output\n" +
"in your configuration with the `output` directive and re-run\n" +
"`terraform apply` for it to become available."))
return 1
}
v, ok := state.RootModule().Outputs[name]

v, ok := mod.Outputs[name]
if !ok {
c.Ui.Error(fmt.Sprintf(
"The output variable requested could not be found in the state\n" +
Expand Down
77 changes: 77 additions & 0 deletions command/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,83 @@ func TestOutput(t *testing.T) {
}
}

func TestModuleOutput(t *testing.T) {
originalState := &terraform.State{
Modules: []*terraform.ModuleState{
&terraform.ModuleState{
Path: []string{"root"},
Outputs: map[string]string{
"foo": "bar",
},
},
&terraform.ModuleState{
Path: []string{"root", "my_module"},
Outputs: map[string]string{
"blah": "tastatur",
},
},
},
}

statePath := testStateFile(t, originalState)

ui := new(cli.MockUi)
c := &OutputCommand{
Meta: Meta{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
},
}

args := []string{
"-state", statePath,
"-module", "my_module",
"blah",
}

if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}

actual := strings.TrimSpace(ui.OutputWriter.String())
if actual != "tastatur" {
t.Fatalf("bad: %#v", actual)
}
}

func TestMissingModuleOutput(t *testing.T) {
originalState := &terraform.State{
Modules: []*terraform.ModuleState{
&terraform.ModuleState{
Path: []string{"root"},
Outputs: map[string]string{
"foo": "bar",
},
},
},
}

statePath := testStateFile(t, originalState)

ui := new(cli.MockUi)
c := &OutputCommand{
Meta: Meta{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
},
}

args := []string{
"-state", statePath,
"-module", "not_existing_module",
"blah",
}

if code := c.Run(args); code != 1 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}
}

func TestOutput_badVar(t *testing.T) {
originalState := &terraform.State{
Modules: []*terraform.ModuleState{
Expand Down
6 changes: 5 additions & 1 deletion website/source/docs/commands/output.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ current directory for the state file to query.
The command-line flags are all optional. The list of available flags are:

* `-state=path` - Path to the state file. Defaults to "terraform.tfstate".

* `-module=module_name` - The module path which has needed output.
By default this is the root path. Other modules can be specified by
a period-separated list. Example: "foo" would reference the module
"foo" but "foo.bar" would reference the "bar" module in the "foo"
module.

0 comments on commit e91705a

Please sign in to comment.