11package api
22
33import (
4+ "encoding/json"
45 "fmt"
6+ "strings"
57)
68
9+ // Alert defines an alert for a trigger.
710type Alert struct {
811 ID int64
912 Name string
@@ -21,6 +24,43 @@ func (e Alert) GetId() int64 {
2124 return e .ID
2225}
2326
27+ // AlertType defines a type for which Alerts could be inserted.
28+ type AlertType struct {
29+ ID int64
30+ Name string
31+ Description string
32+ Handle string
33+ BackupHandle string // Optional
34+ EscalationHandle string // Optional
35+ BackupSeconds int64 // Optional
36+ EscalationSeconds int64 // Optional
37+ Source string
38+ Version int64
39+ }
40+
41+ func (a AlertType ) GetId () int64 {
42+ return a .ID
43+ }
44+
45+ // AlertTrigger defines what Triggers an Alert of AlertType
46+ type AlertTrigger struct {
47+ ID int64
48+ Name string // Unique
49+ Description string
50+ AutoResolve int64 `json:"autoresolveSeconds"` // Optional
51+ Metric int64
52+ Config string
53+ OnApp bool
54+ Group int64 // Optional
55+ Server int64 // Optional
56+ Source string
57+ Version int64
58+ }
59+
60+ func (a AlertTrigger ) GetId () int64 {
61+ return a .ID
62+ }
63+
2464//GetAlertsBy will use a custom query to get a alert by unresolved/unacknowledged
2565func (api * Api ) GetAlertsBy (query string ) (string , error ) {
2666 var result string
@@ -41,3 +81,184 @@ func (api *Api) AlertSolution(alert *Alert, solutionType string) (string, error)
4181 }
4282 return result , nil
4383}
84+
85+ // CreateType is used to add a new Alert type.
86+ func (api * Api ) CreateType (name , description , handle , backupHandle , escalationHandle , source string , backupSeconds , escalationSeconds int64 ) (string , error ) {
87+
88+ data := map [string ][]string {
89+ "name" : {name },
90+ "description" : {description },
91+ "handle" : {handle },
92+ "source" : {source },
93+ }
94+
95+ // Set the optional values if they have value.
96+ if backupSeconds != - 1 {
97+ data ["backupSeconds" ] = []string {fmt .Sprintf ("%d" , backupSeconds )}
98+ }
99+ if backupHandle != DEFAULT_STRING_VALUE {
100+ data ["backupHandle" ] = []string {backupHandle }
101+ }
102+ if escalationSeconds != - 1 {
103+ data ["escalationSeconds" ] = []string {fmt .Sprintf ("%d" , escalationSeconds )}
104+ }
105+ if escalationHandle != DEFAULT_STRING_VALUE {
106+ data ["escalationHandle" ] = []string {escalationHandle }
107+ }
108+
109+ var result string
110+ if err := api .makeCall ("POST" , fmt .Sprintf ("/api/v1/app/%s/alerttypes/" , api .AppID ), data , true , & result ); err != nil {
111+ if duplicate , id := IsDuplicate (err ); duplicate {
112+ return api .GetObject ("alerttype" , id )
113+ }
114+ return "" , err
115+ }
116+ return result , nil
117+ }
118+
119+ // UpdateType is used to update an existing Alert type.
120+ func (api * Api ) UpdateType (alertType * AlertType ) (string , error ) {
121+
122+ data := map [string ][]string {
123+ "name" : {alertType .Name },
124+ "description" : {alertType .Description },
125+ "handle" : {alertType .Handle },
126+ "source" : {alertType .Source },
127+ "version" : {fmt .Sprintf ("%d" , alertType .Version )},
128+ }
129+
130+ // Set the optional values if they have value.
131+ if alertType .BackupSeconds != 0 {
132+ data ["backupSeconds" ] = []string {fmt .Sprintf ("%d" , alertType .BackupSeconds )}
133+ }
134+ if alertType .BackupHandle != "" {
135+ data ["backupHandle" ] = []string {alertType .BackupHandle }
136+ }
137+ if alertType .EscalationSeconds != 0 {
138+ data ["escalationSeconds" ] = []string {fmt .Sprintf ("%d" , alertType .EscalationSeconds )}
139+ }
140+ if alertType .EscalationHandle != "" {
141+ data ["escalationHandle" ] = []string {alertType .EscalationHandle }
142+ }
143+
144+ var result string
145+ if err := api .makeCall ("PUT" , fmt .Sprintf ("/api/v1/app/%s/alerttypes/%d/" , api .AppID , alertType .GetId ()), data , true , & result ); err != nil {
146+ return "" , err
147+ }
148+ return result , nil
149+ }
150+
151+ // GetTriggers will return all triggers for an alert type
152+ func (api * Api ) GetTriggers (alertTypeID int64 ) (string , error ) {
153+ var result string
154+ if err := api .makeCall ("GET" , fmt .Sprintf ("/api/v1/app/%s/alerttypes/%d/triggers/" , api .AppID , alertTypeID ), nil , true , & result ); err != nil {
155+ return "" , err
156+ }
157+ return result , nil
158+ }
159+
160+ // CreateTrigger is used to add a new Trigger for alerts.
161+ func (api * Api ) CreateTrigger (name , description , config , source string , alertTypeID , autoResolve , metricID , serverID , serverGroupID int64 , onApp bool ) (string , error ) {
162+
163+ data := map [string ][]string {
164+ "name" : {name },
165+ "description" : {description },
166+ "metric" : {fmt .Sprintf ("%d" , metricID )},
167+ "config" : {config },
168+ "onApp" : {fmt .Sprintf ("%t" , onApp )},
169+ "source" : {source },
170+ }
171+
172+ // Set the option values if they have value.
173+ if serverID != - 1 {
174+ data ["server" ] = []string {fmt .Sprintf ("%d" , serverID )}
175+ } else if serverGroupID != - 1 {
176+ data ["group" ] = []string {fmt .Sprintf ("%d" , serverGroupID )}
177+ }
178+ if autoResolve != - 1 {
179+ data ["autoresolveSeconds" ] = []string {fmt .Sprintf ("%d" , autoResolve )}
180+ }
181+
182+ var result string
183+ if err := api .makeCall ("POST" , fmt .Sprintf ("/api/v1/app/%s/alerttypes/%d/triggers/" , api .AppID , alertTypeID ), data , true , & result ); err != nil {
184+ if duplicate , id := IsDuplicate (err ); duplicate {
185+ return api .GetObjectFromGroup ("alerttype" , "trigger" , alertTypeID , id )
186+ }
187+ return "" , err
188+ }
189+ return result , nil
190+ }
191+
192+ // UpdateTrigger is used to update a existing Trigger for alerts.
193+ func (api * Api ) UpdateTrigger (typeID int64 , trigger * AlertTrigger ) (string , error ) {
194+
195+ data := map [string ][]string {
196+ "name" : {trigger .Name },
197+ "description" : {trigger .Description },
198+ "metric" : {fmt .Sprintf ("%d" , trigger .Metric )},
199+ "config" : {trigger .Config },
200+ "onApp" : {fmt .Sprintf ("%t" , trigger .OnApp )},
201+ "source" : {trigger .Source },
202+ "version" : {fmt .Sprintf ("%d" , trigger .Version )},
203+ }
204+
205+ // Set the option values if they have value.
206+ if trigger .Server != 0 {
207+ data ["server" ] = []string {fmt .Sprintf ("%d" , trigger .Server )}
208+ } else if trigger .Group != 0 {
209+ data ["group" ] = []string {fmt .Sprintf ("%d" , trigger .Group )}
210+ }
211+ if trigger .AutoResolve != 0 {
212+ data ["autoresolveSeconds" ] = []string {fmt .Sprintf ("%d" , trigger .AutoResolve )}
213+ }
214+
215+ var result string
216+ if err := api .makeCall ("PUT" , fmt .Sprintf ("/api/v1/app/%s/alerttypes/%d/triggers/%d/" , api .AppID , typeID , trigger .ID ), data , true , & result ); err != nil {
217+ return "" , err
218+ }
219+ return api .GetObjectFromGroup ("alerttype" , "trigger" , typeID , trigger .ID )
220+ }
221+
222+ // ParseHandle is used to parse the handle provided by user and serialize into json format.
223+ func ParseHandle (handle string ) (string , error ) {
224+ var result []map [string ]string
225+
226+ // Parse the handle parameter.
227+ contacts := strings .Split (handle , " " )
228+ for _ , contact := range contacts {
229+ // e.g. SLACK:slack/webhook/here
230+ if i := strings .Index (contact , ":" ); i != - 1 {
231+ contactType := contact [:i ]
232+ contact = contact [i + 1 :]
233+
234+ var contactRes map [string ]string
235+ switch contactType {
236+ case "EMAILUSER" :
237+ contactRes = map [string ]string {
238+ "type" : contactType ,
239+ "id" : contact ,
240+ }
241+ case "EMAIL" :
242+ contactRes = map [string ]string {
243+ "type" : contactType ,
244+ "address" : contact ,
245+ }
246+ case "SLACK" :
247+ contactRes = map [string ]string {
248+ "type" : contactType ,
249+ "webhook" : contact ,
250+ }
251+ }
252+
253+ if contactRes != nil {
254+ result = append (result , contactRes )
255+ }
256+ }
257+ }
258+ if result == nil {
259+ return "" , fmt .Errorf ("Could not parse the alert handle" )
260+ }
261+
262+ jsonHandle , err := json .Marshal (result )
263+ return string (jsonHandle ), err
264+ }
0 commit comments