Page MenuHomeDevCentral

D1421.id3624.diff
No OneTemporary

D1421.id3624.diff

diff --git a/_modules/node.py b/_modules/node.py
--- a/_modules/node.py
+++ b/_modules/node.py
@@ -145,6 +145,36 @@
return filtered_list
+def filter_by_name(pillar_key, nodename=None):
+ '''
+ A function to filter a dictionary by node name.
+
+ The dictionary must respect the following structure:
+ - keys are names to check the current node against
+ - values are list of items
+
+ If a key '*' is also present, it will be included
+ for every node.
+
+ Returns a list, extending all the filtered lists.
+
+ CLI Example:
+
+ salt * node.filter_by_name mars
+ '''
+ if nodename is None:
+ nodename = __grains__['id']
+
+ dictionary = __pillar__.get(pillar_key, {})
+ filtered_list = []
+
+ for name, items in dictionary.items():
+ if name == '*' or name == nodename:
+ filtered_list.extend(items)
+
+ return filtered_list
+
+
def has_web_content(content, nodename=None):
return content in filter_by_role('web_content_sls', nodename)
diff --git a/_tests/data/forests.yaml b/_tests/data/forests.yaml
--- a/_tests/data/forests.yaml
+++ b/_tests/data/forests.yaml
@@ -29,6 +29,16 @@
border:
- Onodlo
+items_by_name:
+ egladil:
+ - Caras Galadhon
+
+items_by_name_with_star:
+ '*':
+ - Air
+ egladil:
+ - Caras Galadhon
+
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
@@ -79,3 +79,40 @@
['Air', 'Caras Galadhon', 'Onodlo'],
sorted(node.filter_by_role('items_by_role_with_star'))
)
+
+ def test_filter_by_name(self):
+ node_key = self.grains['id']
+ self.assertEqual(
+ ['Caras Galadhon'],
+ node.filter_by_name('items_by_name')
+ )
+
+ self.assertEqual(
+ ['Caras Galadhon'],
+ node.filter_by_name('items_by_name', 'egladil')
+ )
+
+ self.grains['id'] = 'entwash'
+ self.assertEqual(
+ [],
+ node.filter_by_name('items_by_name')
+ )
+
+
+ def test_filter_by_name_with_star(self):
+ node_key = self.grains['id']
+ self.assertEqual(
+ ['Air', 'Caras Galadhon'],
+ node.filter_by_name('items_by_name_with_star')
+ )
+
+ self.assertEqual(
+ ['Air', 'Caras Galadhon'],
+ node.filter_by_name('items_by_name_with_star', 'egladil')
+ )
+
+ self.grains['id'] = 'entwash'
+ self.assertEqual(
+ ['Air'],
+ node.filter_by_name('items_by_name_with_star')
+ )

File Metadata

Mime Type
text/plain
Expires
Wed, Nov 27, 11:26 (21 h, 33 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2266825
Default Alt Text
D1421.id3624.diff (2 KB)

Event Timeline