config: validate that variables reference valid modules

This commit is contained in:
Mitchell Hashimoto 2014-09-15 11:45:41 -07:00
parent 46c140c797
commit b60da29d48
5 changed files with 42 additions and 2 deletions

View File

@ -110,7 +110,7 @@ func ProviderConfigName(t string, pcs []*ProviderConfig) string {
// A unique identifier for this module. // A unique identifier for this module.
func (r *Module) Id() string { func (r *Module) Id() string {
return fmt.Sprintf("module.%s", r.Name) return fmt.Sprintf("%s", r.Name)
} }
// A unique identifier for this resource. // A unique identifier for this resource.
@ -195,6 +195,24 @@ func (c *Config) Validate() error {
} }
dupped = nil dupped = nil
// Check that all variables for modules reference modules that
// exist.
for source, vs := range vars {
for _, v := range vs {
mv, ok := v.(*ModuleVariable)
if !ok {
continue
}
if _, ok := modules[mv.Name]; !ok {
errs = append(errs, fmt.Errorf(
"%s: unknown module referenced: %s",
source,
mv.Name))
}
}
}
// Check that all references to resources are valid // Check that all references to resources are valid
resources := make(map[string]*Resource) resources := make(map[string]*Resource)
dupped = make(map[string]struct{}) dupped = make(map[string]struct{})

View File

@ -114,6 +114,20 @@ func TestConfigValidate_varDefaultInterpolate(t *testing.T) {
} }
} }
func TestConfigValidate_varModule(t *testing.T) {
c := testConfig(t, "validate-var-module")
if err := c.Validate(); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestConfigValidate_varModuleInvalid(t *testing.T) {
c := testConfig(t, "validate-var-module-invalid")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestProviderConfigName(t *testing.T) { func TestProviderConfigName(t *testing.T) {
pcs := []*ProviderConfig{ pcs := []*ProviderConfig{
&ProviderConfig{Name: "aw"}, &ProviderConfig{Name: "aw"},

View File

@ -699,7 +699,7 @@ foo
` `
const modulesModulesStr = ` const modulesModulesStr = `
module.bar bar
source = baz source = baz
memory memory
` `

View File

@ -0,0 +1,3 @@
resource "aws_instance" "foo" {
foo = "${module.foo.bar}"
}

View File

@ -0,0 +1,5 @@
module "foo" {}
resource "aws_instance" "foo" {
foo = "${module.foo.bar}"
}