2014-05-28 22:56:43 +02:00
|
|
|
package terraform
|
|
|
|
|
2014-06-05 11:32:10 +02:00
|
|
|
import (
|
2014-06-10 20:22:32 +02:00
|
|
|
"bytes"
|
2014-06-19 05:34:39 +02:00
|
|
|
"encoding/gob"
|
2014-06-19 05:46:13 +02:00
|
|
|
"errors"
|
2014-06-10 20:22:32 +02:00
|
|
|
"fmt"
|
2014-06-19 05:34:39 +02:00
|
|
|
"io"
|
2014-06-10 20:22:32 +02:00
|
|
|
"sort"
|
2014-06-10 20:30:54 +02:00
|
|
|
"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.
|
2014-06-19 05:46:13 +02:00
|
|
|
const diffFormatByte byte = 1
|
|
|
|
|
2014-05-28 22:56:43 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2014-06-19 05:34:39 +02:00
|
|
|
// 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
|
|
|
|
|
2014-06-19 05:46:13 +02:00
|
|
|
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])
|
|
|
|
}
|
|
|
|
|
2014-06-19 05:34:39 +02:00
|
|
|
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 {
|
2014-06-19 05:46:13 +02:00
|
|
|
n, err := dst.Write([]byte{diffFormatByte})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n != 1 {
|
|
|
|
return errors.New("failed to write diff version byte")
|
|
|
|
}
|
|
|
|
|
2014-06-19 05:34:39 +02:00
|
|
|
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-05-28 22:56:43 +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 {
|
2014-06-26 07:10:25 +02:00
|
|
|
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 {
|
2014-06-10 20:27:17 +02:00
|
|
|
rdiff := d.Resources[name]
|
|
|
|
|
2014-06-10 20:37:04 +02:00
|
|
|
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() {
|
2014-06-10 20:37:04 +02:00
|
|
|
crud = "CREATE"
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf(
|
|
|
|
"%s: %s\n",
|
|
|
|
crud,
|
|
|
|
name))
|
2014-06-10 20:22:32 +02:00
|
|
|
|
2014-06-10 20:30:54 +02:00
|
|
|
keyLen := 0
|
2014-06-10 20:27:17 +02:00
|
|
|
keys := make([]string, 0, len(rdiff.Attributes))
|
|
|
|
for key, _ := range rdiff.Attributes {
|
2014-07-09 01:58:31 +02:00
|
|
|
if key == "id" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-06-10 20:27:17 +02:00
|
|
|
keys = append(keys, key)
|
2014-06-10 20:30:54 +02:00
|
|
|
if len(key) > keyLen {
|
|
|
|
keyLen = len(key)
|
|
|
|
}
|
2014-06-10 20:27:17 +02:00
|
|
|
}
|
|
|
|
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>"
|
|
|
|
}
|
|
|
|
|
2014-06-10 20:33:59 +02:00
|
|
|
newResource := ""
|
|
|
|
if attrDiff.RequiresNew {
|
|
|
|
newResource = " (forces new resource)"
|
|
|
|
}
|
|
|
|
|
2014-06-10 20:22:32 +02:00
|
|
|
buf.WriteString(fmt.Sprintf(
|
2014-06-10 20:33:59 +02:00
|
|
|
" %s:%s %#v => %#v%s\n",
|
2014-06-10 20:22:32 +02:00
|
|
|
attrK,
|
2014-06-10 20:30:54 +02:00
|
|
|
strings.Repeat(" ", keyLen-len(attrK)),
|
2014-06-10 20:22:32 +02:00
|
|
|
attrDiff.Old,
|
2014-06-10 20:33:59 +02:00
|
|
|
v,
|
|
|
|
newResource))
|
2014-06-10 20:22:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
2014-06-10 20:37:04 +02:00
|
|
|
|
|
|
|
// 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
|
2014-06-10 20:37:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceAttrDiff is the diff of a single attribute of a resource.
|
|
|
|
type ResourceAttrDiff struct {
|
2014-07-01 20:23:40 +02:00
|
|
|
Old string // Old Value
|
|
|
|
New string // New Value
|
|
|
|
NewComputed bool // True if new value is computed (unknown currently)
|
2014-07-09 18:51:36 +02:00
|
|
|
NewRemoved bool // True if this attribute is being removed
|
2014-07-01 20:23:40 +02:00
|
|
|
NewExtra interface{} // Extra information for the provider
|
|
|
|
RequiresNew bool // True if change requires new resource
|
2014-06-23 21:49:30 +02:00
|
|
|
Type DiffAttrType
|
2014-06-10 20:37:04 +02:00
|
|
|
}
|
|
|
|
|
2014-06-23 21:49:30 +02:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2014-06-10 20:37:04 +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
|
|
|
|
}
|
|
|
|
|
2014-06-10 20:37:04 +02:00
|
|
|
for _, rd := range d.Attributes {
|
2014-07-23 04:32:46 +02:00
|
|
|
if rd != nil && rd.RequiresNew {
|
2014-06-10 20:37:04 +02:00
|
|
|
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 {
|
2014-07-23 04:39:48 +02:00
|
|
|
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
|
|
|
|
}
|