helper/diff: calculate removed keys
This commit is contained in:
parent
fdfed5000f
commit
2d97738636
|
@ -55,20 +55,21 @@ func (b *ResourceBuilder) Diff(
|
||||||
flatRaw := flatmap.Flatten(c.Raw)
|
flatRaw := flatmap.Flatten(c.Raw)
|
||||||
flatConfig := flatmap.Flatten(c.Config)
|
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 {
|
for ak, at := range b.Attrs {
|
||||||
if strings.HasPrefix(k, ak) {
|
// Keep track of all the keys we saw in the raw structure
|
||||||
attr = at
|
// so that we can prune our attributes later.
|
||||||
break
|
seenKeys := make([]string, 0)
|
||||||
}
|
|
||||||
}
|
// Go through and find the added/changed keys in flatRaw
|
||||||
if attr == AttrTypeUnknown {
|
for k, v := range flatRaw {
|
||||||
|
// Find only the attributes that match our prefix
|
||||||
|
if !strings.HasPrefix(k, ak) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Track that we saw this key
|
||||||
|
seenKeys = append(seenKeys, k)
|
||||||
|
|
||||||
// If this key is in the cleaned config, then use that value
|
// If this key is in the cleaned config, then use that value
|
||||||
// because it'll have its variables properly interpolated
|
// because it'll have its variables properly interpolated
|
||||||
if cleanV, ok := flatConfig[k]; ok {
|
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
|
// If this requires a new resource, record that and flag our
|
||||||
// boolean.
|
// boolean.
|
||||||
if attr == AttrTypeCreate {
|
if at == AttrTypeCreate {
|
||||||
attrs[k].RequiresNew = true
|
attrs[k].RequiresNew = true
|
||||||
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
|
// If we require a new resource, then process all the attributes
|
||||||
// that will be changing due to the creation of the resource.
|
// that will be changing due to the creation of the resource.
|
||||||
if requiresNew {
|
if requiresNew {
|
||||||
|
|
|
@ -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) {
|
func TestResourceBuilder_new(t *testing.T) {
|
||||||
rb := &ResourceBuilder{
|
rb := &ResourceBuilder{
|
||||||
Attrs: map[string]AttrType{
|
Attrs: map[string]AttrType{
|
||||||
|
@ -200,6 +239,11 @@ const testRBComplexDiff = `UPDATE
|
||||||
IN listener.0.port: "80" => "3000"
|
IN listener.0.port: "80" => "3000"
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const testRBComplexReplaceDiff = `UPDATE
|
||||||
|
IN listener.0.port: "80" => ""
|
||||||
|
IN listener.0.value: "" => "50"
|
||||||
|
`
|
||||||
|
|
||||||
const testRBNewDiff = `UPDATE
|
const testRBNewDiff = `UPDATE
|
||||||
IN foo: "" => "bar"
|
IN foo: "" => "bar"
|
||||||
OUT private_ip: "" => "<computed>"
|
OUT private_ip: "" => "<computed>"
|
||||||
|
|
Loading…
Reference in New Issue