Skip to content

Commit 8844307

Browse files
author
Frederick Ross
committed
Added paging of collections.
1 parent 1443f05 commit 8844307

2 files changed

Lines changed: 58 additions & 14 deletions

File tree

‎splunklib/client.py‎

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,8 @@ class Collection(Endpoint):
834834
def __init__(self, service, path, item=Entity):
835835
Endpoint.__init__(self, service, path)
836836
self.item = item # Item accessor
837+
self.null_count = -1
838+
837839

838840
def __contains__(self, name):
839841
"""Is there at least one entry called *name* in this collection?
@@ -927,7 +929,7 @@ def __iter__(self):
927929
for entity in saved_searches:
928930
print "Saved search named %s" % entity.name
929931
"""
930-
for item in self.list():
932+
for item in self.iter():
931933
yield item
932934

933935
def __len__(self):
@@ -1140,8 +1142,39 @@ def itemmeta(self):
11401142
content = _load_atom(response, MATCH_ENTRY_CONTENT)
11411143
return _parse_atom_metadata(content)
11421144

1145+
def iter(self, offset=0, count=None, pagesize=None, **kwargs):
1146+
"""Iterate (possibly lazily) over the collection.
1147+
1148+
This is equivalent to the :meth:`list` method, but
1149+
it returns an iterator, and can load the entities a few at a
1150+
time from the server (the number so loaded is controlled by
1151+
the *pagesize* argument).
1152+
1153+
**Example**::
1154+
1155+
import splunklib.client as client
1156+
s = client.connect(...)
1157+
for saved_search in s.saved_searches.iter(pagesize=10):
1158+
# Loads 10 saved searches at a time from the
1159+
# server.
1160+
...
1161+
"""
1162+
if count is None:
1163+
count = self.null_count
1164+
fetched = 0
1165+
while count == self.null_count or fetched < count:
1166+
response = self.get(count=pagesize or count, offset=offset, **kwargs)
1167+
items = self._load_list(response)
1168+
N = len(items)
1169+
fetched += N
1170+
for item in items:
1171+
yield item
1172+
if pagesize is None or N < pagesize:
1173+
break
1174+
offset += N
1175+
11431176
# kwargs: count, offset, search, sort_dir, sort_key, sort_mode
1144-
def list(self, count=-1, **kwargs):
1177+
def list(self, count=None, **kwargs):
11451178
"""Fetch a list of the entities in this collection.
11461179
11471180
There is no laziness in this function. The entire collection
@@ -1162,8 +1195,9 @@ def list(self, count=-1, **kwargs):
11621195
This function always makes a roundtrip to the server, and
11631196
makes no attempt at caching.
11641197
"""
1165-
response = self.get(count=count, **kwargs)
1166-
return self._load_list(response)
1198+
# response = self.get(count=count, **kwargs)
1199+
# return self._load_list(response)
1200+
return list(self.iter(count=count, **kwargs))
11671201

11681202
class ConfigurationFile(Collection):
11691203
"""This class contains a single configuration, which is a collection of
@@ -1487,6 +1521,14 @@ def list(self, *args):
14871521

14881522
return entities
14891523

1524+
def __iter__(self, **kwargs):
1525+
for item in self.list(**kwargs):
1526+
yield item
1527+
1528+
def iter(self, **kwargs):
1529+
for item in self.list(**kwargs):
1530+
yield item
1531+
14901532
def oneshot(self, **kwargs):
14911533
pass
14921534

@@ -1712,6 +1754,9 @@ class Jobs(Collection):
17121754
"""This class represents a collection of search jobs."""
17131755
def __init__(self, service):
17141756
Collection.__init__(self, service, PATH_JOBS, item=Job)
1757+
# The count value to say list all the contents of this
1758+
# Collection is 0, not -1 as it is on most.
1759+
self.null_count = 0
17151760

17161761
def create(self, query, **kwargs):
17171762
if kwargs.get("exec_mode", None) == "oneshot":
@@ -1798,9 +1843,6 @@ def oneshot(self, query, **params):
17981843
else:
17991844
raise
18001845

1801-
def list(self, count=0, **kwargs):
1802-
return Collection.list(self, count, **kwargs)
1803-
18041846
class Loggers(Collection):
18051847
"""This class represents a collection of service logging categories."""
18061848
def __init__(self, service):
@@ -2054,15 +2096,16 @@ class NotSupportedError(Exception):
20542096
class DeploymentCollection(Collection):
20552097
def __init__(self, service, path, item):
20562098
Collection.__init__(self, service, path, item=item)
2099+
# The count value to say list all the contents of this
2100+
# Collection is 0, not -1 as it is on most.
2101+
self.null_count = 0
20572102

20582103
def create(self, name, **kwargs):
20592104
raise NotSupportedError("Cannot create %s with the REST API." % self.__class__.__name__)
20602105

20612106
def delete(self, name):
20622107
raise NotSupportedError("Cannot delete %s with the REST API." % self.__class__.__name__)
20632108

2064-
def list(self, count=0, **kwargs):
2065-
return Collection.list(self, count=count, **kwargs)
20662109

20672110
class DeploymentTenant(Entity):
20682111
"""Binding for /deployments/tenants/{name}."""
@@ -2148,10 +2191,9 @@ class DeploymentServers(DeploymentCollection):
21482191
"""Binding for /deployment/server"""
21492192
def __init__(self, service):
21502193
Collection.__init__(self, service, PATH_DEPLOYMENT_SERVERS, item=DeploymentServer)
2151-
2152-
# Override this because Collection defaults to count=-1
2153-
def list(self, count=0, **kwargs):
2154-
return Collection.list(self, count=count, **kwargs)
2194+
# The count value to say list all the contents of this
2195+
# Collection is 0, not -1 as it is on most.
2196+
self.null_count = 0
21552197

21562198
def create(self, name, **kwargs):
21572199
raise NotSupportedError("Cannot create deployment servers with the REST API.")

‎tests/test_collection.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def check_collection(self, collection):
4444
total = len(items)
4545

4646
# Make sure the default list method returns all items
47-
items0 = collection.list(count=0)
47+
items0 = collection.list()
4848
total0 = len(items0)
4949
self.assertEqual(total, total0)
5050

@@ -77,6 +77,7 @@ def check_iterable(self, collection, count):
7777
seen = 0
7878
for item in collection:
7979
seen += 1
80+
print item.name
8081
self.assertEqual(seen, count)
8182

8283
def test_apps(self):
@@ -96,6 +97,7 @@ def test_inputs(self):
9697
# input endpoints, and does not support the paging interface.
9798
service = client.connect(**self.opts.kwargs)
9899
count = len(service.inputs.list())
100+
print [x.name for x in service.inputs.list()]
99101
self.check_iterable(service.inputs, count)
100102

101103
def test_jobs(self):

0 commit comments

Comments
 (0)