terraform: Adding module lookups for state

This commit is contained in:
Armon Dadgar 2014-09-15 17:11:36 -07:00
parent ab7ae0516c
commit a85d6fa6c3
2 changed files with 26 additions and 4 deletions

View File

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

View File

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