소스 검색

Add filters to oo_collect filter so that we can pick and choose what we want to receive back based on more than a single attribute.

Kenny Woodson 10 년 전
부모
커밋
2272985a2d
1개의 변경된 파일12개의 추가작업 그리고 3개의 파일을 삭제
  1. 12 3
      filter_plugins/oo_filters.py

+ 12 - 3
filter_plugins/oo_filters.py

@@ -31,10 +31,16 @@ def get_attr(data, attribute=None):
 
 
   return ptr
   return ptr
 
 
-def oo_collect(data, attribute=None):
+def oo_collect(data, attribute=None, filters={}):
   ''' This takes a list of dict and collects all attributes specified into a list
   ''' This takes a list of dict and collects all attributes specified into a list
-        Ex: data = [ {'a':1,'b':5}, {'a':2}, {'a':3} ]
+      If filter is specified then we will include all items that match _ALL_ of filters.
+        Ex: data = [ {'a':1, 'b':5, 'z': 'z'}, # True, return
+                     {'a':2, 'z': 'z'},        # True, return
+                     {'a':3, 'z': 'z'},        # True, return
+                     {'a':4, 'z': 'b'},        # FAILED, obj['z'] != obj['z']
+                   ]
             attribute = 'a'
             attribute = 'a'
+            filters   = {'z': 'z'}
             returns [1, 2, 3]
             returns [1, 2, 3]
   '''
   '''
 
 
@@ -44,7 +50,10 @@ def oo_collect(data, attribute=None):
   if not attribute:
   if not attribute:
     raise errors.AnsibleFilterError("|failed expects attribute to be set")
     raise errors.AnsibleFilterError("|failed expects attribute to be set")
 
 
-  retval = [get_attr(d, attribute) for d in data]
+  if filters:
+    retval = [get_attr(d, attribute) for d in data if all([ d[key] == filters[key] for key in filters ]) ]
+  else:
+    retval = [get_attr(d, attribute) for d in data]
 
 
   return retval
   return retval