helper/diff: can specify PreProcess functions
This commit is contained in:
parent
901785d04a
commit
2cfad3fa71
|
@ -44,8 +44,16 @@ type ResourceBuilder struct {
|
||||||
// ComputedAttrs are the attributes that are computed at
|
// ComputedAttrs are the attributes that are computed at
|
||||||
// resource creation time.
|
// resource creation time.
|
||||||
ComputedAttrs []string
|
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
|
// Diff returns the ResourceDiff for a resource given its state and
|
||||||
// configuration.
|
// configuration.
|
||||||
func (b *ResourceBuilder) Diff(
|
func (b *ResourceBuilder) Diff(
|
||||||
|
@ -76,10 +84,20 @@ func (b *ResourceBuilder) Diff(
|
||||||
// Track that we saw this key
|
// Track that we saw this key
|
||||||
seenKeys = append(seenKeys, k)
|
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
|
// 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 {
|
||||||
v = cleanV
|
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]
|
oldV, ok := s.Attributes[k]
|
||||||
|
@ -91,9 +109,10 @@ func (b *ResourceBuilder) Diff(
|
||||||
|
|
||||||
// Record the change
|
// Record the change
|
||||||
attrs[k] = &terraform.ResourceAttrDiff{
|
attrs[k] = &terraform.ResourceAttrDiff{
|
||||||
Old: oldV,
|
Old: oldV,
|
||||||
New: v,
|
New: v,
|
||||||
Type: terraform.DiffAttrInput,
|
NewExtra: originalV,
|
||||||
|
Type: terraform.DiffAttrInput,
|
||||||
}
|
}
|
||||||
|
|
||||||
// If this requires a new resource, record that and flag our
|
// If this requires a new resource, record that and flag our
|
||||||
|
|
|
@ -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) {
|
func TestResourceBuilder_requiresNew(t *testing.T) {
|
||||||
rb := &ResourceBuilder{
|
rb := &ResourceBuilder{
|
||||||
ComputedAttrs: []string{"private_ip"},
|
ComputedAttrs: []string{"private_ip"},
|
||||||
|
@ -338,6 +412,14 @@ const testRBNewDiff = `UPDATE
|
||||||
OUT private_ip: "" => "<computed>"
|
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
|
const testRBRequiresNewDiff = `CREATE
|
||||||
IN ami: "foo" => "bar" (forces new resource)
|
IN ami: "foo" => "bar" (forces new resource)
|
||||||
OUT private_ip: "127.0.0.1" => "<computed>"
|
OUT private_ip: "127.0.0.1" => "<computed>"
|
||||||
|
|
Loading…
Reference in New Issue