Skip to content

Commit 231b801

Browse files
refactor: separate action triggers from action invocations through --invoke
1 parent 642a27e commit 231b801

File tree

3 files changed

+64
-35
lines changed

3 files changed

+64
-35
lines changed

internal/terraform/graph_builder_plan.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ func (b *PlanGraphBuilder) Steps() []GraphTransformer {
172172
queryPlanMode: b.queryPlan,
173173
},
174174

175+
&ActionInvokeTransformer{
176+
Config: b.Config,
177+
Operation: b.Operation,
178+
Targets: b.ActionTargets,
179+
queryPlanMode: b.queryPlan,
180+
},
181+
175182
// Add dynamic values
176183
&RootVariableTransformer{
177184
Config: b.Config,
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package terraform
5+
6+
import (
7+
"fmt"
8+
9+
"github.com/hashicorp/terraform/internal/addrs"
10+
"github.com/hashicorp/terraform/internal/configs"
11+
)
12+
13+
type ActionInvokeTransformer struct {
14+
Config *configs.Config
15+
Targets []addrs.Targetable
16+
Operation walkOperation
17+
18+
queryPlanMode bool
19+
}
20+
21+
func (t *ActionInvokeTransformer) Transform(g *Graph) error {
22+
if t.Operation != walkPlan || t.queryPlanMode || len(t.Targets) == 0 {
23+
return nil
24+
}
25+
26+
// Then we're invoking and we're just going to include the actions that
27+
// have been specifically asked for.
28+
for _, target := range t.Targets {
29+
var config *configs.Action
30+
switch target := target.(type) {
31+
case addrs.AbsAction:
32+
module := t.Config.DescendantForInstance(target.Module)
33+
if module != nil {
34+
config = module.Module.Actions[target.Action.String()]
35+
}
36+
case addrs.AbsActionInstance:
37+
module := t.Config.DescendantForInstance(target.Module)
38+
if module != nil {
39+
config = module.Module.Actions[target.Action.Action.String()]
40+
}
41+
}
42+
43+
if config == nil {
44+
return fmt.Errorf("action %s does not exist in the configuration", target.String())
45+
}
46+
47+
g.Add(&nodeActionInvokeExpand{
48+
Target: target,
49+
Config: config,
50+
})
51+
}
52+
53+
return nil
54+
55+
}

internal/terraform/transform_action_trigger_config.go

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,11 @@ type ActionTriggerConfigTransformer struct {
2121
}
2222

2323
func (t *ActionTriggerConfigTransformer) Transform(g *Graph) error {
24-
if t.Operation != walkPlan || t.queryPlanMode {
24+
// We don't want to run if we are using the query plan mode or have targets in place
25+
if t.Operation != walkPlan || t.queryPlanMode || len(t.Targets) > 0 {
2526
return nil
2627
}
2728

28-
if len(t.Targets) > 0 {
29-
// Then we're invoking and we're just going to include the actions that
30-
// have been specifically asked for.
31-
32-
for _, target := range t.Targets {
33-
var config *configs.Action
34-
switch target := target.(type) {
35-
case addrs.AbsAction:
36-
module := t.Config.DescendantForInstance(target.Module)
37-
if module != nil {
38-
config = module.Module.Actions[target.Action.String()]
39-
}
40-
case addrs.AbsActionInstance:
41-
module := t.Config.DescendantForInstance(target.Module)
42-
if module != nil {
43-
config = module.Module.Actions[target.Action.Action.String()]
44-
}
45-
}
46-
47-
if config == nil {
48-
return fmt.Errorf("action %s does not exist in the configuration", target.String())
49-
}
50-
51-
g.Add(&nodeActionInvokeExpand{
52-
Target: target,
53-
Config: config,
54-
})
55-
}
56-
57-
return nil
58-
}
59-
60-
// otherwise, add all the action triggers from the config.
61-
6229
return t.transform(g, t.Config)
6330
}
6431

0 commit comments

Comments
 (0)