Fix: nextToken value in logs:FilterLogEvents response (#3883)

* Fix: `nextToken` value in `logs:FilterLogEvents` response

Plagiarizing freely from @bpandola and his PR #3398, I have
modified the pagination for FilterLogEvents to more closely follow
the real AWS behaviour.

Fixes #3882

* Black reformatted my code.

* Remove timezone for python2.7 compatibility.

* Hopefully fix python2.7 compatibility for real.

* Additional test for a non-matching log group name in the nextToken.
This commit is contained in:
Neil Greenwood 2021-04-29 12:56:20 +01:00 committed by GitHub
commit bcc7938615
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 92 additions and 10 deletions

View file

@ -366,13 +366,35 @@ class LogGroup:
if interleaved:
events = sorted(events, key=lambda event: event["timestamp"])
if next_token is None:
next_token = 0
first_index = 0
if next_token:
try:
group, stream, event_id = next_token.split("/")
if group != log_group_name:
raise ValueError()
first_index = (
next(
index
for (index, e) in enumerate(events)
if e["logStreamName"] == stream and e["eventId"] == event_id
)
+ 1
)
except (ValueError, StopIteration):
first_index = 0
# AWS returns an empty list if it receives an invalid token.
events = []
events_page = events[next_token : next_token + limit]
next_token += limit
if next_token >= len(events):
next_token = None
last_index = first_index + limit
if last_index > len(events):
last_index = len(events)
events_page = events[first_index:last_index]
next_token = None
if events_page and last_index < len(events):
last_event = events_page[-1]
next_token = "{}/{}/{}".format(
log_group_name, last_event["logStreamName"], last_event["eventId"]
)
searched_streams = [
{"logStreamName": stream.logStreamName, "searchedCompletely": True}