diff --git a/terraform/context.go b/terraform/context.go index 61d6ad509..2bdf0935f 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -131,15 +131,17 @@ func (c *Context) Apply() (*State, error) { c.state.prune() // If we have no errors, then calculate the outputs if we have any - if err == nil && len(c.config.Outputs) > 0 && len(c.state.Resources) > 0 { - c.state.Outputs = make(map[string]string) + if err == nil && len(c.config.Outputs) > 0 { + outputs := make(map[string]string) for _, o := range c.config.Outputs { if err = c.computeVars(o.RawConfig); err != nil { break } - - c.state.Outputs[o.Name] = o.RawConfig.Config()["value"].(string) + outputs[o.Name] = o.RawConfig.Config()["value"].(string) } + + // Assign the outputs to the root module + c.state.RootModule().Outputs = outputs } return c.state, err diff --git a/terraform/state.go b/terraform/state.go index a98bc8c39..44a763dc4 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -7,6 +7,9 @@ import ( "io" ) +// rootModulePath is the path of the root module +var rootModulePath = []string{"root"} + // State keeps track of a snapshot state-of-the-world that Terraform // can use to keep track of what real world resources it is actually // managing. This is the latest format as of Terraform 0.3 @@ -23,6 +26,23 @@ type State struct { Modules []*ModuleState `json:"modules"` } +// ModuleByPath is used to lookup the module state for the given path. +// This should be the prefered lookup mechanism as it allows for future +// lookup optimizations. +func (s *State) ModuleByPath(path []string) *ModuleState { + for _, mod := range s.Modules { + if reflect.Equal(mod.Path, path) { + return mod + } + } + return nil +} + +// RootModule returns the ModuleState for the root module +func (s *State) RootModule() *ModuleState { + return s.ModuleByPath(rootModulePath) +} + func (s *State) deepcopy() *State { n := &State{ Version: n.Version,