Skip to content

Commit

Permalink
error out when a referenced provider is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
jbardin committed Nov 10, 2017
1 parent e81642d commit d1d33f5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
11 changes: 11 additions & 0 deletions terraform/test-fixtures/transform-provider-invalid/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
provider "aws" {
}

module "mod" {
source = "./mod"

# aws.foo doesn't exist, and should report an error
providers = {
"aws" = "aws.foo"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resource "aws_resource" "foo" {
}
3 changes: 2 additions & 1 deletion terraform/transform_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func (t *ProviderConfigTransformer) Transform(g *Graph) error {

// Start the transformation process
if err := t.transform(g, t.Module); err != nil {
return nil
return err
}

// finally attach the configs to the new nodes
Expand Down Expand Up @@ -524,6 +524,7 @@ func (t *ProviderConfigTransformer) addProxyProviders(g *Graph, m *module.Tree)
for name, parentName := range parentCfg.Providers {
fullName := ResolveProviderName(name, path)
fullParentName := ResolveProviderName(parentName, parentPath)

parentProvider := t.providers[fullParentName]

if parentProvider == nil {
Expand Down
29 changes: 29 additions & 0 deletions terraform/transform_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,35 @@ provider.aws.foo`)
}
}

// error out when a non-existent provider is named in a module providers map
func TestProviderConfigTransformer_invalidProvider(t *testing.T) {
mod := testModule(t, "transform-provider-invalid")
concrete := func(a *NodeAbstractProvider) dag.Vertex { return a }

g := Graph{Path: RootModulePath}
{
tf := &ConfigTransformer{Module: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
{
tf := &AttachResourceConfigTransformer{Module: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}

tf := TransformProviders([]string{"aws"}, concrete, mod)
err := tf.Transform(&g)
if err == nil {
t.Fatal("expected missing provider error")
}
if !strings.Contains(err.Error(), "provider.aws.foo") {
t.Fatalf("error should reference missing provider, got: %s", err)
}
}

const testTransformProviderBasicStr = `
aws_instance.web
provider.aws
Expand Down

0 comments on commit d1d33f5

Please sign in to comment.