diff --git a/terraform/graph.go b/terraform/graph.go index 46df1b4e4..e8741e5d6 100644 --- a/terraform/graph.go +++ b/terraform/graph.go @@ -1088,10 +1088,16 @@ func graphAddTainted(g *depgraph.Graph, mod *ModuleState) { continue } - // Find the untainted resource of this in the noun list + // Find the untainted resource of this in the noun list. If our + // name is 3 parts, then we mus be a count instance, so our + // untainted node is just the noun without the count. var untainted *depgraph.Noun + untaintedK := k + if ps := strings.Split(k, "."); len(ps) == 3 { + untaintedK = strings.Join(ps[0:2], ".") + } for _, n := range g.Nouns { - if n.Name == k { + if n.Name == untaintedK { untainted = n break } diff --git a/terraform/graph_test.go b/terraform/graph_test.go index 58a6d1c08..a9b8194e5 100644 --- a/terraform/graph_test.go +++ b/terraform/graph_test.go @@ -43,6 +43,38 @@ func TestGraph_count(t *testing.T) { } } +func TestGraph_countTainted(t *testing.T) { + m := testModule(t, "graph-count") + state := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: []string{"root"}, + Resources: map[string]*ResourceState{ + "aws_instance.web.0": &ResourceState{ + Type: "aws_instance", + Tainted: []*InstanceState{ + &InstanceState{ + ID: "foo", + }, + }, + }, + }, + }, + }, + } + + g, err := Graph(&GraphOpts{Module: m, State: state}) + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(g.String()) + expected := strings.TrimSpace(testTerraformGraphCountTaintedStr) + if actual != expected { + t.Fatalf("bad:\n\n%s", actual) + } +} + func TestGraph_varResource(t *testing.T) { m := testModule(t, "graph-count-var-resource") @@ -1069,6 +1101,19 @@ root root -> aws_load_balancer.weblb ` +const testTerraformGraphCountTaintedStr = ` +root: root +aws_instance.web + aws_instance.web -> aws_instance.web.0 (tainted #1) +aws_instance.web.0 (tainted #1) +aws_load_balancer.weblb + aws_load_balancer.weblb -> aws_instance.web +root + root -> aws_instance.web + root -> aws_instance.web.0 (tainted #1) + root -> aws_load_balancer.weblb +` + const testTerraformGraphCountVarResourceStr = ` root: root aws_instance.foo