forked from wormhole-foundation/wormhole-explorer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
alert.go
127 lines (111 loc) · 3.04 KB
/
alert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package alert
import (
"fmt"
opsgenieAlert "github.com/opsgenie/opsgenie-go-sdk-v2/alert"
)
// Respoder struct to define alert routing notifications.
type Responder struct {
Id string
Type string
Name string
Username string
}
// Priority is the alert priority.
type Priority string
const (
CRITICAL Priority = "CRITICAL"
HIGH Priority = "HIGH"
MODERATE Priority = "MODERATE"
LOW Priority = "LOW"
INFORMATIONAL Priority = "INFORMATIONAL"
)
// Alert is the alert struct.
type Alert struct {
Message string
Alias string
Description string
Actions []string
Tags []string
Entity string
Priority Priority
Responder []Responder
VisibleTo []Responder
context AlertContext
}
// AlertContext contains the alert execution context.
type AlertContext struct {
Details map[string]string
Error error
Note string
}
// toOpsgenieResponder converts a Responder to an Opsgenie Responder.
func (a *Responder) toOpsgenieResponder() opsgenieAlert.Responder {
var responderType opsgenieAlert.ResponderType
switch a.Type {
case "user":
responderType = opsgenieAlert.UserResponder
case "team":
responderType = opsgenieAlert.TeamResponder
case "escalation":
responderType = opsgenieAlert.EscalationResponder
case "schedule":
responderType = opsgenieAlert.ScheduleResponder
}
opsgenieResponder := opsgenieAlert.Responder{
Id: a.Id,
Type: responderType,
Name: a.Name,
Username: a.Username,
}
return opsgenieResponder
}
// toOpsgeniePriority converts a Priority to an Opsgenie Priority.
func (p Priority) toOpsgeniePriority() opsgenieAlert.Priority {
switch p {
case CRITICAL:
return opsgenieAlert.P1
case HIGH:
return opsgenieAlert.P2
case MODERATE:
return opsgenieAlert.P3
case LOW:
return opsgenieAlert.P4
case INFORMATIONAL:
return opsgenieAlert.P5
default:
return opsgenieAlert.P5
}
}
// toOpsgenieRequest converts an Alert to an Opsgenie CreateAlertRequest.
func (a Alert) toOpsgenieRequest() opsgenieAlert.CreateAlertRequest {
// convert priority to opsgenie priority.
priotity := a.Priority.toOpsgeniePriority()
// convert responders to opsgenie responders.
var responders []opsgenieAlert.Responder
for _, responder := range a.Responder {
responders = append(responders, responder.toOpsgenieResponder())
}
// convert visibleTo to opsgenie responders.
var visibleTo []opsgenieAlert.Responder
for _, responder := range a.VisibleTo {
visibleTo = append(visibleTo, responder.toOpsgenieResponder())
}
// add error details to opsgenie alert details data.
description := a.Description
if a.context.Error != nil {
description = fmt.Sprintf("%s\n%s", a.Description, a.context.Error.Error())
}
return opsgenieAlert.CreateAlertRequest{
Message: a.Message,
Alias: a.Alias,
Description: description,
Actions: a.Actions,
Tags: a.Tags,
Details: a.context.Details,
Entity: a.Entity,
Priority: priotity,
Note: a.context.Note,
Responders: responders,
VisibleTo: visibleTo,
}
}