diff --git a/config/config.go b/config/config.go index 431807c06..e0733081f 100644 --- a/config/config.go +++ b/config/config.go @@ -110,7 +110,7 @@ func ProviderConfigName(t string, pcs []*ProviderConfig) string { // A unique identifier for this module. func (r *Module) Id() string { - return fmt.Sprintf("module.%s", r.Name) + return fmt.Sprintf("%s", r.Name) } // A unique identifier for this resource. @@ -195,6 +195,24 @@ func (c *Config) Validate() error { } 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 resources := make(map[string]*Resource) dupped = make(map[string]struct{}) diff --git a/config/config_test.go b/config/config_test.go index 581109e9c..26becdd30 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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) { pcs := []*ProviderConfig{ &ProviderConfig{Name: "aw"}, diff --git a/config/loader_test.go b/config/loader_test.go index a07d506a8..b92307a70 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -699,7 +699,7 @@ foo ` const modulesModulesStr = ` -module.bar +bar source = baz memory ` diff --git a/config/test-fixtures/validate-var-module-invalid/main.tf b/config/test-fixtures/validate-var-module-invalid/main.tf new file mode 100644 index 000000000..73897c008 --- /dev/null +++ b/config/test-fixtures/validate-var-module-invalid/main.tf @@ -0,0 +1,3 @@ +resource "aws_instance" "foo" { + foo = "${module.foo.bar}" +} diff --git a/config/test-fixtures/validate-var-module/main.tf b/config/test-fixtures/validate-var-module/main.tf new file mode 100644 index 000000000..402c235a1 --- /dev/null +++ b/config/test-fixtures/validate-var-module/main.tf @@ -0,0 +1,5 @@ +module "foo" {} + +resource "aws_instance" "foo" { + foo = "${module.foo.bar}" +}