terraform/terraform/diff.go

255 lines
5.3 KiB
Go
Raw Normal View History

package terraform
2014-06-05 11:32:10 +02:00
import (
2014-06-10 20:22:32 +02:00
"bytes"
"encoding/gob"
"errors"
2014-06-10 20:22:32 +02:00
"fmt"
"io"
2014-06-10 20:22:32 +02:00
"sort"
"strings"
2014-06-05 11:32:10 +02:00
"sync"
)
2014-06-19 05:46:46 +02:00
// The format byte is prefixed into the diff file format so that we have
// the ability in the future to change the file format if we want for any
// reason.
const diffFormatByte byte = 1
// Diff tracks the differences between resources to apply.
type Diff struct {
2014-06-10 20:22:32 +02:00
Resources map[string]*ResourceDiff
2014-06-05 11:32:10 +02:00
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
var formatByte [1]byte
n, err := src.Read(formatByte[:])
if err != nil {
return nil, err
}
if n != len(formatByte) {
return nil, errors.New("failed to read diff version byte")
}
if formatByte[0] != diffFormatByte {
return nil, fmt.Errorf("unknown diff file version: %d", formatByte[0])
}
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 {
n, err := dst.Write([]byte{diffFormatByte})
if err != nil {
return err
}
if n != 1 {
return errors.New("failed to write diff version byte")
}
return gob.NewEncoder(dst).Encode(d)
}
2014-06-05 11:32:10 +02:00
func (d *Diff) init() {
d.once.Do(func() {
2014-06-10 20:22:32 +02:00
if d.Resources == nil {
d.Resources = make(map[string]*ResourceDiff)
}
2014-06-05 11:32:10 +02:00
})
}
2014-06-19 23:57:36 +02:00
// 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 !rd.Empty() {
2014-06-19 23:57:36 +02:00
return false
}
}
return true
}
2014-06-10 20:22:32 +02:00
// 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 {
rdiff := d.Resources[name]
crud := "UPDATE"
2014-06-26 06:58:33 +02:00
if rdiff.RequiresNew() && rdiff.Destroy {
crud = "DESTROY/CREATE"
} else if rdiff.Destroy {
crud = "DESTROY"
} else if rdiff.RequiresNew() {
crud = "CREATE"
}
buf.WriteString(fmt.Sprintf(
"%s: %s\n",
crud,
name))
2014-06-10 20:22:32 +02:00
keyLen := 0
keys := make([]string, 0, len(rdiff.Attributes))
for key, _ := range rdiff.Attributes {
2014-07-09 01:58:31 +02:00
if key == "id" {
continue
}
keys = append(keys, key)
if len(key) > keyLen {
keyLen = len(key)
}
}
sort.Strings(keys)
for _, attrK := range keys {
attrDiff := rdiff.Attributes[attrK]
2014-06-10 20:22:32 +02:00
v := attrDiff.New
if attrDiff.NewComputed {
v = "<computed>"
}
newResource := ""
if attrDiff.RequiresNew {
newResource = " (forces new resource)"
}
2014-06-10 20:22:32 +02:00
buf.WriteString(fmt.Sprintf(
" %s:%s %#v => %#v%s\n",
2014-06-10 20:22:32 +02:00
attrK,
strings.Repeat(" ", keyLen-len(attrK)),
2014-06-10 20:22:32 +02:00
attrDiff.Old,
v,
newResource))
2014-06-10 20:22:32 +02:00
}
}
return buf.String()
}
// ResourceDiff is the diff of a resource from some state to another.
type ResourceDiff struct {
Attributes map[string]*ResourceAttrDiff
2014-06-24 21:54:05 +02:00
Destroy bool
2014-07-09 01:58:31 +02:00
once sync.Once
}
// ResourceAttrDiff is the diff of a single attribute of a resource.
type ResourceAttrDiff struct {
Old string // Old Value
New string // New Value
NewComputed bool // True if new value is computed (unknown currently)
NewRemoved bool // True if this attribute is being removed
NewExtra interface{} // Extra information for the provider
RequiresNew bool // True if change requires new resource
Type DiffAttrType
}
// DiffAttrType is an enum type that says whether a resource attribute
// diff is an input attribute (comes from the configuration) or an
// output attribute (comes as a result of applying the configuration). An
// example input would be "ami" for AWS and an example output would be
// "private_ip".
type DiffAttrType byte
const (
DiffAttrUnknown DiffAttrType = iota
DiffAttrInput
DiffAttrOutput
)
2014-07-09 01:58:31 +02:00
func (d *ResourceDiff) init() {
d.once.Do(func() {
if d.Attributes == nil {
d.Attributes = make(map[string]*ResourceAttrDiff)
}
})
}
2014-06-20 20:03:33 +02:00
// Empty returns true if this diff encapsulates no changes.
func (d *ResourceDiff) Empty() bool {
if d == nil {
return true
}
2014-06-26 06:58:33 +02:00
return !d.Destroy && len(d.Attributes) == 0
2014-06-20 20:03:33 +02:00
}
// RequiresNew returns true if the diff requires the creation of a new
// resource (implying the destruction of the old).
func (d *ResourceDiff) RequiresNew() bool {
2014-06-18 03:10:38 +02:00
if d == nil {
return false
}
for _, rd := range d.Attributes {
2014-07-23 04:32:46 +02:00
if rd != nil && rd.RequiresNew {
return true
}
}
return false
}
2014-07-23 04:32:46 +02:00
// Same checks whether or not to ResourceDiffs are the "same." When
// we say "same", it is not necessarily exactly equal. Instead, it is
// just checking that the same attributes are changing, a destroy
// isn't suddenly happening, etc.
func (d *ResourceDiff) Same(d2 *ResourceDiff) bool {
if d == nil && d2 == nil {
return true
} else if d == nil || d2 == nil {
return false
}
2014-07-23 04:32:46 +02:00
if d.Destroy != d2.Destroy {
return false
}
if d.RequiresNew() != d2.RequiresNew() {
return false
}
if len(d.Attributes) != len(d2.Attributes) {
return false
}
ks := make(map[string]struct{})
for k, _ := range d.Attributes {
ks[k] = struct{}{}
}
for k, _ := range d2.Attributes {
delete(ks, k)
}
if len(ks) > 0 {
return false
}
return true
}