Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit f19404c

Browse files
authored
Add support for EventService API flush endpoint (#118)
1 parent 787de57 commit f19404c

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

bin/summit.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,10 @@ def nack(api, options):
447447
k = 'EventService.nack'
448448
generic(api, options, api.nack, k)
449449

450+
def flush(api, options):
451+
k = 'EventService.flush'
452+
generic(api, options, api.flush, k)
453+
450454
k = 'EventService'
451455

452456
R = options['R']
@@ -481,6 +485,9 @@ def nack(api, options):
481485
if options['nack']:
482486
nack(api, options)
483487

488+
if options['flush']:
489+
flush(api, options)
490+
484491
setters(options, api)
485492
methods(options, api)
486493

@@ -973,6 +980,7 @@ def parse_opts():
973980
'get': False,
974981
'ack': False,
975982
'nack': False,
983+
'flush': False,
976984
'follow': False,
977985
'count': False,
978986
'domains': False,
@@ -1049,8 +1057,8 @@ def _options_print(opt, last_c, last_m, arg):
10491057
'delete', 'poll', 'xpoll', 'query', 'write',
10501058
'start=', 'midpoint=', 'end=', 'window=',
10511059
'id=', 'seq=',
1052-
'set', 'get', 'ack', 'nack', 'follow',
1053-
'count', 'domains', 'attributes',
1060+
'set', 'get', 'ack', 'nack', 'flush',
1061+
'follow', 'count', 'domains', 'attributes',
10541062
'R0=', 'R1=', 'R2=',
10551063
'debug=', 'version', 'help',
10561064
]
@@ -1144,6 +1152,9 @@ def _options_print(opt, last_c, last_m, arg):
11441152
elif opt == '--nack':
11451153
options['nack'] = True
11461154
last_m = 'nack'
1155+
elif opt == '--flush':
1156+
options['flush'] = True
1157+
last_m = 'flush'
11471158
elif opt == '--follow':
11481159
options['follow'] = True
11491160
elif opt == '--count':
@@ -1295,6 +1306,8 @@ def usage():
12951306
(sets ack point = read point)
12961307
--nack negative acknowledgement for events read
12971308
(sets read point = ack point)
1309+
--flush discard unread events
1310+
(set read and ack point to end of channel)
12981311
--poll read events
12991312
--xpoll poll all events until drained
13001313
(use --ack to ack after each poll)

examples/event_flush.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""Example interaction with Event Service using flush."""
5+
6+
import os
7+
import sys
8+
9+
curpath = os.path.dirname(os.path.abspath(__file__))
10+
sys.path[:0] = [os.path.join(curpath, os.pardir)]
11+
12+
from pancloud import EventService, Credentials
13+
14+
url = 'https://api.us.paloaltonetworks.com'
15+
16+
c = Credentials()
17+
18+
# Create Event Service instance
19+
es = EventService(
20+
url=url,
21+
credentials=c
22+
)
23+
24+
channel_id = 'EventFilter'
25+
26+
# Flush event channel
27+
a = es.flush(channel_id)
28+
29+
# Print results
30+
print(
31+
"\nSTATUS_CODE: {}, RESULT: \n\n{}\n".format(a.status_code, a.text)
32+
)

pancloud/event.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,33 @@ def ack(self, channel_id=None, **kwargs): # pragma: no cover
8787
)
8888
return r
8989

90+
def flush(self, channel_id=None, **kwargs): # pragma: no cover
91+
"""Discard all existing events from the event channel.
92+
93+
This endpoint removes all events from the channel. Use this
94+
endpoint only if you currently have unread events in your
95+
channel that you never intend to process.
96+
97+
Args:
98+
channel_id (str): The channel ID.
99+
**kwargs: Supported :meth:`~pancloud.httpclient.HTTPClient.request` parameters.
100+
101+
Returns:
102+
requests.Response: Requests Response() object.
103+
104+
Examples:
105+
Refer to ``event_flush.py`` example.
106+
107+
"""
108+
path = "/event-service/v1/channels/{}/flush".format(channel_id)
109+
r = self._httpclient.request(
110+
method="POST",
111+
url=self.url,
112+
path=path,
113+
**kwargs
114+
)
115+
return r
116+
90117
def get_filters(self, channel_id=None, **kwargs): # pragma: no cover
91118
"""Retrieve the filters currently set on the channel.
92119

tests/test_event.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def test_entry_points(self):
3131
EventService(url=TARPIT).poll
3232
EventService(url=TARPIT).ack
3333
EventService(url=TARPIT).nack
34+
EventService(url=TARPIT).flush
3435

3536
def test_unexpected_kwargs(self):
3637
with pytest.raises(UnexpectedKwargsError):

0 commit comments

Comments
 (0)