mirror of
https://github.com/drewkerrigan/nagios-http-json.git
synced 2024-11-24 03:13:51 +01:00
Rework JsonHelper.get() to work with bracket in keys
This commit is contained in:
parent
ce9c5fdada
commit
dfcdf4d872
@ -156,24 +156,29 @@ class JsonHelper:
|
||||
data = temp_data
|
||||
else:
|
||||
data = self.data
|
||||
|
||||
if len(key) <= 0:
|
||||
return data
|
||||
if key.find(self.separator) != -1 and \
|
||||
key.find(self.arrayOpener) != -1:
|
||||
|
||||
|
||||
if key.find(self.separator) != -1 and key.find(self.arrayOpener) != -1:
|
||||
if key.find(self.separator) < key.find(self.arrayOpener):
|
||||
return self.getSubElement(key, data)
|
||||
else:
|
||||
return self.getSubArrayElement(key, data)
|
||||
else:
|
||||
|
||||
if key.find(self.separator) != -1:
|
||||
return self.getSubElement(key, data)
|
||||
else:
|
||||
|
||||
if key.find(self.arrayOpener) != -1:
|
||||
# If we got an arrayOpener but the next char is not [0-9] or * then it might just be a string
|
||||
# This isn't optimal since this 'update (foobar)(0)' still won't work
|
||||
if key[key.find(self.arrayOpener)+1].isnumeric() or key[key.find(self.arrayOpener)+1] == "*":
|
||||
return self.getSubArrayElement(key, data)
|
||||
else:
|
||||
|
||||
if isinstance(data, dict) and key in data:
|
||||
return data[key]
|
||||
else:
|
||||
|
||||
return (None, 'not_found')
|
||||
|
||||
def expandKey(self, key, keys):
|
||||
|
@ -302,3 +302,30 @@ class UtilTest(unittest.TestCase):
|
||||
# This should throw an error
|
||||
data = '[]'
|
||||
self.check_data(rules.dash_q(['(*).update_status,warn_me']), data, CRITICAL_CODE)
|
||||
|
||||
def test_bracket_in_key(self):
|
||||
"""
|
||||
https://github.com/drewkerrigan/nagios-http-json/issues/76
|
||||
"""
|
||||
|
||||
rules = RulesHelper()
|
||||
|
||||
# This should work
|
||||
data = '[{"update status": "failure"}]'
|
||||
self.check_data(rules.dash_q(['(*).update status,failure']), data, OK_CODE)
|
||||
|
||||
data = '[{"update (status)": "failure"}]'
|
||||
self.check_data(rules.dash_q(['(*).update (status),failure']), data, OK_CODE)
|
||||
|
||||
data = '[{"update (((status)": "failure"}]'
|
||||
self.check_data(rules.dash_q(['(*).update (((status),failure']), data, OK_CODE)
|
||||
|
||||
data = '[{"update )status)": "failure"}]'
|
||||
self.check_data(rules.dash_q(['(*).update )status),failure']), data, OK_CODE)
|
||||
|
||||
data = '[{"update (status": "failure"}]'
|
||||
self.check_data(rules.dash_q(['(*).update (status),failure']), data, WARNING_CODE)
|
||||
|
||||
# Does not yet work
|
||||
# data = '{"update (foobar)": ["bar"]}'
|
||||
# self.check_data(rules.dash_q(['update (foobar)(0),bar']), data, OK_CODE)
|
||||
|
Loading…
Reference in New Issue
Block a user