Page MenuHomeDevCentral

D1257.id3233.diff
No OneTemporary

D1257.id3233.diff

diff --git a/_modules/node.py b/_modules/node.py
--- a/_modules/node.py
+++ b/_modules/node.py
@@ -117,6 +117,31 @@
return role in list('roles', nodename)
+def filter_by_role(pillar_key, nodename=None):
+ '''
+ A function to filter a dictionary by roles.
+
+ The dictionary must respect the following structure:
+ - keys are role to check the current node against
+ - values are list of items
+
+ Returns a list, extending all the filtered lists.
+
+ CLI Example:
+
+ salt * node.filter_by_role web_content_sls
+ '''
+ roles = list('roles', nodename)
+ dictionary = __pillar__.get(pillar_key, {})
+ filtered_list = []
+
+ for role, items in dictionary.iteritems():
+ if role in roles:
+ filtered_list.extend(items)
+
+ return filtered_list
+
+
def get_wwwroot(nodename=None):
'''
A function to determine the wwwroot folder to use.
diff --git a/_tests/data/forests.yaml b/_tests/data/forests.yaml
--- a/_tests/data/forests.yaml
+++ b/_tests/data/forests.yaml
@@ -7,9 +7,19 @@
egladil:
forest: lothlorien
hostname: egladil.lothlorien.forest
+ roles:
+ - treecity
entwash:
forest: fangorn
hostname: entwash.node
+ roles:
+ - border
+
+items_by_role:
+ treecity:
+ - Caras Galadhon
+ border:
+ - Onodlo
shellgroups_ubiquity:
- ubiquity
diff --git a/_tests/modules/test_node.py b/_tests/modules/test_node.py
--- a/_tests/modules/test_node.py
+++ b/_tests/modules/test_node.py
@@ -22,3 +22,25 @@
node.get_wwwroot())
self.assertEqual("wwwroot/entwash.node/www",
node.get_wwwroot('entwash'))
+
+ def test_filter_by_role(self):
+ node_key = self.grains['id']
+
+ self.assertEqual(['Caras Galadhon'],
+ node.filter_by_role('items_by_role'))
+
+ self.assertEqual(['Onodlo'],
+ node.filter_by_role('items_by_role', 'entwash'))
+
+ # No role
+ self.pillar['nodes'][node_key]['roles'] = []
+ self.assertEqual([],
+ node.filter_by_role('items_by_role'))
+
+ # More than one role
+ self.pillar['nodes'][node_key]['roles'] = [
+ 'border',
+ 'treecity'
+ ]
+ self.assertEqual(['Caras Galadhon', 'Onodlo'],
+ sorted(node.filter_by_role('items_by_role')))

File Metadata

Mime Type
text/plain
Expires
Tue, Nov 26, 00:22 (19 h, 28 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2263261
Default Alt Text
D1257.id3233.diff (2 KB)

Event Timeline