terraform: read/write diff to binary format

This commit is contained in:
Mitchell Hashimoto 2014-06-18 20:34:39 -07:00
parent 965d403d3d
commit e5b82931ff
2 changed files with 60 additions and 0 deletions

View File

@ -2,7 +2,9 @@ package terraform
import (
"bytes"
"encoding/gob"
"fmt"
"io"
"sort"
"strings"
"sync"
@ -14,6 +16,24 @@ type Diff struct {
once sync.Once
}
// ReadDiff reads a diff structure out of a reader in the format that
// was written by WriteDiff.
func ReadDiff(src io.Reader) (*Diff, error) {
var result *Diff
dec := gob.NewDecoder(src)
if err := dec.Decode(&result); err != nil {
return nil, err
}
return result, nil
}
// WriteDiff writes a diff somewhere in a binary format.
func WriteDiff(d *Diff, dst io.Writer) error {
return gob.NewEncoder(dst).Encode(d)
}
func (d *Diff) init() {
d.once.Do(func() {
if d.Resources == nil {

View File

@ -1,6 +1,8 @@
package terraform
import (
"bytes"
"reflect"
"strings"
"testing"
)
@ -61,6 +63,44 @@ func TestResourceDiff_RequiresNew_nil(t *testing.T) {
}
}
func TestReadWriteDiff(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,
},
"longfoo": &ResourceAttrDiff{
Old: "foo",
New: "bar",
RequiresNew: true,
},
},
},
},
}
buf := new(bytes.Buffer)
if err := WriteDiff(diff, buf); err != nil {
t.Fatalf("err: %s", err)
}
actual, err := ReadDiff(buf)
if err != nil {
t.Fatalf("err: %s", err)
}
if !reflect.DeepEqual(actual, diff) {
t.Fatalf("bad: %#v", actual)
}
}
const diffStrBasic = `
CREATE: nodeA
bar: "foo" => "<computed>"