Skip to content

Commit 43c681e

Browse files
committed
Sync from Bitbucket
1 parent 622cef7 commit 43c681e

File tree

7 files changed

+960
-23
lines changed

7 files changed

+960
-23
lines changed

src/coscale/api/alert.go

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package api
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"strings"
57
)
68

9+
// Alert defines an alert for a trigger.
710
type 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
2565
func (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+
}

src/coscale/api/common.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ func (api *Api) GetObject(objectName string, id int64) (string, error) {
2626
return result, nil
2727
}
2828

29+
// GetObjectFromGroup will return the object (json) specified by objectName from objectGroup that have a certain id
30+
func (api *Api) GetObjectFromGroup(objectGroup, objectName string, groupID, objectID int64) (string, error) {
31+
var result string
32+
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/%d/%ss/%d/", api.AppID, objectGroup, groupID, objectName, objectID), nil, true, &result); err != nil {
33+
return "", err
34+
}
35+
return result, nil
36+
}
37+
2938
//GetObjectRef will put in result a reference to a object specified by objectName and that have a certain id
3039
func (api *Api) GetObjectRef(objectName string, id int64, result Object) error {
3140
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/%d/", api.AppID, objectName, id), nil, false, &result); err != nil {
@@ -34,6 +43,14 @@ func (api *Api) GetObjectRef(objectName string, id int64, result Object) error {
3443
return nil
3544
}
3645

46+
// GetObjectRefFromGroup will return the object specified by objectName from objectGroup that have a certain id
47+
func (api *Api) GetObjectRefFromGroup(objectGroup, objectName string, groupID, objectID int64, result Object) error {
48+
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/%d/%ss/%d/", api.AppID, objectGroup, groupID, objectName, objectID), nil, false, &result); err != nil {
49+
return err
50+
}
51+
return nil
52+
}
53+
3754
//GetObjectByName will return the object (json) specified by objectName and name
3855
func (api *Api) GetObjectByName(objectName string, name string) (string, error) {
3956
name = strings.Replace(name, " ", "%20", -1)
@@ -46,17 +63,34 @@ func (api *Api) GetObjectByName(objectName string, name string) (string, error)
4663

4764
//GetObejctRefByName will put in result a reference to the oject specified by objectName and name
4865
func (api *Api) GetObejctRefByName(objectName string, name string, result Object) error {
66+
// In go %% is % escaped, we need to escape the name to work with string fmt.
67+
name = strings.Replace(name, "%", "%%", -1)
4968
name = strings.Replace(name, " ", "%20", -1)
5069
objects := []*Object{&result}
5170
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/?selectByName=%s", api.AppID, objectName, name), nil, false, &objects); err != nil {
5271
return err
5372
}
5473
if len(objects) == 0 {
5574
return fmt.Errorf("Not Found")
56-
} else {
57-
result = *objects[0]
58-
return nil
5975
}
76+
result = *objects[0]
77+
return nil
78+
}
79+
80+
// GetObejctRefByNameFromGroup will return the object specified by objectName from objectGroup that have a certain name
81+
func (api *Api) GetObejctRefByNameFromGroup(objectGroup, objectName string, groupID int64, name string, result Object) error {
82+
// In go %% is % escaped, we need to escape the name to work with string fmt.
83+
name = strings.Replace(name, "%", "%%", -1)
84+
name = strings.Replace(name, " ", "%20", -1)
85+
objects := []*Object{&result}
86+
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/%d/%ss/?selectByName=%s", api.AppID, objectGroup, groupID, objectName, name), nil, false, &objects); err != nil {
87+
return err
88+
}
89+
if len(objects) == 0 {
90+
return fmt.Errorf("Not Found")
91+
}
92+
result = *objects[0]
93+
return nil
6094
}
6195

6296
//DeleteObject will delete a object
@@ -79,7 +113,12 @@ func (api *Api) AddObjectToGroup(objectName string, object Object, group Object)
79113
return nil
80114
}
81115

82-
// AddObjectToGroup remove a object (metric, event, etc) from a group of objects.
116+
// DeleteObjectFromGroup remove a object (metric, event, etc) from a group of objects.
83117
func (api *Api) DeleteObjectFromGroup(objectName string, object Object, group Object) error {
84118
return api.makeCall("DELETE", fmt.Sprintf("/api/v1/app/%s/%sgroups/%d/%ss/%d/", api.AppID, objectName, group.GetId(), objectName, object.GetId()), nil, false, nil)
85119
}
120+
121+
// DeleteObjectFromGroup remove a object (metric, event, etc) from a group of objects.
122+
func (api *Api) DeleteObjectFromGroupByID(groupName, objectName string, groupID, id int64) error {
123+
return api.makeCall("DELETE", fmt.Sprintf("/api/v1/app/%s/%ss/%d/%ss/%d/", api.AppID, groupName, groupID, objectName, id), nil, false, nil)
124+
}

0 commit comments

Comments
 (0)