Skip to content

Commit

Permalink
Merge pull request #16621 from hashicorp/jbardin/provider-panic
Browse files Browse the repository at this point in the history
Fix panic with invalid module providers
  • Loading branch information
jbardin authored Nov 14, 2017
2 parents f15d95f + b357c02 commit aef082d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
13 changes: 11 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ func (c *Config) Validate() error {

// Check that providers aren't declared multiple times and that their
// version constraints, where present, are syntactically valid.
providerSet := make(map[string]struct{})
providerSet := make(map[string]bool)
for _, p := range c.ProviderConfigs {
name := p.FullName()
if _, ok := providerSet[name]; ok {
Expand All @@ -410,7 +410,7 @@ func (c *Config) Validate() error {
}
}

providerSet[name] = struct{}{}
providerSet[name] = true
}

// Check that all references to modules are valid
Expand Down Expand Up @@ -500,6 +500,15 @@ func (c *Config) Validate() error {
"%s: can't initialize configuration: %s",
m.Id(), err))
}

// check that all named providers actually exist
for _, p := range m.Providers {
if !providerSet[p] {
errs = append(errs, fmt.Errorf(
"provider %q named in module %q does not exist", p, m.Name))
}
}

}
dupped = nil

Expand Down
6 changes: 6 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ func TestConfigValidate_table(t *testing.T) {
true,
"invalid version constraint",
},
{
"invalid provider name in module block",
"validate-missing-provider",
true,
"does not exist",
},
}

for i, tc := range cases {
Expand Down
10 changes: 10 additions & 0 deletions config/test-fixtures/validate-missing-provider/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
provider "test" {
alias = "bar"
}

module "mod" {
source = "./mod"
providers = {
"test" = "test.foo"
}
}
3 changes: 2 additions & 1 deletion terraform/transform_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,8 @@ func (t *ProviderConfigTransformer) addProxyProvider(g *Graph, m *module.Tree, p
parentProvider := t.providers[fullParentName]

if parentProvider == nil {
panic(fmt.Sprintf("missing provider %s in module %s", parentProviderName, m.Name()))
log.Printf("[ERROR] missing provider %s in module %s", parentProviderName, m.Name())
return false
}

v := &graphNodeProxyProvider{
Expand Down

0 comments on commit aef082d

Please sign in to comment.