Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3785921
D1421.id3624.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D1421.id3624.diff
View Options
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
Details
Attached
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)
Attached To
Mode
D1421: Allow to filter a dictionary by node name
Attached
Detach File
Event Timeline
Log In to Comment