diff --git a/config/module/test-fixtures/validate-bad-var/child/main.tf b/config/module/test-fixtures/validate-bad-var/child/main.tf new file mode 100644 index 000000000..e69de29bb diff --git a/config/module/test-fixtures/validate-bad-var/main.tf b/config/module/test-fixtures/validate-bad-var/main.tf new file mode 100644 index 000000000..7cc785d17 --- /dev/null +++ b/config/module/test-fixtures/validate-bad-var/main.tf @@ -0,0 +1,5 @@ +module "child" { + source = "./child" + + memory = "foo" +} diff --git a/config/module/test-fixtures/validate-child-good/child/main.tf b/config/module/test-fixtures/validate-child-good/child/main.tf index dd0d95b59..618ae3c42 100644 --- a/config/module/test-fixtures/validate-child-good/child/main.tf +++ b/config/module/test-fixtures/validate-child-good/child/main.tf @@ -1 +1 @@ -# Good +variable "memory" {} diff --git a/config/module/test-fixtures/validate-child-good/main.tf b/config/module/test-fixtures/validate-child-good/main.tf index 0f6991c53..7c70782f1 100644 --- a/config/module/test-fixtures/validate-child-good/main.tf +++ b/config/module/test-fixtures/validate-child-good/main.tf @@ -1,3 +1,4 @@ module "child" { source = "./child" + memory = "1G" } diff --git a/config/module/tree.go b/config/module/tree.go index 019f5c1c0..867f812c8 100644 --- a/config/module/tree.go +++ b/config/module/tree.go @@ -211,16 +211,20 @@ func (t *Tree) Validate() error { return fmt.Errorf("tree must be loaded before calling Validate") } + // If something goes wrong, here is our error template + newErr := &ValidateError{Name: []string{t.Name()}} + // Validate our configuration first. if err := t.config.Validate(); err != nil { - return &ValidateError{ - Name: []string{t.Name()}, - Err: err, - } + newErr.Err = err + return newErr } + // Get the child trees + children := t.Children() + // Validate all our children - for _, c := range t.Children() { + for _, c := range children { err := c.Validate() if err == nil { continue @@ -237,6 +241,32 @@ func (t *Tree) Validate() error { return verr } + // Go over all the modules and verify that any parameters are valid + // variables into the module in question. + for _, m := range t.config.Modules { + tree, ok := children[m.Name] + if !ok { + // This should never happen because Load watches us + panic("module not found in children: " + m.Name) + } + + // Build the variables that the module defines + varMap := make(map[string]struct{}) + for _, v := range tree.config.Variables { + varMap[v.Name] = struct{}{} + } + + // Compare to the keys in our raw config for the module + for k, _ := range m.RawConfig.Raw { + if _, ok := varMap[k]; !ok { + newErr.Err = fmt.Errorf( + "module %s: %s is not a valid parameter", + m.Name, k) + return newErr + } + } + } + return nil } diff --git a/config/module/tree_test.go b/config/module/tree_test.go index ed74eb66c..3fbad23d7 100644 --- a/config/module/tree_test.go +++ b/config/module/tree_test.go @@ -92,6 +92,18 @@ func TestTreeValidate_badChild(t *testing.T) { } } +func TestTreeValidate_badChildVar(t *testing.T) { + tree := NewTree(testConfig(t, "validate-bad-var")) + + if err := tree.Load(testStorage(t), GetModeGet); err != nil { + t.Fatalf("err: %s", err) + } + + if err := tree.Validate(); err == nil { + t.Fatal("should error") + } +} + func TestTreeValidate_badRoot(t *testing.T) { tree := NewTree(testConfig(t, "validate-root-bad"))