Skip to content

Commit f6ad8d3

Browse files
mildwonkeyDanielMSchmidt
authored andcommitted
actions: fix panic during query plan by not planning actions
We don't need to plan any actions during query plan mode. Test stolen from DanielMSchmidt, thank you!
1 parent 152ce1d commit f6ad8d3

File tree

3 files changed

+113
-5
lines changed

3 files changed

+113
-5
lines changed

internal/terraform/context_plan_actions_test.go

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package terraform
55

66
import (
7+
"maps"
78
"path/filepath"
89
"slices"
910
"sort"
@@ -144,7 +145,70 @@ action "test_action" "hello" {}
144145
}
145146
},
146147
},
147-
148+
"query run": {
149+
module: map[string]string{
150+
"main.tf": `
151+
action "test_action" "hello" {}
152+
resource "test_object" "a" {
153+
lifecycle {
154+
action_trigger {
155+
events = [before_create, after_update]
156+
actions = [action.test_action.hello]
157+
}
158+
}
159+
}
160+
`,
161+
"main.tfquery.hcl": `
162+
list "test_resource" "test1" {
163+
provider = "test"
164+
config {
165+
filter = {
166+
attr = "foo"
167+
}
168+
}
169+
}
170+
`,
171+
},
172+
expectPlanActionCalled: false,
173+
planOpts: &PlanOpts{
174+
Mode: plans.NormalMode,
175+
Query: true,
176+
},
177+
},
178+
"query run, action references resource": {
179+
module: map[string]string{
180+
"main.tf": `
181+
action "test_action" "hello" {
182+
config {
183+
attr = resource.test_object.a
184+
}
185+
}
186+
resource "test_object" "a" {
187+
lifecycle {
188+
action_trigger {
189+
events = [before_create, after_update]
190+
actions = [action.test_action.hello]
191+
}
192+
}
193+
}
194+
`,
195+
"main.tfquery.hcl": `
196+
list "test_resource" "test1" {
197+
provider = "test"
198+
config {
199+
filter = {
200+
attr = "foo"
201+
}
202+
}
203+
}
204+
`,
205+
},
206+
expectPlanActionCalled: false,
207+
planOpts: &PlanOpts{
208+
Mode: plans.NormalMode,
209+
Query: true,
210+
},
211+
},
148212
"invalid config": {
149213
module: map[string]string{
150214
"main.tf": `
@@ -3269,6 +3333,47 @@ resource "test_object" "a" {
32693333
},
32703334
},
32713335
},
3336+
ListResourceTypes: map[string]providers.Schema{
3337+
"test_resource": {
3338+
Body: &configschema.Block{
3339+
Attributes: map[string]*configschema.Attribute{
3340+
"data": {
3341+
Type: cty.DynamicPseudoType,
3342+
Computed: true,
3343+
},
3344+
},
3345+
BlockTypes: map[string]*configschema.NestedBlock{
3346+
"config": {
3347+
Block: configschema.Block{
3348+
Attributes: map[string]*configschema.Attribute{
3349+
"filter": {
3350+
Required: true,
3351+
NestedType: &configschema.Object{
3352+
Nesting: configschema.NestingSingle,
3353+
Attributes: map[string]*configschema.Attribute{
3354+
"attr": {
3355+
Type: cty.String,
3356+
Required: true,
3357+
},
3358+
},
3359+
},
3360+
},
3361+
},
3362+
},
3363+
Nesting: configschema.NestingSingle,
3364+
},
3365+
},
3366+
},
3367+
},
3368+
},
3369+
},
3370+
ListResourceFn: func(req providers.ListResourceRequest) providers.ListResourceResponse {
3371+
resp := []cty.Value{}
3372+
ret := req.Config.AsValueMap()
3373+
maps.Copy(ret, map[string]cty.Value{
3374+
"data": cty.TupleVal(resp),
3375+
})
3376+
return providers.ListResourceResponse{Result: cty.ObjectVal(ret)}
32723377
},
32733378
}
32743379

internal/terraform/graph_builder_plan.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,10 @@ func (b *PlanGraphBuilder) Steps() []GraphTransformer {
174174
},
175175

176176
&ActionPlanTransformer{
177-
Config: b.Config,
178-
Operation: b.Operation,
179-
Targets: b.ActionTargets,
177+
Config: b.Config,
178+
Operation: b.Operation,
179+
Targets: b.ActionTargets,
180+
queryPlanMode: b.queryPlan,
180181
},
181182

182183
// Add dynamic values

internal/terraform/transform_action_plan.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ type ActionPlanTransformer struct {
1616
Config *configs.Config
1717
Targets []addrs.Targetable
1818
Operation walkOperation
19+
20+
queryPlanMode bool
1921
}
2022

2123
func (t *ActionPlanTransformer) Transform(g *Graph) error {
22-
if t.Operation != walkPlan {
24+
if t.Operation != walkPlan || t.queryPlanMode {
2325
return nil
2426
}
2527

0 commit comments

Comments
 (0)