Skip to content

Commit 6b647b9

Browse files
add error to Pre and PostDiff hook
We need this information for stacks to forward the error to the progress stream as stacks expects everything that reports itself starting to e.g. plan to also finish either in an error or with success.
1 parent 8536a75 commit 6b647b9

File tree

9 files changed

+66
-30
lines changed

9 files changed

+66
-30
lines changed

internal/command/views/hook_count.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,15 @@ func (h *countHook) PostApply(id terraform.HookResourceIdentity, dk addrs.Depose
8888
return terraform.HookActionContinue, nil
8989
}
9090

91-
func (h *countHook) PostDiff(id terraform.HookResourceIdentity, dk addrs.DeposedKey, action plans.Action, priorState, plannedNewState cty.Value) (terraform.HookAction, error) {
91+
func (h *countHook) PostDiff(id terraform.HookResourceIdentity, dk addrs.DeposedKey, action plans.Action, priorState, plannedNewState cty.Value, err error) (terraform.HookAction, error) {
9292
h.Lock()
9393
defer h.Unlock()
9494

95+
// Skip counting if there was an error
96+
if err != nil {
97+
return terraform.HookActionContinue, nil
98+
}
99+
95100
// We don't count anything for data resources
96101
if id.Addr.Resource.Resource.Mode == addrs.DataResourceMode {
97102
return terraform.HookActionContinue, nil

internal/command/views/hook_count_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestCountHookPostDiff_DestroyDeposed(t *testing.T) {
4646
Name: k,
4747
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
4848

49-
h.PostDiff(testCountHookResourceID(addr), states.DeposedKey("deadbeef"), plans.Delete, cty.DynamicVal, cty.DynamicVal)
49+
h.PostDiff(testCountHookResourceID(addr), states.DeposedKey("deadbeef"), plans.Delete, cty.DynamicVal, cty.DynamicVal, nil)
5050
}
5151

5252
expected := new(countHook)
@@ -77,7 +77,7 @@ func TestCountHookPostDiff_DestroyOnly(t *testing.T) {
7777
Name: k,
7878
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
7979

80-
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, plans.Delete, cty.DynamicVal, cty.DynamicVal)
80+
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, plans.Delete, cty.DynamicVal, cty.DynamicVal, nil)
8181
}
8282

8383
expected := new(countHook)
@@ -119,7 +119,7 @@ func TestCountHookPostDiff_AddOnly(t *testing.T) {
119119
Name: k,
120120
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
121121

122-
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, plans.Create, cty.DynamicVal, cty.DynamicVal)
122+
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, plans.Create, cty.DynamicVal, cty.DynamicVal, nil)
123123
}
124124

125125
expected := new(countHook)
@@ -164,7 +164,7 @@ func TestCountHookPostDiff_ChangeOnly(t *testing.T) {
164164
Name: k,
165165
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
166166

167-
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, plans.Update, cty.DynamicVal, cty.DynamicVal)
167+
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, plans.Update, cty.DynamicVal, cty.DynamicVal, nil)
168168
}
169169

170170
expected := new(countHook)
@@ -195,7 +195,7 @@ func TestCountHookPostDiff_Mixed(t *testing.T) {
195195
Name: k,
196196
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
197197

198-
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, a, cty.DynamicVal, cty.DynamicVal)
198+
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, a, cty.DynamicVal, cty.DynamicVal, nil)
199199
}
200200

201201
expected := new(countHook)
@@ -227,7 +227,7 @@ func TestCountHookPostDiff_NoChange(t *testing.T) {
227227
Name: k,
228228
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
229229

230-
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, plans.NoOp, cty.DynamicVal, cty.DynamicVal)
230+
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, plans.NoOp, cty.DynamicVal, cty.DynamicVal, nil)
231231
}
232232

233233
expected := new(countHook)
@@ -259,7 +259,7 @@ func TestCountHookPostDiff_DataSource(t *testing.T) {
259259
Name: k,
260260
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
261261

262-
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, a, cty.DynamicVal, cty.DynamicVal)
262+
h.PostDiff(testCountHookResourceID(addr), addrs.NotDeposed, a, cty.DynamicVal, cty.DynamicVal, nil)
263263
}
264264

265265
expected := new(countHook)

