helper/diff: calculate removed keys

This commit is contained in:
Mitchell Hashimoto 2014-07-09 09:47:21 -07:00
parent fdfed5000f
commit 2d97738636
2 changed files with 103 additions and 34 deletions

View File

@ -55,20 +55,21 @@ func (b *ResourceBuilder) Diff(
flatRaw := flatmap.Flatten(c.Raw)
flatConfig := flatmap.Flatten(c.Config)
for k, v := range flatRaw {
// Make sure this is an attribute that actually affects
// the diff in some way.
var attr AttrType
for ak, at := range b.Attrs {
if strings.HasPrefix(k, ak) {
attr = at
break
}
}
if attr == AttrTypeUnknown {
// Keep track of all the keys we saw in the raw structure
// so that we can prune our attributes later.
seenKeys := make([]string, 0)
// Go through and find the added/changed keys in flatRaw
for k, v := range flatRaw {
// Find only the attributes that match our prefix
if !strings.HasPrefix(k, ak) {
continue
}
// Track that we saw this key
seenKeys = append(seenKeys, k)
// If this key is in the cleaned config, then use that value
// because it'll have its variables properly interpolated
if cleanV, ok := flatConfig[k]; ok {
@ -91,12 +92,36 @@ func (b *ResourceBuilder) Diff(
// If this requires a new resource, record that and flag our
// boolean.
if attr == AttrTypeCreate {
if at == AttrTypeCreate {
attrs[k].RequiresNew = true
requiresNew = true
}
}
// Go through our attribues and find the deleted keys
matchingKeys := make(map[string]struct{})
for k, _ := range s.Attributes {
// Find only the attributes that match our prefix
if !strings.HasPrefix(k, ak) {
continue
}
matchingKeys[k] = struct{}{}
}
// Delete the keys we saw to find the deleted keys
for _, k := range seenKeys {
delete(matchingKeys, k)
}
for k, _ := range matchingKeys {
attrs[k] = &terraform.ResourceAttrDiff{
Old: s.Attributes[k],
New: "",
Type: terraform.DiffAttrInput,
}
}
}
// If we require a new resource, then process all the attributes
// that will be changing due to the creation of the resource.
if requiresNew {

View File

@ -46,6 +46,45 @@ func TestResourceBuilder_complex(t *testing.T) {
}
}
func TestResourceBuilder_complexReplace(t *testing.T) {
rb := &ResourceBuilder{
Attrs: map[string]AttrType{
"listener": AttrTypeUpdate,
},
}
state := &terraform.ResourceState{
ID: "foo",
Attributes: map[string]string{
"ignore": "1",
"listener.#": "1",
"listener.0.port": "80",
},
}
c := testConfig(t, map[string]interface{}{
"listener": []interface{}{
map[interface{}]interface{}{
"value": "50",
},
},
}, nil)
diff, err := rb.Diff(state, c)
if err != nil {
t.Fatalf("err: %s", err)
}
if diff == nil {
t.Fatal("should not be nil")
}
actual := testResourceDiffStr(diff)
expected := testRBComplexReplaceDiff
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
func TestResourceBuilder_new(t *testing.T) {
rb := &ResourceBuilder{
Attrs: map[string]AttrType{
@ -200,6 +239,11 @@ const testRBComplexDiff = `UPDATE
IN listener.0.port: "80" => "3000"
`
const testRBComplexReplaceDiff = `UPDATE
IN listener.0.port: "80" => ""
IN listener.0.value: "" => "50"
`
const testRBNewDiff = `UPDATE
IN foo: "" => "bar"
OUT private_ip: "" => "<computed>"