helper/diff: ComputedAttrsUpdate

This commit is contained in:
Mitchell Hashimoto 2014-07-24 20:24:40 -07:00
parent 79a9dfce4a
commit b8660c2a65
2 changed files with 58 additions and 0 deletions

View File

@ -45,6 +45,10 @@ type ResourceBuilder struct {
// resource creation time.
ComputedAttrs []string
// ComputedAttrsUpdate are the attributes that are computed
// at resource update time (this includes creation).
ComputedAttrsUpdate []string
// PreProcess is a mapping of exact keys that are sent through
// a pre-processor before comparing values. The original value will
// be put in the "NewExtra" field of the diff.
@ -185,6 +189,23 @@ func (b *ResourceBuilder) Diff(
}
}
// If we're changing anything, then mark the updated
// attributes.
if len(attrs) > 0 {
for _, k := range b.ComputedAttrsUpdate {
if _, ok := attrs[k]; ok {
continue
}
old := s.Attributes[k]
attrs[k] = &terraform.ResourceAttrDiff{
Old: old,
NewComputed: true,
Type: terraform.DiffAttrOutput,
}
}
}
// Build our resulting diff if we had attributes change
var result *terraform.ResourceDiff
if len(attrs) > 0 {

View File

@ -170,6 +170,38 @@ func TestResourceBuilder_complexReplace(t *testing.T) {
}
}
func TestResourceBuilder_computedAttrsUpdate(t *testing.T) {
rb := &ResourceBuilder{
Attrs: map[string]AttrType{
"foo": AttrTypeUpdate,
},
ComputedAttrsUpdate: []string{
"bar",
},
}
state := &terraform.ResourceState{
Attributes: map[string]string{"foo": "foo"},
}
c := testConfig(t, map[string]interface{}{
"foo": "bar",
}, nil)
diff, err := rb.Diff(state, c)
if err != nil {
t.Fatalf("err: %s", err)
}
if diff == nil {
t.Fatal("diff shold not be nil")
}
actual := testResourceDiffStr(diff)
expected := testRBComputedAttrUpdate
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
func TestResourceBuilder_new(t *testing.T) {
rb := &ResourceBuilder{
Attrs: map[string]AttrType{
@ -407,6 +439,11 @@ const testRBComplexReplaceDiff = `UPDATE
IN listener.0.value: "" => "50"
`
const testRBComputedAttrUpdate = `UPDATE
OUT bar: "" => "<computed>"
IN foo: "foo" => "bar"
`
const testRBNewDiff = `UPDATE
IN foo: "" => "bar"
OUT private_ip: "" => "<computed>"