saltstack/salt — pull request #8323 by regilero (2013-11-07 16:23:23+00:00)
Title: Default for filter_by method in module grains 

This is both a new feature (adding a default key handling in filter_by) so that you set some values in the dictionnary for two cases:
* when the grain is not present
* when the grain value as no matching case in the given dictionnary

{% set apache = salt['grains.filter_by']({
'Debian': {'pkg': 'apache2', 'srv': 'apache2'},
'RedHat': {'pkg': 'httpd', 'srv': 'httpd'},
'default': {'pkg': 'apache', 'srv': 'apache'},
}) %}

And it also feature an Exception when the merge dictionnary is not a dictionnary (very easy to make when using the pillar for merge data, easy to add a ``-`` and transform the dictionnary in list). Makina this error when using filter_by in jinja will not prevent a Stack Trace but the message will be easyier to understand I think.

In the current implementation if grain is not matched in the dictionnary or not present the result is always None (or a crash if you pass a merge dictionnary which try to merge with None)

Unit tests present.