mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
config/module: validate that required parameters are passed through
This commit is contained in:
parent
8420b58015
commit
1ef167602e
@ -0,0 +1 @@
|
||||
variable "memory" {}
|
@ -0,0 +1,3 @@
|
||||
module "child" {
|
||||
source = "./child"
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user