Add inequality check to ignore redundant diff attributes

This commit is contained in:
Nathan Evans 2017-11-28 10:11:19 -07:00
parent ec9d4f1d0f
commit 45439d0ac3
4 changed files with 77 additions and 5 deletions

View File

@ -396,6 +396,11 @@ type ResourceAttrDiff struct {
Type DiffAttrType
}
// Modified returns the inequality of Old and New for this attr
func (d *ResourceAttrDiff) Modified() bool {
return d.Old != d.New
}
// Empty returns true if the diff for this attr is neutral
func (d *ResourceAttrDiff) Empty() bool {
return d.Old == d.New && !d.NewComputed && !d.NewRemoved

View File

@ -258,12 +258,14 @@ func (n *EvalDiff) processIgnoreChanges(diff *InstanceDiff) error {
for _, v := range containers {
if v.keepDiff() {
// At least one key has changes, so list all the sibling keys
// to keep in the diff.
// to keep in the diff if any values have changed
for k := range v {
if v[k].Modified() {
keep[k] = true
}
}
}
}
for k, v := range attrs {
if (v.Empty() || v.NewComputed) && !keep[k] {

View File

@ -3,6 +3,8 @@ package terraform
import (
"reflect"
"testing"
"github.com/hashicorp/terraform/config"
)
func TestEvalFilterDiff(t *testing.T) {
@ -76,3 +78,69 @@ func TestEvalFilterDiff(t *testing.T) {
}
}
}
func TestProcessIgnoreChangesOnResourceIgnoredWithRequiresNew(t *testing.T) {
var evalDiff *EvalDiff
var instanceDiff *InstanceDiff
var testDiffs = func(ignoreChanges []string, newAttribute string) (*EvalDiff, *InstanceDiff) {
return &EvalDiff{
Resource: &config.Resource{
Lifecycle: config.ResourceLifecycle{
IgnoreChanges: ignoreChanges,
},
},
},
&InstanceDiff{
Destroy: true,
Attributes: map[string]*ResourceAttrDiff{
"resource.changed": {
RequiresNew: true,
Type: DiffAttrInput,
Old: "old",
New: "new",
},
"resource.unchanged": {
Old: "unchanged",
New: newAttribute,
},
},
}
}
evalDiff, instanceDiff = testDiffs([]string{"resource.changed"}, "unchanged")
err := evalDiff.processIgnoreChanges(instanceDiff)
if err != nil {
t.Fatalf("err: %s", err)
}
if len(instanceDiff.Attributes) > 0 {
t.Fatalf("Expected all resources to be ignored, found %d", len(instanceDiff.Attributes))
}
evalDiff, instanceDiff = testDiffs([]string{}, "unchanged")
err = evalDiff.processIgnoreChanges(instanceDiff)
if err != nil {
t.Fatalf("err: %s", err)
}
if len(instanceDiff.Attributes) != 2 {
t.Fatalf("Expected 2 resources to be found, found %d", len(instanceDiff.Attributes))
}
evalDiff, instanceDiff = testDiffs([]string{"resource.changed"}, "changed")
err = evalDiff.processIgnoreChanges(instanceDiff)
if err != nil {
t.Fatalf("err: %s", err)
}
if len(instanceDiff.Attributes) != 1 {
t.Fatalf("Expected 1 resource to be found, found %d", len(instanceDiff.Attributes))
}
evalDiff, instanceDiff = testDiffs([]string{}, "changed")
err = evalDiff.processIgnoreChanges(instanceDiff)
if err != nil {
t.Fatalf("err: %s", err)
}
if len(instanceDiff.Attributes) != 2 {
t.Fatalf("Expected 2 resource to be found, found %d", len(instanceDiff.Attributes))
}
}

View File

@ -1675,10 +1675,7 @@ aws_instance.foo:
const testTFPlanDiffIgnoreChangesWithFlatmaps = `
UPDATE: aws_instance.foo
lst.#: "1" => "2"
lst.0: "j" => "j"
lst.1: "" => "k"
set.#: "1" => "1"
set.0.a: "1" => "1"
set.0.b: "" => "2"
type: "" => "aws_instance"
`