From d5a941a0a48967f264728a5aa4c1e7fd6c87fa36 Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Mon, 11 Jul 2016 16:35:01 -0500 Subject: [PATCH] command: Do not count data sources in plan totals Fixes #7483 --- command/hook_count.go | 6 ++++++ command/hook_count_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/command/hook_count.go b/command/hook_count.go index 5f3f514c2..3e642b254 100644 --- a/command/hook_count.go +++ b/command/hook_count.go @@ -1,6 +1,7 @@ package command import ( + "strings" "sync" "github.com/hashicorp/terraform/terraform" @@ -90,6 +91,11 @@ func (h *CountHook) PostDiff( h.Lock() defer h.Unlock() + // We don't count anything for data sources + if strings.HasPrefix(n.Id, "data.") { + return terraform.HookActionContinue, nil + } + switch d.ChangeType() { case terraform.DiffDestroyCreate: h.ToRemoveAndAdd += 1 diff --git a/command/hook_count_test.go b/command/hook_count_test.go index deb822699..5f0b000e8 100644 --- a/command/hook_count_test.go +++ b/command/hook_count_test.go @@ -182,3 +182,37 @@ func TestCountHookPostDiff_NoChange(t *testing.T) { expected, h) } } + +func TestCountHookPostDiff_DataSource(t *testing.T) { + h := new(CountHook) + + resources := map[string]*terraform.InstanceDiff{ + "data.foo": &terraform.InstanceDiff{ + Destroy: true, + }, + "data.bar": &terraform.InstanceDiff{}, + "data.lorem": &terraform.InstanceDiff{ + Destroy: false, + Attributes: map[string]*terraform.ResourceAttrDiff{ + "foo": &terraform.ResourceAttrDiff{}, + }, + }, + "data.ipsum": &terraform.InstanceDiff{Destroy: true}, + } + + for k, d := range resources { + n := &terraform.InstanceInfo{Id: k} + h.PostDiff(n, d) + } + + expected := new(CountHook) + expected.ToAdd = 0 + expected.ToChange = 0 + expected.ToRemoveAndAdd = 0 + expected.ToRemove = 0 + + if !reflect.DeepEqual(expected, h) { + t.Fatalf("Expected %#v, got %#v instead.", + expected, h) + } +}