Skip to content

feat: Allow to retrieve runtime policy event by ID #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions sdcclient/secure/_policy_events_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,21 @@ def get_more_policy_events(self, ctx):
`examples/get_secure_policy_events.py <https://github.com/draios/python-sdc-client/blob/master/examples/get_secure_policy_events.py>`_
'''
return self._get_policy_events_int(ctx)

def get_policy_event(self, event_id):
"""

Args:
event_id: The ID of the Runtime Policy event to retrieve more info from.

Returns:
A tuple where the first parameter indicates if the request was successful, and the second parameter
holds the info from the policy event or the error.
"""
policy_events_url = f'{self.url}/api/v1/secureEvents/{event_id}'

res = self.http.get(policy_events_url, headers=self.hdrs, verify=self.ssl_verify)
if not self._checkResponse(res):
return False, self.lasterr

return True, res.json()
29 changes: 21 additions & 8 deletions specs/secure/policy_events_v1_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
token=os.getenv("SDC_SECURE_TOKEN"))
with context("when we try to retrieve policy events from the last 7 days"):
with it("returns the list of all events happened"):
day_in_seconds = 7 * 24 * 60 * 60
week_in_seconds = 7 * 24 * 60 * 60

ok, res = self.client.get_policy_events_duration(day_in_seconds)
ok, res = self.client.get_policy_events_duration(week_in_seconds)

expect((ok, res)).to(be_successful_api_call)
expect(res).to(have_keys("ctx", "data"))
Expand All @@ -34,18 +34,18 @@
contain(have_keys("id", "timestamp", "customerId", "source", "name", "description", "cursor")))

with it("returns the list of all events from the last 7 days that match a filter"):
day_in_seconds = 7 * 24 * 60 * 60
week_in_seconds = 7 * 24 * 60 * 60

ok, res = self.client.get_policy_events_duration(day_in_seconds, filter='severity in ("4","5")')
ok, res = self.client.get_policy_events_duration(week_in_seconds, filter='severity in ("4","5")')

expect((ok, res)).to(be_successful_api_call)
expect(res).to(have_keys("ctx", "data"))
expect(res["data"]).to(contain(have_key("severity", be_within(3, 6))))

with it("returns an empty list if the filter does not match"):
day_in_seconds = 7 * 24 * 60 * 60
week_in_seconds = 7 * 24 * 60 * 60

ok, res = self.client.get_policy_events_duration(day_in_seconds, filter='severity in ("-1")')
ok, res = self.client.get_policy_events_duration(week_in_seconds, filter='severity in ("-1")')

expect((ok, res)).to(be_successful_api_call)
expect(res).to(have_keys("ctx", "data"))
Expand All @@ -54,8 +54,8 @@
with _context("and from the first event we retrieve the rest of events"):
# Deactivated tests. There seems to be a bug in the API -- need confirmation
with it("returns the list of all events except the first"):
day_in_seconds = 7 * 24 * 60 * 60
_, res = self.client.get_policy_events_duration(day_in_seconds)
week_in_seconds = 7 * 24 * 60 * 60
_, res = self.client.get_policy_events_duration(week_in_seconds)
ctx = {"cursor": res["data"][0]["cursor"]}
qty_before = len(res["data"])

Expand All @@ -76,3 +76,16 @@
}
call = self.client.get_more_policy_events(wrong_context)
expect(call).to_not(be_successful_api_call)

with context("while retrieving a single event"):
with it("retrieves the event correctly"):
week_in_seconds = 7 * 24 * 60 * 60
ok, res = self.client.get_policy_events_duration(week_in_seconds)

expect((ok, res)).to(be_successful_api_call)

event_id = res["data"][0]["id"]
ok, res = self.client.get_policy_event(event_id)

expect((ok, res)).to(be_successful_api_call)
expect(res).to(have_keys("name", "timestamp", "customerId", "originator", "machineId", id=event_id))