config: validate that a multi provider is only configured once each

This commit is contained in:
Mitchell Hashimoto 2015-04-20 14:25:33 -07:00
parent 5ba091450f
commit a599d5f224
3 changed files with 36 additions and 0 deletions

View File

@ -239,6 +239,20 @@ func (c *Config) Validate() error {
}
}
// Check that providers aren't declared multiple times.
providerSet := make(map[string]struct{})
for _, p := range c.ProviderConfigs {
name := p.FullName()
if _, ok := providerSet[name]; ok {
errs = append(errs, fmt.Errorf(
"provider.%s: declared multiple times, you can only declare a provider once",
name))
continue
}
providerSet[name] = struct{}{}
}
// Check that all references to modules are valid
modules := make(map[string]*Module)
dupped := make(map[string]struct{})
@ -651,6 +665,14 @@ func (o *Output) mergerMerge(m merger) merger {
return &result
}
func (c *ProviderConfig) FullName() string {
if c.Alias == "" {
return c.Name
}
return fmt.Sprintf("%s.%s", c.Name, c.Alias)
}
func (c *ProviderConfig) mergerName() string {
return c.Name
}

View File

@ -203,6 +203,13 @@ func TestConfigValidate_pathVarInvalid(t *testing.T) {
}
}
func TestConfigValidate_providerMulti(t *testing.T) {
c := testConfig(t, "validate-provider-multi")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_provConnSplatOther(t *testing.T) {
c := testConfig(t, "validate-prov-conn-splat-other")
if err := c.Validate(); err != nil {

View File

@ -0,0 +1,7 @@
provider "aws" {
alias = "foo"
}
provider "aws" {
alias = "foo"
}