Skip to content

Commit dc668df

Browse files
committed
ensure UI hooks are called for data sources
The UI hooks for data source reads were missed during planning. Move the hook calls to immediatley before and after the ReadDataSource calls to ensure they are called during both plan and apply.
1 parent e75bcdc commit dc668df

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

internal/terraform/context_apply_test.go

Lines changed: 9 additions & 0 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) {

internal/terraform/node_resource_abstract_instance.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,13 @@ func (n *NodeAbstractResourceInstance) readDataSource(ctx EvalContext, configVal
13811381
// to actually call the provider to read the data.
13821382
log.Printf("[TRACE] readDataSource: %s configuration is complete, so reading from provider", n.Addr)
13831383

1384+
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
1385+
return h.PreApply(n.Addr, states.CurrentGen, plans.Read, cty.NullVal(configVal.Type()), configVal)
1386+
}))
1387+
if diags.HasErrors() {
1388+
return newVal, diags
1389+
}
1390+
13841391
resp := provider.ReadDataSource(providers.ReadDataSourceRequest{
13851392
TypeName: n.Addr.ContainingResource().Resource.Type,
13861393
Config: configVal,
@@ -1445,6 +1452,10 @@ func (n *NodeAbstractResourceInstance) readDataSource(ctx EvalContext, configVal
14451452
newVal = newVal.MarkWithPaths(pvm)
14461453
}
14471454

1455+
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
1456+
return h.PostApply(n.Addr, states.CurrentGen, newVal, diags.Err())
1457+
}))
1458+
14481459
return newVal, diags
14491460
}
14501461

@@ -1703,13 +1714,6 @@ func (n *NodeAbstractResourceInstance) applyDataSource(ctx EvalContext, planned
17031714
return nil, keyData, diags
17041715
}
17051716

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-
17131717
config := *n.Config
17141718
schema, _ := providerSchema.SchemaForResourceAddr(n.Addr.ContainingResource().Resource)
17151719
if schema == nil {
@@ -1751,10 +1755,6 @@ func (n *NodeAbstractResourceInstance) applyDataSource(ctx EvalContext, planned
17511755
Status: states.ObjectReady,
17521756
}
17531757

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

0 commit comments

Comments
 (0)