forked from machadovilaca/monitoring-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalert_rule_bulk_update.go
More file actions
221 lines (197 loc) · 6.47 KB
/
alert_rule_bulk_update.go
File metadata and controls
221 lines (197 loc) · 6.47 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package managementrouter
import (
"encoding/json"
"errors"
"net/http"
"strings"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/openshift/monitoring-plugin/pkg/management"
)
// Note: router no longer filters provenance/identity labels here.
// Backend enforces ARC scoping and ignores/guards protected labels as needed.
type BulkUpdateAlertRulesRequest struct {
RuleIds []string `json:"ruleIds"`
// Use pointer values so we can distinguish null (delete) vs string value (set)
Labels map[string]*string `json:"labels,omitempty"`
AlertingRuleEnabled *bool `json:"AlertingRuleEnabled,omitempty"`
Classification *AlertRuleClassificationPatch `json:"classification,omitempty"`
}
type BulkUpdateAlertRulesResponse struct {
Rules []UpdateAlertRuleResponse `json:"rules"`
}
func (hr *httpRouter) BulkUpdateAlertRules(w http.ResponseWriter, req *http.Request) {
var payload BulkUpdateAlertRulesRequest
if err := json.NewDecoder(req.Body).Decode(&payload); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
return
}
if len(payload.RuleIds) == 0 {
writeError(w, http.StatusBadRequest, "ruleIds is required")
return
}
if payload.AlertingRuleEnabled == nil && payload.Labels == nil && payload.Classification == nil {
writeError(w, http.StatusBadRequest, "AlertingRuleEnabled (toggle drop/restore) or labels (set/unset) or classification is required")
return
}
var haveToggle bool
var enabled bool
if payload.AlertingRuleEnabled != nil {
enabled = *payload.AlertingRuleEnabled
haveToggle = true
}
results := make([]UpdateAlertRuleResponse, 0, len(payload.RuleIds))
for _, rawId := range payload.RuleIds {
id, err := parseParam(rawId, "ruleId")
if err != nil {
results = append(results, UpdateAlertRuleResponse{
Id: rawId,
StatusCode: http.StatusBadRequest,
Message: err.Error(),
})
continue
}
// Handle enabled drop/restore first if requested
notAllowedEnabled := false
if haveToggle {
var derr error
if !enabled {
derr = hr.managementClient.DropPlatformAlertRule(req.Context(), id)
} else {
derr = hr.managementClient.RestorePlatformAlertRule(req.Context(), id)
}
if derr != nil {
// If NotAllowed (likely user-defined), we still allow label updates.
var na *management.NotAllowedError
if errors.As(derr, &na) {
notAllowedEnabled = true
} else {
status, message := parseError(derr)
results = append(results, UpdateAlertRuleResponse{
Id: id,
StatusCode: status,
Message: message,
})
continue
}
}
}
if payload.Classification != nil {
update := management.UpdateRuleClassificationRequest{RuleId: id}
if payload.Classification.ComponentSet {
update.Component = payload.Classification.Component
update.ComponentSet = true
}
if payload.Classification.LayerSet {
update.Layer = payload.Classification.Layer
update.LayerSet = true
}
if payload.Classification.ComponentFromSet {
update.ComponentFrom = payload.Classification.ComponentFrom
update.ComponentFromSet = true
}
if payload.Classification.LayerFromSet {
update.LayerFrom = payload.Classification.LayerFrom
update.LayerFromSet = true
}
if update.ComponentSet || update.LayerSet || update.ComponentFromSet || update.LayerFromSet {
if err := hr.managementClient.UpdateAlertRuleClassification(req.Context(), update); err != nil {
status, message := parseError(err)
results = append(results, UpdateAlertRuleResponse{
Id: id,
StatusCode: status,
Message: message,
})
continue
}
}
}
if payload.Labels != nil {
// For bulk update, merge labels and handle empty strings as drops
currentRule, err := hr.managementClient.GetRuleById(req.Context(), id)
if err != nil {
status, message := parseError(err)
results = append(results, UpdateAlertRuleResponse{
Id: id,
StatusCode: status,
Message: message,
})
continue
}
mergedLabels := make(map[string]string)
intentLabels := make(map[string]string)
for k, v := range currentRule.Labels {
mergedLabels[k] = v
}
for k, pv := range payload.Labels {
if pv == nil || *pv == "" {
intentLabels[k] = ""
delete(mergedLabels, k)
continue
}
mergedLabels[k] = *pv
intentLabels[k] = *pv
}
updatedPlatformRule := monitoringv1.Rule{Labels: intentLabels}
err = hr.managementClient.UpdatePlatformAlertRule(req.Context(), id, updatedPlatformRule)
if err != nil {
var ve *management.ValidationError
var nf *management.NotFoundError
if errors.As(err, &ve) || errors.As(err, &nf) {
status, message := parseError(err)
results = append(results, UpdateAlertRuleResponse{
Id: id,
StatusCode: status,
Message: message,
})
continue
}
var na *management.NotAllowedError
if errors.As(err, &na) && strings.Contains(na.Error(), "cannot update non-platform alert rule") {
// Not a platform rule, try user-defined
// For user-defined, we apply the merged labels to the PR
updatedUserRule := currentRule
updatedUserRule.Labels = mergedLabels
newRuleId, err := hr.managementClient.UpdateUserDefinedAlertRule(req.Context(), id, updatedUserRule)
if err != nil {
status, message := parseError(err)
results = append(results, UpdateAlertRuleResponse{
Id: id,
StatusCode: status,
Message: message,
})
continue
}
results = append(results, UpdateAlertRuleResponse{
Id: newRuleId,
StatusCode: http.StatusNoContent,
})
continue
}
status, message := parseError(err)
results = append(results, UpdateAlertRuleResponse{
Id: id,
StatusCode: status,
Message: message,
})
continue
}
}
// If only enabled was requested and it was NotAllowed, return 405 for this id.
if notAllowedEnabled && payload.Labels == nil && payload.Classification == nil {
results = append(results, UpdateAlertRuleResponse{
Id: id,
StatusCode: http.StatusMethodNotAllowed,
})
continue
}
results = append(results, UpdateAlertRuleResponse{
Id: id,
StatusCode: http.StatusNoContent,
})
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(BulkUpdateAlertRulesResponse{
Rules: results,
})
}