Skip to content

Commit e7fb0db

Browse files
add error-case pre and post diff hook
1 parent c619544 commit e7fb0db

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

internal/terraform/node_resource_abstract_instance.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,9 @@ func (n *NodeAbstractResourceInstance) plan(
12071207
// append these new diagnostics if there's at least one error inside.
12081208
if resp.Diagnostics.HasErrors() {
12091209
diags = diags.Append(resp.Diagnostics.InConfigBody(config.Config, n.Addr.String()))
1210+
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
1211+
return h.PostDiff(n.HookResourceIdentity(), addrs.NotDeposed, plans.Read, priorVal, proposedNewVal, diags.Err())
1212+
}))
12101213
return nil, nil, deferred, keyData, diags
12111214
}
12121215

@@ -1233,6 +1236,9 @@ func (n *NodeAbstractResourceInstance) plan(
12331236
))
12341237
}
12351238
if diags.HasErrors() {
1239+
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
1240+
return h.PostDiff(n.HookResourceIdentity(), addrs.NotDeposed, plans.Read, priorVal, proposedNewVal, diags.Err())
1241+
}))
12361242
return nil, nil, deferred, keyData, diags
12371243
}
12381244

@@ -1251,6 +1257,9 @@ func (n *NodeAbstractResourceInstance) plan(
12511257
diags = diags.Append(writeOnlyDiags)
12521258

12531259
if writeOnlyDiags.HasErrors() {
1260+
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
1261+
return h.PostDiff(n.HookResourceIdentity(), addrs.NotDeposed, plans.Read, priorVal, proposedNewVal, diags.Err())
1262+
}))
12541263
return nil, nil, deferred, keyData, diags
12551264
}
12561265
}

internal/terraform/node_resource_plan_instance.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ func (n *NodePlannableResourceInstance) managedResourceExecute(ctx EvalContext)
255255
instanceRefreshState, readDiags = n.readResourceInstanceState(ctx, addr)
256256
diags = diags.Append(readDiags)
257257
if diags.HasErrors() {
258+
// Pre-Diff error hook
259+
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
260+
return h.PreDiff(n.HookResourceIdentity(), addrs.NotDeposed, cty.DynamicVal, cty.DynamicVal, diags.Err())
261+
}))
258262
return diags
259263
}
260264
}
@@ -267,12 +271,20 @@ func (n *NodePlannableResourceInstance) managedResourceExecute(ctx EvalContext)
267271
// refresh step below.
268272
diags = diags.Append(n.writeResourceInstanceState(ctx, instanceRefreshState, prevRunState))
269273
if diags.HasErrors() {
274+
// Pre-Diff error hook
275+
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
276+
return h.PreDiff(n.HookResourceIdentity(), addrs.NotDeposed, cty.DynamicVal, cty.DynamicVal, diags.Err())
277+
}))
270278
return diags
271279
}
272280
// Also the refreshState, because that should still reflect schema upgrades
273281
// even if it doesn't reflect upstream changes.
274282
diags = diags.Append(n.writeResourceInstanceState(ctx, instanceRefreshState, refreshState))
275283
if diags.HasErrors() {
284+
// Pre-Diff error hook
285+
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
286+
return h.PreDiff(n.HookResourceIdentity(), addrs.NotDeposed, cty.DynamicVal, cty.DynamicVal, diags.Err())
287+
}))
276288
return diags
277289
}
278290
}
@@ -319,6 +331,10 @@ func (n *NodePlannableResourceInstance) managedResourceExecute(ctx EvalContext)
319331
}
320332

321333
if diags.HasErrors() {
334+
// Pre-Diff error hook
335+
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
336+
return h.PreDiff(n.HookResourceIdentity(), addrs.NotDeposed, cty.DynamicVal, cty.DynamicVal, diags.Err())
337+
}))
322338
return diags
323339
}
324340
}
@@ -329,6 +345,10 @@ func (n *NodePlannableResourceInstance) managedResourceExecute(ctx EvalContext)
329345
// sure we still update any changes to CreateBeforeDestroy.
330346
diags = diags.Append(n.writeResourceInstanceState(ctx, instanceRefreshState, refreshState))
331347
if diags.HasErrors() {
348+
// Pre-Diff error hook
349+
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
350+
return h.PreDiff(n.HookResourceIdentity(), addrs.NotDeposed, cty.DynamicVal, cty.DynamicVal, diags.Err())
351+
}))
332352
return diags
333353
}
334354
}
@@ -349,6 +369,10 @@ func (n *NodePlannableResourceInstance) managedResourceExecute(ctx EvalContext)
349369

350370
diags = diags.Append(n.replaceTriggered(ctx, repData))
351371
if diags.HasErrors() {
372+
// Pre-Diff error hook
373+
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
374+
return h.PreDiff(n.HookResourceIdentity(), addrs.NotDeposed, cty.DynamicVal, cty.DynamicVal, diags.Err())
375+
}))
352376
return diags
353377
}
354378

0 commit comments

Comments
 (0)