terraform: Diff.String
This commit is contained in:
parent
061d96a08b
commit
32afc6dc70
|
@ -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 = "<computed>"
|
||||
}
|
||||
|
||||
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
|
||||
|
|
|
@ -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 = "<computed>"
|
||||
}
|
||||
|
||||
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" => "<computed>"
|
||||
`
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue