mirror of https://github.com/hashicorp/terraform
parent
5595229430
commit
23d097ee53
@ -0,0 +1,64 @@
|
||||
package terraform
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform/config"
|
||||
)
|
||||
|
||||
// EvalSetVariables is an EvalNode implementation that sets the variables
|
||||
// explicitly for interpolation later.
|
||||
type EvalSetVariables struct {
|
||||
Variables map[string]string
|
||||
}
|
||||
|
||||
func (n *EvalSetVariables) Args() ([]EvalNode, []EvalType) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// TODO: test
|
||||
func (n *EvalSetVariables) Eval(
|
||||
ctx EvalContext, args []interface{}) (interface{}, error) {
|
||||
ctx.SetVariables(n.Variables)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (n *EvalSetVariables) Type() EvalType {
|
||||
return EvalTypeNull
|
||||
}
|
||||
|
||||
// EvalVariableBlock is an EvalNode implementation that evaluates the
|
||||
// given configuration, and uses the final values as a way to set the
|
||||
// mapping.
|
||||
type EvalVariableBlock struct {
|
||||
Config EvalNode
|
||||
Variables map[string]string
|
||||
}
|
||||
|
||||
func (n *EvalVariableBlock) Args() ([]EvalNode, []EvalType) {
|
||||
return []EvalNode{n.Config}, []EvalType{EvalTypeConfig}
|
||||
}
|
||||
|
||||
// TODO: test
|
||||
func (n *EvalVariableBlock) Eval(
|
||||
ctx EvalContext, args []interface{}) (interface{}, error) {
|
||||
// Clear out the existing mapping
|
||||
for k, _ := range n.Variables {
|
||||
delete(n.Variables, k)
|
||||
}
|
||||
|
||||
// Get our configuration
|
||||
rc := args[0].(*ResourceConfig)
|
||||
for k, v := range rc.Config {
|
||||
n.Variables[k] = v.(string)
|
||||
}
|
||||
for k, _ := range rc.Raw {
|
||||
if _, ok := n.Variables[k]; !ok {
|
||||
n.Variables[k] = config.UnknownVariableValue
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (n *EvalVariableBlock) Type() EvalType {
|
||||
return EvalTypeNull
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package terraform
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
// PathCacheKey returns a cache key for a module path.
|
||||
//
|
||||
// TODO: test
|
||||
func PathCacheKey(path []string) string {
|
||||
// There is probably a better way to do this, but this is working for now.
|
||||
// We just create an MD5 hash of all the MD5 hashes of all the path
|
||||
// elements. This gets us the property that it is unique per ordering.
|
||||
hash := md5.New()
|
||||
for _, p := range path {
|
||||
single := md5.Sum([]byte(p))
|
||||
if _, err := hash.Write(single[:]); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
return hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
package terraform
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform/dag"
|
||||
)
|
||||
|
||||
// ModuleInputTransformer is a GraphTransformer that adds a node to the
|
||||
// graph for setting the module input variables for the remainder of the
|
||||
// graph.
|
||||
type ModuleInputTransformer struct {
|
||||
Variables map[string]string
|
||||
}
|
||||
|
||||
func (t *ModuleInputTransformer) Transform(g *Graph) error {
|
||||
// Create the node
|
||||
n := &graphNodeModuleInput{Variables: t.Variables}
|
||||
|
||||
// Add it to the graph
|
||||
g.Add(n)
|
||||
|
||||
// Connect the inputs to the bottom of the graph so that it happens
|
||||
// first.
|
||||
for _, v := range g.Vertices() {
|
||||
if v == n {
|
||||
continue
|
||||
}
|
||||
|
||||
if g.DownEdges(v).Len() == 0 {
|
||||
g.Connect(dag.BasicEdge(v, n))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type graphNodeModuleInput struct {
|
||||
Variables map[string]string
|
||||
}
|
||||
|
||||
func (n *graphNodeModuleInput) Name() string {
|
||||
return "module inputs"
|
||||
}
|
||||
|
||||
// GraphNodeEvalable impl.
|
||||
func (n *graphNodeModuleInput) EvalTree() EvalNode {
|
||||
return &EvalSetVariables{Variables: n.Variables}
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
package terraform
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/dag"
|
||||
)
|
||||
|
||||
func TestModuleInputTransformer(t *testing.T) {
|
||||
var g Graph
|
||||
g.Add(1)
|
||||
g.Add(2)
|
||||
g.Add(3)
|
||||
g.Connect(dag.BasicEdge(1, 2))
|
||||
g.Connect(dag.BasicEdge(1, 3))
|
||||
|
||||
{
|
||||
tf := &ModuleInputTransformer{}
|
||||
if err := tf.Transform(&g); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
actual := strings.TrimSpace(g.String())
|
||||
expected := strings.TrimSpace(testModuleInputTransformStr)
|
||||
if actual != expected {
|
||||
t.Fatalf("bad:\n\n%s", actual)
|
||||
}
|
||||
}
|
||||
|
||||
const testModuleInputTransformStr = `
|
||||
1
|
||||
2
|
||||
3
|
||||
2
|
||||
module inputs
|
||||
3
|
||||
module inputs
|
||||
module inputs
|
||||
`
|
||||
Loading…
Reference in new issue