config: validate that variables reference valid modules
This commit is contained in:
parent
46c140c797
commit
b60da29d48
|
@ -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{})
|
||||
|
|
|
@ -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"},
|
||||
|
|
|
@ -699,7 +699,7 @@ foo
|
|||
`
|
||||
|
||||
const modulesModulesStr = `
|
||||
module.bar
|
||||
bar
|
||||
source = baz
|
||||
memory
|
||||
`
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
resource "aws_instance" "foo" {
|
||||
foo = "${module.foo.bar}"
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
module "foo" {}
|
||||
|
||||
resource "aws_instance" "foo" {
|
||||
foo = "${module.foo.bar}"
|
||||
}
|
Loading…
Reference in New Issue