topology_override.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. "strings"
  7. "github.com/gnacho/netpulse/server-go/internal/adapters"
  8. "github.com/gnacho/netpulse/server-go/internal/auth"
  9. "github.com/gnacho/netpulse/server-go/internal/topooverride"
  10. )
  11. // registerTopologyOverrideRoutes registra las rutas admin de overrides:
  12. // - GET /api/topology/overrides → lista
  13. // - POST /api/topology/overrides → crear (upsert por MAC)
  14. // - PUT /api/topology/overrides/{id} → actualizar (id = MAC)
  15. // - DELETE /api/topology/overrides/{id} → borrar
  16. func (s *server) registerTopologyOverrideRoutes(mux *http.ServeMux) {
  17. mux.Handle("GET /api/topology/overrides", auth.RequireAdmin(http.HandlerFunc(s.handleTopologyOverrideList)))
  18. mux.Handle("POST /api/topology/overrides", auth.RequireAdmin(http.HandlerFunc(s.handleTopologyOverrideCreate)))
  19. mux.Handle("PUT /api/topology/overrides/{id}", auth.RequireAdmin(http.HandlerFunc(s.handleTopologyOverrideUpdate)))
  20. mux.Handle("DELETE /api/topology/overrides/{id}", auth.RequireAdmin(http.HandlerFunc(s.handleTopologyOverrideDelete)))
  21. }
  22. func (s *server) handleTopologyOverrideList(w http.ResponseWriter, r *http.Request) {
  23. writeJSON(w, http.StatusOK, map[string]any{"overrides": topooverride.List(s.db.DB)})
  24. }
  25. func (s *server) handleTopologyOverrideCreate(w http.ResponseWriter, r *http.Request) {
  26. var body struct {
  27. MAC string `json:"mac"`
  28. Kind string `json:"kind"`
  29. Name string `json:"name"`
  30. Parent string `json:"parent"`
  31. }
  32. if st := readJSONBody(w, r, &body); st != 0 {
  33. writeBodyError(w, st, "invalid_body", "")
  34. return
  35. }
  36. // MAC del device, o id de router para una unidad sin MAC de bridge (#1485).
  37. body.MAC = adapters.NormalizeMAC(body.MAC)
  38. if body.MAC == "" {
  39. writeError(w, http.StatusBadRequest, "invalid_mac")
  40. return
  41. }
  42. if body.Kind != "hypervisor" && body.Kind != "switch" && body.Kind != "attach" && body.Kind != "facet" {
  43. writeError(w, http.StatusBadRequest, "invalid_kind")
  44. return
  45. }
  46. // parent: obligatorio en attach y facet. En switch es opcional y dice de
  47. // dónde cuelga el switch (#2179): "routerId:puerto" fija el puerto donde
  48. // se aprende, y una MAC o un id de router lo cuelgan sin depender de la
  49. // evidencia del FDB. En hypervisor se ignora: su sitio sale de su puerto.
  50. switch body.Kind {
  51. case "attach", "facet":
  52. if strings.TrimSpace(body.Parent) == "" {
  53. writeError(w, http.StatusBadRequest, "attach_requires_parent")
  54. return
  55. }
  56. case "switch":
  57. // parent opcional, tal cual venga
  58. default:
  59. body.Parent = ""
  60. }
  61. o, err := topooverride.Add(s.db.DB, topooverride.AddInput{
  62. MAC: body.MAC, Kind: body.Kind, Name: body.Name, Parent: body.Parent,
  63. })
  64. if err != nil {
  65. writeError(w, http.StatusInternalServerError, "internal_error")
  66. return
  67. }
  68. writeJSON(w, http.StatusCreated, map[string]any{"override": o})
  69. }
  70. func (s *server) handleTopologyOverrideUpdate(w http.ResponseWriter, r *http.Request) {
  71. id := r.PathValue("id")
  72. var body struct {
  73. Kind *string `json:"kind"`
  74. Name *string `json:"name"`
  75. Parent *string `json:"parent"`
  76. Enabled *bool `json:"enabled"`
  77. }
  78. if st := readJSONBody(w, r, &body); st != 0 {
  79. writeBodyError(w, st, "invalid_body", "")
  80. return
  81. }
  82. if body.Kind != nil && *body.Kind != "hypervisor" && *body.Kind != "switch" && *body.Kind != "attach" && *body.Kind != "facet" {
  83. writeError(w, http.StatusBadRequest, "invalid_kind")
  84. return
  85. }
  86. o, ok := topooverride.Update(s.db.DB, id, topooverride.UpdateInput{
  87. Kind: body.Kind, Name: body.Name, Parent: body.Parent, Enabled: body.Enabled,
  88. })
  89. if !ok {
  90. writeError(w, http.StatusNotFound, "not_found")
  91. return
  92. }
  93. writeJSON(w, http.StatusOK, map[string]any{"override": o})
  94. }
  95. func (s *server) handleTopologyOverrideDelete(w http.ResponseWriter, r *http.Request) {
  96. if !topooverride.Remove(s.db.DB, r.PathValue("id")) {
  97. writeError(w, http.StatusNotFound, "not_found")
  98. return
  99. }
  100. w.WriteHeader(http.StatusNoContent)
  101. }