Explorar o código

fix(routers): keep gateway and agent-only on a partial update (#1937) (#1940)

Nacho hai 4 semanas
pai
achega
9b4a62c676

+ 12 - 9
server-go/internal/httpapi/config.go

@@ -146,11 +146,15 @@ func (s *server) handleListConfigRouters(w http.ResponseWriter, r *http.Request)
 }
 
 type routerInput struct {
-	Name      *string `json:"name"`
-	Host      *string `json:"host"`
-	Type      string  `json:"type"`
-	Gateway   bool    `json:"gateway"`
-	AgentOnly bool    `json:"agent_only"`
+	Name *string `json:"name"`
+	Host *string `json:"host"`
+	Type string  `json:"type"`
+	// Gateway y AgentOnly son punteros por el mismo motivo que el resto:
+	// ausente significa "no lo toques". Con bool a secas, un PUT parcial
+	// (por ejemplo el que solo cambia inventory_mode) los reiniciaba a
+	// false y tiraba el gateway o el agent-only de la unidad (#1937).
+	Gateway   *bool `json:"gateway"`
+	AgentOnly *bool `json:"agent_only"`
 	// InventoryMode (#1917): "auto" | "agent" | "ssh". Manda sobre agent_only.
 	InventoryMode *string `json:"inventory_mode"`
 	// Disabled (issue #1085): pausa la unidad (nil = no tocar).
@@ -314,7 +318,8 @@ func (s *server) handleAddConfigRouter(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 	created, err := routerstore.AddRouter(s.db.DB, routerstore.AddInput{
-		Name: name, Host: host, Type: typ, IsGateway: in.Gateway, AgentOnly: in.AgentOnly,
+		Name: name, Host: host, Type: typ,
+		IsGateway: in.Gateway != nil && *in.Gateway, AgentOnly: in.AgentOnly != nil && *in.AgentOnly,
 		InventoryMode:  invMode,
 		FirmwareTarget: firmwareTarget,
 		SnmpEnabled:    snmpEnabled, SnmpCommunity: snmpCommunity, SnmpPort: snmpPort, SnmpPollInterval: snmpInterval,
@@ -412,8 +417,6 @@ func (s *server) handleUpdateConfigRouter(w http.ResponseWriter, r *http.Request
 		}
 		typ = &t
 	}
-	gw := in.Gateway
-	ao := in.AgentOnly
 	var invMode *string
 	if in.InventoryMode != nil {
 		m, msg := validateInventoryMode(in.InventoryMode)
@@ -471,7 +474,7 @@ func (s *server) handleUpdateConfigRouter(w http.ResponseWriter, r *http.Request
 	}
 	updated, ok := routerstore.UpdateRouter(s.db.DB, id, routerstore.UpdateInput{
 		Name: name, Host: host, Type: typ,
-		IsGateway: &gw, AgentOnly: &ao,
+		IsGateway: in.Gateway, AgentOnly: in.AgentOnly,
 		InventoryMode:  invMode,
 		Disabled:       in.Disabled,
 		FirmwareTarget: firmwareTarget,

+ 90 - 0
server-go/internal/httpapi/config_routers_update_1937_test.go

@@ -0,0 +1,90 @@
+package httpapi
+
+// #1937: un PUT parcial reiniciaba gateway y agent_only porque los dos campos
+// del body eran bool y "ausente" se leia como false. El resto de campos ya son
+// punteros (nil = no tocar) y el store tambien los espera asi.
+
+import (
+	"net/http/httptest"
+	"path/filepath"
+	"strings"
+	"testing"
+
+	"github.com/gnacho/netpulse/server-go/internal/adapters"
+	"github.com/gnacho/netpulse/server-go/internal/db"
+	"github.com/gnacho/netpulse/server-go/internal/routerstore"
+)
+
+func newRouterUpdateServer(t *testing.T) *server {
+	t.Helper()
+	d, err := db.Open(filepath.Join(t.TempDir(), "test.db"))
+	if err != nil {
+		t.Fatalf("db.Open: %v", err)
+	}
+	t.Cleanup(func() { _ = d.Close() })
+	return &server{db: d}
+}
+
+func putConfigRouter(t *testing.T, s *server, id, body string) *httptest.ResponseRecorder {
+	t.Helper()
+	req := httptest.NewRequest("PUT", "/api/config/routers/"+id, strings.NewReader(body))
+	req.Header.Set("Content-Type", "application/json")
+	req.SetPathValue("id", id)
+	rec := httptest.NewRecorder()
+	s.handleUpdateConfigRouter(rec, req)
+	if rec.Code != 200 {
+		t.Fatalf("status = %d, body %s", rec.Code, rec.Body.String())
+	}
+	return rec
+}
+
+func routerByID(t *testing.T, s *server, id string) adapters.RouterConfig {
+	t.Helper()
+	for _, rt := range routerstore.ListRouters(s.db.DB) {
+		if rt.ID == id {
+			return rt
+		}
+	}
+	t.Fatalf("router %s no encontrado", id)
+	return adapters.RouterConfig{}
+}
+
+func TestUpdateRouterKeepsFlagsItWasNotSent(t *testing.T) {
+	s := newRouterUpdateServer(t)
+	gw, err := routerstore.AddRouter(s.db.DB, routerstore.AddInput{
+		Name: "gateway", Host: "10.0.0.1", Type: "openwrt", IsGateway: true,
+	})
+	if err != nil {
+		t.Fatalf("AddRouter: %v", err)
+	}
+	ap, err := routerstore.AddRouter(s.db.DB, routerstore.AddInput{
+		Name: "ap", Host: "10.0.0.2", Type: "openwrt", AgentOnly: true,
+	})
+	if err != nil {
+		t.Fatalf("AddRouter: %v", err)
+	}
+
+	// El PUT que solo cambia el modo de inventario no debe tocar nada mas.
+	putConfigRouter(t, s, gw.ID, `{"inventory_mode":"ssh"}`)
+	if got := routerByID(t, s, gw.ID); !got.IsGateway {
+		t.Fatal("el PUT parcial demovio el gateway")
+	}
+	putConfigRouter(t, s, ap.ID, `{"name":"ap2"}`)
+	if got := routerByID(t, s, ap.ID); !got.AgentOnly {
+		t.Fatal("el PUT parcial se llevo agent_only")
+	}
+
+	// Enviado a proposito, el flag sigue mandando: mover el gateway lo quita
+	// de la unidad anterior.
+	putConfigRouter(t, s, ap.ID, `{"gateway":true}`)
+	if got := routerByID(t, s, gw.ID); got.IsGateway {
+		t.Fatal("el gateway anterior sigue marcado")
+	}
+	if got := routerByID(t, s, ap.ID); !got.IsGateway {
+		t.Fatal("el gateway nuevo no quedo marcado")
+	}
+	putConfigRouter(t, s, ap.ID, `{"gateway":false}`)
+	if got := routerByID(t, s, ap.ID); got.IsGateway {
+		t.Fatal("gateway:false no se aplico")
+	}
+}