-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert_action.go
144 lines (122 loc) · 3.98 KB
/
alert_action.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package alert
import (
"net/http"
"strings"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type Handler struct {
ClusterAlertRule v3.ClusterAlertRuleInterface
ProjectAlertRule v3.ProjectAlertRuleInterface
Notifiers v3.NotifierInterface
}
func RuleFormatter(apiContext *types.APIContext, resource *types.RawResource) {
resource.AddAction(apiContext, "unmute")
resource.AddAction(apiContext, "activate")
resource.AddAction(apiContext, "mute")
resource.AddAction(apiContext, "deactivate")
}
func GroupFormatter(apiContext *types.APIContext, resource *types.RawResource) {
resource.AddAction(apiContext, "unmute")
resource.AddAction(apiContext, "activate")
resource.AddAction(apiContext, "mute")
resource.AddAction(apiContext, "deactivate")
}
func (h *Handler) ClusterAlertRuleActionHandler(actionName string, action *types.Action, request *types.APIContext) error {
parts := strings.Split(request.ID, ":")
ns := parts[0]
id := parts[1]
alert, err := h.ClusterAlertRule.GetNamespaced(ns, id, metav1.GetOptions{})
if err != nil {
logrus.Errorf("Error while getting alert for %s :%v", request.ID, err)
return err
}
switch actionName {
case "activate":
if alert.Status.AlertState == "inactive" {
alert.Status.AlertState = "active"
} else {
return httperror.NewAPIError(httperror.ActionNotAvailable, "state is not inactive")
}
case "deactivate":
if alert.Status.AlertState == "active" {
alert.Status.AlertState = "inactive"
} else {
return httperror.NewAPIError(httperror.ActionNotAvailable, "state is not active")
}
case "mute":
if alert.Status.AlertState == "alerting" {
alert.Status.AlertState = "muted"
} else {
return httperror.NewAPIError(httperror.ActionNotAvailable, "state is not alerting")
}
case "unmute":
if alert.Status.AlertState == "muted" {
alert.Status.AlertState = "alerting"
} else {
return httperror.NewAPIError(httperror.ActionNotAvailable, "state is not muted")
}
}
alert, err = h.ClusterAlertRule.Update(alert)
if err != nil {
logrus.Errorf("Error while updating alert:%v", err)
return err
}
data := map[string]interface{}{}
if err := access.ByID(request, request.Version, request.Type, request.ID, &data); err != nil {
return err
}
request.WriteResponse(http.StatusOK, data)
return nil
}
func (h *Handler) ProjectAlertRuleActionHandler(actionName string, action *types.Action, request *types.APIContext) error {
parts := strings.Split(request.ID, ":")
ns := parts[0]
id := parts[1]
alert, err := h.ProjectAlertRule.GetNamespaced(ns, id, metav1.GetOptions{})
if err != nil {
logrus.Errorf("Error while getting alert for %s :%v", request.ID, err)
return err
}
switch actionName {
case "activate":
if alert.Status.AlertState == "inactive" {
alert.Status.AlertState = "active"
} else {
return httperror.NewAPIError(httperror.ActionNotAvailable, "state is not inactive")
}
case "deactivate":
if alert.Status.AlertState == "active" {
alert.Status.AlertState = "inactive"
} else {
return httperror.NewAPIError(httperror.ActionNotAvailable, "state is not active")
}
case "mute":
if alert.Status.AlertState == "alerting" {
alert.Status.AlertState = "muted"
} else {
return httperror.NewAPIError(httperror.ActionNotAvailable, "state is not alerting")
}
case "unmute":
if alert.Status.AlertState == "muted" {
alert.Status.AlertState = "alerting"
} else {
return httperror.NewAPIError(httperror.ActionNotAvailable, "state is not muted")
}
}
alert, err = h.ProjectAlertRule.Update(alert)
if err != nil {
logrus.Errorf("Error while updating alert:%v", err)
return err
}
data := map[string]interface{}{}
if err := access.ByID(request, request.Version, request.Type, request.ID, &data); err != nil {
return err
}
request.WriteResponse(http.StatusOK, data)
return nil
}