Skip to content

Commit 23066d4

Browse files
committed
Add trigger information to action invocation status and progress
Include the action trigger (lifecycle or invoke) in ActionInvocationStatus and ActionInvocationProgress messages to uniquely identify action invocations triggered by different events. Additional context: hashicorp#38051 (comment)
1 parent 264b785 commit 23066d4

4 files changed

Lines changed: 84 additions & 17 deletions

File tree

internal/rpcapi/stacks.go

Lines changed: 78 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,13 +1238,18 @@ func stackChangeHooks(send func(*stacks.StackChangeProgress) error, mainStackSou
12381238
providerAddr = statusData.ProviderAddr.String()
12391239
}
12401240

1241+
protoStatus := &stacks.StackChangeProgress_ActionInvocationStatus{
1242+
Addr: stacks.NewActionInvocationInStackAddr(statusData.Addr),
1243+
Status: statusData.Status.ForProtobuf(),
1244+
ProviderAddr: providerAddr,
1245+
}
1246+
1247+
// Set the action trigger oneof
1248+
setActionInvocationStatusTrigger(protoStatus, statusData.Addr.Component, statusData.Trigger)
1249+
12411250
send(&stacks.StackChangeProgress{
12421251
Event: &stacks.StackChangeProgress_ActionInvocationStatus_{
1243-
ActionInvocationStatus: &stacks.StackChangeProgress_ActionInvocationStatus{
1244-
Addr: stacks.NewActionInvocationInStackAddr(statusData.Addr),
1245-
Status: statusData.Status.ForProtobuf(),
1246-
ProviderAddr: providerAddr,
1247-
},
1252+
ActionInvocationStatus: protoStatus,
12481253
},
12491254
})
12501255

@@ -1263,13 +1268,18 @@ func stackChangeHooks(send func(*stacks.StackChangeProgress) error, mainStackSou
12631268
providerAddr = progressData.ProviderAddr.String()
12641269
}
12651270

1271+
protoProgress := &stacks.StackChangeProgress_ActionInvocationProgress{
1272+
Addr: stacks.NewActionInvocationInStackAddr(progressData.Addr),
1273+
Message: progressData.Message,
1274+
ProviderAddr: providerAddr,
1275+
}
1276+
1277+
// Set the action trigger oneof
1278+
setActionInvocationProgressTrigger(protoProgress, progressData.Addr.Component, progressData.Trigger)
1279+
12661280
send(&stacks.StackChangeProgress{
12671281
Event: &stacks.StackChangeProgress_ActionInvocationProgress_{
1268-
ActionInvocationProgress: &stacks.StackChangeProgress_ActionInvocationProgress{
1269-
Addr: stacks.NewActionInvocationInStackAddr(progressData.Addr),
1270-
Message: progressData.Message,
1271-
ProviderAddr: providerAddr,
1272-
},
1282+
ActionInvocationProgress: protoProgress,
12731283
},
12741284
})
12751285

@@ -1394,13 +1404,20 @@ func actionInvocationPlanned(ai *hooks.ActionInvocation) (*stacks.StackChangePro
13941404
ProviderAddr: ai.ProviderAddr.String(),
13951405
}
13961406

