|
| 1 | +package managementrouter |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + |
| 7 | + monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" |
| 8 | + |
| 9 | + "github.com/openshift/monitoring-plugin/pkg/management" |
| 10 | +) |
| 11 | + |
| 12 | +type CreateAlertRuleRequest struct { |
| 13 | + AlertingRule *monitoringv1.Rule `json:"alertingRule,omitempty"` |
| 14 | + PrometheusRule *management.PrometheusRuleOptions `json:"prometheusRule,omitempty"` |
| 15 | +} |
| 16 | + |
| 17 | +type CreateAlertRuleResponse struct { |
| 18 | + Id string `json:"id"` |
| 19 | +} |
| 20 | + |
| 21 | +func (hr *httpRouter) CreateAlertRule(w http.ResponseWriter, req *http.Request) { |
| 22 | + var payload CreateAlertRuleRequest |
| 23 | + if err := json.NewDecoder(req.Body).Decode(&payload); err != nil { |
| 24 | + writeError(w, http.StatusBadRequest, "invalid request body") |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + if payload.AlertingRule == nil { |
| 29 | + writeError(w, http.StatusBadRequest, "alertingRule is required") |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + alertRule := *payload.AlertingRule |
| 34 | + |
| 35 | + var ( |
| 36 | + id string |
| 37 | + err error |
| 38 | + ) |
| 39 | + |
| 40 | + if payload.PrometheusRule != nil { |
| 41 | + id, err = hr.managementClient.CreateUserDefinedAlertRule(req.Context(), alertRule, *payload.PrometheusRule) |
| 42 | + } else { |
| 43 | + id, err = hr.managementClient.CreatePlatformAlertRule(req.Context(), alertRule) |
| 44 | + } |
| 45 | + |
| 46 | + if err != nil { |
| 47 | + handleError(w, err) |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + w.Header().Set("Content-Type", "application/json") |
| 52 | + w.WriteHeader(http.StatusCreated) |
| 53 | + _ = json.NewEncoder(w).Encode(CreateAlertRuleResponse{Id: id}) |
| 54 | +} |
0 commit comments