topology_override.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // topology_override.go — API admin de overrides manuales de topología
  2. // (issue #142, Fase A): CRUD sobre la tabla topology_overrides.
  3. package httpapi
  4. import (
  5. "net/http"
  6. "github.com/gnacho/netpulse/server-go/internal/adapters"
  7. "github.com/gnacho/netpulse/server-go/internal/auth"
  8. "github.com/gnacho/netpulse/server-go/internal/topooverride"
  9. )
  10. // registerTopologyOverrideRoutes registra las rutas admin de overrides:
  11. // - GET /api/topology/overrides → lista
  12. // - POST /api/topology/overrides → crear (upsert por MAC)
  13. // - PUT /api/topology/overrides/{id} → actualizar (id = MAC)
  14. // - DELETE /api/topology/overrides/{id} → borrar
  15. func (s *server) registerTopologyOverrideRoutes(mux *http.ServeMux) {
  16. mux.Handle("GET /api/topology/overrides", auth.RequireAdmin(http.HandlerFunc(s.handleTopologyOverrideList)))
  17. mux.Handle("POST /api/topology/overrides", auth.RequireAdmin(http.HandlerFunc(s.handleTopologyOverrideCreate)))
  18. mux.Handle("PUT /api/topology/overrides/{id}", auth.RequireAdmin(http.HandlerFunc(s.handleTopologyOverrideUpdate)))
  19. mux.Handle("DELETE /api/topology/overrides/{id}", auth.RequireAdmin(http.HandlerFunc(s.handleTopologyOverrideDelete)))
  20. }
  21. func (s *server) handleTopologyOverrideList(w http.ResponseWriter, r *http.Request) {
  22. writeJSON(w, http.StatusOK, map[string]any{"overrides": topooverride.List(s.db.DB)})
  23. }
  24. func (s *server) handleTopologyOverrideCreate(w http.ResponseWriter, r *http.Request) {
  25. var body struct {
  26. MAC string `json:"mac"`
  27. Kind string `json:"kind"`
  28. Name string `json:"name"`
  29. Parent string `json:"parent"`
  30. }
  31. if st := readJSONBody(w, r, &body); st != 0 {
  32. writeBodyError(w, st, "invalid_body", "")
  33. return
  34. }
  35. body.MAC = adapters.NormalizeMAC(body.MAC)
  36. if body.MAC == "" {
  37. writeError(w, http.StatusBadRequest, "invalid_mac")
  38. return
  39. }
  40. if body.Kind != "hypervisor" && body.Kind != "switch" && body.Kind != "attach" {
  41. writeError(w, http.StatusBadRequest, "invalid_kind")
  42. return
  43. }
  44. // parent (solo kind=attach) acepta: MAC de device (formato original),
  45. // id de router, o "routerId:puerto" (issue #690). Solo se exige no vacío;
  46. // la resolución final (adapters.applyTopologyOverrides) ignora formas
  47. // que no casen con ningún device/router conocido.
  48. if body.Kind == "attach" && adapters.NormalizeMAC(body.Parent) == "" {
  49. writeError(w, http.StatusBadRequest, "attach_requires_parent")
  50. return
  51. }
  52. if body.Kind != "attach" {
  53. body.Parent = ""
  54. }
  55. o, err := topooverride.Add(s.db.DB, topooverride.AddInput{
  56. MAC: body.MAC, Kind: body.Kind, Name: body.Name, Parent: body.Parent,
  57. })
  58. if err != nil {
  59. writeError(w, http.StatusInternalServerError, "internal_error")
  60. return
  61. }
  62. writeJSON(w, http.StatusCreated, map[string]any{"override": o})
  63. }
  64. func (s *server) handleTopologyOverrideUpdate(w http.ResponseWriter, r *http.Request) {
  65. id := r.PathValue("id")
  66. var body struct {
  67. Kind *string `json:"kind"`
  68. Name *string `json:"name"`
  69. Parent *string `json:"parent"`
  70. Enabled *bool `json:"enabled"`
  71. }
  72. if st := readJSONBody(w, r, &body); st != 0 {
  73. writeBodyError(w, st, "invalid_body", "")
  74. return
  75. }
  76. if body.Kind != nil && *body.Kind != "hypervisor" && *body.Kind != "switch" && *body.Kind != "attach" {
  77. writeError(w, http.StatusBadRequest, "invalid_kind")
  78. return
  79. }
  80. o, ok := topooverride.Update(s.db.DB, id, topooverride.UpdateInput{
  81. Kind: body.Kind, Name: body.Name, Parent: body.Parent, Enabled: body.Enabled,
  82. })
  83. if !ok {
  84. writeError(w, http.StatusNotFound, "not_found")
  85. return
  86. }
  87. writeJSON(w, http.StatusOK, map[string]any{"override": o})
  88. }
  89. func (s *server) handleTopologyOverrideDelete(w http.ResponseWriter, r *http.Request) {
  90. if !topooverride.Remove(s.db.DB, r.PathValue("id")) {
  91. writeError(w, http.StatusNotFound, "not_found")
  92. return
  93. }
  94. w.WriteHeader(http.StatusNoContent)
  95. }