Skip to content

Commit 9a801dc

Browse files
Remove old notification settings and use the new notification channel
1 parent 17f20ef commit 9a801dc

File tree

3 files changed

+68
-32
lines changed

3 files changed

+68
-32
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ script:
2929
- examples/print_user_info.py XXX
3030
- examples/list_sysdig_captures.py XXX
3131
- examples/create_sysdig_capture.py XXX ip-10-0-1-110.ec2.internal apicapture 10
32+
- examples/notification_channels.py XXX
3233
- echo "Testing pip version"
3334
- rm -rf sdcclient
3435
- pip install sdcclient
@@ -54,7 +55,7 @@ script:
5455
# - examples/print_explore_grouping.py XXX
5556
# - examples/print_user_info.py XXX
5657
# - examples/list_sysdig_captures.py XXX
57-
# - examples/create_sysdig_capture.py XXX ip-10-0-1-110.ec2.internal apicapture 10
58+
# - examples/create_sysdig_capture.py XXX ip-10-0-2-202.ec2.internal apicapture 10
5859
notifications:
5960
slack:
6061
secure: GJ0H2wAaW67t3+x+cCjOVLw/b+YAZjH9rebAfluCFJklJS9u+V6/qqIyiNDAkMPIcgilFyhiyOQCZYXapgthQ1ieFbZZo//mrRtuGo2wuxCAwcOx2mXAZpZ4I5b3+Q/znVqSuqkwFRid1d138z4TW7sYSIburouDX3QUNoUOy+g7VJxCFQCcqlN8LYxGJHQYdOZa9zIGCtKrOtZ/B8C1TLgXmDMwAAVNO2WzL4GiBTLCGuMsQWMTLw2Qmv1ayZPztmeDWo1C9oa7HIH8Bg2YVjssR87el28X+EqEO533mgYjPmW2/hii30WVFOUE5hMdvKeQLBvy5N3/OCch1np0RQBd8eYEtaPv38rc5L2wAnUq9G5Zzr252z7vnwSLi6lap9jWU8tOerSTEPU+jG05PnuCnufVDXVNPyiPsH6BDP4qxHmLjooNpxfe63Df7NNyUi2I3QoroLj/UzI7zZVQjJEqsTrr5BbsH4z6NTGY91+ZqobBn62+hV3ESAam0ivQgC7s2AKko0qkKyIUGjj7ozU8ebo1UpagNvKC/J9szMqtdXJgKtG8BeonyLMeN6MEEyEvcMJbB4dCcfet+1Sb9AZWnGvYVdajhVLb1HE6OrbzZyhC3KqCe06J9O5BCY9ncy+l16i7MyIfcKTibHQlxPU+Id/VijD97JSRXxnd2i4=

examples/notification_channels.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python
2+
#
3+
# This script shows how to manipulate the notification channel list for alerts
4+
#
5+
6+
import os
7+
import sys
8+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
9+
from sdcclient import SdcClient
10+
11+
#
12+
# Parse arguments
13+
#
14+
if len(sys.argv) != 2:
15+
print 'usage: %s <sysdig-token>' % sys.argv[0]
16+
print 'You can find your token at https://app.sysdigcloud.com/#/settings/user'
17+
sys.exit(1)
18+
19+
sdc_token = sys.argv[1]
20+
21+
#
22+
# Instantiate the SDC client
23+
#
24+
sdclient = SdcClient(sdc_token)
25+
26+
#
27+
# Create an email notification channel
28+
#
29+
res = sdclient.create_email_notification_channel('Api Channel', ['[email protected]', '[email protected]', '[email protected]'])
30+
if not res[0]:
31+
print res[1]
32+
sys.exit(1)
33+
34+
#
35+
# The notification channel will contain the id, that can be used when creating alerts
36+
#
37+
channel = res[1]['notificationChannel']
38+
print channel
39+
40+
#
41+
# Notification channels can also be programmatically deleted
42+
#
43+
res = sdclient.delete_notification_channel(channel)
44+
if not res[0]:
45+
print res[1]
46+
sys.exit(1)

sdcclient/_client.py

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def get_notifications(self, from_ts, to_ts, state=None, resolved=None):
8888

8989
def update_notification_resolution(self, notification, resolved):
9090
if 'id' not in notification:
91-
return [False, "Invalid notification format"]
91+
return [False, 'Invalid notification format']
9292

9393
notification['resolved'] = resolved
9494
data = {'notification': notification}
@@ -189,50 +189,39 @@ def create_alert(self, name, description, severity, for_atleast_s, condition, se
189189

190190
def delete_alert(self, alert):
191191
if 'id' not in alert:
192-
return [False, "Invalid alert format"]
192+
return [False, 'Invalid alert format']
193193

194194
res = requests.delete(self.url + '/api/alerts/' + str(alert['id']), headers=self.hdrs)
195195
if not self.__checkResponse(res):
196196
return [False, self.lasterr]
197197

198198
return [True, None]
199199

200-
def get_notification_settings(self):
201-
res = requests.get(self.url + '/api/settings/notifications', headers=self.hdrs)
202-
if not self.__checkResponse(res):
203-
return [False, self.lasterr]
204-
return [True, res.json()]
200+
def create_email_notification_channel(self, channel_name, email_recipients):
201+
channel_json = {
202+
'notificationChannel' : {
203+
'type' : 'EMAIL',
204+
'name' : channel_name,
205+
'enabled' : True,
206+
'options' : {
207+
'emailRecipients' : email_recipients
208+
}
209+
}
210+
}
205211

206-
def set_notification_settings(self, settings):
207-
res = requests.put(self.url + '/api/settings/notifications', headers=self.hdrs,
208-
data=json.dumps(settings))
212+
res = requests.post(self.url + '/api/notificationChannels', headers=self.hdrs, data=json.dumps(channel_json))
209213
if not self.__checkResponse(res):
210214
return [False, self.lasterr]
211215
return [True, res.json()]
212216

213-
def add_email_notification_recipient(self, email):
214-
#
215-
# Retirieve the user's notification settings
216-
#
217-
res = requests.get(self.url + '/api/settings/notifications', headers=self.hdrs)
217+
def delete_notification_channel(self, channel):
218+
if 'id' not in channel:
219+
return [False, "Invalid channel format"]
220+
221+
res = requests.delete(self.url + '/api/notificationChannels/' + str(channel['id']), headers=self.hdrs)
218222
if not self.__checkResponse(res):
219223
return [False, self.lasterr]
220-
j = res.json()
221-
222-
#
223-
# Enable email notifications
224-
#
225-
j['userNotification']['email']['enabled'] = True
226-
227-
#
228-
# Add the given recipient
229-
#
230-
if email not in j['userNotification']['email']['recipients']:
231-
j['userNotification']['email']['recipients'].append(email)
232-
else:
233-
return [False, 'notification target ' + email + ' already present']
234-
235-
return self.set_notification_settings(j)
224+
return [True, None]
236225

237226
def get_explore_grouping_hierarchy(self):
238227
res = requests.get(self.url + '/api/groupConfigurations', headers=self.hdrs)

0 commit comments

Comments
 (0)