Skip to content

Commit accaa8d

Browse files
also check against action references in depends_on during config parsing
1 parent 085c762 commit accaa8d

File tree

4 files changed

+38
-70
lines changed

4 files changed

+38
-70
lines changed

internal/configs/config_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,19 @@ func TestConfigImportProviderWithNoResourceProvider(t *testing.T) {
581581
Use the provider argument in the target resource block to configure the provider for a resource with explicit provider configuration.`,
582582
})
583583
}
584+
585+
func TestConfigActionInResourceDependsOn(t *testing.T) {
586+
src, err := os.ReadFile("testdata/invalid-modules/action-in-depends_on/action-in-resource-depends_on.tf")
587+
if err != nil {
588+
t.Fatal(err)
589+
}
590+
591+
parser := testParser(map[string]string{
592+
"main.tf": string(src),
593+
})
594+
595+
_, diags := parser.LoadConfigFile("main.tf")
596+
assertExactDiagnostics(t, diags, []string{
597+
`main.tf:5,17-42: Invalid depends_on Action Reference; The depends_on attribute cannot reference action blocks directly. You must reference a resource or data source instead.`,
598+
})
599+
}

internal/configs/depends_on.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,18 @@ func DecodeDependsOn(attr *hcl.Attribute) ([]hcl.Traversal, hcl.Diagnostics) {
1717

1818
traversal, travDiags := hcl.AbsTraversalForExpr(expr)
1919
diags = append(diags, travDiags...)
20+
2021
if len(traversal) != 0 {
21-
ret = append(ret, traversal)
22+
if traversal.RootName() == "action" {
23+
diags = append(diags, &hcl.Diagnostic{
24+
Severity: hcl.DiagError,
25+
Summary: "Invalid depends_on Action Reference",
26+
Detail: "The depends_on attribute cannot reference action blocks directly. You must reference a resource or data source instead.",
27+
Subject: expr.Range().Ptr(),
28+
})
29+
} else {
30+
ret = append(ret, traversal)
31+
}
2232
}
2333
}
2434

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
action "aws_action" "example" {
2+
}
3+
4+
resource "aws_instance" "web" {
5+
depends_on = [action.aws_action.example]
6+
ami = "ami-1234"
7+
security_groups = [
8+
"foo",
9+
"bar",
10+
]
11+
}

internal/terraform/context_plan_actions_test.go

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -319,75 +319,6 @@ output "my_output2" {
319319
},
320320
},
321321

322-
"actions can't be used in depends_on": {
323-
module: map[string]string{
324-
"main.tf": `
325-
action "test_action" "my_action" {
326-
config {
327-
attr = "value"
328-
}
329-
}
330-
resource "test_object" "a" {
331-
depends_on = [action.test_action.my_action]
332-
lifecycle {
333-
action_trigger {
334-
events = [before_create]
335-
actions = [action.test_action.my_action]
336-
}
337-
}
338-
}
339-
`,
340-
},
341-
expectValidateDiagnostics: func(m *configs.Config) tfdiags.Diagnostics {
342-
return tfdiags.Diagnostics{}.Append(
343-
&hcl.Diagnostic{
344-
Severity: hcl.DiagError,
345-
Summary: "Invalid depends_on reference",
346-
Detail: "Actions can not be referenced in depends_on. Use depends_on on the resource that triggers the action instead.",
347-
Subject: &hcl.Range{
348-
Filename: filepath.Join(m.Module.SourceDir, "main.tf"),
349-
Start: hcl.Pos{Line: 8, Column: 17, Byte: 117},
350-
End: hcl.Pos{Line: 8, Column: 45, Byte: 145},
351-
},
352-
})
353-
},
354-
},
355-
356-
"action instances can't be used in depends_on": {
357-
module: map[string]string{
358-
"main.tf": `
359-
action "test_action" "my_action" {
360-
count = 3
361-
config {
362-
attr = "value"
363-
}
364-
}
365-
resource "test_object" "a" {
366-
depends_on = [action.test_action.my_action[1]]
367-
lifecycle {
368-
action_trigger {
369-
events = [before_create]
370-
actions = [action.test_action.my_action[1]]
371-
}
372-
}
373-
}
374-
`,
375-
},
376-
expectValidateDiagnostics: func(m *configs.Config) tfdiags.Diagnostics {
377-
return tfdiags.Diagnostics{}.Append(
378-
&hcl.Diagnostic{
379-
Severity: hcl.DiagError,
380-
Summary: "Invalid depends_on reference",
381-
Detail: "Actions can not be referenced in depends_on. Use depends_on on the resource that triggers the action instead.",
382-
Subject: &hcl.Range{
383-
Filename: filepath.Join(m.Module.SourceDir, "main.tf"),
384-
Start: hcl.Pos{Line: 9, Column: 17, Byte: 129},
385-
End: hcl.Pos{Line: 9, Column: 48, Byte: 160},
386-
},
387-
})
388-
},
389-
},
390-
391322
"destroy run": {
392323
module: map[string]string{
393324
"main.tf": `

0 commit comments

Comments
 (0)