config/module: validate that required parameters are passed through

This commit is contained in:
Mitchell Hashimoto 2014-09-24 19:40:06 -07:00
parent 8420b58015
commit 1ef167602e
4 changed files with 31 additions and 0 deletions

View File

@ -0,0 +1 @@
variable "memory" {}

View File

@ -0,0 +1,3 @@
module "child" {
source = "./child"
}

View File

@ -257,9 +257,14 @@ func (t *Tree) Validate() error {
}
// Build the variables that the module defines
requiredMap := make(map[string]struct{})
varMap := make(map[string]struct{})
for _, v := range tree.config.Variables {
varMap[v.Name] = struct{}{}
if v.Required() {
requiredMap[v.Name] = struct{}{}
}
}
// Compare to the keys in our raw config for the module
@ -270,6 +275,17 @@ func (t *Tree) Validate() error {
m.Name, k)
return newErr
}
// Remove the required
delete(requiredMap, k)
}
// If we have any required left over, they aren't set.
for k, _ := range requiredMap {
newErr.Err = fmt.Errorf(
"module %s: required variable %s not set",
m.Name, k)
return newErr
}
}

View File

@ -148,6 +148,17 @@ func TestTreeValidate_notLoaded(t *testing.T) {
}
}
func TestTreeValidate_requiredChildVar(t *testing.T) {
tree := NewTree("", testConfig(t, "validate-required-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")
}
}
const treeLoadStr = `
root