rules_api.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package httpapi
  2. import (
  3. "net/http"
  4. "github.com/gnacho/netpulse/server-go/internal/alerts"
  5. )
  6. func (s *server) handleAlertRulesList(w http.ResponseWriter, _ *http.Request) {
  7. rules, err := alerts.ListRules(s.db)
  8. if err != nil {
  9. writeError(w, http.StatusInternalServerError, "internal_error", err.Error())
  10. return
  11. }
  12. writeJSON(w, http.StatusOK, rules)
  13. }
  14. func (s *server) handleAlertRulesGet(w http.ResponseWriter, r *http.Request) {
  15. id := r.PathValue("id")
  16. rule, err := alerts.GetRule(s.db, id)
  17. if err != nil {
  18. writeError(w, http.StatusNotFound, "not_found", err.Error())
  19. return
  20. }
  21. writeJSON(w, http.StatusOK, rule)
  22. }
  23. func (s *server) handleAlertRulesCreate(w http.ResponseWriter, r *http.Request) {
  24. var rule alerts.Rule
  25. if st := readJSONBody(w, r, &rule); st != 0 {
  26. writeBodyError(w, st, "invalid_body", "body JSON inválido")
  27. return
  28. }
  29. if err := validateRule(rule); err != nil {
  30. writeError(w, http.StatusBadRequest, "invalid_rule", err.Error())
  31. return
  32. }
  33. if err := alerts.CreateRule(s.db, rule); err != nil {
  34. writeError(w, http.StatusInternalServerError, "internal_error", err.Error())
  35. return
  36. }
  37. writeJSON(w, http.StatusCreated, map[string]any{"ok": true})
  38. }
  39. func (s *server) handleAlertRulesUpdate(w http.ResponseWriter, r *http.Request) {
  40. id := r.PathValue("id")
  41. var rule alerts.Rule
  42. if st := readJSONBody(w, r, &rule); st != 0 {
  43. writeBodyError(w, st, "invalid_body", "body JSON inválido")
  44. return
  45. }
  46. rule.ID = id
  47. if err := validateRule(rule); err != nil {
  48. writeError(w, http.StatusBadRequest, "invalid_rule", err.Error())
  49. return
  50. }
  51. if err := alerts.UpdateRule(s.db, rule); err != nil {
  52. writeError(w, http.StatusNotFound, "not_found", err.Error())
  53. return
  54. }
  55. writeJSON(w, http.StatusOK, map[string]any{"ok": true})
  56. }
  57. func (s *server) handleAlertRulesDelete(w http.ResponseWriter, r *http.Request) {
  58. id := r.PathValue("id")
  59. if err := alerts.DeleteRule(s.db, id); err != nil {
  60. writeError(w, http.StatusNotFound, "not_found", err.Error())
  61. return
  62. }
  63. writeJSON(w, http.StatusOK, map[string]any{"ok": true})
  64. }
  65. func validateRule(r alerts.Rule) error {
  66. if r.Name == "" {
  67. return &validationError{"nombre requerido"}
  68. }
  69. if !alerts.IsCategory(r.Category) {
  70. return &validationError{"categoría inválida: " + r.Category}
  71. }
  72. validOps := map[string]bool{"gt": true, "gte": true, "lt": true, "lte": true, "eq": true}
  73. if !validOps[r.Condition.Operator] {
  74. return &validationError{"operador inválido: " + r.Condition.Operator}
  75. }
  76. if r.Condition.Metric == "" {
  77. return &validationError{"métrica requerida"}
  78. }
  79. if r.Condition.Duration <= 0 {
  80. return &validationError{"duración debe ser > 0"}
  81. }
  82. validScopes := map[string]bool{"global": true, "router": true}
  83. if !validScopes[r.Scope.Type] {
  84. return &validationError{"scope inválido: " + r.Scope.Type}
  85. }
  86. if r.Scope.Type == "router" && len(r.Scope.RouterIDs) == 0 {
  87. return &validationError{"scope=router requiere al menos un routerId"}
  88. }
  89. validSeverities := map[string]bool{"info": true, "warn": true, "critical": true}
  90. if !validSeverities[r.Severity] {
  91. return &validationError{"severidad inválida: " + r.Severity}
  92. }
  93. return nil
  94. }
  95. type validationError struct{ msg string }
  96. func (e *validationError) Error() string { return e.msg }