Skip to content

Commit a39640d

Browse files
authored
feat: Add get_event by ID method to Events client v1 and v2 (#188)
* feat: Add get_event by ID method to Events client v1 and v2 * fix: Solve linting problems * fix(ci): Update custom fixture rules file
1 parent 11d306a commit a39640d

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

sdcclient/monitor/_events_v1.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,27 @@ def get_events(self, from_s=None, to_s=None, last_s=None):
3636
res = self.http.get(self.url + '/api/events/', headers=self.hdrs, params=params, verify=self.ssl_verify)
3737
return self._request_result(res)
3838

39+
def get_event(self, id):
40+
"""
41+
Retrieve an event using the ID
42+
Args:
43+
id(str): ID of the event to retrieve
44+
45+
Returns:
46+
A tuple where the first parameter indicates if the call was successful,
47+
and the second parameter holds either the error as string, or the event matching this ID.
48+
49+
Examples:
50+
>>> from sdcclient.monitor import EventsClientV1
51+
>>> client = EventsClientV1(token=SECURE_TOKEN)
52+
>>> ok, res = client.get_event(id='2343214984')
53+
>>> if ok:
54+
>>> print(res["event"])
55+
"""
56+
url = f'{self.url}/api/events/{id}'
57+
res = self.http.get(url, headers=self.hdrs, verify=self.ssl_verify)
58+
return self._request_result(res)
59+
3960
def post_event(self, name, description=None, severity=None, event_filter=None, tags=None):
4061
'''**Description**
4162
Send an event to Sysdig Monitor. The events you post are available in the Events tab in the Sysdig Monitor UI and can be overlied to charts.

sdcclient/monitor/_events_v2.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,28 @@ def get_events(self, name=None, category=None, direction='before', status=None,
7979
res = self.http.get(self.url + '/api/v2/events/', headers=self.hdrs, params=params, verify=self.ssl_verify)
8080
return self._request_result(res)
8181

82+
def get_event(self, id):
83+
"""
84+
Retrieve an event using the ID
85+
Args:
86+
id(str): ID of the event to retrieve
87+
88+
Returns:
89+
A tuple where the first parameter indicates if the call was successful,
90+
and the second parameter holds either the error as string, or the event matching this ID.
91+
92+
Examples:
93+
>>> from sdcclient.monitor import EventsClientV2
94+
>>> client = EventsClientV2(token=SECURE_TOKEN)
95+
>>> ok, res = client.get_event(id='2343214984')
96+
>>> if ok:
97+
>>> print(res["event"])
98+
"""
99+
100+
url = f'{self.url}/api/v2/events/{id}'
101+
res = self.http.get(url, headers=self.hdrs, verify=self.ssl_verify)
102+
return self._request_result(res)
103+
82104
def delete_event(self, event):
83105
'''**Description**
84106
Deletes an event.

specs/monitor/events_v1_spec.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import time
33

4-
from expects import expect, have_key, contain, have_keys, be_empty
4+
from expects import expect, have_key, contain, have_keys, be_empty, equal
55
from mamba import it, before, description
66

77
from sdcclient.monitor import EventsClientV1
@@ -18,6 +18,19 @@
1818
description="This event was created in a CI pipeline for the Python SDK library")
1919
expect(call).to(be_successful_api_call)
2020

21+
with it("is able to retrieve an event by ID"):
22+
ok, res = self.client.post_event(name=self.event_name,
23+
description="This event was created in a CI pipeline for the Python SDK library")
24+
expect((ok, res)).to(be_successful_api_call)
25+
26+
event = res["event"]
27+
event_id = event["id"]
28+
29+
ok, res = self.client.get_event(id=event_id)
30+
expect((ok, res)).to(be_successful_api_call)
31+
32+
expect(res["event"]).to(equal(event))
33+
2134
with it("is able to list the events happened without any filter"):
2235
time.sleep(3) # Wait for the event to appear in the feed
2336
ok, res = self.client.get_events()

specs/monitor/events_v2_spec.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@
3232
expect(res).to(have_key("events"))
3333
expect(res["events"]).to(contain(have_key("scope", equal("host.hostName = 'ci'"))))
3434

35+
with it("is able to retrieve an event by ID"):
36+
ok, res = self.client.post_event(name=self.event_name,
37+
description="This event was created in a CI pipeline for the Python SDK library")
38+
expect((ok, res)).to(be_successful_api_call)
39+
40+
event = res["event"]
41+
event_id = event["id"]
42+
43+
ok, res = self.client.get_event(id=event_id)
44+
expect((ok, res)).to(be_successful_api_call)
45+
46+
expect(res["event"]).to(equal(event))
47+
3548
with it("is able to list the events happened without any filter"):
3649
time.sleep(3) # Wait for the event to appear in the feed
3750
ok, res = self.client.get_events()

0 commit comments

Comments
 (0)