From 95e788f13b35eecfa1fc1ff0bf6a59790d2917d0 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 20 May 2021 09:36:45 -0400 Subject: [PATCH] deposed instances should not be counted as orphans Do not add orphan nodes for resources that only contain deposed instances. --- .../terraform/transform_orphan_resource.go | 7 ++++++- .../transform_orphan_resource_test.go | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/internal/terraform/transform_orphan_resource.go b/internal/terraform/transform_orphan_resource.go index b52334ea2..af8302c49 100644 --- a/internal/terraform/transform_orphan_resource.go +++ b/internal/terraform/transform_orphan_resource.go @@ -79,7 +79,12 @@ func (t *OrphanResourceInstanceTransformer) transform(g *Graph, ms *states.Modul } } - for key := range rs.Instances { + for key, inst := range rs.Instances { + // deposed instances will be taken care of separately + if inst.Current == nil { + continue + } + addr := rs.Addr.Instance(key) abstract := NewNodeAbstractResourceInstance(addr) var node dag.Vertex = abstract diff --git a/internal/terraform/transform_orphan_resource_test.go b/internal/terraform/transform_orphan_resource_test.go index 2f638276e..f44f08152 100644 --- a/internal/terraform/transform_orphan_resource_test.go +++ b/internal/terraform/transform_orphan_resource_test.go @@ -50,6 +50,26 @@ func TestOrphanResourceInstanceTransformer(t *testing.T) { Module: addrs.RootModule, }, ) + + // A deposed orphan should not be handled by this transformer + s.SetResourceInstanceDeposed( + addrs.Resource{ + Mode: addrs.ManagedResourceMode, + Type: "test_instance", + Name: "deposed", + }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance), + states.NewDeposedKey(), + &states.ResourceInstanceObjectSrc{ + AttrsFlat: map[string]string{ + "id": "foo", + }, + Status: states.ObjectReady, + }, + addrs.AbsProviderConfig{ + Provider: addrs.NewDefaultProvider("test"), + Module: addrs.RootModule, + }, + ) }) g := Graph{Path: addrs.RootModuleInstance}