Skip to content

Commit

Permalink
remove PreDiff and PostDiff hook calls
Browse files Browse the repository at this point in the history
PreDiff and PostDiff hooks were designed to be called immediately before
and after the PlanResourceChange calls to the provider. Probably due to
the confusing legacy naming of the hooks, these were scattered about the
nodes involved with planning, causing the hooks to be called in a number
of places where they were designed, including data sources and destroy
plans. Since these hooks are not used at all any longer anyway, we can
removed the extra calls with no effect.

If we choose in the future to call PlanResourceChange for resource
destroy plans, the hooks can be re-inserted (even though they currently
are unused) into the new code path which must diverge from the current
combined path of managed and data sources.
  • Loading branch information
jbardin committed Mar 8, 2022
1 parent dc668df commit 05a10f0
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 44 deletions.
6 changes: 2 additions & 4 deletions internal/terraform/context_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1512,10 +1512,8 @@ func TestContext2Apply_destroyData(t *testing.T) {
}

wantHookCalls := []*testHookCall{
{"PreDiff", "data.null_data_source.testing"},
{"PostDiff", "data.null_data_source.testing"},
{"PreDiff", "data.null_data_source.testing"},
{"PostDiff", "data.null_data_source.testing"},
{"PreApply", "data.null_data_source.testing"},
{"PostApply", "data.null_data_source.testing"},
{"PostStateUpdate", ""},
}
if !reflect.DeepEqual(hook.Calls, wantHookCalls) {
Expand Down
40 changes: 0 additions & 40 deletions internal/terraform/node_resource_abstract_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,18 +410,6 @@ func (n *NodeAbstractResourceInstance) planDestroy(ctx EvalContext, currentState
return noop, nil
}

// Call pre-diff hook
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
return h.PreDiff(
absAddr, deposedKey.Generation(),
currentState.Value,
cty.NullVal(cty.DynamicPseudoType),
)
}))
if diags.HasErrors() {
return nil, diags
}

// Plan is always the same for a destroy. We don't need the provider's
// help for this one.
plan := &plans.ResourceInstanceChange{
Expand All @@ -437,17 +425,6 @@ func (n *NodeAbstractResourceInstance) planDestroy(ctx EvalContext, currentState
ProviderAddr: n.ResolvedProvider,
}

// Call post-diff hook
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
return h.PostDiff(
absAddr,
deposedKey.Generation(),
plan.Action,
plan.Before,
plan.After,
)
}))

return plan, diags
}

Expand Down Expand Up @@ -1567,13 +1544,6 @@ func (n *NodeAbstractResourceInstance) planDataSource(ctx EvalContext, currentSt
}

proposedNewVal := objchange.PlannedDataResourceObject(schema, unmarkedConfigVal)

diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
return h.PreDiff(n.Addr, states.CurrentGen, priorVal, proposedNewVal)
}))
if diags.HasErrors() {
return nil, nil, keyData, diags
}
proposedNewVal = proposedNewVal.MarkWithPaths(configMarkPaths)

// Apply detects that the data source will need to be read by the After
Expand Down Expand Up @@ -1601,13 +1571,6 @@ func (n *NodeAbstractResourceInstance) planDataSource(ctx EvalContext, currentSt
return plannedChange, plannedNewState, keyData, diags
}

// While this isn't a "diff", continue to call this for data sources.
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
return h.PreDiff(n.Addr, states.CurrentGen, priorVal, configVal)
}))
if diags.HasErrors() {
return nil, nil, keyData, diags
}
// We have a complete configuration with no dependencies to wait on, so we
// can read the data source into the state.
newVal, readDiags := n.readDataSource(ctx, configVal)
Expand Down Expand Up @@ -1643,9 +1606,6 @@ func (n *NodeAbstractResourceInstance) planDataSource(ctx EvalContext, currentSt
Status: states.ObjectReady,
}

diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
return h.PostDiff(n.Addr, states.CurrentGen, plans.Update, priorVal, newVal)
}))
return nil, plannedNewState, keyData, diags
}

Expand Down

0 comments on commit 05a10f0

Please sign in to comment.