| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- // topology_override.go — API admin de overrides manuales de topología
- // (issue #142, Fase A): CRUD sobre la tabla topology_overrides.
- package httpapi
- import (
- "net/http"
- "strings"
- "github.com/gnacho/netpulse/server-go/internal/adapters"
- "github.com/gnacho/netpulse/server-go/internal/auth"
- "github.com/gnacho/netpulse/server-go/internal/topooverride"
- )
- // registerTopologyOverrideRoutes registra las rutas admin de overrides:
- // - GET /api/topology/overrides → lista
- // - POST /api/topology/overrides → crear (upsert por MAC)
- // - PUT /api/topology/overrides/{id} → actualizar (id = MAC)
- // - DELETE /api/topology/overrides/{id} → borrar
- func (s *server) registerTopologyOverrideRoutes(mux *http.ServeMux) {
- mux.Handle("GET /api/topology/overrides", auth.RequireAdmin(http.HandlerFunc(s.handleTopologyOverrideList)))
- mux.Handle("POST /api/topology/overrides", auth.RequireAdmin(http.HandlerFunc(s.handleTopologyOverrideCreate)))
- mux.Handle("PUT /api/topology/overrides/{id}", auth.RequireAdmin(http.HandlerFunc(s.handleTopologyOverrideUpdate)))
- mux.Handle("DELETE /api/topology/overrides/{id}", auth.RequireAdmin(http.HandlerFunc(s.handleTopologyOverrideDelete)))
- }
- func (s *server) handleTopologyOverrideList(w http.ResponseWriter, r *http.Request) {
- writeJSON(w, http.StatusOK, map[string]any{"overrides": topooverride.List(s.db.DB)})
- }
- func (s *server) handleTopologyOverrideCreate(w http.ResponseWriter, r *http.Request) {
- var body struct {
- MAC string `json:"mac"`
- Kind string `json:"kind"`
- Name string `json:"name"`
- Parent string `json:"parent"`
- }
- if st := readJSONBody(w, r, &body); st != 0 {
- writeBodyError(w, st, "invalid_body", "")
- return
- }
- // MAC del device, o id de router para una unidad sin MAC de bridge (#1485).
- body.MAC = adapters.NormalizeMAC(body.MAC)
- if body.MAC == "" {
- writeError(w, http.StatusBadRequest, "invalid_mac")
- return
- }
- if body.Kind != "hypervisor" && body.Kind != "switch" && body.Kind != "attach" && body.Kind != "facet" {
- writeError(w, http.StatusBadRequest, "invalid_kind")
- return
- }
- // parent: obligatorio en attach y facet. En switch es opcional y dice de
- // dónde cuelga el switch (#2179): "routerId:puerto" fija el puerto donde
- // se aprende, y una MAC o un id de router lo cuelgan sin depender de la
- // evidencia del FDB. En hypervisor se ignora: su sitio sale de su puerto.
- switch body.Kind {
- case "attach", "facet":
- if strings.TrimSpace(body.Parent) == "" {
- writeError(w, http.StatusBadRequest, "attach_requires_parent")
- return
- }
- case "switch":
- // parent opcional, tal cual venga
- default:
- body.Parent = ""
- }
- o, err := topooverride.Add(s.db.DB, topooverride.AddInput{
- MAC: body.MAC, Kind: body.Kind, Name: body.Name, Parent: body.Parent,
- })
- if err != nil {
- writeError(w, http.StatusInternalServerError, "internal_error")
- return
- }
- writeJSON(w, http.StatusCreated, map[string]any{"override": o})
- }
- func (s *server) handleTopologyOverrideUpdate(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- var body struct {
- Kind *string `json:"kind"`
- Name *string `json:"name"`
- Parent *string `json:"parent"`
- Enabled *bool `json:"enabled"`
- }
- if st := readJSONBody(w, r, &body); st != 0 {
- writeBodyError(w, st, "invalid_body", "")
- return
- }
- if body.Kind != nil && *body.Kind != "hypervisor" && *body.Kind != "switch" && *body.Kind != "attach" && *body.Kind != "facet" {
- writeError(w, http.StatusBadRequest, "invalid_kind")
- return
- }
- o, ok := topooverride.Update(s.db.DB, id, topooverride.UpdateInput{
- Kind: body.Kind, Name: body.Name, Parent: body.Parent, Enabled: body.Enabled,
- })
- if !ok {
- writeError(w, http.StatusNotFound, "not_found")
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"override": o})
- }
- func (s *server) handleTopologyOverrideDelete(w http.ResponseWriter, r *http.Request) {
- if !topooverride.Remove(s.db.DB, r.PathValue("id")) {
- writeError(w, http.StatusNotFound, "not_found")
- return
- }
- w.WriteHeader(http.StatusNoContent)
- }
|