Skip to content

Commit e543dda

Browse files
authored
Merge pull request #30629 from hashicorp/jbardin/data-read-hook
ensure UI hooks are called for data sources
2 parents e75bcdc + 05a10f0 commit e543dda

File tree

2 files changed

+22
-55
lines changed

2 files changed

+22
-55
lines changed

internal/terraform/context_apply_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,9 @@ func TestContext2Apply_dataBasic(t *testing.T) {
14281428
}),
14291429
}
14301430

1431+
hook := new(MockHook)
14311432
ctx := testContext2(t, &ContextOpts{
1433+
Hooks: []Hook{hook},
14321434
Providers: map[addrs.Provider]providers.Factory{
14331435
addrs.NewDefaultProvider("null"): testProviderFuncFixed(p),
14341436
},
@@ -1449,6 +1451,13 @@ func TestContext2Apply_dataBasic(t *testing.T) {
14491451
if actual != expected {
14501452
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected)
14511453
}
1454+
1455+
if !hook.PreApplyCalled {
1456+
t.Fatal("PreApply not called for data source read")
1457+
}
1458+
if !hook.PostApplyCalled {
1459+
t.Fatal("PostApply not called for data source read")
1460+
}
14521461
}
14531462

14541463
func TestContext2Apply_destroyData(t *testing.T) {
@@ -1503,10 +1512,8 @@ func TestContext2Apply_destroyData(t *testing.T) {
15031512
}
15041513

15051514
wantHookCalls := []*testHookCall{
1506-
{"PreDiff", "data.null_data_source.testing"},
1507-
{"PostDiff", "data.null_data_source.testing"},
1508-
{"PreDiff", "data.null_data_source.testing"},
1509-
{"PostDiff", "data.null_data_source.testing"},
1515+
{"PreApply", "data.null_data_source.testing"},
1516+
{"PostApply", "data.null_data_source.testing"},
15101517
{"PostStateUpdate", ""},
15111518
}
15121519
if !reflect.DeepEqual(hook.Calls, wantHookCalls) {

internal/terraform/node_resource_abstract_instance.go

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -410,18 +410,6 @@ func (n *NodeAbstractResourceInstance) planDestroy(ctx EvalContext, currentState
410410
return noop, nil
411411
}
412412

413-
// Call pre-diff hook
414-
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
415-
return h.PreDiff(
416-
absAddr, deposedKey.Generation(),
417-
currentState.Value,
418-
cty.NullVal(cty.DynamicPseudoType),
419-
)
420-
}))
421-
if diags.HasErrors() {
422-
return nil, diags
423-
}
424-
425413
// Plan is always the same for a destroy. We don't need the provider's
426414
// help for this one.
427415
plan := &plans.ResourceInstanceChange{
@@ -437,17 +425,6 @@ func (n *NodeAbstractResourceInstance) planDestroy(ctx EvalContext, currentState
437425
ProviderAddr: n.ResolvedProvider,
438426
}
439427

440-
// Call post-diff hook
441-
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
442-
return h.PostDiff(
443-
absAddr,
444-
deposedKey.Generation(),
445-
plan.Action,
446-
plan.Before,
447-
plan.After,
448-
)
449-
}))
450-
451428
return plan, diags
452429
}
453430

@@ -1381,6 +1358,13 @@ func (n *NodeAbstractResourceInstance) readDataSource(ctx EvalContext, configVal
13811358
// to actually call the provider to read the data.
13821359
log.Printf("[TRACE] readDataSource: %s configuration is complete, so reading from provider", n.Addr)
13831360

1361+
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
1362+
return h.PreApply(n.Addr, states.CurrentGen, plans.Read, cty.NullVal(configVal.Type()), configVal)
1363+
}))
1364+
if diags.HasErrors() {
1365+
return newVal, diags
1366+
}
1367+
13841368
resp := provider.ReadDataSource(providers.ReadDataSourceRequest{
13851369
TypeName: n.Addr.ContainingResource().Resource.Type,
13861370
Config: configVal,
@@ -1445,6 +1429,10 @@ func (n *NodeAbstractResourceInstance) readDataSource(ctx EvalContext, configVal
14451429
newVal = newVal.MarkWithPaths(pvm)
14461430
}
14471431

1432+
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
1433+
return h.PostApply(n.Addr, states.CurrentGen, newVal, diags.Err())
1434+
}))
1435+
14481436
return newVal, diags
14491437
}
14501438

@@ -1556,13 +1544,6 @@ func (n *NodeAbstractResourceInstance) planDataSource(ctx EvalContext, currentSt
15561544
}
15571545

15581546
proposedNewVal := objchange.PlannedDataResourceObject(schema, unmarkedConfigVal)
1559-
1560-
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
1561-
return h.PreDiff(n.Addr, states.CurrentGen, priorVal, proposedNewVal)
1562-
}))
1563-
if diags.HasErrors() {
1564-
return nil, nil, keyData, diags
1565-
}
15661547
proposedNewVal = proposedNewVal.MarkWithPaths(configMarkPaths)
15671548

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

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

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

@@ -1703,13 +1674,6 @@ func (n *NodeAbstractResourceInstance) applyDataSource(ctx EvalContext, planned
17031674
return nil, keyData, diags
17041675
}
17051676

1706-
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
1707-
return h.PreApply(n.Addr, states.CurrentGen, planned.Action, planned.Before, planned.After)
1708-
}))
1709-
if diags.HasErrors() {
1710-
return nil, keyData, diags
1711-
}
1712-
17131677
config := *n.Config
17141678
schema, _ := providerSchema.SchemaForResourceAddr(n.Addr.ContainingResource().Resource)
17151679
if schema == nil {
@@ -1751,10 +1715,6 @@ func (n *NodeAbstractResourceInstance) applyDataSource(ctx EvalContext, planned
17511715
Status: states.ObjectReady,
17521716
}
17531717

1754-
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
1755-
return h.PostApply(n.Addr, states.CurrentGen, newVal, diags.Err())
1756-
}))
1757-
17581718
return state, keyData, diags
17591719
}
17601720

0 commit comments

Comments
 (0)