Skip to content

Commit edf0cd6

Browse files
authored
actions: fail invoke plans when action configurations are unknown (#37793)
1 parent 88e4c9e commit edf0cd6

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

internal/terraform/context_plan_actions_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2726,6 +2726,43 @@ action "test_action" "one" {
27262726
},
27272727
},
27282728

2729+
"invoke action with partially applied configuration": {
2730+
module: map[string]string{
2731+
"main.tf": `
2732+
resource "test_object" "a" {
2733+
name = "hello"
2734+
}
2735+
2736+
action "test_action" "one" {
2737+
config {
2738+
attr = test_object.a.name
2739+
}
2740+
}
2741+
`,
2742+
},
2743+
planOpts: &PlanOpts{
2744+
Mode: plans.RefreshOnlyMode,
2745+
ActionTargets: []addrs.Targetable{
2746+
addrs.AbsAction{
2747+
Action: addrs.Action{
2748+
Type: "test_action",
2749+
Name: "one",
2750+
},
2751+
},
2752+
},
2753+
},
2754+
expectPlanActionCalled: false,
2755+
assertPlanDiagnostics: func(t *testing.T, diagnostics tfdiags.Diagnostics) {
2756+
if len(diagnostics) != 1 {
2757+
t.Errorf("expected exactly one diagnostic but got %d", len(diagnostics))
2758+
}
2759+
2760+
if diagnostics[0].Description().Summary != "Partially applied configuration" {
2761+
t.Errorf("wrong diagnostic: %s", diagnostics[0].Description().Summary)
2762+
}
2763+
},
2764+
},
2765+
27292766
"non-referenced resource isn't refreshed during invoke": {
27302767
module: map[string]string{
27312768
"main.tf": `

internal/terraform/node_action_invoke.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,20 @@ func (n *nodeActionInvokeInstance) Execute(ctx EvalContext, _ walkOperation) tfd
165165
}
166166

167167
unmarkedConfig, _ := actionInstance.ConfigValue.UnmarkDeepWithPaths()
168+
169+
if !unmarkedConfig.IsWhollyKnown() {
170+
// we're not actually planning or applying changes from the
171+
// configuration. if the configuration of the action has unknown values
172+
// it means one of the resources that are referenced hasn't actually
173+
// been created.
174+
return diags.Append(&hcl.Diagnostic{
175+
Severity: hcl.DiagError,
176+
Summary: "Partially applied configuration",
177+
Detail: fmt.Sprintf("The action %s contains unknown values while planning. This means it is referencing resources that have not yet been created, please run a complete plan/apply cycle to ensure the state matches the configuration before using the -invoke argument.", n.Target.String()),
178+
Subject: n.Config.DeclRange.Ptr(),
179+
})
180+
}
181+
168182
resp := provider.PlanAction(providers.PlanActionRequest{
169183
ActionType: n.Target.Action.Action.Type,
170184
ProposedActionData: unmarkedConfig,

0 commit comments

Comments
 (0)