diff --git a/internal/terraform/context_apply_action_test.go b/internal/terraform/context_apply_action_test.go index 454e7d2f04..ee15c7a3c8 100644 --- a/internal/terraform/context_apply_action_test.go +++ b/internal/terraform/context_apply_action_test.go @@ -1852,6 +1852,46 @@ resource "test_object" "a" { }, expectInvokeActionCalled: true, }, + "referencing triggering resource in after_* condition": { + module: map[string]string{ + "main.tf": ` +action "action_example" "hello" { + config { + attr = "hello" + } +} +action "action_example" "world" { + config { + attr = "world" + } +} +resource "test_object" "a" { + name = "foo" + lifecycle { + action_trigger { + events = [after_create] + condition = test_object.a.name == "foo" + actions = [action.action_example.hello] + } + action_trigger { + events = [after_update] + condition = test_object.a.name == "bar" + actions = [action.action_example.world] + } + } +} +`, + }, + expectInvokeActionCalled: true, + expectInvokeActionCalls: []providers.InvokeActionRequest{ + { + ActionType: "action_example", + PlannedActionData: cty.ObjectVal(map[string]cty.Value{ + "attr": cty.StringVal("hello"), + }), + }, + }, + }, "multiple events triggering in same action trigger": { module: map[string]string{ "main.tf": ` diff --git a/internal/terraform/context_plan_actions_test.go b/internal/terraform/context_plan_actions_test.go index f9d8782397..b928fd6252 100644 --- a/internal/terraform/context_plan_actions_test.go +++ b/internal/terraform/context_plan_actions_test.go @@ -3053,6 +3053,73 @@ resource "test_object" "a" { }, }, + "referencing triggering resource in before_* condition": { + module: map[string]string{ + "main.tf": ` +action "test_action" "hello" {} +action "test_action" "world" {} +resource "test_object" "a" { + name = "foo" + lifecycle { + action_trigger { + events = [before_create] + condition = test_object.a.name == "foo" + actions = [action.test_action.hello] + } + action_trigger { + events = [before_update] + condition = test_object.a.name == "bar" + actions = [action.test_action.world] + } + } +} +`, + }, + expectPlanActionCalled: true, + + assertPlanDiagnostics: func(t *testing.T, diags tfdiags.Diagnostics) { + if !diags.HasErrors() { + t.Errorf("expected errors, got none") + } + + err := diags.Err().Error() + if !strings.Contains(err, "Cycle:") || !strings.Contains(err, "action.test_action.hello") || !strings.Contains(err, "test_object.a") { + t.Fatalf("Expected '[Error] Cycle: action.test_action.hello (instance), test_object.a', got '%s'", err) + } + }, + }, + + "referencing triggering resource in after_* condition": { + module: map[string]string{ + "main.tf": ` +action "test_action" "hello" {} +action "test_action" "world" {} +resource "test_object" "a" { + name = "foo" + lifecycle { + action_trigger { + events = [after_create] + condition = test_object.a.name == "foo" + actions = [action.test_action.hello] + } + action_trigger { + events = [after_update] + condition = test_object.a.name == "bar" + actions = [action.test_action.world] + } + } +} +`, + }, + expectPlanActionCalled: true, + + assertPlan: func(t *testing.T, p *plans.Plan) { + if len(p.Changes.ActionInvocations) != 1 { + t.Errorf("expected 1 action invocation, got %d", len(p.Changes.ActionInvocations)) + } + }, + }, + "using each in before_* condition": { module: map[string]string{ "main.tf": `