Shared lib of small but usefull functions and other
This commit adds 'sharedlib' loader that allows to organize
functions into tree structure. Salt doesn't allow this as it
only imports top-level modules
https://github.com/saltstack/salt/issues/37273
See README for more details.
Change-Id: I7827c42f8f0d4caef56eff6352a49fe1a95a50cc
diff --git a/_modules/sharedlib/misc.py b/_modules/sharedlib/misc.py
new file mode 100644
index 0000000..5a6f5c4
--- /dev/null
+++ b/_modules/sharedlib/misc.py
@@ -0,0 +1,22 @@
+def cast_dict_keys_to_int(x, *args, **kwargs):
+ '''
+ Return a dictionary with keys casted to int.
+ This usually is required when you want sort the dict later.
+
+ Jinja examples:
+
+ .. code-block: jinja
+
+ {%- set ruleset = salt['sharedlib.call']('misc.cast_dict_keys_to_int', c.get('ruleset', {})) %}
+
+ .. code-block:: jinja
+
+ {%- set func = salt['sharedlib.get']('misc.cast_dict_keys_to_int') %}
+ {%- for c_name, c in t.chains.items() %}
+ {%- set ruleset = func(c.get('ruleset', {})) %}
+ {%- for rule_id, r in ruleset | dictsort %}
+ ...
+ {%- endfor %}
+ '''
+ return dict([(int(key),value) for key,value in x.items()])
+