Skip to content

Commit 4d15eeb

Browse files
sradcoCursor AI Agentcursoragent
committed
router: add single alert rule update and delete-by-ID endpoints
Signed-off-by: avlitman <alitman@redhat.com> Signed-off-by: Shirly Radco <sradco@redhat.com> Signed-off-by: machadovilaca <machadovilaca@gmail.com> Co-authored-by: Cursor AI Agent <cursor-ai@users.noreply.github.com> Co-authored-by: Cursor <cursoragent@cursor.com> Made-with: Cursor
1 parent b1fdfb4 commit 4d15eeb

5 files changed

Lines changed: 727 additions & 0 deletions

File tree

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,175 @@
11
package managementrouter
22

3+
import (
4+
"encoding/json"
5+
"errors"
6+
"net/http"
7+
"strings"
8+
9+
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
10+
11+
"github.com/openshift/monitoring-plugin/pkg/management"
12+
)
13+
14+
type UpdateAlertRuleRequest struct {
15+
AlertingRule *monitoringv1.Rule `json:"alertingRule,omitempty"`
16+
AlertingRuleEnabled *bool `json:"AlertingRuleEnabled,omitempty"`
17+
Classification *AlertRuleClassificationPatch `json:"classification,omitempty"`
18+
}
19+
320
type UpdateAlertRuleResponse struct {
421
Id string `json:"id"`
522
StatusCode int `json:"status_code"`
623
Message string `json:"message,omitempty"`
724
}
25+
26+
func (hr *httpRouter) UpdateAlertRule(w http.ResponseWriter, req *http.Request) {
27+
ruleId, err := getParam(req, "ruleId")
28+
if err != nil {
29+
writeError(w, http.StatusBadRequest, err.Error())
30+
return
31+
}
32+
33+
var payload UpdateAlertRuleRequest
34+
if err := json.NewDecoder(req.Body).Decode(&payload); err != nil {
35+
writeError(w, http.StatusBadRequest, "invalid request body")
36+
return
37+
}
38+
39+
if payload.AlertingRule == nil && payload.AlertingRuleEnabled == nil && payload.Classification == nil {
40+
writeError(w, http.StatusBadRequest, "either alertingRule, AlertingRuleEnabled, or classification is required")
41+
return
42+
}
43+
44+
// Handle drop/restore for platform alerts
45+
if payload.AlertingRuleEnabled != nil {
46+
var derr error
47+
if !*payload.AlertingRuleEnabled {
48+
derr = hr.managementClient.DropPlatformAlertRule(req.Context(), ruleId)
49+
} else {
50+
derr = hr.managementClient.RestorePlatformAlertRule(req.Context(), ruleId)
51+
}
52+
if derr != nil {
53+
status, message := parseError(derr)
54+
w.Header().Set("Content-Type", "application/json")
55+
w.WriteHeader(http.StatusOK)
56+
_ = json.NewEncoder(w).Encode(UpdateAlertRuleResponse{
57+
Id: ruleId,
58+
StatusCode: status,
59+
Message: message,
60+
})
61+
return
62+
}
63+
if payload.AlertingRule == nil && payload.Classification == nil {
64+
w.Header().Set("Content-Type", "application/json")
65+
w.WriteHeader(http.StatusOK)
66+
_ = json.NewEncoder(w).Encode(UpdateAlertRuleResponse{
67+
Id: ruleId,
68+
StatusCode: http.StatusNoContent,
69+
})
70+
return
71+
}
72+
}
73+
74+
if payload.Classification != nil {
75+
update := management.UpdateRuleClassificationRequest{RuleId: ruleId}
76+
if payload.Classification.ComponentSet {
77+
update.Component = payload.Classification.Component
78+
update.ComponentSet = true
79+
}
80+
if payload.Classification.LayerSet {
81+
update.Layer = payload.Classification.Layer
82+
update.LayerSet = true
83+
}
84+
if payload.Classification.ComponentFromSet {
85+
update.ComponentFrom = payload.Classification.ComponentFrom
86+
update.ComponentFromSet = true
87+
}
88+
if payload.Classification.LayerFromSet {
89+
update.LayerFrom = payload.Classification.LayerFrom
90+
update.LayerFromSet = true
91+
}
92+
if err := hr.managementClient.UpdateAlertRuleClassification(req.Context(), update); err != nil {
93+
status, message := parseError(err)
94+
w.Header().Set("Content-Type", "application/json")
95+
w.WriteHeader(http.StatusOK)
96+
_ = json.NewEncoder(w).Encode(UpdateAlertRuleResponse{
97+
Id: ruleId,
98+
StatusCode: status,
99+
Message: message,
100+
})
101+
return
102+
}
103+
104+
// If this is a classification-only patch, return success now.
105+
if payload.AlertingRule == nil {
106+
w.Header().Set("Content-Type", "application/json")
107+
w.WriteHeader(http.StatusOK)
108+
_ = json.NewEncoder(w).Encode(UpdateAlertRuleResponse{
109+
Id: ruleId,
110+
StatusCode: http.StatusNoContent,
111+
})
112+
return
113+
}
114+
}
115+
116+
alertRule := *payload.AlertingRule
117+
118+
err = hr.managementClient.UpdatePlatformAlertRule(req.Context(), ruleId, alertRule)
119+
if err != nil {
120+
var ve *management.ValidationError
121+
var nf *management.NotFoundError
122+
if errors.As(err, &ve) || errors.As(err, &nf) {
123+
status, message := parseError(err)
124+
w.Header().Set("Content-Type", "application/json")
125+
w.WriteHeader(http.StatusOK)
126+
_ = json.NewEncoder(w).Encode(UpdateAlertRuleResponse{
127+
Id: ruleId,
128+
StatusCode: status,
129+
Message: message,
130+
})
131+
return
132+
}
133+
134+
var na *management.NotAllowedError
135+
if errors.As(err, &na) && strings.Contains(na.Error(), "cannot update non-platform alert rule") {
136+
// Not a platform rule, try user-defined update
137+
newRuleId, err := hr.managementClient.UpdateUserDefinedAlertRule(req.Context(), ruleId, alertRule)
138+
if err != nil {
139+
status, message := parseError(err)
140+
w.Header().Set("Content-Type", "application/json")
141+
w.WriteHeader(http.StatusOK)
142+
_ = json.NewEncoder(w).Encode(UpdateAlertRuleResponse{
143+
Id: ruleId,
144+
StatusCode: status,
145+
Message: message,
146+
})
147+
return
148+
}
149+
w.Header().Set("Content-Type", "application/json")
150+
w.WriteHeader(http.StatusOK)
151+
_ = json.NewEncoder(w).Encode(UpdateAlertRuleResponse{
152+
Id: newRuleId,
153+
StatusCode: http.StatusNoContent,
154+
})
155+
return
156+
}
157+
158+
status, message := parseError(err)
159+
w.Header().Set("Content-Type", "application/json")
160+
w.WriteHeader(http.StatusOK)
161+
_ = json.NewEncoder(w).Encode(UpdateAlertRuleResponse{
162+
Id: ruleId,
163+
StatusCode: status,
164+
Message: message,
165+
})
166+
return
167+
}
168+
169+
w.Header().Set("Content-Type", "application/json")
170+
w.WriteHeader(http.StatusOK)
171+
_ = json.NewEncoder(w).Encode(UpdateAlertRuleResponse{
172+
Id: ruleId,
173+
StatusCode: http.StatusNoContent,
174+
})
175+
}

0 commit comments

Comments
 (0)