| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- // alert_exceptions_api.go — CRUD de excepciones de alertas (#2292).
- // GET es de lectura para cualquier sesión; las mutaciones son de admin.
- package httpapi
- import (
- "errors"
- "net/http"
- "strconv"
- "time"
- "github.com/gnacho/netpulse/server-go/internal/alerts"
- )
- type alertExceptionBody struct {
- Kind string `json:"kind"`
- Host string `json:"host"`
- Target string `json:"target"`
- Comment string `json:"comment"`
- ExpiresAt int64 `json:"expiresAt"` // ms; 0 = sin caducidad
- }
- func (b alertExceptionBody) toException() alerts.Exception {
- return alerts.Exception{
- Kind: b.Kind,
- Host: b.Host,
- Target: b.Target,
- Comment: b.Comment,
- ExpiresAt: b.ExpiresAt,
- }
- }
- func (s *server) handleAlertExceptionsList(w http.ResponseWriter, _ *http.Request) {
- xs, err := alerts.ListExceptions(s.db)
- if err != nil {
- writeError(w, http.StatusInternalServerError, "internal_error", err.Error())
- return
- }
- writeJSON(w, http.StatusOK, xs)
- }
- func (s *server) handleAlertExceptionsCreate(w http.ResponseWriter, r *http.Request) {
- var body alertExceptionBody
- if st := readJSONBody(w, r, &body); st != 0 {
- writeBodyError(w, st, "invalid_body", "body JSON inválido")
- return
- }
- if body.ExpiresAt > 0 && body.ExpiresAt <= time.Now().UnixMilli() {
- writeError(w, http.StatusBadRequest, "invalid_expires", "la caducidad debe ser futura")
- return
- }
- x, err := alerts.CreateException(s.db, body.toException())
- if err != nil {
- if errors.Is(err, alerts.ErrExceptionInvalid) {
- writeError(w, http.StatusBadRequest, "invalid_exception", err.Error())
- return
- }
- writeError(w, http.StatusInternalServerError, "internal_error", err.Error())
- return
- }
- writeJSON(w, http.StatusCreated, x)
- }
- func (s *server) handleAlertExceptionsUpdate(w http.ResponseWriter, r *http.Request) {
- id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
- if err != nil || id <= 0 {
- writeError(w, http.StatusBadRequest, "invalid_id", "id inválido")
- return
- }
- var body alertExceptionBody
- if st := readJSONBody(w, r, &body); st != 0 {
- writeBodyError(w, st, "invalid_body", "body JSON inválido")
- return
- }
- x, err := alerts.UpdateException(s.db, id, body.toException())
- if err != nil {
- switch {
- case errors.Is(err, alerts.ErrExceptionNotFound):
- writeError(w, http.StatusNotFound, "not_found", err.Error())
- case errors.Is(err, alerts.ErrExceptionInvalid):
- writeError(w, http.StatusBadRequest, "invalid_exception", err.Error())
- default:
- writeError(w, http.StatusInternalServerError, "internal_error", err.Error())
- }
- return
- }
- writeJSON(w, http.StatusOK, x)
- }
- func (s *server) handleAlertExceptionsDelete(w http.ResponseWriter, r *http.Request) {
- id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
- if err != nil || id <= 0 {
- writeError(w, http.StatusBadRequest, "invalid_id", "id inválido")
- return
- }
- if err := alerts.DeleteException(s.db, id); err != nil {
- if errors.Is(err, alerts.ErrExceptionNotFound) {
- writeError(w, http.StatusNotFound, "not_found", err.Error())
- return
- }
- writeError(w, http.StatusInternalServerError, "internal_error", err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"ok": true})
- }
|