terraform: add some helpers to ModuleDiff and InstanceDiff for UX

This commit is contained in:
Mitchell Hashimoto 2014-09-24 17:51:45 -07:00
parent 72e6f97093
commit a12f785211
2 changed files with 212 additions and 4 deletions

View File

@ -9,6 +9,18 @@ import (
"strings"
)
// DiffChangeType is an enum with the kind of changes a diff has planned.
type DiffChangeType byte
const (
DiffInvalid DiffChangeType = iota
DiffNone
DiffCreate
DiffUpdate
DiffDestroy
DiffDestroyCreate
)
// Diff trackes the changes that are necessary to apply a configuration
// to an existing infrastructure.
type Diff struct {
@ -113,6 +125,33 @@ func (d *ModuleDiff) init() {
}
}
// ChangeType returns the type of changes that the diff for this
// module includes.
//
// At a module level, this will only be DiffNone, DiffUpdate, DiffDestroy, or
// DiffCreate. If an instance within the module has a DiffDestroyCreate
// then this will register as a DiffCreate for a module.
func (d *ModuleDiff) ChangeType() DiffChangeType {
result := DiffNone
for _, r := range d.Resources {
change := r.ChangeType()
switch change {
case DiffCreate:
fallthrough
case DiffDestroy:
if result == DiffNone {
result = change
}
case DiffDestroyCreate:
fallthrough
case DiffUpdate:
result = DiffUpdate
}
}
return result
}
// Empty returns true if the diff has no changes within this module.
func (d *ModuleDiff) Empty() bool {
if len(d.Resources) == 0 {
@ -237,6 +276,28 @@ func (d *InstanceDiff) init() {
}
}
// ChangeType returns the DiffChangeType represented by the diff
// for this single instance.
func (d *InstanceDiff) ChangeType() DiffChangeType {
if d.Empty() {
return DiffNone
}
if d.RequiresNew() && (d.Destroy || d.DestroyTainted) {
return DiffDestroyCreate
}
if d.Destroy {
return DiffDestroy
}
if d.RequiresNew() {
return DiffCreate
}
return DiffUpdate
}
// Empty returns true if this diff encapsulates no changes.
func (d *InstanceDiff) Empty() bool {
if d == nil {

View File

@ -26,6 +26,81 @@ func TestDiffEmpty(t *testing.T) {
}
}
func TestModuleDiff_ChangeType(t *testing.T) {
cases := []struct {
Diff *ModuleDiff
Result DiffChangeType
}{
{
&ModuleDiff{},
DiffNone,
},
{
&ModuleDiff{
Resources: map[string]*InstanceDiff{
"foo": &InstanceDiff{Destroy: true},
},
},
DiffDestroy,
},
{
&ModuleDiff{
Resources: map[string]*InstanceDiff{
"foo": &InstanceDiff{
Attributes: map[string]*ResourceAttrDiff{
"foo": &ResourceAttrDiff{
Old: "",
New: "bar",
},
},
},
},
},
DiffUpdate,
},
{
&ModuleDiff{
Resources: map[string]*InstanceDiff{
"foo": &InstanceDiff{
Attributes: map[string]*ResourceAttrDiff{
"foo": &ResourceAttrDiff{
Old: "",
New: "bar",
RequiresNew: true,
},
},
},
},
},
DiffCreate,
},
{
&ModuleDiff{
Resources: map[string]*InstanceDiff{
"foo": &InstanceDiff{
Destroy: true,
Attributes: map[string]*ResourceAttrDiff{
"foo": &ResourceAttrDiff{
Old: "",
New: "bar",
RequiresNew: true,
},
},
},
},
},
DiffUpdate,
},
}
for i, tc := range cases {
actual := tc.Diff.ChangeType()
if actual != tc.Result {
t.Fatalf("%d: %s", i, actual)
}
}
}
func TestModuleDiff_Empty(t *testing.T) {
diff := new(ModuleDiff)
if !diff.Empty() {
@ -89,7 +164,79 @@ func TestModuleDiff_String(t *testing.T) {
}
}
func TestResourceDiff_Empty(t *testing.T) {
func TestInstanceDiff_ChangeType(t *testing.T) {
cases := []struct {
Diff *InstanceDiff
Result DiffChangeType
}{
{
&InstanceDiff{},
DiffNone,
},
{
&InstanceDiff{Destroy: true},
DiffDestroy,
},
{
&InstanceDiff{
Attributes: map[string]*ResourceAttrDiff{
"foo": &ResourceAttrDiff{
Old: "",
New: "bar",
},
},
},
DiffUpdate,
},
{
&InstanceDiff{
Attributes: map[string]*ResourceAttrDiff{
"foo": &ResourceAttrDiff{
Old: "",
New: "bar",
RequiresNew: true,
},
},
},
DiffCreate,
},
{
&InstanceDiff{
Destroy: true,
Attributes: map[string]*ResourceAttrDiff{
"foo": &ResourceAttrDiff{
Old: "",
New: "bar",
RequiresNew: true,
},
},
},
DiffDestroyCreate,
},
{
&InstanceDiff{
DestroyTainted: true,
Attributes: map[string]*ResourceAttrDiff{
"foo": &ResourceAttrDiff{
Old: "",
New: "bar",
RequiresNew: true,
},
},
},
DiffDestroyCreate,
},
}
for i, tc := range cases {
actual := tc.Diff.ChangeType()
if actual != tc.Result {
t.Fatalf("%d: %s", i, actual)
}
}
}
func TestInstanceDiff_Empty(t *testing.T) {
var rd *InstanceDiff
if !rd.Empty() {
@ -121,7 +268,7 @@ func TestResourceDiff_Empty(t *testing.T) {
}
}
func TestResourceDiff_RequiresNew(t *testing.T) {
func TestInstanceDiff_RequiresNew(t *testing.T) {
rd := &InstanceDiff{
Attributes: map[string]*ResourceAttrDiff{
"foo": &ResourceAttrDiff{},
@ -139,7 +286,7 @@ func TestResourceDiff_RequiresNew(t *testing.T) {
}
}
func TestResourceDiff_RequiresNew_nil(t *testing.T) {
func TestInstanceDiff_RequiresNew_nil(t *testing.T) {
var rd *InstanceDiff
if rd.RequiresNew() {
@ -147,7 +294,7 @@ func TestResourceDiff_RequiresNew_nil(t *testing.T) {
}
}
func TestResourceDiffSame(t *testing.T) {
func TestInstanceDiffSame(t *testing.T) {
cases := []struct {
One, Two *InstanceDiff
Same bool