internal/stacks/stackruntime/internal/stackeval/terraform_hook.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,28 @@ func (h *componentInstanceTerraformHook) resourceInstanceObjectAddr(riAddr addrs
5656
}
5757
}
5858

59-
func (h *componentInstanceTerraformHook) PreDiff(id terraform.HookResourceIdentity, dk addrs.DeposedKey, priorState, proposedNewState cty.Value) (terraform.HookAction, error) {
59+
func (h *componentInstanceTerraformHook) PreDiff(id terraform.HookResourceIdentity, dk addrs.DeposedKey, priorState, proposedNewState cty.Value, err error) (terraform.HookAction, error) {
60+
status := hooks.ResourceInstancePlanning
61+
if err != nil {
62+
status = hooks.ResourceInstanceErrored
63+
}
6064
hookMore(h.ctx, h.seq, h.hooks.ReportResourceInstanceStatus, &hooks.ResourceInstanceStatusHookData{
6165
Addr: h.resourceInstanceObjectAddr(id.Addr, dk),
6266
ProviderAddr: id.ProviderAddr,
63-
Status: hooks.ResourceInstancePlanning,
67+
Status: status,
6468
})
6569
return terraform.HookActionContinue, nil
6670
}
6771

68-
func (h *componentInstanceTerraformHook) PostDiff(id terraform.HookResourceIdentity, dk addrs.DeposedKey, action plans.Action, priorState, plannedNewState cty.Value) (terraform.HookAction, error) {
72+
func (h *componentInstanceTerraformHook) PostDiff(id terraform.HookResourceIdentity, dk addrs.DeposedKey, action plans.Action, priorState, plannedNewState cty.Value, err error) (terraform.HookAction, error) {
73+
status := hooks.ResourceInstancePlanned
74+
if err != nil {
75+
status = hooks.ResourceInstanceErrored
76+
}
6977
hookMore(h.ctx, h.seq, h.hooks.ReportResourceInstanceStatus, &hooks.ResourceInstanceStatusHookData{
7078
Addr: h.resourceInstanceObjectAddr(id.Addr, dk),
7179
ProviderAddr: id.ProviderAddr,
72-
Status: hooks.ResourceInstancePlanned,
80+
Status: status,
7381
})
7482
return terraform.HookActionContinue, nil
7583
}

internal/stacks/stackruntime/internal/stackeval/terraform_hook_test.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestTerraformHook(t *testing.T) {
7272

7373
t.Run("PreDiff", func(t *testing.T) {
7474
hook := makeHook()
75-
action, err := hook.PreDiff(resourceIdentity, addrs.NotDeposed, cty.NilVal, cty.NilVal)
75+
action, err := hook.PreDiff(resourceIdentity, addrs.NotDeposed, cty.NilVal, cty.NilVal, nil)
7676
if err != nil {
7777
t.Errorf("unexpected error: %s", err)
7878
}
@@ -93,9 +93,9 @@ func TestTerraformHook(t *testing.T) {
9393
}
9494
})
9595

96-
t.Run("PostDiff", func(t *testing.T) {
96+
t.Run("PostDiff - success", func(t *testing.T) {
9797
hook := makeHook()
98-
action, err := hook.PostDiff(resourceIdentity, addrs.NotDeposed, plans.Create, cty.NilVal, cty.NilVal)
98+
action, err := hook.PostDiff(resourceIdentity, addrs.NotDeposed, plans.Create, cty.NilVal, cty.NilVal, nil)
9999
if err != nil {
100100
t.Errorf("unexpected error: %s", err)
101101
}
@@ -116,6 +116,29 @@ func TestTerraformHook(t *testing.T) {
116116
}
117117
})
118118

119+
t.Run("PostDiff - error", func(t *testing.T) {
120+
hook := makeHook()
121+
action, err := hook.PostDiff(resourceIdentity, addrs.NotDeposed, plans.Create, cty.NilVal, cty.NilVal, errors.New("oh no"))
122+
if err != nil {
123+
t.Errorf("unexpected error: %s", err)
124+
}
125+
if action != terraform.HookActionContinue {
126+
t.Errorf("wrong action: %#v", action)
127+
}
128+
if hook.seq.tracking != "boop" {
129+
t.Errorf("wrong tracking value: %#v", hook.seq.tracking)
130+
}
131+
132+
wantRihd := &hooks.ResourceInstanceStatusHookData{
133+
Addr: stackAddr,
134+
ProviderAddr: providerAddr,
135+
Status: hooks.ResourceInstanceErrored,
136+
}
137+
if diff := cmp.Diff(gotRihd, wantRihd); diff != "" {
138+
t.Errorf("wrong status hook data:\n%s", diff)
139+
}
140+
})
141+
119142
t.Run("PreApply", func(t *testing.T) {
120143
hook := makeHook()
121144
action, err := hook.PreApply(resourceIdentity, addrs.NotDeposed, plans.Create, cty.NilVal, cty.NilVal)

internal/terraform/hook.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ type Hook interface {
6262
// PreDiff and PostDiff are called before and after a provider is given
6363
// the opportunity to customize the proposed new state to produce the
6464
// planned new state.
65-
PreDiff(id HookResourceIdentity, dk addrs.DeposedKey, priorState, proposedNewState cty.Value) (HookAction, error)
66-
PostDiff(id HookResourceIdentity, dk addrs.DeposedKey, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error)
65+
PreDiff(id HookResourceIdentity, dk addrs.DeposedKey, priorState, proposedNewState cty.Value, err error) (HookAction, error)
66+
PostDiff(id HookResourceIdentity, dk addrs.DeposedKey, action plans.Action, priorState, plannedNewState cty.Value, err error) (HookAction, error)
6767

6868
// The provisioning hooks signal both the overall start end end of
6969
// provisioning for a particular instance and of each of the individual
@@ -165,11 +165,11 @@ func (*NilHook) PostApply(id HookResourceIdentity, dk addrs.DeposedKey, newState
165165
return HookActionContinue, nil
166166
}
167167

168-
func (*NilHook) PreDiff(id HookResourceIdentity, dk addrs.DeposedKey, priorState, proposedNewState cty.Value) (HookAction, error) {
168+
func (*NilHook) PreDiff(id HookResourceIdentity, dk addrs.DeposedKey, priorState, proposedNewState cty.Value, err error) (HookAction, error) {
169169
return HookActionContinue, nil
170170
}
171171

172-
func (*NilHook) PostDiff(id HookResourceIdentity, dk addrs.DeposedKey, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) {
172+
func (*NilHook) PostDiff(id HookResourceIdentity, dk addrs.DeposedKey, action plans.Action, priorState, plannedNewState cty.Value, err error) (HookAction, error) {
173173
return HookActionContinue, nil
174174
}
175175

internal/terraform/hook_mock.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func (h *MockHook) PostApply(id HookResourceIdentity, dk addrs.DeposedKey, newSt
210210
return h.PostApplyReturn, h.PostApplyReturnError
211211
}
212212

213-
func (h *MockHook) PreDiff(id HookResourceIdentity, dk addrs.DeposedKey, priorState, proposedNewState cty.Value) (HookAction, error) {
213+
func (h *MockHook) PreDiff(id HookResourceIdentity, dk addrs.DeposedKey, priorState, proposedNewState cty.Value, err error) (HookAction, error) {
214214
h.Lock()
215215
defer h.Unlock()
216216

@@ -222,7 +222,7 @@ func (h *MockHook) PreDiff(id HookResourceIdentity, dk addrs.DeposedKey, priorSt
222222
return h.PreDiffReturn, h.PreDiffError
223223
}
224224

225-
func (h *MockHook) PostDiff(id HookResourceIdentity, dk addrs.DeposedKey, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) {
225+
func (h *MockHook) PostDiff(id HookResourceIdentity, dk addrs.DeposedKey, action plans.Action, priorState, plannedNewState cty.Value, err error) (HookAction, error) {
226226
h.Lock()
227227
defer h.Unlock()
228228

internal/terraform/hook_stop.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ func (h *stopHook) PostApply(id HookResourceIdentity, dk addrs.DeposedKey, newSt
3131
return h.hook()
3232
}
3333

34-
func (h *stopHook) PreDiff(id HookResourceIdentity, dk addrs.DeposedKey, priorState, proposedNewState cty.Value) (HookAction, error) {
34+
func (h *stopHook) PreDiff(id HookResourceIdentity, dk addrs.DeposedKey, priorState, proposedNewState cty.Value, err error) (HookAction, error) {
3535
return h.hook()
3636
}
3737

38-
func (h *stopHook) PostDiff(id HookResourceIdentity, dk addrs.DeposedKey, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) {
38+
func (h *stopHook) PostDiff(id HookResourceIdentity, dk addrs.DeposedKey, action plans.Action, priorState, plannedNewState cty.Value, err error) (HookAction, error) {
3939
return h.hook()
4040
}
4141

internal/terraform/hook_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ func (h *testHook) PostApply(id HookResourceIdentity, dk addrs.DeposedKey, newSt
5151
return HookActionContinue, nil
5252
}
5353

54-
func (h *testHook) PreDiff(id HookResourceIdentity, dk addrs.DeposedKey, priorState, proposedNewState cty.Value) (HookAction, error) {
54+
func (h *testHook) PreDiff(id HookResourceIdentity, dk addrs.DeposedKey, priorState, proposedNewState cty.Value, err error) (HookAction, error) {
5555
h.mu.Lock()
5656
defer h.mu.Unlock()
5757
h.Calls = append(h.Calls, &testHookCall{"PreDiff", id.Addr.String()})
5858
return HookActionContinue, nil
5959
}
6060

61-
func (h *testHook) PostDiff(id HookResourceIdentity, dk addrs.DeposedKey, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) {
61+
func (h *testHook) PostDiff(id HookResourceIdentity, dk addrs.DeposedKey, action plans.Action, priorState, plannedNewState cty.Value, err error) (HookAction, error) {
6262
h.mu.Lock()
6363
defer h.mu.Unlock()
6464
h.Calls = append(h.Calls, &testHookCall{"PostDiff", id.Addr.String()})

internal/terraform/node_resource_abstract_instance.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ func (n *NodeAbstractResourceInstance) planDestroy(ctx EvalContext, currentState
429429

430430
// Call pre-diff hook
431431
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
432-
return h.PreDiff(n.HookResourceIdentity(), deposedKey, currentState.Value, nullVal)
432+
return h.PreDiff(n.HookResourceIdentity(), deposedKey, currentState.Value, nullVal, nil)
433433
}))
434434
if diags.HasErrors() {
435435
return plan, deferred, diags
@@ -501,7 +501,7 @@ func (n *NodeAbstractResourceInstance) planDestroy(ctx EvalContext, currentState
501501

502502
// Call post-refresh hook
503503
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
504-
return h.PostDiff(n.HookResourceIdentity(), deposedKey, plans.Delete, currentState.Value, nullVal)
504+
return h.PostDiff(n.HookResourceIdentity(), deposedKey, plans.Delete, currentState.Value, nullVal, nil)
505505
}))
506506
if diags.HasErrors() {
507507
return plan, deferred, diags
@@ -948,7 +948,7 @@ func (n *NodeAbstractResourceInstance) plan(
948948

949949
// Call pre-diff hook
950950
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
951-
return h.PreDiff(n.HookResourceIdentity(), addrs.NotDeposed, priorVal, proposedNewVal)
951+
return h.PreDiff(n.HookResourceIdentity(), addrs.NotDeposed, priorVal, proposedNewVal, nil)
952952
}))
953953
if diags.HasErrors() {
954954
return nil, nil, deferred, keyData, diags
@@ -1303,7 +1303,7 @@ func (n *NodeAbstractResourceInstance) plan(
13031303

13041304
// Call post-refresh hook
13051305
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
1306-
return h.PostDiff(n.HookResourceIdentity(), addrs.NotDeposed, action, priorVal, plannedNewVal)
1306+
return h.PostDiff(n.HookResourceIdentity(), addrs.NotDeposed, action, priorVal, plannedNewVal, nil)
13071307
}))
13081308
if diags.HasErrors() {
13091309
return nil, nil, deferred, keyData, diags
@@ -1945,7 +1945,7 @@ func (n *NodeAbstractResourceInstance) planDataSource(ctx EvalContext, checkRule
19451945
}
19461946

19471947
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
1948-
return h.PostDiff(n.HookResourceIdentity(), addrs.NotDeposed, plans.Read, priorVal, proposedNewVal)
1948+
return h.PostDiff(n.HookResourceIdentity(), addrs.NotDeposed, plans.Read, priorVal, proposedNewVal, nil)
19491949
}))
19501950

19511951
return plannedChange, plannedNewState, deferred, keyData, diags

0 commit comments

Comments
 (0)