1397-
switch trig := ai.Trigger.(type) {
1407+
setActionInvocationPlannedTrigger(res, ai.Addr.Component, ai.Trigger)
1408+
1409+
return res, nil
1410+
}
1411+
1412+
// setActionInvocationStatusTrigger sets the ActionTrigger oneof field on an ActionInvocationStatus message.
1413+
func setActionInvocationStatusTrigger(msg *stacks.StackChangeProgress_ActionInvocationStatus, component stackaddrs.AbsComponentInstance, trigger plans.ActionTrigger) {
1414+
switch trig := trigger.(type) {
13981415
case *plans.LifecycleActionTrigger:
1399-
res.ActionTrigger = &stacks.StackChangeProgress_ActionInvocationPlanned_LifecycleActionTrigger{
1416+
msg.ActionTrigger = &stacks.StackChangeProgress_ActionInvocationStatus_LifecycleActionTrigger{
14001417
LifecycleActionTrigger: &stacks.StackChangeProgress_LifecycleActionTrigger{
14011418
TriggeringResourceAddress: stacks.NewResourceInstanceInStackAddr(
14021419
stackaddrs.AbsResourceInstance{
1403-
Component: ai.Addr.Component,
1420+
Component: component,
14041421
Item: trig.TriggeringResourceAddr,
14051422
},
14061423
),
@@ -1410,14 +1427,58 @@ func actionInvocationPlanned(ai *hooks.ActionInvocation) (*stacks.StackChangePro
14101427
},
14111428
}
14121429
case *plans.InvokeActionTrigger:
1413-
res.ActionTrigger = &stacks.StackChangeProgress_ActionInvocationPlanned_InvokeActionTrigger{
1430+
msg.ActionTrigger = &stacks.StackChangeProgress_ActionInvocationStatus_InvokeActionTrigger{
14141431
InvokeActionTrigger: &stacks.StackChangeProgress_InvokeActionTrigger{},
14151432
}
1416-
default:
1417-
return nil, fmt.Errorf("unsupported action invocation trigger type")
14181433
}
1434+
}
14191435

1420-
return res, nil
1436+
// setActionInvocationProgressTrigger sets the ActionTrigger oneof field on an ActionInvocationProgress message.
1437+
func setActionInvocationProgressTrigger(msg *stacks.StackChangeProgress_ActionInvocationProgress, component stackaddrs.AbsComponentInstance, trigger plans.ActionTrigger) {
1438+
switch trig := trigger.(type) {
1439+
case *plans.LifecycleActionTrigger:
1440+
msg.ActionTrigger = &stacks.StackChangeProgress_ActionInvocationProgress_LifecycleActionTrigger{
1441+
LifecycleActionTrigger: &stacks.StackChangeProgress_LifecycleActionTrigger{
1442+
TriggeringResourceAddress: stacks.NewResourceInstanceInStackAddr(
1443+
stackaddrs.AbsResourceInstance{
1444+
Component: component,
1445+
Item: trig.TriggeringResourceAddr,
1446+
},
1447+
),
1448+
TriggerEvent: stacks.StackChangeProgress_ActionTriggerEvent(trig.TriggerEvent()),
1449+
ActionTriggerBlockIndex: int64(trig.ActionTriggerBlockIndex),
1450+
ActionsListIndex: int64(trig.ActionsListIndex),
1451+
},
1452+
}
1453+
case *plans.InvokeActionTrigger:
1454+
msg.ActionTrigger = &stacks.StackChangeProgress_ActionInvocationProgress_InvokeActionTrigger{
1455+
InvokeActionTrigger: &stacks.StackChangeProgress_InvokeActionTrigger{},
1456+
}
1457+
}
1458+
}
1459+
1460+
// setActionInvocationPlannedTrigger sets the ActionTrigger oneof field on an ActionInvocationPlanned message.
1461+
func setActionInvocationPlannedTrigger(msg *stacks.StackChangeProgress_ActionInvocationPlanned, component stackaddrs.AbsComponentInstance, trigger plans.ActionTrigger) {
1462+
switch trig := trigger.(type) {
1463+
case *plans.LifecycleActionTrigger:
1464+
msg.ActionTrigger = &stacks.StackChangeProgress_ActionInvocationPlanned_LifecycleActionTrigger{
1465+
LifecycleActionTrigger: &stacks.StackChangeProgress_LifecycleActionTrigger{
1466+
TriggeringResourceAddress: stacks.NewResourceInstanceInStackAddr(
1467+
stackaddrs.AbsResourceInstance{
1468+
Component: component,
1469+
Item: trig.TriggeringResourceAddr,
1470+
},
1471+
),
1472+
TriggerEvent: stacks.StackChangeProgress_ActionTriggerEvent(trig.TriggerEvent()),
1473+
ActionTriggerBlockIndex: int64(trig.ActionTriggerBlockIndex),
1474+
ActionsListIndex: int64(trig.ActionsListIndex),
1475+
},
1476+
}
1477+
case *plans.InvokeActionTrigger:
1478+
msg.ActionTrigger = &stacks.StackChangeProgress_ActionInvocationPlanned_InvokeActionTrigger{
1479+
InvokeActionTrigger: &stacks.StackChangeProgress_InvokeActionTrigger{},
1480+
}
1481+
}
14211482
}
14221483

14231484
func evtComponentInstanceStatus(ci stackaddrs.AbsComponentInstance, status hooks.ComponentInstanceStatus) *stacks.StackChangeProgress {

internal/stacks/stackruntime/hooks/resource_instance.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ type ActionInvocationStatusHookData struct {
157157
Addr stackaddrs.AbsActionInvocationInstance
158158
ProviderAddr addrs.Provider
159159
Status ActionInvocationStatus
160+
Trigger plans.ActionTrigger
160161
}
161162

162163
// String returns a concise string representation of the action invocation status.
@@ -171,6 +172,7 @@ type ActionInvocationProgressHookData struct {
171172
Addr stackaddrs.AbsActionInvocationInstance
172173
ProviderAddr addrs.Provider
173174
Message string
175+
Trigger plans.ActionTrigger
174176
}
175177

176178
// String returns a concise string representation of the action invocation progress.

internal/stacks/stackruntime/internal/stackeval/applying.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func ApplyComponentPlan(ctx context.Context, main *Main, plan *plans.Plan, requi
140140
Addr: absActionAddr,
141141
ProviderAddr: action.ProviderAddr.Provider,
142142
Status: hooks.ActionInvocationPending,
143+
Trigger: action.ActionTrigger,
143144
})
144145
}
145146
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ func (h *componentInstanceTerraformHook) StartAction(id terraform.HookActionIden
231231
Addr: ai.Addr,
232232
ProviderAddr: providerAddr,
233233
Status: hooks.ActionInvocationRunning,
234+
Trigger: ai.Trigger,
234235
})
235236
return terraform.HookActionContinue, nil
236237
}
@@ -249,6 +250,7 @@ func (h *componentInstanceTerraformHook) ProgressAction(id terraform.HookActionI
249250
Addr: ai.Addr,
250251
ProviderAddr: providerAddr,
251252
Message: progress,
253+
Trigger: ai.Trigger,
252254
})
253255
return terraform.HookActionContinue, nil
254256
}
@@ -273,6 +275,7 @@ func (h *componentInstanceTerraformHook) CompleteAction(id terraform.HookActionI
273275
Addr: ai.Addr,
274276
ProviderAddr: providerAddr,
275277
Status: status,
278+
Trigger: ai.Trigger,
276279
})
277280
return terraform.HookActionContinue, nil
278281
}

0 commit comments

Comments
 (0)