helper/diff: can specify PreProcess functions

This commit is contained in:
Mitchell Hashimoto 2014-07-16 15:58:47 -07:00
parent 901785d04a
commit 2cfad3fa71
2 changed files with 104 additions and 3 deletions

View File

@ -44,8 +44,16 @@ type ResourceBuilder struct {
// ComputedAttrs are the attributes that are computed at
// resource creation time.
ComputedAttrs []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.
PreProcess map[string]PreProcessFunc
}
// PreProcessFunc is used with the PreProcess field in a ResourceBuilder
type PreProcessFunc func(string) string
// Diff returns the ResourceDiff for a resource given its state and
// configuration.
func (b *ResourceBuilder) Diff(
@ -76,10 +84,20 @@ func (b *ResourceBuilder) Diff(
// Track that we saw this key
seenKeys = append(seenKeys, k)
// We keep track of this in case we have a pre-processor
// so that we can store the original value still.
originalV := v
// 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 {
v = cleanV
originalV = v
// If we have a pre-processor for this, run it.
if pp, ok := b.PreProcess[k]; ok {
v = pp(k)
}
}
oldV, ok := s.Attributes[k]
@ -93,6 +111,7 @@ func (b *ResourceBuilder) Diff(
attrs[k] = &terraform.ResourceAttrDiff{
Old: oldV,
New: v,
NewExtra: originalV,
Type: terraform.DiffAttrInput,
}

View File

@ -199,6 +199,80 @@ func TestResourceBuilder_new(t *testing.T) {
}
}
func TestResourceBuilder_preProcess(t *testing.T) {
rb := &ResourceBuilder{
Attrs: map[string]AttrType{
"foo": AttrTypeCreate,
},
PreProcess: map[string]PreProcessFunc{
"foo": func(string) string {
return "bar"
},
},
}
state := &terraform.ResourceState{}
c := testConfig(t, map[string]interface{}{
"foo": "foo",
}, 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 := testRBPreProcessDiff
if actual != expected {
t.Fatalf("bad: %s", actual)
}
actual = diff.Attributes["foo"].NewExtra.(string)
expected = "foo"
if actual != expected {
t.Fatalf("bad: %#v", actual)
}
}
func TestResourceBuilder_preProcessUnknown(t *testing.T) {
rb := &ResourceBuilder{
Attrs: map[string]AttrType{
"foo": AttrTypeCreate,
},
PreProcess: map[string]PreProcessFunc{
"foo": func(string) string {
return "bar"
},
},
}
state := &terraform.ResourceState{}
c := testConfig(t, map[string]interface{}{
"foo": "${var.unknown}",
}, map[string]string{
"var.unknown": config.UnknownVariableValue,
})
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 := testRBPreProcessUnknownDiff
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
func TestResourceBuilder_requiresNew(t *testing.T) {
rb := &ResourceBuilder{
ComputedAttrs: []string{"private_ip"},
@ -338,6 +412,14 @@ const testRBNewDiff = `UPDATE
OUT private_ip: "" => "<computed>"
`
const testRBPreProcessDiff = `CREATE
IN foo: "" => "bar" (forces new resource)
`
const testRBPreProcessUnknownDiff = `CREATE
IN foo: "" => "${var.unknown}" (forces new resource)
`
const testRBRequiresNewDiff = `CREATE
IN ami: "foo" => "bar" (forces new resource)
OUT private_ip: "127.0.0.1" => "<computed>"