Skip to content

Commit ddb4a13

Browse files
author
Frederick Ross
committed
Fixed several failing tests by improving __contains__; added workaround for an error in the returns of fired alerts.
__contains__ had used __getitem__, but the confs endpoints make that untenable: they return collections, not entities, from __getitem__, which means they don't make a roundtrip to the server to check for actual existence. I also had to add handling of atom feeds containing no entries to __contains__ and __getitem__ so that it would work for fired alerts, which return empty atom feeds instead of 404s if the alert doesn't exist.
1 parent a5dc397 commit ddb4a13

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

splunklib/client.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -844,18 +844,26 @@ def __init__(self, service, path, item=Entity):
844844
self.item = item # Item accessor
845845
self.null_count = -1
846846

847-
848847
def __contains__(self, name):
849848
"""Is there at least one entry called *name* in this collection?
850849
851850
Makes a single roundtrip to the server, plus at most two more
852851
if autologin is enabled.
853852
"""
854853
try:
855-
self[name]
856-
return True
857-
except KeyError:
858-
return False
854+
response = self.get(name)
855+
entries = self._load_list(response)
856+
if len(entries) == 0:
857+
# We need this because in 4.2 and 4.3,
858+
# fired alerts return an empty feed instead of 404.
859+
return False
860+
else:
861+
return True
862+
except HTTPError as he:
863+
if he.status == 404:
864+
return False
865+
else:
866+
raise
859867

860868
def __getitem__(self, key):
861869
"""Fetch an item named *key* from this collection.
@@ -916,6 +924,8 @@ def __getitem__(self, key):
916924
entries = self._load_list(response)
917925
if len(entries) > 1:
918926
raise ValueError("Found multiple entities named '%s'; please specify a namespace." % key)
927+
elif len(entries) == 0:
928+
raise KeyError(key)
919929
else:
920930
return entries[0]
921931
except HTTPError as he:

0 commit comments

Comments
 (0)