Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relax import for_each validation #36119

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
relax import for_each validation
Import blocks with for_each were intended to require resource blocks
with a matching expansion. This validation however did not take place in
prior releases, and configurations with this mismatch currently exist in
production.

That simple validation also does not take into account a single resource
instance inside of an expanded module, which is a valid use case and
harder to check for at the point of validation. That will still fail
correctly during plan if the targets are not available.

We can skip this validation for now, and work on generating static
validation errors later on if possible, taking into account the expanded
module case.
  • Loading branch information
jbardin committed Nov 27, 2024
commit 926b688c5aa2c92f69efa7983f8ab7fbe6d99e97
2 changes: 1 addition & 1 deletion internal/terraform/context_apply_deferred_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2915,7 +2915,7 @@ import {
wantDeferred: make(map[string]ExpectedDeferred),
wantDiagnostic: func(diags tfdiags.Diagnostics) bool {
for _, diag := range diags {
if diag.Description().Summary == "Use of import for_each in an invalid context" {
if diag.Description().Summary == "Resource has no configuration" {
return true
}
}
Expand Down
13 changes: 0 additions & 13 deletions internal/terraform/node_resource_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,20 +572,7 @@ func (n *NodeValidatableResource) validateImportTargets(ctx EvalContext) tfdiags
return diags
}

// Resource config might be nil here since we are also validating config generation.
expanded := n.Config != nil && (n.Config.ForEach != nil || n.Config.Count != nil)

if imp.Config.ForEach != nil {
if !expanded {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Use of import for_each in an invalid context",
Detail: "Use of for_each in import requires a resource using count or for_each.",
// FIXME: minor issue, but this points to the for_each expression rather than for_each itself.
Subject: imp.Config.ForEach.Range().Ptr(),
})
}

forEachData, _, forEachDiags := newForEachEvaluator(imp.Config.ForEach, ctx, true).ImportValues()
diags = diags.Append(forEachDiags)
if forEachDiags.HasErrors() {
Expand Down
Loading