LiveQuery: 'fields' is now optional

Change-Id: I68db54460f356b07af0691057e7f7c7f14ce0576
This commit is contained in:
aviau 2015-04-20 11:36:10 -04:00
parent fa47ae8302
commit b16491e9f4
3 changed files with 38 additions and 5 deletions

View File

@ -26,7 +26,7 @@ class LiveQuery(types.Base):
filters = wsme.wsattr(wtypes.text, mandatory=True)
"The filter expression encoded in json."
fields = wsme.wsattr(wtypes.text, mandatory=True)
fields = wsme.wsattr(wtypes.text, mandatory=False)
"List of fields to include in the response."
@classmethod

View File

@ -14,6 +14,8 @@
import json
import wsme
def filter_dict_list_with_live_query(item_list, live_query):
filters = json.loads(live_query.filters)
@ -42,11 +44,13 @@ def filter_dict_list_with_live_query(item_list, live_query):
break
if matches:
fields = json.loads(live_query.fields)
matching_item = {}
for field in fields:
matching_item[field] = item[field]
if live_query.fields != wsme.Unset:
fields = json.loads(live_query.fields)
for field in fields:
matching_item[field] = item[field]
else:
matching_item = item
matching_items.append(matching_item)
return matching_items

View File

@ -84,3 +84,32 @@ class LiveQueryFilterTest(base.BaseTestCase):
expected = [{"host_name": "localhost"}]
self.assertItemsEqual(result, expected)
def test_query_builder_filter_all_fields(self):
query = live_query.LiveQuery(
filters=json.dumps({
"is": {
"state": [0],
"description": ["localhost"]
},
"isnot": {
"state": [1]
}
})
)
result = query_filter.filter_dict_list_with_live_query(
self.items,
query
)
expected = [
{'description': 'localhost',
'last_state_change': 1429400991,
'plugin_output': 'OK - localhost: rta 0.047ms, lost 0%',
'last_check': 1429400990,
'state': 0,
'host_name': 'localhost'}
]
self.assertItemsEqual(result, expected)