Skip to content

Commit 917c9b1

Browse files
backport of commit fd2830d (#37766)
Co-authored-by: Kristin Laemmert <mildwonkey@users.noreply.github.com>
1 parent 1bc1647 commit 917c9b1

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

internal/terraform/context_apply_action_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package terraform
55

66
import (
77
"path/filepath"
8+
"sync"
89
"testing"
910

1011
"github.com/google/go-cmp/cmp"
@@ -2563,7 +2564,7 @@ lifecycle {
25632564
InvokeActionFn: invokeActionFn,
25642565
}
25652566

2566-
hookCapture := actionHookCapture{}
2567+
hookCapture := newActionHookCapture()
25672568
ctx := testContext2(t, &ContextOpts{
25682569
Providers: map[addrs.Provider]providers.Factory{
25692570
addrs.NewDefaultProvider("test"): testProviderFuncFixed(testProvider),
@@ -2654,10 +2655,17 @@ lifecycle {
26542655
var _ Hook = (*actionHookCapture)(nil)
26552656

26562657
type actionHookCapture struct {
2658+
mu *sync.Mutex
26572659
startActionHooks []HookActionIdentity
26582660
completeActionHooks []HookActionIdentity
26592661
}
26602662

2663+
func newActionHookCapture() actionHookCapture {
2664+
return actionHookCapture{
2665+
mu: &sync.Mutex{},
2666+
}
2667+
}
2668+
26612669
func (a *actionHookCapture) PreApply(HookResourceIdentity, addrs.DeposedKey, plans.Action, cty.Value, cty.Value) (HookAction, error) {
26622670
return HookActionContinue, nil
26632671
}
@@ -2741,6 +2749,8 @@ func (a *actionHookCapture) PostListQuery(HookResourceIdentity, plans.QueryResul
27412749
}
27422750

27432751
func (a *actionHookCapture) StartAction(identity HookActionIdentity) (HookAction, error) {
2752+
a.mu.Lock()
2753+
defer a.mu.Unlock()
27442754
a.startActionHooks = append(a.startActionHooks, identity)
27452755
return HookActionContinue, nil
27462756
}
@@ -2750,6 +2760,8 @@ func (a *actionHookCapture) ProgressAction(HookActionIdentity, string) (HookActi
27502760
}
27512761

27522762
func (a *actionHookCapture) CompleteAction(identity HookActionIdentity, _ error) (HookAction, error) {
2763+
a.mu.Lock()
2764+
defer a.mu.Unlock()
27532765
a.completeActionHooks = append(a.completeActionHooks, identity)
27542766
return HookActionContinue, nil
27552767
}

internal/terraform/node_action_trigger_instance_apply.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,12 @@ func (n *nodeActionTriggerApplyInstance) Execute(ctx EvalContext, wo walkOperati
136136
ActionTrigger: ai.ActionTrigger,
137137
}
138138

139-
ctx.Hook(func(h Hook) (HookAction, error) {
139+
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
140140
return h.StartAction(hookIdentity)
141-
})
141+
}))
142+
if diags.HasErrors() {
143+
return diags
144+
}
142145

143146
// We don't want to send the marks, but all marks are okay in the context
144147
// of an action invocation. We can't reuse our ephemeral free value from
@@ -153,28 +156,34 @@ func (n *nodeActionTriggerApplyInstance) Execute(ctx EvalContext, wo walkOperati
153156
respDiags := n.AddSubjectToDiagnostics(resp.Diagnostics)
154157
diags = diags.Append(respDiags)
155158
if respDiags.HasErrors() {
156-
ctx.Hook(func(h Hook) (HookAction, error) {
159+
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
157160
return h.CompleteAction(hookIdentity, respDiags.Err())
158-
})
161+
}))
159162
return diags
160163
}
161164

162165
if resp.Events != nil { // should only occur in misconfigured tests
163166
for event := range resp.Events {
164167
switch ev := event.(type) {
165168
case providers.InvokeActionEvent_Progress:
166-
ctx.Hook(func(h Hook) (HookAction, error) {
169+
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
167170
return h.ProgressAction(hookIdentity, ev.Message)
168-
})
171+
}))
172+
if diags.HasErrors() {
173+
return diags
174+
}
169175
case providers.InvokeActionEvent_Completed:
170176
// Enhance the diagnostics
171177
diags = diags.Append(n.AddSubjectToDiagnostics(ev.Diagnostics))
172-
ctx.Hook(func(h Hook) (HookAction, error) {
178+
diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) {
173179
return h.CompleteAction(hookIdentity, ev.Diagnostics.Err())
174-
})
180+
}))
175181
if ev.Diagnostics.HasErrors() {
176182
return diags
177183
}
184+
if diags.HasErrors() {
185+
return diags
186+
}
178187
default:
179188
panic(fmt.Sprintf("unexpected action event type %T", ev))
180189
}

0 commit comments

Comments
 (0)