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

core: move import validation into the validate graph #35543

Merged
merged 6 commits into from
Aug 9, 2024

Conversation

jbardin
Copy link
Member

@jbardin jbardin commented Aug 8, 2024

When import blocks were first introduced they had to be fully static, which allowed them to be validated during the configuration loading process. This early static validation was carried forward as more features were introduced, but that led to some validation in awkward places like the graph builder, and some validation which could not be done like validating the to expression.

Moving the import validation into the validate graph allows us to more fully validate the block contents, in the same way as normal resource configuration and mostly unifies where validation happens. We still have a couple outliers due to the possibility of config generation, which requires some checks in the config transformer for entirely missing modules when the resource has no config, and the plan node to suggest config generation if the resource has no configuration.

Now that Validate handles most of the actual validation, I inserted Validate calls before the import Plan calls in tests, or replace the Plan calls entirely when it was clear that the desired diagnostics had moved into Validate. This was to leverage the existing tests both for the new code, and to verify there are no test regressions since Validate` would always be called during a plan from the command line.

Fixes #35516

Import blocks were originally static and had no use for dynamic
evaluation. Once we added dynamic IDs and for_each expansion however,
validation should have moved into the validation graph for evaluation.
Have the resource validation node check if config generation is possible
when there is no config given. We can't check the output path here
because that is a plan option, so a final suggestion to use
-generate-config-out will have to wait until plan.
This functionality was never developed further, no need to leave the
comment here.
Remove the redundant validation from the config transformers, making
sure most of the validation is now done by NodeValidatableResource.
Inset Validate calls into import tests to ensure both that existing
errors are still found, and that existing plans don't fail the new
validation code.
@jbardin jbardin requested a review from a team August 8, 2024 18:57
}
}
*/
// BUG(paddy): we're not validating provider_meta blocks on EvalValidate right now
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ProviderMetas are not being developed further, and we can removed these large block comments

@@ -28,11 +28,6 @@ func evaluateImportIdExpression(expr hcl.Expression, target addrs.AbsResourceIns
})
}

diags = diags.Append(validateImportSelfRef(target.Resource.Resource, expr))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validateImportSelfRef is now being done during validation, so we can remove the call along with the extra target argument

Copy link
Member

@liamcervante liamcervante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, totally optional thought about the new validation added!

if diags.HasErrors() {
return diags
}
diags = diags.Append(n.validateImportTargetExpansion(to, imp.Config.To))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth also adding this new validation to the equivalent part of the plan? It only matters in situations where the expansion is unknown during the validation, and I guess we might get a different error message about the resource being missing or something.

Just wondering if it's worth being consistent?

Copy link
Member Author

@jbardin jbardin Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, the plan error will just say the resource is missing, and these errors are probably more helpful. I initially wanted to do this all statically (which I think is nearly possible except when the use has any typed variables involved) but carrying the type info forward to determine what each.key is is not so easy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can often still get the type of the for_each expression when it's unknown, and we have the To traversal. It would be easy to validate if the To expression only allowed indexing with each.key but in reality it can use anything from each, so we need to find and extract the resource index key expression and apply that path to the for_each type to determine if we have a numeric or string index. We're so close to being able to statically verify this, but the required code just felt too big and cumbersome to throw in for these tiny edge cases and still not cover the cases where the type is unknown.

Instead added the same validation checks into the plan node too for the better error messages.

@jbardin jbardin merged commit cf94229 into main Aug 9, 2024
6 checks passed
@jbardin jbardin deleted the jbardin/import-validate branch August 9, 2024 15:34
Copy link

github-actions bot commented Aug 9, 2024

Reminder for the merging maintainer: if this is a user-visible change, please update the changelog on the appropriate release branch.

Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 11, 2024
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

terraform validate does not validate import blocks
2 participants