2014-05-28 15:56:43 -05:00
|
|
|
package terraform
|
|
|
|
|
2014-06-05 04:32:10 -05:00
|
|
|
import (
|
2014-06-10 13:22:32 -05:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
2014-06-10 13:30:54 -05:00
|
|
|
"strings"
|
2014-06-05 04:32:10 -05:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2014-05-28 15:56:43 -05:00
|
|
|
// Diff tracks the differences between resources to apply.
|
|
|
|
type Diff struct {
|
2014-09-17 18:33:24 -05:00
|
|
|
Resources map[string]*InstanceDiff
|
2014-06-05 04:32:10 -05:00
|
|
|
once sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Diff) init() {
|
|
|
|
d.once.Do(func() {
|
2014-06-10 13:22:32 -05:00
|
|
|
if d.Resources == nil {
|
2014-09-17 18:33:24 -05:00
|
|
|
d.Resources = make(map[string]*InstanceDiff)
|
2014-06-10 13:22:32 -05:00
|
|
|
}
|
2014-06-05 04:32:10 -05:00
|
|
|
})
|
2014-05-28 15:56:43 -05:00
|
|
|
}
|
|
|
|
|
2014-06-19 16:57:36 -05: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 00:10:25 -05:00
|
|
|
if !rd.Empty() {
|
2014-06-19 16:57:36 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-06-10 13:22:32 -05: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 13:27:17 -05:00
|
|
|
rdiff := d.Resources[name]
|
|
|
|
|
2014-06-10 13:37:04 -05:00
|
|
|
crud := "UPDATE"
|
2014-09-19 23:47:53 -05:00
|
|
|
if rdiff.RequiresNew() && (rdiff.Destroy || rdiff.DestroyTainted) {
|
2014-06-25 23:58:33 -05:00
|
|
|
crud = "DESTROY/CREATE"
|
|
|
|
} else if rdiff.Destroy {
|
|
|
|
crud = "DESTROY"
|
|
|
|
} else if rdiff.RequiresNew() {
|
2014-06-10 13:37:04 -05:00
|
|
|
crud = "CREATE"
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf(
|
|
|
|
"%s: %s\n",
|
|
|
|
crud,
|
|
|
|
name))
|
2014-06-10 13:22:32 -05:00
|
|
|
|
2014-06-10 13:30:54 -05:00
|
|
|
keyLen := 0
|
2014-06-10 13:27:17 -05:00
|
|
|
keys := make([]string, 0, len(rdiff.Attributes))
|
|
|
|
for key, _ := range rdiff.Attributes {
|
2014-07-08 18:58:31 -05:00
|
|
|
if key == "id" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-06-10 13:27:17 -05:00
|
|
|
keys = append(keys, key)
|
2014-06-10 13:30:54 -05:00
|
|
|
if len(key) > keyLen {
|
|
|
|
keyLen = len(key)
|
|
|
|
}
|
2014-06-10 13:27:17 -05:00
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, attrK := range keys {
|
|
|
|
attrDiff := rdiff.Attributes[attrK]
|
|
|
|
|
2014-06-10 13:22:32 -05:00
|
|
|
v := attrDiff.New
|
|
|
|
if attrDiff.NewComputed {
|
|
|
|
v = "<computed>"
|
|
|
|
}
|
|
|
|
|
2014-06-10 13:33:59 -05:00
|
|
|
newResource := ""
|
|
|
|
if attrDiff.RequiresNew {
|
|
|
|
newResource = " (forces new resource)"
|
|
|
|
}
|
|
|
|
|
2014-06-10 13:22:32 -05:00
|
|
|
buf.WriteString(fmt.Sprintf(
|
2014-06-10 13:33:59 -05:00
|
|
|
" %s:%s %#v => %#v%s\n",
|
2014-06-10 13:22:32 -05:00
|
|
|
attrK,
|
2014-06-10 13:30:54 -05:00
|
|
|
strings.Repeat(" ", keyLen-len(attrK)),
|
2014-06-10 13:22:32 -05:00
|
|
|
attrDiff.Old,
|
2014-06-10 13:33:59 -05:00
|
|
|
v,
|
|
|
|
newResource))
|
2014-06-10 13:22:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
2014-06-10 13:37:04 -05:00
|
|
|
|
2014-09-17 18:33:24 -05:00
|
|
|
// InstanceDiff is the diff of a resource from some state to another.
|
|
|
|
type InstanceDiff struct {
|
2014-09-19 23:47:53 -05:00
|
|
|
Attributes map[string]*ResourceAttrDiff
|
|
|
|
Destroy bool
|
|
|
|
DestroyTainted bool
|
2014-07-08 18:58:31 -05:00
|
|
|
|
|
|
|
once sync.Once
|
2014-06-10 13:37:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceAttrDiff is the diff of a single attribute of a resource.
|
|
|
|
type ResourceAttrDiff struct {
|
2014-07-01 13:23:40 -05:00
|
|
|
Old string // Old Value
|
|
|
|
New string // New Value
|
|
|
|
NewComputed bool // True if new value is computed (unknown currently)
|
2014-07-09 11:51:36 -05:00
|
|
|
NewRemoved bool // True if this attribute is being removed
|
2014-07-01 13:23:40 -05:00
|
|
|
NewExtra interface{} // Extra information for the provider
|
|
|
|
RequiresNew bool // True if change requires new resource
|
2014-06-23 14:49:30 -05:00
|
|
|
Type DiffAttrType
|
2014-06-10 13:37:04 -05:00
|
|
|
}
|
|
|
|
|
2014-07-26 23:02:34 -05:00
|
|
|
func (d *ResourceAttrDiff) GoString() string {
|
|
|
|
return fmt.Sprintf("*%#v", *d)
|
|
|
|
}
|
|
|
|
|
2014-06-23 14:49:30 -05: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-09-17 18:33:24 -05:00
|
|
|
func (d *InstanceDiff) init() {
|
2014-07-08 18:58:31 -05:00
|
|
|
d.once.Do(func() {
|
|
|
|
if d.Attributes == nil {
|
|
|
|
d.Attributes = make(map[string]*ResourceAttrDiff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-06-20 13:03:33 -05:00
|
|
|
// Empty returns true if this diff encapsulates no changes.
|
2014-09-17 18:33:24 -05:00
|
|
|
func (d *InstanceDiff) Empty() bool {
|
2014-06-20 13:03:33 -05:00
|
|
|
if d == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-06-25 23:58:33 -05:00
|
|
|
return !d.Destroy && len(d.Attributes) == 0
|
2014-06-20 13:03:33 -05:00
|
|
|
}
|
|
|
|
|
2014-06-10 13:37:04 -05:00
|
|
|
// RequiresNew returns true if the diff requires the creation of a new
|
|
|
|
// resource (implying the destruction of the old).
|
2014-09-17 18:33:24 -05:00
|
|
|
func (d *InstanceDiff) RequiresNew() bool {
|
2014-06-17 20:10:38 -05:00
|
|
|
if d == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-06-10 13:37:04 -05:00
|
|
|
for _, rd := range d.Attributes {
|
2014-07-22 21:32:46 -05:00
|
|
|
if rd != nil && rd.RequiresNew {
|
2014-06-10 13:37:04 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2014-07-22 21:32:46 -05:00
|
|
|
|
2014-09-17 18:33:24 -05:00
|
|
|
// Same checks whether or not to InstanceDiff are the "same." When
|
2014-07-22 21:32:46 -05:00
|
|
|
// 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.
|
2014-09-17 18:33:24 -05:00
|
|
|
func (d *InstanceDiff) Same(d2 *InstanceDiff) bool {
|
2014-07-22 21:39:48 -05:00
|
|
|
if d == nil && d2 == nil {
|
|
|
|
return true
|
|
|
|
} else if d == nil || d2 == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-07-22 21:32:46 -05: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
|
|
|
|
}
|