Skip to content

Commit 4234997

Browse files
authored
feat: Allow to retrieve runtime policy event by ID (#173)
Adds a method called `get_policy_event` that retrieves an event by it's ID.
1 parent a29825f commit 4234997

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

sdcclient/secure/_policy_events_v1.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,21 @@ def get_more_policy_events(self, ctx):
115115
`examples/get_secure_policy_events.py <https://github.com/draios/python-sdc-client/blob/master/examples/get_secure_policy_events.py>`_
116116
'''
117117
return self._get_policy_events_int(ctx)
118+
119+
def get_policy_event(self, event_id):
120+
"""
121+
122+
Args:
123+
event_id: The ID of the Runtime Policy event to retrieve more info from.
124+
125+
Returns:
126+
A tuple where the first parameter indicates if the request was successful, and the second parameter
127+
holds the info from the policy event or the error.
128+
"""
129+
policy_events_url = f'{self.url}/api/v1/secureEvents/{event_id}'
130+
131+
res = self.http.get(policy_events_url, headers=self.hdrs, verify=self.ssl_verify)
132+
if not self._checkResponse(res):
133+
return False, self.lasterr
134+
135+
return True, res.json()

specs/secure/policy_events_v1_spec.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
token=os.getenv("SDC_SECURE_TOKEN"))
1414
with context("when we try to retrieve policy events from the last 7 days"):
1515
with it("returns the list of all events happened"):
16-
day_in_seconds = 7 * 24 * 60 * 60
16+
week_in_seconds = 7 * 24 * 60 * 60
1717

18-
ok, res = self.client.get_policy_events_duration(day_in_seconds)
18+
ok, res = self.client.get_policy_events_duration(week_in_seconds)
1919

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

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

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

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

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

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

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

@@ -76,3 +76,16 @@
7676
}
7777
call = self.client.get_more_policy_events(wrong_context)
7878
expect(call).to_not(be_successful_api_call)
79+
80+
with context("while retrieving a single event"):
81+
with it("retrieves the event correctly"):
82+
week_in_seconds = 7 * 24 * 60 * 60
83+
ok, res = self.client.get_policy_events_duration(week_in_seconds)
84+
85+
expect((ok, res)).to(be_successful_api_call)
86+
87+
event_id = res["data"][0]["id"]
88+
ok, res = self.client.get_policy_event(event_id)
89+
90+
expect((ok, res)).to(be_successful_api_call)
91+
expect(res).to(have_keys("name", "timestamp", "customerId", "originator", "machineId", id=event_id))

0 commit comments

Comments
 (0)