44import copy
55
66class SdcClient :
7- userinfo = None
8- n_connected_agents = None
97 lasterr = None
108
119 def __init__ (self , token = "" , sdc_url = 'https://app.sysdigcloud.com' ):
@@ -44,12 +42,10 @@ def __checkResponse(self, res):
4442
4543
4644 def get_user_info (self ):
47- if self .userinfo is None :
48- res = requests .get (self .url + '/api/user/me' , headers = self .hdrs )
49- if not self .__checkResponse (res ):
50- return [False , self .lasterr ]
51- self .userinfo = res .json ()
52- return [True , self .userinfo ]
45+ res = requests .get (self .url + '/api/user/me' , headers = self .hdrs )
46+ if not self .__checkResponse (res ):
47+ return [False , self .lasterr ]
48+ return [True , res .json ()]
5349
5450 def get_connected_agents (self ):
5551 res = requests .get (self .url + '/api/agents/connected' , headers = self .hdrs )
@@ -93,7 +89,7 @@ def get_notifications(self, from_ts, to_ts, state=None, resolved=None):
9389
9490 def update_notification_resolution (self , notification , resolved ):
9591 if 'id' not in notification :
96- return [False , " Invalid notification format" ]
92+ return [False , ' Invalid notification format' ]
9793
9894 notification ['resolved' ] = resolved
9995 data = {'notification' : notification }
@@ -103,35 +99,42 @@ def update_notification_resolution(self, notification, resolved):
10399 return [False , self .lasterr ]
104100 return [True , res .json ()]
105101
106- def get_notification_channel_ids (self , channels ):
107- res = requests .get (self .url + '/api/notificationChannels' , headers = self .hdrs )
108- ids = []
109- if not self .__checkResponse (res ):
110- return [False , self .lasterr ]
111- # Should try and improve this M * N lookup
112- for ch in res .json ()["notificationChannels" ]:
102+ def get_notification_ids (self , channels ):
103+ res = requests .get (self .url + '/api/notificationChannels' , headers = self .hdrs )
104+
105+ if not self .__checkResponse (res ):
106+ return [False , self .lasterr ]
107+
108+ # Should try and improve this M * N lookup
109+ ids = []
113110 for c in channels :
114- if c ['type' ] == ch ['type' ]:
115- print c , ch
116- if c ['type' ] == 'SNS' :
117- opt = ch ['options' ]
118- if set (opt ['snsTopicARNs' ]) == set (c ['snsTopicARNs' ]):
119- ids .append (ch ['id' ])
120- elif c ['type' ] == 'EMAIL' :
121- opt = ch ['options' ]
122- if set (c ['emailRecipients' ]) == set (opt ['emailRecipients' ]):
123- ids .append (ch ['id' ])
124- elif c ['type' ] == 'PAGER_DUTY' :
125- opt = ch ['options' ]
126- if opt ['account' ] == c ['account' ] and opt ['serviceName' ] == c ['serviceName' ]:
127- ids .append (ch ['id' ])
128- elif c ['type' ] == 'SLACK' :
129- opt = ch ['options' ]
130- if opt ['channel' ] == c ['channel' ]:
131- ids .append (ch ['id' ])
132- print ids
133- return [True , ids ]
134-
111+ found = False
112+ for ch in res .json ()["notificationChannels" ]:
113+ if c ['type' ] == ch ['type' ]:
114+ if c ['type' ] == 'SNS' :
115+ opt = ch ['options' ]
116+ if set (opt ['snsTopicARNs' ]) == set (c ['snsTopicARNs' ]):
117+ found = True
118+ ids .append (ch ['id' ])
119+ elif c ['type' ] == 'EMAIL' :
120+ opt = ch ['options' ]
121+ if set (c ['emailRecipients' ]) == set (opt ['emailRecipients' ]):
122+ found = True
123+ ids .append (ch ['id' ])
124+ elif c ['type' ] == 'PAGER_DUTY' :
125+ opt = ch ['options' ]
126+ if opt ['account' ] == c ['account' ] and opt ['serviceName' ] == c ['serviceName' ]:
127+ found = True
128+ ids .append (ch ['id' ])
129+ elif c ['type' ] == 'SLACK' :
130+ opt = ch ['options' ]
131+ if 'channel' in opt and opt ['channel' ] == c ['channel' ]:
132+ found = True
133+ ids .append (ch ['id' ])
134+ if not found :
135+ return [False , "Channel not found: " + str (c )]
136+
137+ return [True , ids ]
135138
136139 def create_alert (self , name , description , severity , for_atleast_s , condition , segmentby = [],
137140 segment_condition = 'ANY' , user_filter = '' , notify = None , enabled = True , annotations = {}):
@@ -187,50 +190,39 @@ def create_alert(self, name, description, severity, for_atleast_s, condition, se
187190
188191 def delete_alert (self , alert ):
189192 if 'id' not in alert :
190- return [False , " Invalid alert format" ]
193+ return [False , ' Invalid alert format' ]
191194
192195 res = requests .delete (self .url + '/api/alerts/' + str (alert ['id' ]), headers = self .hdrs )
193196 if not self .__checkResponse (res ):
194197 return [False , self .lasterr ]
195198
196199 return [True , None ]
197200
198- def get_notification_settings (self ):
199- res = requests .get (self .url + '/api/settings/notifications' , headers = self .hdrs )
200- if not self .__checkResponse (res ):
201- return [False , self .lasterr ]
202- return [True , res .json ()]
201+ def create_email_notification_channel (self , channel_name , email_recipients ):
202+ channel_json = {
203+ 'notificationChannel' : {
204+ 'type' : 'EMAIL' ,
205+ 'name' : channel_name ,
206+ 'enabled' : True ,
207+ 'options' : {
208+ 'emailRecipients' : email_recipients
209+ }
210+ }
211+ }
203212
204- def set_notification_settings (self , settings ):
205- res = requests .put (self .url + '/api/settings/notifications' , headers = self .hdrs ,
206- data = json .dumps (settings ))
213+ res = requests .post (self .url + '/api/notificationChannels' , headers = self .hdrs , data = json .dumps (channel_json ))
207214 if not self .__checkResponse (res ):
208215 return [False , self .lasterr ]
209216 return [True , res .json ()]
210217
211- def add_email_notification_recipient (self , email ):
212- #
213- # Retirieve the user's notification settings
214- #
215- res = requests .get (self .url + '/api/settings/notifications' , headers = self .hdrs )
218+ def delete_notification_channel (self , channel ):
219+ if 'id' not in channel :
220+ return [ False , "Invalid channel format" ]
221+
222+ res = requests .delete (self .url + '/api/notificationChannels/' + str ( channel [ 'id' ]) , headers = self .hdrs )
216223 if not self .__checkResponse (res ):
217224 return [False , self .lasterr ]
218- j = res .json ()
219-
220- #
221- # Enable email notifications
222- #
223- j ['userNotification' ]['email' ]['enabled' ] = True
224-
225- #
226- # Add the given recipient
227- #
228- if email not in j ['userNotification' ]['email' ]['recipients' ]:
229- j ['userNotification' ]['email' ]['recipients' ].append (email )
230- else :
231- return [False , 'notification target ' + email + ' already present' ]
232-
233- return self .set_notification_settings (j )
225+ return [True , None ]
234226
235227 def get_explore_grouping_hierarchy (self ):
236228 res = requests .get (self .url + '/api/groupConfigurations' , headers = self .hdrs )
@@ -803,7 +795,7 @@ def delete_event(self, event):
803795 return [True , None ]
804796
805797 def get_data (self , metrics , start_ts , end_ts = 0 , sampling_s = 0 ,
806- filter = '' , datasource_type = 'host' ):
798+ filter = '' , datasource_type = 'host' , paging = None ):
807799 reqbody = {
808800 'metrics' : metrics ,
809801 'dataSourceType' : datasource_type ,
@@ -820,6 +812,9 @@ def get_data(self, metrics, start_ts, end_ts=0, sampling_s=0,
820812 if filter != '' :
821813 reqbody ['filter' ] = filter
822814
815+ if paging is not None :
816+ reqbody ['paging' ] = paging
817+
823818 if sampling_s != 0 :
824819 reqbody ['sampling' ] = sampling_s
825820
0 commit comments