diff --git a/terraform/graph.go b/terraform/graph.go index d7da7d098..4a8528090 100644 --- a/terraform/graph.go +++ b/terraform/graph.go @@ -1190,8 +1190,20 @@ func nounAddVariableDeps( name = fmt.Sprintf("module.%s", v.Name) target = g.Noun(name) case *config.ResourceVariable: - name = v.ResourceId() - target = g.Noun(v.ResourceId()) + // For resource variables, if we ourselves are a resource, then + // we have to check whether to expand or not to create the proper + // resource dependency. + rn, ok := n.Meta.(*GraphNodeResource) + if !ok || rn.ExpandMode > ResourceExpandNone { + name = v.ResourceId() + target = g.Noun(v.ResourceId()) + break + } + + // We're an expanded resource, so add the specific index + // as the dependency. + name = fmt.Sprintf("%s.%d", v.ResourceId(), v.Index) + target = g.Noun(name) default: } @@ -1640,6 +1652,9 @@ func (n *GraphNodeResource) Expand() (*depgraph.Graph, error) { } } + // Add all the variable dependencies + graphAddVariableDeps(g) + // If we're just expanding the apply, then filter those out and // return them now. if n.ExpandMode == ResourceExpandApply { diff --git a/terraform/graph_test.go b/terraform/graph_test.go index 237c5a73f..238e96f14 100644 --- a/terraform/graph_test.go +++ b/terraform/graph_test.go @@ -1007,6 +1007,38 @@ func TestGraphNodeResourceExpand(t *testing.T) { } } +func TestGraphNodeResourceExpand_provDeps(t *testing.T) { + m := testModule(t, "graph-resource-expand-prov-deps") + provs := map[string]ResourceProvisionerFactory{ + "remote-exec": func() (ResourceProvisioner, error) { + return new(MockResourceProvisioner), nil + }, + } + + g, err := Graph(&GraphOpts{Module: m, Provisioners: provs}) + if err != nil { + t.Fatalf("err: %s", err) + } + + // Get the resource we care about expanding + n := g.Noun("aws_instance.web") + if n == nil { + t.Fatal("could not find") + } + rn := n.Meta.(*GraphNodeResource) + + g, err = rn.Expand() + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(g.String()) + expected := strings.TrimSpace(testTerraformGraphResourceExpandProvDepsStr) + if actual != expected { + t.Fatalf("bad:\n\nactual:\n%s\n\nexpected:\n%s", actual, expected) + } +} + /* func TestGraphNodeResourceExpand_tainted(t *testing.T) { m := testModule(t, "graph-resource-expand") @@ -1381,6 +1413,19 @@ root root -> aws_instance.web.2 ` +const testTerraformGraphResourceExpandProvDepsStr = ` +root: root +aws_instance.web.0 +aws_instance.web.1 + aws_instance.web.1 -> aws_instance.web.0 +aws_instance.web.2 + aws_instance.web.2 -> aws_instance.web.0 +root + root -> aws_instance.web.0 + root -> aws_instance.web.1 + root -> aws_instance.web.2 +` + const testTerraformGraphResourceExpandTaintStr = ` root: root aws_instance.web.0 diff --git a/terraform/test-fixtures/graph-resource-expand-prov-deps/main.tf b/terraform/test-fixtures/graph-resource-expand-prov-deps/main.tf new file mode 100644 index 000000000..a8c8efd85 --- /dev/null +++ b/terraform/test-fixtures/graph-resource-expand-prov-deps/main.tf @@ -0,0 +1,7 @@ +resource "aws_instance" "web" { + count = 3 + + provisioner "remote-exec" { + inline = ["echo ${aws_instance.web.0.foo}"] + } +}