7 "github.com/hashicorp/go-multierror"
8 "github.com/hashicorp/terraform/config"
9 "github.com/hashicorp/terraform/dag"
12 // GraphSemanticChecker is the interface that semantic checks across
13 // the entire Terraform graph implement.
15 // The graph should NOT be modified by the semantic checker.
16 type GraphSemanticChecker interface {
17 Check(*dag.Graph) error
20 // UnorderedSemanticCheckRunner is an implementation of GraphSemanticChecker
21 // that runs a list of SemanticCheckers against the vertices of the graph
22 // in no specified order.
23 type UnorderedSemanticCheckRunner struct {
24 Checks []SemanticChecker
27 func (sc *UnorderedSemanticCheckRunner) Check(g *dag.Graph) error {
29 for _, v := range g.Vertices() {
30 for _, check := range sc.Checks {
31 if e := check.Check(g, v); e != nil {
32 err = multierror.Append(err, e)
40 // SemanticChecker is the interface that semantic checks across the
41 // Terraform graph implement. Errors are accumulated. Even after an error
42 // is returned, child vertices in the graph will still be visited.
44 // The graph should NOT be modified by the semantic checker.
46 // The order in which vertices are visited is left unspecified, so the
47 // semantic checks should not rely on that.
48 type SemanticChecker interface {
49 Check(*dag.Graph, dag.Vertex) error
52 // smcUserVariables does all the semantic checks to verify that the
53 // variables given satisfy the configuration itself.
54 func smcUserVariables(c *config.Config, vs map[string]interface{}) []error {
57 cvs := make(map[string]*config.Variable)
58 for _, v := range c.Variables {
62 // Check that all required variables are present
63 required := make(map[string]struct{})
64 for _, v := range c.Variables {
66 required[v.Name] = struct{}{}
69 for k, _ := range vs {
72 if len(required) > 0 {
73 for k, _ := range required {
74 errs = append(errs, fmt.Errorf(
75 "Required variable not set: %s", k))
79 // Check that types match up
80 for name, proposedValue := range vs {
81 // Check for "map.key" fields. These stopped working with Terraform
82 // 0.7 but we do this to surface a better error message informing
83 // the user what happened.
84 if idx := strings.Index(name, "."); idx > 0 {
86 if _, ok := cvs[key]; ok {
87 errs = append(errs, fmt.Errorf(
88 "%s: Overriding map keys with the format `name.key` is no "+
89 "longer allowed. You may still override keys by setting "+
90 "`name = { key = value }`. The maps will be merged. This "+
91 "behavior appeared in 0.7.0.", name))
96 schema, ok := cvs[name]
101 declaredType := schema.Type()
103 switch declaredType {
104 case config.VariableTypeString:
105 switch proposedValue.(type) {
109 case config.VariableTypeMap:
110 switch v := proposedValue.(type) {
111 case map[string]interface{}:
113 case []map[string]interface{}:
114 // if we have a list of 1 map, it will get coerced later as needed
119 case config.VariableTypeList:
120 switch proposedValue.(type) {
125 errs = append(errs, fmt.Errorf("variable %s should be type %s, got %s",
126 name, declaredType.Printable(), hclTypeName(proposedValue)))
129 // TODO(mitchellh): variables that are unknown