Skip to content

Commit afa3acf

Browse files
author
Frederick Ross
committed
Fired alert endpoints added in a sensible way to saved searches; all tests passing.
1 parent 816eed1 commit afa3acf

4 files changed

Lines changed: 49 additions & 16 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
* Added User.role_entities to return a list of the actual entity objects for the
88
roles of a user. User.roles still returns a list of the role names.
9-
* Job.enable_preview() and Job.disable_preview() have been removed, since the corresponding
10-
endpoints in Splunk's REST API are broken in Splunk 4.x.
119
* The first time .cancel() is called on job, it cancels it. Any calls to it thereafter on that
1210
job are a nop.
1311
* Service.restart now takes a timeout argument. If it is specified, the function blocks until
@@ -16,7 +14,13 @@
1614
* If you are creating indexes with the SDK, restart splunkd after you have created them but before
1715
you try to do anything with them. Submitting data and similar actions simply won't work without
1816
a restart.
19-
17+
* Added .alert_count and .fired_alerts properties to SavedSearch entity.
18+
* Added Index.attached_socket(), which provides the same functionality as Index.attach(), but as
19+
a with block.
20+
* Added Indexes.default() which returns the name of the default index that data will be submitted into.
21+
* Added .isReady() and .isDone() methods to Job to make it easy to loop until either point as been reached.
22+
* Connecting with a preexisting token works whether the token begins with 'Splunk ' or not;
23+
the SDK will handle either case correctly.
2024

2125
## 0.8.5
2226

‎splunklib/client.py‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1433,7 +1433,7 @@ def __init__(self, service, path, **kwargs):
14331433
Entity.__init__(self, service, path, **kwargs)
14341434

14351435
def __len__(self):
1436-
return self.count()
1436+
return self.count
14371437

14381438
@property
14391439
def alerts(self):
@@ -2293,6 +2293,25 @@ def dispatch(self, **kwargs):
22932293
sid = _load_sid(response)
22942294
return Job(self.service, sid)
22952295

2296+
@property
2297+
def fired_alerts(self):
2298+
"""Return a collection of AlertGroups corresponding to this saved search's alerts.
2299+
2300+
If the search is not scheduled, raises ``IllegalOperationException``.
2301+
2302+
:raises: IllegalOperationException
2303+
:returns: Collection of AlertGroup entities.
2304+
"""
2305+
if self['is_scheduled'] == '0':
2306+
raise IllegalOperationException('Unscheduled saved searches have no alerts.')
2307+
c = Collection(
2308+
self.service,
2309+
self.service._abspath(PATH_FIRED_ALERTS + self.name,
2310+
owner=self.namespace.owner, app=self.namespace.app,
2311+
sharing=self.namespace.sharing),
2312+
item=AlertGroup)
2313+
return c
2314+
22962315
def history(self):
22972316
"""Returns a list of search jobs corresponding to this saved search.
22982317

‎tests/test_binding.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ def test_preexisting_token_sans_splunk(self):
514514
token = self.context.token
515515
if token.startswith('Splunk'):
516516
token = token.split(' ', 1)[1]
517+
self.assertFalse(token.startswith('Splunk'))
517518
else:
518519
self.fail("Token did not start with Splunk.")
519520
opts = self.opts.kwargs.copy()

‎tests/test_fired_alert.py‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@
2222
class TestCase(testlib.TestCase):
2323
def setUp(self):
2424
super(TestCase, self).setUp()
25-
self.index = self.service.indexes['_internal']
25+
self.index_name = testlib.tmpname()
26+
self.assertFalse(self.index_name in self.service.indexes)
27+
self.index = self.service.indexes.create(self.index_name)
28+
self.service.restart(120)
29+
self.index = self.service.indexes[self.index_name]
2630
saved_searches = self.service.saved_searches
2731
self.saved_search_name = testlib.tmpname()
2832
self.assertFalse(self.saved_search_name in saved_searches)
29-
query = "search index=_internal sourcetype=sdk_use"
33+
query = "search index=%s" % self.index_name
3034
kwargs = {'alert_type': 'always',
3135
'alert.severity': "3",
3236
'alert.suppress': "0",
3337
'alert.track': "1",
34-
'dispatch.earliest_time': "rt-1h",
35-
'dispatch.latest_time': "rt",
38+
'dispatch.earliest_time': "-1h",
39+
'dispatch.latest_time': "now",
3640
'is_scheduled': "1",
3741
'cron_schedule': "* * * * *"}
3842
self.saved_search = saved_searches.create(
@@ -50,19 +54,24 @@ def tearDown(self):
5054
def test_new_search_is_empty(self):
5155
self.assertEqual(self.saved_search.alert_count, 0)
5256
self.assertEqual(len(self.saved_search.history()), 0)
57+
self.assertEqual(len(self.saved_search.fired_alerts), 0)
5358
self.assertFalse(self.saved_search_name in self.service.fired_alerts)
5459

5560
def test_alerts_on_events(self):
56-
N_alerts = self.saved_search.alert_count
61+
self.assertEqual(self.saved_search.alert_count, 0)
62+
self.assertEqual(len(self.saved_search.fired_alerts), 0)
5763
eventCount = int(self.index['totalEventCount'])
64+
self.assertEqual(self.index['sync'], '0')
65+
self.assertEqual(self.index['disabled'], '0')
5866
self.index.refresh()
59-
while int(self.index['totalEventCount']) == eventCount:
60-
self.index.refresh()
61-
import time; time.sleep(0.1)
62-
self.index.refresh()
63-
self.assertGreater(int(self.index['totalEventCount']), eventCount)
64-
self.saved_search.refresh()
65-
self.assertGreater(self.saved_search.alert_count, N_alerts)
67+
self.index.submit('This is a test ' + testlib.tmpname(),
68+
sourcetype='sdk_use', host='boris')
69+
testlib.retry(self.index, 'totalEventCount', str(eventCount+1), step=1, times=50)
70+
self.assertEqual(self.index['totalEventCount'], str(eventCount+1))
71+
testlib.retry(self.saved_search, lambda s: s.alert_count, 1, step=2, times=100)
72+
self.assertEqual(self.saved_search.alert_count, 1)
73+
alerts = self.saved_search.fired_alerts
74+
self.assertEqual(len(alerts), 1)
6675

6776
def test_read(self):
6877
service = client.connect(**self.opts.kwargs)

0 commit comments

Comments
 (0)