From bc4eb08da4d6bc84d52b81c2a3e99bd0d64413c8 Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Mon, 14 Jul 2025 12:02:05 +0200 Subject: [PATCH] refactor: parse references outside of config --- internal/configs/action.go | 36 ++++++------------- internal/terraform/node_resource_abstract.go | 8 ++++- .../terraform/node_resource_plan_instance.go | 10 ++++-- 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/internal/configs/action.go b/internal/configs/action.go index e0c6dcf98d..e45307f5f6 100644 --- a/internal/configs/action.go +++ b/internal/configs/action.go @@ -35,7 +35,7 @@ type Action struct { type ActionTrigger struct { Condition hcl.Expression Events []ActionTriggerEvent - Actions []*addrs.Reference // References to actions + Actions []ActionRef // References to actions DeclRange hcl.Range } @@ -57,28 +57,16 @@ const ( // ActionRef represents a reference to a configured Action type ActionRef struct { - Type string - Name string - Key addrs.InstanceKey + Traversal hcl.Traversal Range hcl.Range } -// Addr returns the address of the action reference. -// Since we don't allow referencing actions across module boundaries, -// this is always a relative address. -func (a ActionRef) Addr() addrs.ActionInstance { - return addrs.Action{ - Type: a.Type, - Name: a.Name, - }.Instance(a.Key) -} - func decodeActionTriggerBlock(block *hcl.Block) (*ActionTrigger, hcl.Diagnostics) { var diags hcl.Diagnostics a := &ActionTrigger{ Events: []ActionTriggerEvent{}, - Actions: []*addrs.Reference{}, + Actions: []ActionRef{}, Condition: nil, } @@ -126,7 +114,7 @@ func decodeActionTriggerBlock(block *hcl.Block) (*ActionTrigger, hcl.Diagnostics if attr, exists := content.Attributes["actions"]; exists { exprs, ediags := hcl.ExprList(attr.Expr) diags = append(diags, ediags...) - actions := []*addrs.Reference{} + actions := []ActionRef{} for _, expr := range exprs { traversal, travDiags := hcl.AbsTraversalForExpr(expr) diags = append(diags, travDiags...) @@ -141,17 +129,13 @@ func decodeActionTriggerBlock(block *hcl.Block) (*ActionTrigger, hcl.Diagnostics continue } - ref, refDiags := addrs.ParseRef(traversal) - if refDiags.HasErrors() { - diags = append(diags, &hcl.Diagnostic{ - Severity: hcl.DiagError, - Summary: "Invalid action reference", - Detail: fmt.Sprintf("The action reference %q is invalid: %s", traversal, refDiags.Err()), - Subject: expr.Range().Ptr(), - }) - continue + if len(traversal) != 0 { + actionRef := ActionRef{ + Traversal: traversal, + Range: expr.Range(), + } + actions = append(actions, actionRef) } - actions = append(actions, ref) } a.Actions = actions diff --git a/internal/terraform/node_resource_abstract.go b/internal/terraform/node_resource_abstract.go index e2555af7c3..b7f8db72b9 100644 --- a/internal/terraform/node_resource_abstract.go +++ b/internal/terraform/node_resource_abstract.go @@ -218,7 +218,13 @@ func (n *NodeAbstractResource) References() []*addrs.Reference { // All actions referenced in the action triggeres should be evaluated prior. for _, at := range c.Managed.ActionTriggers { - result = append(result, at.Actions...) + for _, actionRef := range at.Actions { + // This should have been caught during validation + ref, _ := addrs.ParseRef(actionRef.Traversal) + if ref != nil { + result = append(result, ref) + } + } } } diff --git a/internal/terraform/node_resource_plan_instance.go b/internal/terraform/node_resource_plan_instance.go index 54550814dd..1f1310f203 100644 --- a/internal/terraform/node_resource_plan_instance.go +++ b/internal/terraform/node_resource_plan_instance.go @@ -573,9 +573,15 @@ func (n *NodePlannableResourceInstance) planActionTriggers(ctx EvalContext, chan for j, actionRef := range at.Actions { var actionAddr addrs.ActionInstance - if a, ok := actionRef.Subject.(addrs.ActionInstance); ok { + ref, parseRefDiags := addrs.ParseRef(actionRef.Traversal) + diags = diags.Append(parseRefDiags) + if parseRefDiags.HasErrors() { + return diags + } + + if a, ok := ref.Subject.(addrs.ActionInstance); ok { actionAddr = a - } else if a, ok := actionRef.Subject.(addrs.Action); ok { + } else if a, ok := ref.Subject.(addrs.Action); ok { // Could be a reference to an action without an instance specified // TODO: This is where we should auto-expand, for now we will just default to taking // the action address with no key