From 2b917054da0716eda3f089d87c2fe6dec2b496e4 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 11 Feb 2015 17:50:07 -0800 Subject: [PATCH] terraform: orphan transform should work if state is nil --- terraform/transform_orphan.go | 5 +++++ terraform/transform_orphan_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/terraform/transform_orphan.go b/terraform/transform_orphan.go index 5c23f6147..8d05b556c 100644 --- a/terraform/transform_orphan.go +++ b/terraform/transform_orphan.go @@ -21,6 +21,11 @@ type OrphanTransformer struct { } func (t *OrphanTransformer) Transform(g *Graph) error { + if t.State == nil { + // If the entire state is nil, there can't be any orphans + return nil + } + var config *config.Config if module := t.Module.Child(g.Path[1:]); module != nil { config = module.Config() diff --git a/terraform/transform_orphan_test.go b/terraform/transform_orphan_test.go index b6900b4e9..17708c683 100644 --- a/terraform/transform_orphan_test.go +++ b/terraform/transform_orphan_test.go @@ -300,6 +300,29 @@ func TestOrphanTransformer_resourceDepends(t *testing.T) { } } +func TestOrphanTransformer_nilState(t *testing.T) { + mod := testModule(t, "transform-orphan-basic") + + g := Graph{Path: RootModulePath} + { + tf := &ConfigTransformer{Module: mod} + if err := tf.Transform(&g); err != nil { + t.Fatalf("err: %s", err) + } + } + + transform := &OrphanTransformer{State: nil, Module: mod} + if err := transform.Transform(&g); err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(g.String()) + expected := strings.TrimSpace(testTransformOrphanNilStateStr) + if actual != expected { + t.Fatalf("bad:\n\n%s", actual) + } +} + func TestGraphNodeOrphanModule_impl(t *testing.T) { var _ dag.Vertex = new(graphNodeOrphanModule) var _ dag.NamedVertex = new(graphNodeOrphanModule) @@ -342,6 +365,10 @@ module.foo (orphan) aws_instance.web (orphan) ` +const testTransformOrphanNilStateStr = ` +aws_instance.web +` + const testTransformOrphanResourceDependsStr = ` aws_instance.db (orphan) aws_instance.web