Merge pull request #24587 from hashicorp/jbardin/disallow-count-and-foreach

don't allow count && for_each in module calls
This commit is contained in:
James Bardin 2020-04-07 17:30:04 -04:00 committed by GitHub
commit 2eba39a671
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View File

@ -71,6 +71,15 @@ func decodeModuleBlock(block *hcl.Block, override bool) (*ModuleCall, hcl.Diagno
} }
if attr, exists := content.Attributes["for_each"]; exists { if attr, exists := content.Attributes["for_each"]; exists {
if mc.Count != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: `Invalid combination of "count" and "for_each"`,
Detail: `The "count" and "for_each" meta-arguments are mutually-exclusive, only one should be used to be explicit about the number of resources to be created.`,
Subject: &attr.NameRange,
})
}
mc.ForEach = attr.Expr mc.ForEach = attr.Expr
} }

View File

@ -21,6 +21,7 @@ func TestLoadModuleCall(t *testing.T) {
file, diags := parser.LoadConfigFile("module-calls.tf") file, diags := parser.LoadConfigFile("module-calls.tf")
assertExactDiagnostics(t, diags, []string{ assertExactDiagnostics(t, diags, []string{
`module-calls.tf:22,3-13: Reserved argument name in module block; The name "depends_on" is reserved for use in a future version of Terraform.`, `module-calls.tf:22,3-13: Reserved argument name in module block; The name "depends_on" is reserved for use in a future version of Terraform.`,
`module-calls.tf:20,3-11: Invalid combination of "count" and "for_each"; The "count" and "for_each" meta-arguments are mutually-exclusive, only one should be used to be explicit about the number of resources to be created.`,
}) })
gotModules := file.ModuleCalls gotModules := file.ModuleCalls