From 32afc6dc70ebb9dbc592d905064bd5a4d5d8023b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 10 Jun 2014 11:22:32 -0700 Subject: [PATCH] terraform: Diff.String --- terraform/diff.go | 41 ++++++++++++++++++++++++-- terraform/diff_test.go | 58 ++++++++++++++++++------------------- terraform/terraform.go | 2 +- terraform/terraform_test.go | 4 +-- 4 files changed, 71 insertions(+), 34 deletions(-) diff --git a/terraform/diff.go b/terraform/diff.go index 015fb7895..713c0ad17 100644 --- a/terraform/diff.go +++ b/terraform/diff.go @@ -1,21 +1,58 @@ package terraform import ( + "bytes" + "fmt" + "sort" "sync" ) // Diff tracks the differences between resources to apply. type Diff struct { - Resources map[string]map[string]*ResourceAttrDiff + Resources map[string]*ResourceDiff once sync.Once } func (d *Diff) init() { d.once.Do(func() { - d.Resources = make(map[string]map[string]*ResourceAttrDiff) + if d.Resources == nil { + d.Resources = make(map[string]*ResourceDiff) + } }) } +// 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 { + var buf bytes.Buffer + + names := make([]string, 0, len(d.Resources)) + for name, _ := range d.Resources { + names = append(names, name) + } + sort.Strings(names) + + for _, name := range names { + buf.WriteString(name + "\n") + + rdiff := d.Resources[name] + for attrK, attrDiff := range rdiff.Attributes { + v := attrDiff.New + if attrDiff.NewComputed { + v = "" + } + + buf.WriteString(fmt.Sprintf( + " %s: %#v => %#v\n", + attrK, + attrDiff.Old, + v)) + } + } + + return buf.String() +} + // ResourceDiff is the diff of a resource from some state to another. type ResourceDiff struct { Attributes map[string]*ResourceAttrDiff diff --git a/terraform/diff_test.go b/terraform/diff_test.go index efc16e29a..d11bff4c9 100644 --- a/terraform/diff_test.go +++ b/terraform/diff_test.go @@ -1,37 +1,37 @@ package terraform import ( - "bytes" - "fmt" - "sort" + "strings" + "testing" ) -func testDiffStr(d *Diff) string { - var buf bytes.Buffer - - names := make([]string, len(d.Resources)) - for n, _ := range d.Resources { - names = append(names, n) - } - sort.Strings(names) - - for _, n := range names { - r := d.Resources[n] - buf.WriteString(fmt.Sprintf("%s\n", n)) - for attr, attrDiff := range r { - v := attrDiff.New - if attrDiff.NewComputed { - v = "" - } - - buf.WriteString(fmt.Sprintf( - " %s: %#v => %#v\n", - attr, - attrDiff.Old, - v, - )) - } +func TestDiff_String(t *testing.T) { + diff := &Diff{ + Resources: map[string]*ResourceDiff{ + "nodeA": &ResourceDiff{ + Attributes: map[string]*ResourceAttrDiff{ + "foo": &ResourceAttrDiff{ + Old: "foo", + New: "bar", + }, + "bar": &ResourceAttrDiff{ + Old: "foo", + NewComputed: true, + }, + }, + }, + }, } - return buf.String() + actual := strings.TrimSpace(diff.String()) + expected := strings.TrimSpace(diffStrBasic) + if actual != expected { + t.Fatalf("bad:\n%s", actual) + } } + +const diffStrBasic = ` +nodeA + foo: "foo" => "bar" + bar: "foo" => "" +` diff --git a/terraform/terraform.go b/terraform/terraform.go index 1f7f469ff..c2fc131ed 100644 --- a/terraform/terraform.go +++ b/terraform/terraform.go @@ -163,7 +163,7 @@ func (t *Terraform) diffWalkFn( defer l.Unlock() // Update the resulting diff - result.Resources[r.Id()] = diff.Attributes + result.Resources[r.Id()] = diff // Determine the new state and update variables rs = rs.MergeDiff(diff.Attributes, ComputedPlaceholder) diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 7f4ef8211..901f0891e 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -195,7 +195,7 @@ func TestTerraformDiff(t *testing.T) { t.Fatalf("bad: %#v", diff.Resources) } - actual := strings.TrimSpace(testDiffStr(diff)) + actual := strings.TrimSpace(diff.String()) expected := strings.TrimSpace(testTerraformDiffStr) if actual != expected { t.Fatalf("bad:\n%s", actual) @@ -226,7 +226,7 @@ func TestTerraformDiff_computed(t *testing.T) { t.Fatalf("bad: %#v", diff.Resources) } - actual := strings.TrimSpace(testDiffStr(diff)) + actual := strings.TrimSpace(diff.String()) expected := strings.TrimSpace(testTerraformDiffComputedStr) if actual != expected { t.Fatalf("bad:\n%s", actual)