alert_exceptions_api.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // alert_exceptions_api.go — CRUD de excepciones de alertas (#2292).
  2. // GET es de lectura para cualquier sesión; las mutaciones son de admin.
  3. package httpapi
  4. import (
  5. "errors"
  6. "net/http"
  7. "strconv"
  8. "time"
  9. "github.com/gnacho/netpulse/server-go/internal/alerts"
  10. )
  11. type alertExceptionBody struct {
  12. Kind string `json:"kind"`
  13. Host string `json:"host"`
  14. Target string `json:"target"`
  15. Comment string `json:"comment"`
  16. ExpiresAt int64 `json:"expiresAt"` // ms; 0 = sin caducidad
  17. }
  18. func (b alertExceptionBody) toException() alerts.Exception {
  19. return alerts.Exception{
  20. Kind: b.Kind,
  21. Host: b.Host,
  22. Target: b.Target,
  23. Comment: b.Comment,
  24. ExpiresAt: b.ExpiresAt,
  25. }
  26. }
  27. func (s *server) handleAlertExceptionsList(w http.ResponseWriter, _ *http.Request) {
  28. xs, err := alerts.ListExceptions(s.db)
  29. if err != nil {
  30. writeError(w, http.StatusInternalServerError, "internal_error", err.Error())
  31. return
  32. }
  33. writeJSON(w, http.StatusOK, xs)
  34. }
  35. func (s *server) handleAlertExceptionsCreate(w http.ResponseWriter, r *http.Request) {
  36. var body alertExceptionBody
  37. if st := readJSONBody(w, r, &body); st != 0 {
  38. writeBodyError(w, st, "invalid_body", "body JSON inválido")
  39. return
  40. }
  41. if body.ExpiresAt > 0 && body.ExpiresAt <= time.Now().UnixMilli() {
  42. writeError(w, http.StatusBadRequest, "invalid_expires", "la caducidad debe ser futura")
  43. return
  44. }
  45. x, err := alerts.CreateException(s.db, body.toException())
  46. if err != nil {
  47. if errors.Is(err, alerts.ErrExceptionInvalid) {
  48. writeError(w, http.StatusBadRequest, "invalid_exception", err.Error())
  49. return
  50. }
  51. writeError(w, http.StatusInternalServerError, "internal_error", err.Error())
  52. return
  53. }
  54. writeJSON(w, http.StatusCreated, x)
  55. }
  56. func (s *server) handleAlertExceptionsUpdate(w http.ResponseWriter, r *http.Request) {
  57. id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
  58. if err != nil || id <= 0 {
  59. writeError(w, http.StatusBadRequest, "invalid_id", "id inválido")
  60. return
  61. }
  62. var body alertExceptionBody
  63. if st := readJSONBody(w, r, &body); st != 0 {
  64. writeBodyError(w, st, "invalid_body", "body JSON inválido")
  65. return
  66. }
  67. x, err := alerts.UpdateException(s.db, id, body.toException())
  68. if err != nil {
  69. switch {
  70. case errors.Is(err, alerts.ErrExceptionNotFound):
  71. writeError(w, http.StatusNotFound, "not_found", err.Error())
  72. case errors.Is(err, alerts.ErrExceptionInvalid):
  73. writeError(w, http.StatusBadRequest, "invalid_exception", err.Error())
  74. default:
  75. writeError(w, http.StatusInternalServerError, "internal_error", err.Error())
  76. }
  77. return
  78. }
  79. writeJSON(w, http.StatusOK, x)
  80. }
  81. func (s *server) handleAlertExceptionsDelete(w http.ResponseWriter, r *http.Request) {
  82. id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
  83. if err != nil || id <= 0 {
  84. writeError(w, http.StatusBadRequest, "invalid_id", "id inválido")
  85. return
  86. }
  87. if err := alerts.DeleteException(s.db, id); err != nil {
  88. if errors.Is(err, alerts.ErrExceptionNotFound) {
  89. writeError(w, http.StatusNotFound, "not_found", err.Error())
  90. return
  91. }
  92. writeError(w, http.StatusInternalServerError, "internal_error", err.Error())
  93. return
  94. }
  95. writeJSON(w, http.StatusOK, map[string]any{"ok": true})
  96. }