Skip to content

Commit 2cfad3f

Browse files
committed
helper/diff: can specify PreProcess functions
1 parent 901785d commit 2cfad3f

2 files changed

Lines changed: 104 additions & 3 deletions

File tree

helper/diff/resource_builder.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,16 @@ type ResourceBuilder struct {
4444
// ComputedAttrs are the attributes that are computed at
4545
// resource creation time.
4646
ComputedAttrs []string
47+
48+
// PreProcess is a mapping of exact keys that are sent through
49+
// a pre-processor before comparing values. The original value will
50+
// be put in the "NewExtra" field of the diff.
51+
PreProcess map[string]PreProcessFunc
4752
}
4853

54+
// PreProcessFunc is used with the PreProcess field in a ResourceBuilder
55+
type PreProcessFunc func(string) string
56+
4957
// Diff returns the ResourceDiff for a resource given its state and
5058
// configuration.
5159
func (b *ResourceBuilder) Diff(
@@ -76,10 +84,20 @@ func (b *ResourceBuilder) Diff(
7684
// Track that we saw this key
7785
seenKeys = append(seenKeys, k)
7886

87+
// We keep track of this in case we have a pre-processor
88+
// so that we can store the original value still.
89+
originalV := v
90+
7991
// If this key is in the cleaned config, then use that value
8092
// because it'll have its variables properly interpolated
8193
if cleanV, ok := flatConfig[k]; ok {
8294
v = cleanV
95+
originalV = v
96+
97+
// If we have a pre-processor for this, run it.
98+
if pp, ok := b.PreProcess[k]; ok {
99+
v = pp(k)
100+
}
83101
}
84102

85103
oldV, ok := s.Attributes[k]
@@ -91,9 +109,10 @@ func (b *ResourceBuilder) Diff(
91109

92110
// Record the change
93111
attrs[k] = &terraform.ResourceAttrDiff{
94-
Old: oldV,
95-
New: v,
96-
Type: terraform.DiffAttrInput,
112+
Old: oldV,
113+
New: v,
114+
NewExtra: originalV,
115+
Type: terraform.DiffAttrInput,
97116
}
98117

99118
// If this requires a new resource, record that and flag our

helper/diff/resource_builder_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,80 @@ func TestResourceBuilder_new(t *testing.T) {
199199
}
200200
}
201201

202+
func TestResourceBuilder_preProcess(t *testing.T) {
203+
rb := &ResourceBuilder{
204+
Attrs: map[string]AttrType{
205+
"foo": AttrTypeCreate,
206+
},
207+
208+
PreProcess: map[string]PreProcessFunc{
209+
"foo": func(string) string {
210+
return "bar"
211+
},
212+
},
213+
}
214+
215+
state := &terraform.ResourceState{}
216+
c := testConfig(t, map[string]interface{}{
217+
"foo": "foo",
218+
}, nil)
219+
220+
diff, err := rb.Diff(state, c)
221+
if err != nil {
222+
t.Fatalf("err: %s", err)
223+
}
224+
if diff == nil {
225+
t.Fatal("diff shold not be nil")
226+
}
227+
228+
actual := testResourceDiffStr(diff)
229+
expected := testRBPreProcessDiff
230+
if actual != expected {
231+
t.Fatalf("bad: %s", actual)
232+
}
233+
234+
actual = diff.Attributes["foo"].NewExtra.(string)
235+
expected = "foo"
236+
if actual != expected {
237+
t.Fatalf("bad: %#v", actual)
238+
}
239+
}
240+
241+
func TestResourceBuilder_preProcessUnknown(t *testing.T) {
242+
rb := &ResourceBuilder{
243+
Attrs: map[string]AttrType{
244+
"foo": AttrTypeCreate,
245+
},
246+
247+
PreProcess: map[string]PreProcessFunc{
248+
"foo": func(string) string {
249+
return "bar"
250+
},
251+
},
252+
}
253+
254+
state := &terraform.ResourceState{}
255+
c := testConfig(t, map[string]interface{}{
256+
"foo": "${var.unknown}",
257+
}, map[string]string{
258+
"var.unknown": config.UnknownVariableValue,
259+
})
260+
261+
diff, err := rb.Diff(state, c)
262+
if err != nil {
263+
t.Fatalf("err: %s", err)
264+
}
265+
if diff == nil {
266+
t.Fatal("diff shold not be nil")
267+
}
268+
269+
actual := testResourceDiffStr(diff)
270+
expected := testRBPreProcessUnknownDiff
271+
if actual != expected {
272+
t.Fatalf("bad: %s", actual)
273+
}
274+
}
275+
202276
func TestResourceBuilder_requiresNew(t *testing.T) {
203277
rb := &ResourceBuilder{
204278
ComputedAttrs: []string{"private_ip"},
@@ -338,6 +412,14 @@ const testRBNewDiff = `UPDATE
338412
OUT private_ip: "" => "<computed>"
339413
`
340414

415+
const testRBPreProcessDiff = `CREATE
416+
IN foo: "" => "bar" (forces new resource)
417+
`
418+
419+
const testRBPreProcessUnknownDiff = `CREATE
420+
IN foo: "" => "${var.unknown}" (forces new resource)
421+
`
422+
341423
const testRBRequiresNewDiff = `CREATE
342424
IN ami: "foo" => "bar" (forces new resource)
343425
OUT private_ip: "127.0.0.1" => "<computed>"

0 commit comments

Comments
 (0)