config/module: validate outputs line up with ModuleVariables

This commit is contained in:
Mitchell Hashimoto 2014-09-15 13:57:07 -07:00
parent b60da29d48
commit 292f57ea0a
7 changed files with 59 additions and 3 deletions

View File

@ -127,7 +127,7 @@ func (c *Config) Validate() error {
"Unknown root level key: %s", k))
}
vars := c.allVariables()
vars := c.InterpolatedVariables()
varMap := make(map[string]*Variable)
for _, v := range c.Variables {
varMap[v.Name] = v
@ -302,10 +302,10 @@ func (c *Config) Validate() error {
return nil
}
// allVariables is a helper that returns a mapping of all the interpolated
// InterpolatedVariables is a helper that returns a mapping of all the interpolated
// variables within the configuration. This is used to verify references
// are valid in the Validate step.
func (c *Config) allVariables() map[string][]InterpolatedVariable {
func (c *Config) InterpolatedVariables() map[string][]InterpolatedVariable {
result := make(map[string][]InterpolatedVariable)
for _, pc := range c.ProviderConfigs {
source := fmt.Sprintf("provider config '%s'", pc.Name)

View File

@ -0,0 +1,7 @@
module "child" {
source = "./child"
}
resource "aws_instance" "foo" {
memory = "${module.child.memory}"
}

View File

@ -1 +1,3 @@
variable "memory" {}
output "result" {}

View File

@ -2,3 +2,7 @@ module "child" {
source = "./child"
memory = "1G"
}
resource "aws_instance" "foo" {
memory = "${module.child.result}"
}

View File

@ -267,6 +267,37 @@ func (t *Tree) Validate() error {
}
}
// Go over all the variables used and make sure that any module
// variables represent outputs properly.
for source, vs := range t.config.InterpolatedVariables() {
for _, v := range vs {
mv, ok := v.(*config.ModuleVariable)
if !ok {
continue
}
tree, ok := children[mv.Name]
if !ok {
// This should never happen because Load watches us
panic("module not found in children: " + mv.Name)
}
found := false
for _, o := range tree.config.Outputs {
if o.Name == mv.Field {
found = true
break
}
}
if !found {
newErr.Err = fmt.Errorf(
"%s: %s is not a valid output for module %s",
source, mv.Field, mv.Name)
return newErr
}
}
}
return nil
}

View File

@ -92,6 +92,18 @@ func TestTreeValidate_badChild(t *testing.T) {
}
}
func TestTreeValidate_badChildOutput(t *testing.T) {
tree := NewTree(testConfig(t, "validate-bad-output"))
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_badChildVar(t *testing.T) {
tree := NewTree(testConfig(t, "validate-bad-var"))