From 1449d8a51039562a4ae00bfcd1a07fcc939beafd Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 19 Jun 2014 14:57:36 -0700 Subject: [PATCH] terraform: Diff.Empty --- terraform/diff.go | 15 +++++++++++++++ terraform/diff_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/terraform/diff.go b/terraform/diff.go index e8f428d0e..0dd1171e1 100644 --- a/terraform/diff.go +++ b/terraform/diff.go @@ -69,6 +69,21 @@ func (d *Diff) init() { }) } +// Empty returns true if the diff has no changes. +func (d *Diff) Empty() bool { + if len(d.Resources) == 0 { + return true + } + + for _, rd := range d.Resources { + if len(rd.Attributes) > 0 { + return false + } + } + + return true +} + // String outputs the diff in a long but command-line friendly output // format that users can read to quickly inspect a diff. func (d *Diff) String() string { diff --git a/terraform/diff_test.go b/terraform/diff_test.go index 5268ba32a..5daf21837 100644 --- a/terraform/diff_test.go +++ b/terraform/diff_test.go @@ -7,6 +7,32 @@ import ( "testing" ) +func TestDiff_Empty(t *testing.T) { + diff := new(Diff) + if !diff.Empty() { + t.Fatal("should be empty") + } + + diff.Resources = map[string]*ResourceDiff{ + "nodeA": &ResourceDiff{}, + } + + if !diff.Empty() { + t.Fatal("should be empty") + } + + diff.Resources["nodeA"].Attributes = map[string]*ResourceAttrDiff{ + "foo": &ResourceAttrDiff{ + Old: "foo", + New: "bar", + }, + } + + if diff.Empty() { + t.Fatal("should not be empty") + } +} + func TestDiff_String(t *testing.T) { diff := &Diff{ Resources: map[string]*ResourceDiff{