terraform: read/write diff to binary format
This commit is contained in:
parent
965d403d3d
commit
e5b82931ff
|
@ -2,7 +2,9 @@ package terraform
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/gob"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -14,6 +16,24 @@ type Diff struct {
|
||||||
once sync.Once
|
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() {
|
func (d *Diff) init() {
|
||||||
d.once.Do(func() {
|
d.once.Do(func() {
|
||||||
if d.Resources == nil {
|
if d.Resources == nil {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package terraform
|
package terraform
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"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 = `
|
const diffStrBasic = `
|
||||||
CREATE: nodeA
|
CREATE: nodeA
|
||||||
bar: "foo" => "<computed>"
|
bar: "foo" => "<computed>"
|
||||||
|
|
Loading…
Reference in New Issue