From 1ef167602e5bb1bbfe46b1a2f17010a1132c8be0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 24 Sep 2014 19:40:06 -0700 Subject: [PATCH] config/module: validate that required parameters are passed through --- .../validate-required-var/child/main.tf | 1 + .../test-fixtures/validate-required-var/main.tf | 3 +++ config/module/tree.go | 16 ++++++++++++++++ config/module/tree_test.go | 11 +++++++++++ 4 files changed, 31 insertions(+) create mode 100644 config/module/test-fixtures/validate-required-var/child/main.tf create mode 100644 config/module/test-fixtures/validate-required-var/main.tf diff --git a/config/module/test-fixtures/validate-required-var/child/main.tf b/config/module/test-fixtures/validate-required-var/child/main.tf new file mode 100644 index 000000000..618ae3c42 --- /dev/null +++ b/config/module/test-fixtures/validate-required-var/child/main.tf @@ -0,0 +1 @@ +variable "memory" {} diff --git a/config/module/test-fixtures/validate-required-var/main.tf b/config/module/test-fixtures/validate-required-var/main.tf new file mode 100644 index 000000000..0f6991c53 --- /dev/null +++ b/config/module/test-fixtures/validate-required-var/main.tf @@ -0,0 +1,3 @@ +module "child" { + source = "./child" +} diff --git a/config/module/tree.go b/config/module/tree.go index e92361180..724b59ef7 100644 --- a/config/module/tree.go +++ b/config/module/tree.go @@ -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 } } diff --git a/config/module/tree_test.go b/config/module/tree_test.go index 7ac574ec4..611d451a5 100644 --- a/config/module/tree_test.go +++ b/config/module/tree_test.go @@ -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