2014-06-05 20:53:07 +02:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2015-01-24 02:52:51 +01:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2014-06-05 20:53:07 +02:00
|
|
|
"github.com/hashicorp/terraform/config"
|
2015-01-24 02:52:51 +01:00
|
|
|
"github.com/hashicorp/terraform/dag"
|
2014-06-05 20:53:07 +02:00
|
|
|
)
|
|
|
|
|
2015-01-24 02:52:51 +01:00
|
|
|
// GraphSemanticChecker is the interface that semantic checks across
|
|
|
|
// the entire Terraform graph implement.
|
|
|
|
//
|
|
|
|
// The graph should NOT be modified by the semantic checker.
|
|
|
|
type GraphSemanticChecker interface {
|
|
|
|
Check(*dag.Graph) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnorderedSemanticCheckRunner is an implementation of GraphSemanticChecker
|
|
|
|
// that runs a list of SemanticCheckers against the vertices of the graph
|
|
|
|
// in no specified order.
|
|
|
|
type UnorderedSemanticCheckRunner struct {
|
|
|
|
Checks []SemanticChecker
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *UnorderedSemanticCheckRunner) Check(g *dag.Graph) error {
|
|
|
|
var err error
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
for _, check := range sc.Checks {
|
|
|
|
if e := check.Check(g, v); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SemanticChecker is the interface that semantic checks across the
|
|
|
|
// Terraform graph implement. Errors are accumulated. Even after an error
|
|
|
|
// is returned, child vertices in the graph will still be visited.
|
|
|
|
//
|
|
|
|
// The graph should NOT be modified by the semantic checker.
|
|
|
|
//
|
|
|
|
// The order in which vertices are visited is left unspecified, so the
|
|
|
|
// semantic checks should not rely on that.
|
|
|
|
type SemanticChecker interface {
|
|
|
|
Check(*dag.Graph, dag.Vertex) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// SemanticCheckModulesExist is an implementation of SemanticChecker that
|
|
|
|
// verifies that all the modules that are referenced in the graph exist.
|
|
|
|
type SemanticCheckModulesExist struct{}
|
|
|
|
|
|
|
|
// TODO: test
|
|
|
|
func (*SemanticCheckModulesExist) Check(g *dag.Graph, v dag.Vertex) error {
|
|
|
|
mn, ok := v.(*GraphNodeConfigModule)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if mn.Tree == nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"module '%s' not found", mn.Module.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-03 05:47:10 +02:00
|
|
|
// smcUserVariables does all the semantic checks to verify that the
|
|
|
|
// variables given satisfy the configuration itself.
|
|
|
|
func smcUserVariables(c *config.Config, vs map[string]string) []error {
|
2014-06-05 20:53:07 +02:00
|
|
|
var errs []error
|
|
|
|
|
2014-07-22 18:27:28 +02:00
|
|
|
cvs := make(map[string]*config.Variable)
|
|
|
|
for _, v := range c.Variables {
|
|
|
|
cvs[v.Name] = v
|
|
|
|
}
|
|
|
|
|
2014-06-05 20:53:07 +02:00
|
|
|
// Check that all required variables are present
|
|
|
|
required := make(map[string]struct{})
|
2014-07-19 07:13:15 +02:00
|
|
|
for _, v := range c.Variables {
|
2014-06-05 20:53:07 +02:00
|
|
|
if v.Required() {
|
2014-07-19 07:13:15 +02:00
|
|
|
required[v.Name] = struct{}{}
|
2014-06-05 20:53:07 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-03 05:47:10 +02:00
|
|
|
for k, _ := range vs {
|
2014-06-05 20:53:07 +02:00
|
|
|
delete(required, k)
|
|
|
|
}
|
|
|
|
if len(required) > 0 {
|
|
|
|
for k, _ := range required {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"Required variable not set: %s", k))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-22 18:27:28 +02:00
|
|
|
// Check that types match up
|
|
|
|
for k, _ := range vs {
|
|
|
|
v, ok := cvs[k]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Type() != config.VariableTypeString {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"%s: cannot assign string value to map type",
|
|
|
|
k))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 20:53:07 +02:00
|
|
|
// TODO(mitchellh): variables that are unknown
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|