terraform: calculate outputs and store it into the state

This commit is contained in:
Mitchell Hashimoto 2015-02-11 22:33:38 -08:00
parent 84cf7f1179
commit de6827b3ed
3 changed files with 67 additions and 2 deletions

View File

@ -396,7 +396,6 @@ func TestContext2Plan_moduleProviderDefaultsVar(t *testing.T) {
}
}
/*
func TestContext2Plan_moduleVar(t *testing.T) {
m := testModule(t, "plan-module-var")
p := testProvider("aws")

60
terraform/eval_output.go Normal file
View File

@ -0,0 +1,60 @@
package terraform
import (
"fmt"
)
// EvalWriteOutput is an EvalNode implementation that writes the output
// for the given name to the current state.
type EvalWriteOutput struct {
Name string
Value EvalNode
}
func (n *EvalWriteOutput) Args() ([]EvalNode, []EvalType) {
return []EvalNode{n.Value}, []EvalType{EvalTypeConfig}
}
// TODO: test
func (n *EvalWriteOutput) Eval(
ctx EvalContext, args []interface{}) (interface{}, error) {
config := args[0].(*ResourceConfig)
state, lock := ctx.State()
if state == nil {
return nil, fmt.Errorf("cannot write state to nil state")
}
// Get a write lock so we can access this instance
lock.Lock()
defer lock.Unlock()
// Look for the module state. If we don't have one, create it.
mod := state.ModuleByPath(ctx.Path())
if mod == nil {
mod = state.AddModule(ctx.Path())
}
// Get the value from the config
valueRaw, ok := config.Get("value")
if !ok {
valueRaw = ""
}
// If it is a list of values, get the first one
if list, ok := valueRaw.([]interface{}); ok {
valueRaw = list[0]
}
if _, ok := valueRaw.(string); !ok {
valueRaw = ""
}
// Write the output
mod.Outputs[n.Name] = valueRaw.(string)
return nil, nil
}
func (n *EvalWriteOutput) Type() EvalType {
return EvalTypeNull
}

View File

@ -122,7 +122,13 @@ func (n *GraphNodeConfigOutput) DependentOn() []string {
// GraphNodeEvalable impl.
func (n *GraphNodeConfigOutput) EvalTree() EvalNode {
return nil
return &EvalOpFilter{
Ops: []walkOperation{walkRefresh, walkPlan},
Node: &EvalWriteOutput{
Name: n.Output.Name,
Value: &EvalInterpolate{Config: n.Output.RawConfig},
},
}
}
// GraphNodeConfigProvider represents a configured provider within the