config: validate no duplicate modules

This commit is contained in:
Mitchell Hashimoto 2014-09-11 16:02:36 -07:00
parent 2a6990e2b9
commit 610e92cab2
3 changed files with 33 additions and 1 deletions

View File

@ -173,9 +173,27 @@ func (c *Config) Validate() error {
} }
} }
// Check that all references to modules are valid
modules := make(map[string]*Module)
dupped := make(map[string]struct{})
for _, m := range c.Modules {
if _, ok := modules[m.Id()]; ok {
if _, ok := dupped[m.Id()]; !ok {
dupped[m.Id()] = struct{}{}
errs = append(errs, fmt.Errorf(
"%s: module repeated multiple times",
m.Id()))
}
}
modules[m.Id()] = m
}
dupped = nil
// 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{})
for _, r := range c.Resources { for _, r := range c.Resources {
if _, ok := resources[r.Id()]; ok { if _, ok := resources[r.Id()]; ok {
if _, ok := dupped[r.Id()]; !ok { if _, ok := dupped[r.Id()]; !ok {

View File

@ -44,6 +44,13 @@ func TestConfigValidate_countZero(t *testing.T) {
} }
} }
func TestConfigValidate_dupModule(t *testing.T) {
c := testConfig(t, "validate-dup-module")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_dupResource(t *testing.T) { func TestConfigValidate_dupResource(t *testing.T) {
c := testConfig(t, "validate-dup-resource") c := testConfig(t, "validate-dup-resource")
if err := c.Validate(); err == nil { if err := c.Validate(); err == nil {

View File

@ -0,0 +1,7 @@
module "aws_instance" "web" {
source = "foo"
}
module "aws_instance" "web" {
source = "bar"
}