refactor: parse references outside of config

pull/37344/head
Daniel Schmidt 7 months ago
parent c3262b8e5d
commit bc4eb08da4

@ -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

@ -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)
}
}
}
}

@ -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

Loading…
Cancel
Save