Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/v1.14/BUG FIXES-20260123-103307.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: actions in modules without instances failed the plan graph
time: 2026-01-23T10:33:07.244665+01:00
custom:
Issue: "38089"
26 changes: 26 additions & 0 deletions internal/terraform/context_apply_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,32 @@ resource "test_object" "a" {
}},
},

"not triggered with no module instances": {
module: map[string]string{
"main.tf": `
module "mod" {
count = 0
source = "./mod"
}

// an empty plan is not applyable so we have this extra resource here
resource "test_object" "a" {}
`,
"mod/mod.tf": `
action "action_example" "hello" {}
resource "test_object" "a" {
lifecycle {
action_trigger {
events = [before_create]
actions = [action.action_example.hello]
}
}
}
`,
},
expectInvokeActionCalled: false,
},

"provider is within module": {
module: map[string]string{
"main.tf": `
Expand Down
134 changes: 130 additions & 4 deletions internal/terraform/context_plan_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,16 @@ resource "test_object" "a" {
t.Fatalf("expected action addresses to be 'action.test_action.hello[\"a\"]' and 'action.test_action.hello[\"b\"]', got %v", actionAddrs)
}

// TODO: Test that action the triggering resource address is set correctly
for _, ai := range p.Changes.ActionInvocations {
at, ok := ai.ActionTrigger.(*plans.LifecycleActionTrigger)
if !ok {
t.Fatalf("expected action trigger to be a LifecycleActionTrigger, got %T", ai.ActionTrigger)
}

if !at.TriggeringResourceAddr.Equal(mustResourceInstanceAddr("test_object.a")) {
t.Fatalf("expected action to have triggering resource address 'test_object.a', but it is %s", at.TriggeringResourceAddr)
}
}
},
},

Expand Down Expand Up @@ -938,7 +947,16 @@ resource "test_object" "a" {
t.Fatalf("expected action addresses to be 'action.test_action.hello[0]' and 'action.test_action.hello[1]', got %v", actionAddrs)
}

// TODO: Test that action the triggering resource address is set correctly
for _, ai := range p.Changes.ActionInvocations {
at, ok := ai.ActionTrigger.(*plans.LifecycleActionTrigger)
if !ok {
t.Fatalf("expected action trigger to be a LifecycleActionTrigger, got %T", ai.ActionTrigger)
}

if !at.TriggeringResourceAddr.Equal(mustResourceInstanceAddr("test_object.a")) {
t.Fatalf("expected action to have triggering resource address 'test_object.a', but it is %s", at.TriggeringResourceAddr)
}
}
},
},

Expand Down Expand Up @@ -1048,7 +1066,23 @@ resource "test_object" "a" {
t.Fatalf("expected action addresses to be 'action.test_action.hello' and 'action.test_action.hello', got %v", actionAddrs)
}

// TODO: Test that action the triggering resource address is set correctly
actionTriggers := []plans.LifecycleActionTrigger{}
for _, ai := range p.Changes.ActionInvocations {
at, ok := ai.ActionTrigger.(*plans.LifecycleActionTrigger)
if !ok {
t.Fatalf("expected action trigger to be a LifecycleActionTrigger, got %T", ai.ActionTrigger)
}

actionTriggers = append(actionTriggers, *at)
}

if !actionTriggers[0].TriggeringResourceAddr.Resource.Resource.Equal(actionTriggers[1].TriggeringResourceAddr.Resource.Resource) {
t.Fatalf("expected both actions to have the same triggering resource address, but got %s and %s", actionTriggers[0].TriggeringResourceAddr, actionTriggers[1].TriggeringResourceAddr)
}

if actionTriggers[0].TriggeringResourceAddr.Resource.Key == actionTriggers[1].TriggeringResourceAddr.Resource.Key {
t.Fatalf("expected both actions to have different triggering resource instance keys, but got the same %s", actionTriggers[0].TriggeringResourceAddr.Resource.Key)
}
},
},
"expanded resource - expanded action": {
Expand Down Expand Up @@ -1093,7 +1127,23 @@ resource "test_object" "a" {
t.Fatalf("expected action addresses to be 'action.test_action.hello[0]' and 'action.test_action.hello[1]', got %v", actionAddrs)
}

// TODO: Test that action the triggering resource address is set correctly
actionTriggers := []plans.LifecycleActionTrigger{}
for _, ai := range p.Changes.ActionInvocations {
at, ok := ai.ActionTrigger.(*plans.LifecycleActionTrigger)
if !ok {
t.Fatalf("expected action trigger to be a LifecycleActionTrigger, got %T", ai.ActionTrigger)
}

actionTriggers = append(actionTriggers, *at)
}

if !actionTriggers[0].TriggeringResourceAddr.Resource.Resource.Equal(actionTriggers[1].TriggeringResourceAddr.Resource.Resource) {
t.Fatalf("expected both actions to have the same triggering resource address, but got %s and %s", actionTriggers[0].TriggeringResourceAddr, actionTriggers[1].TriggeringResourceAddr)
}

if actionTriggers[0].TriggeringResourceAddr.Resource.Key == actionTriggers[1].TriggeringResourceAddr.Resource.Key {
t.Fatalf("expected both actions to have different triggering resource instance keys, but got the same %s", actionTriggers[0].TriggeringResourceAddr.Resource.Key)
}
},
},

Expand Down Expand Up @@ -1699,6 +1749,82 @@ resource "other_object" "a" {
},
},

"not triggered if module is count=0": {
module: map[string]string{
"main.tf": `
module "mod" {
count = 0
source = "./mod"
}
`,
"mod/mod.tf": `
action "test_action" "hello" {}
resource "other_object" "a" {
lifecycle {
action_trigger {
events = [before_create]
actions = [action.test_action.hello]
}
}
}
`,
},
expectPlanActionCalled: false,
},

"not triggered if for_each is empty": {
module: map[string]string{
"main.tf": `
module "mod" {
for_each = toset([])
source = "./mod"
}
`,
"mod/mod.tf": `
action "test_action" "hello" {}
resource "other_object" "a" {
lifecycle {
action_trigger {
events = [before_create]
actions = [action.test_action.hello]
}
}
}
`,
},
expectPlanActionCalled: false,
},

"action declaration in module if module is count=0": {
module: map[string]string{
"main.tf": `
module "mod" {
count = 0
source = "./mod"
}
`,
"mod/mod.tf": `
action "test_action" "hello" {}
`,
},
expectPlanActionCalled: false,
},

"action declaration in module if for_each is empty": {
module: map[string]string{
"main.tf": `
module "mod" {
for_each = toset([])
source = "./mod"
}
`,
"mod/mod.tf": `
action "test_action" "hello" {}
`,
},
expectPlanActionCalled: false,
},

"provider is within module": {
module: map[string]string{
"main.tf": `
Expand Down
4 changes: 1 addition & 3 deletions internal/terraform/node_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func (n *nodeExpandActionDeclaration) DynamicExpand(ctx EvalContext) (*Graph, tf
resolvedProvider: n.ResolvedProvider,
})
}
addRootNodeToGraph(&g)
}

for _, module := range moduleInstances {
Expand Down Expand Up @@ -94,9 +93,8 @@ func (n *nodeExpandActionDeclaration) DynamicExpand(ctx EvalContext) (*Graph, tf

g.Add(&node)
}

addRootNodeToGraph(&g)
}
addRootNodeToGraph(&g)

return &g, diags
}
Expand Down
Loading