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

Support Jinja2's ChainableUndefined to reduce attribute errors #67106

Open
hackery opened this issue Dec 18, 2024 · 1 comment
Open

Support Jinja2's ChainableUndefined to reduce attribute errors #67106

hackery opened this issue Dec 18, 2024 · 1 comment
Labels
Feature new functionality including changes to functionality and code refactors, etc. needs-triage

Comments

@hackery
Copy link

hackery commented Dec 18, 2024

A common need in Jinja rendered states and files is to access nested attributes of pillar data, but you have to take steps to avoid referencing any undefined values in the chain with many non-trivial merged pillars - using myapp.cfg.pipeline.credentials.msk.keystore needs multiple levels of guard to avoid 'dict object' has no attribute 'msk' type errors. The default(...) construct doesn't help except when used for top-level items.

Describe the solution you'd like
Any nested element in chained attribute access which evaluates as Undefined should not throw an error, but continue to evaluate the whole reference as Undefined.

Jinja has supported a ChainableUndefined environment option since v2.11 in early 2020 which provides exactly this functionality at minimal implementation expense for the Salt project. The one significant complexity I foresee is how this should be optionally enabled - whether it can only be a global option, or should have a way to localise it to target states/files (and whether this applies transitively to include/imported files, rendered managed files etc).

Describe alternatives you've considered

  • Multiple levels of guard on all references - makes files much less readable/maintainable
  • Exhaustive setting of default values everywhere in pillar data - not always appropriate, bloats data and reduces maintainability
@hackery hackery added Feature new functionality including changes to functionality and code refactors, etc. needs-triage labels Dec 18, 2024
Copy link

welcome bot commented Dec 18, 2024

Hi there! Welcome to the Salt Community! Thank you for making your first contribution. We have a lengthy process for issues and PRs. Someone from the Core Team will follow up as soon as possible. In the meantime, here’s some information that may help as you continue your Salt journey.
Please be sure to review our Code of Conduct. Also, check out some of our community resources including:

There are lots of ways to get involved in our community. Every month, there are around a dozen opportunities to meet with other contributors and the Salt Core team and collaborate in real time. The best way to keep track is by subscribing to the Salt Community Events Calendar.
If you have additional questions, email us at [email protected]. We’re glad you’ve joined our community and look forward to doing awesome things with you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature new functionality including changes to functionality and code refactors, etc. needs-triage
Projects
None yet
Development

No branches or pull requests

1 participant