Skip to content

Commit

Permalink
core: Check variable types between modules
Browse files Browse the repository at this point in the history
  • Loading branch information
jen20 committed Apr 14, 2016
1 parent 414910d commit 516b04c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
11 changes: 11 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ const (
VariableTypeMap
)

func (v VariableType) Printable() string {
switch v {
case VariableTypeString:
return "string"
case VariableTypeMap:
return "map"
default:
return "unknown"
}
}

// ProviderConfigName returns the name of the provider configuration in
// the given mapping that maps to the proper provider configuration
// for this resource.
Expand Down
46 changes: 46 additions & 0 deletions terraform/eval_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,58 @@ package terraform

import (
"fmt"
"strings"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/module"
"github.com/mitchellh/mapstructure"
)

type EvalTypeCheckVariable struct {
Variables map[string]string
ModulePath []string
ModuleTree *module.Tree
DepPrefix string
}

func (n *EvalTypeCheckVariable) Eval(ctx EvalContext) (interface{}, error) {
currentTree := n.ModuleTree
for _, pathComponent := range n.ModulePath[1:] {
currentTree = currentTree.Children()[pathComponent]
}
targetConfig := currentTree.Config()

prototypes := make(map[string]config.VariableType)
for _, variable := range targetConfig.Variables {
prototypes[variable.Name] = variable.Type()
}

for name, declaredType := range prototypes {
// This is only necessary when we _actually_ check
// proposedValue := n.Variables[name]

switch declaredType {
case config.VariableTypeString:
// This will need actual verification once we aren't dealing with
// a map[string]string but this is sufficient for now.
continue
default:
// Only display a module if we are not in the root module
modulePathDescription := fmt.Sprintf(" in module %s", strings.Join(n.ModulePath[1:], "."))
if len(n.ModulePath) == 1 {
modulePathDescription = ""
}
// This will need the actual type substituting when we have more than
// just strings and maps.
return nil, fmt.Errorf("variable %s%s should be type %s, got type string",
name, modulePathDescription, declaredType.Printable())
}
}

return nil, nil
}

// EvalSetVariables is an EvalNode implementation that sets the variables
// explicitly for interpolation later.
type EvalSetVariables struct {
Expand Down
10 changes: 9 additions & 1 deletion terraform/graph_config_node_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/dag"
)

Expand All @@ -18,7 +19,8 @@ type GraphNodeConfigVariable struct {
Module string
Value *config.RawConfig

depPrefix string
ModuleTree *module.Tree
ModulePath []string
}

func (n *GraphNodeConfigVariable) Name() string {
Expand Down Expand Up @@ -125,6 +127,12 @@ func (n *GraphNodeConfigVariable) EvalTree() EvalNode {
Variables: variables,
},

&EvalTypeCheckVariable{
Variables: variables,
ModulePath: n.ModulePath,
ModuleTree: n.ModuleTree,
},

&EvalSetVariables{
Module: &n.Module,
Variables: variables,
Expand Down
6 changes: 5 additions & 1 deletion terraform/transform_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ func (t *ConfigTransformer) Transform(g *Graph) error {

// Write all the variables out
for _, v := range config.Variables {
nodes = append(nodes, &GraphNodeConfigVariable{Variable: v})
nodes = append(nodes, &GraphNodeConfigVariable{
Variable: v,
ModuleTree: t.Module,
ModulePath: g.Path,
})
}

// Write all the provider configs out
Expand Down

0 comments on commit 516b04c

Please sign in to comment.