Skip to content

Commit 8d9eb21

Browse files
omerazrDavide Schiera
authored andcommitted
Add create and list notification channels (#83)
1 parent d54bbb5 commit 8d9eb21

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
#
3+
# Post a user event to Sysdig Cloud
4+
#
5+
6+
import os
7+
import sys
8+
import json
9+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
10+
from sdcclient import SdcClient
11+
12+
#
13+
# Parse arguments
14+
#
15+
if len(sys.argv) != 2:
16+
print('usage: %s <sysdig-token>' % sys.argv[0])
17+
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
18+
sys.exit(1)
19+
20+
sdc_token = sys.argv[1]
21+
22+
#
23+
# Instantiate the SDC client
24+
#
25+
sdclient = SdcClient(sdc_token)
26+
27+
#
28+
# Post the event
29+
#
30+
ok, res = sdclient.list_notification_channels()
31+
32+
#
33+
# Return the result
34+
#
35+
if ok:
36+
print(json.dumps(res['notificationChannels'], indent=4))
37+
else:
38+
print(res)
39+
sys.exit(1)

sdcclient/_common.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,19 @@ def get_n_connected_agents(self):
108108
data = res.json()
109109
return [True, data['total']]
110110

111+
def list_notification_channels(self):
112+
'''**Description**
113+
List all configured Notification Channels
114+
115+
**Arguments**
116+
none
117+
118+
**Success Return Value**
119+
A JSON representation of all the notification channels
120+
'''
121+
res = requests.get(self.url + '/api/notificationChannels', headers=self.hdrs, verify=self.ssl_verify)
122+
return self._request_result(res)
123+
111124
def get_notification_ids(self, channels=None):
112125
'''**Description**
113126
Get an array of all configured Notification Channel IDs, or a filtered subset of them.
@@ -126,7 +139,7 @@ def get_notification_ids(self, channels=None):
126139
res = requests.get(self.url + '/api/notificationChannels', headers=self.hdrs, verify=self.ssl_verify)
127140

128141
if not self._checkResponse(res):
129-
return [False, self.lasterr]
142+
return False, self.lasterr
130143

131144
ids = []
132145

@@ -184,9 +197,9 @@ def get_notification_ids(self, channels=None):
184197
found = True
185198
ids.append(ch['id'])
186199
if not found:
187-
return [False, "Channel not found: " + str(c)]
200+
return False, "Channel not found: " + str(c)
188201

189-
return [True, ids]
202+
return True, ids
190203

191204
def create_email_notification_channel(self, channel_name, email_recipients):
192205
channel_json = {
@@ -203,13 +216,26 @@ def create_email_notification_channel(self, channel_name, email_recipients):
203216
res = requests.post(self.url + '/api/notificationChannels', headers=self.hdrs, data=json.dumps(channel_json), verify=self.ssl_verify)
204217
return self._request_result(res)
205218

219+
def create_notification_channel(self, channel):
220+
channel["id"] = None
221+
channel["version"] = None
222+
channel["createdOn"] = None
223+
channel["modifiedOn"] = None
224+
channel_json = {
225+
'notificationChannel': channel
226+
}
227+
228+
res = requests.post(self.url + '/api/notificationChannels', headers=self.hdrs, data=json.dumps(channel_json),
229+
verify=self.ssl_verify)
230+
return self._request_result(res)
231+
206232
def get_notification_channel(self, id):
207233

208234
res = requests.get(self.url + '/api/notificationChannels/' + str(id), headers=self.hdrs, verify=self.ssl_verify)
209235
if not self._checkResponse(res):
210-
return [False, self.lasterr]
236+
return False, self.lasterr
211237

212-
return [True, res.json()['notificationChannel']]
238+
return True, res.json()['notificationChannel']
213239

214240
def update_notification_channel(self, channel):
215241
if 'id' not in channel:
@@ -224,8 +250,8 @@ def delete_notification_channel(self, channel):
224250

225251
res = requests.delete(self.url + '/api/notificationChannels/' + str(channel['id']), headers=self.hdrs, verify=self.ssl_verify)
226252
if not self._checkResponse(res):
227-
return [False, self.lasterr]
228-
return [True, None]
253+
return False, self.lasterr
254+
return True, None
229255

230256
def get_data_retention_info(self):
231257
'''**Description**

test/test_monitor_apis.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ date; $SCRIPTDIR/../examples/list_users.py $PYTHON_SDC_TEST_MONITOR_API_TOKEN
4343
date; $SCRIPTDIR/../examples/list_sysdig_captures.py $PYTHON_SDC_TEST_MONITOR_API_TOKEN
4444
date; $SCRIPTDIR/../examples/create_sysdig_capture.py $PYTHON_SDC_TEST_MONITOR_API_TOKEN $AGENT_HOSTNAME $CAPTURE_NAME 10
4545
date; $SCRIPTDIR/../examples/notification_channels.py -c $CHANNEL_NAME $PYTHON_SDC_TEST_MONITOR_API_TOKEN
46+
date; $SCRIPTDIR/../examples/list_notification_channels.py $PYTHON_SDC_TEST_MONITOR_API_TOKEN
4647
date; $SCRIPTDIR/../examples/user_team_mgmt.py $PYTHON_SDC_TEST_MONITOR_API_TOKEN $TEAM_NAME [email protected]
4748
date; $SCRIPTDIR/../examples/user_team_mgmt_extended.py $PYTHON_SDC_TEST_MONITOR_API_TOKEN $TEAM_NAME [email protected]

0 commit comments

Comments
 (0)