Răsfoiți Sursa

feat(vlans): name the VLANs the bridge output only numbers (#1760)

`bridge vlan show` gives ids and flags and nothing else, so the VLAN panel reads as a list of ports with numbers. LuCI does keep a name per VLAN, and the agent already parses those labels for the port names (#258), but nothing joined them with the bridge output. This is the first piece of #1757.

The join happens in `buildRouter`, where both arrive in the same poll, so one function covers the agent path and the SSH path, and it works with agents already deployed: the labels are in the payload since #258, they were just unused.

A VLAN without a label keeps no name rather than an invented one, and the payload the registry holds is not written to, since the copy is made only when there is something to join.

Tests cover the join, the unlabelled VLAN, the flags surviving the copy, the payload staying untouched and the no-labels case returning the same slice.

Refs #1757
Nacho 1 lună în urmă
părinte
comite
c7aa7d8bfc

+ 3 - 0
agent/probe/probe.go

@@ -1568,6 +1568,9 @@ type VlanEntry struct {
 	ID     int  `json:"id"`
 	Tagged bool `json:"tagged"`
 	PVID   bool `json:"pvid"`
+	// Name es la etiqueta que LuCI da a la VLAN (uci show network, #258),
+	// rellenada por el servidor al juntarla con VlanLabels (#1757).
+	Name string `json:"name,omitempty"`
 }
 
 // VlanPort: puerto del bridge con sus VLANs (issue #315).

+ 8 - 1
app/src/components/routers/VlanPanel.tsx

@@ -99,7 +99,14 @@ export function VlanPanel({ vlans }: { vlans: VlanPort[] }) {
                             <span className="pl-[22px] font-mono text-mono-sm text-text-muted">{vp.port}</span>
                           )}
                         </td>
-                        <td className="py-2 pr-4 font-mono text-mono-sm text-text-primary">{v.id}</td>
+                        <td className="py-2 pr-4 font-mono text-mono-sm text-text-primary">
+                          {v.id}
+                          {/* #1757: the name LuCI gives the VLAN, so a row
+                              reads as a network and not only as a number. */}
+                          {v.name && (
+                            <span className="ml-2 font-sans text-[11px] text-text-muted">{v.name}</span>
+                          )}
+                        </td>
                         <td className="py-2 pr-4"><VlanBadge tagged={v.tagged} /></td>
                         <td className="py-2">
                           {v.pvid ? (

+ 2 - 0
app/src/data/types.ts

@@ -441,6 +441,8 @@ export interface VlanEntry {
   id: number
   tagged: boolean
   pvid: boolean
+  /** LuCI's name for the VLAN (uci show network), when the router has one. */
+  name?: string
 }
 
 /** One internet connection of a router that has several. Read-only: the

+ 23 - 1
server-go/internal/adapters/live.go

@@ -1501,6 +1501,28 @@ func allZeroF(v []float64) bool {
 	return true
 }
 
+// vlanPortsWithNames joins the VLAN numbers from `bridge vlan show` with the
+// names LuCI keeps for them (uci show network, #258). The bridge output only
+// carries numbers and both arrive in the same poll, so the join is by id and
+// only where a label exists (#1757).
+func vlanPortsWithNames(ports []VlanPort, luci *probe.LuCILabels) []VlanPort {
+	if luci == nil || len(luci.VlanLabels) == 0 {
+		return ports
+	}
+	out := make([]VlanPort, len(ports))
+	for i, vp := range ports {
+		out[i] = vp
+		out[i].Vlans = make([]VlanEntry, len(vp.Vlans))
+		copy(out[i].Vlans, vp.Vlans)
+		for j := range out[i].Vlans {
+			if name := luci.VlanLabels[strconv.Itoa(out[i].Vlans[j].ID)]; name != "" {
+				out[i].Vlans[j].Name = name
+			}
+		}
+	}
+	return out
+}
+
 func (l *Live) buildRouter(p *routerPolled, history []histPoint) Router {
 	l.mu.Lock()
 	gw := l.gatewayCfg
@@ -4507,7 +4529,7 @@ func (l *Live) GetRouterDetail(ctx context.Context, id string) (*RouterDetail, e
 	}
 	var vlans []VlanPort
 	if p != nil && len(p.vlans) > 0 {
-		vlans = p.vlans
+		vlans = vlanPortsWithNames(p.vlans, p.luci)
 	}
 	var multiWan *MultiWanInfo
 	if p != nil {

+ 47 - 0
server-go/internal/adapters/vlan_names_test.go

@@ -0,0 +1,47 @@
+package adapters
+
+// FORK: #1757 - the VLAN numbers from `bridge vlan show` get the names LuCI
+// keeps for them.
+
+import (
+	"testing"
+
+	"github.com/gnacho/netpulse/agent/probe"
+)
+
+func TestVlanPortsCarryTheLuCINames(t *testing.T) {
+	ports := []VlanPort{
+		{Port: "lan1", Vlans: []VlanEntry{{ID: 1, PVID: true}, {ID: 20, Tagged: true}}},
+		{Port: "lan2", Vlans: []VlanEntry{{ID: 30}}},
+	}
+	luci := &probe.LuCILabels{VlanLabels: map[string]string{"1": "LAN", "20": "IoT"}}
+
+	got := vlanPortsWithNames(ports, luci)
+	if len(got) != 2 || len(got[0].Vlans) != 2 {
+		t.Fatalf("shape: %+v", got)
+	}
+	if got[0].Vlans[0].Name != "LAN" || got[0].Vlans[1].Name != "IoT" {
+		t.Errorf("names: %+v", got[0].Vlans)
+	}
+	// A VLAN without a label keeps no name: an invented one would be worse
+	// than the number alone.
+	if got[1].Vlans[0].Name != "" {
+		t.Errorf("unlabelled VLAN got %q", got[1].Vlans[0].Name)
+	}
+	// The flags and the id survive the copy.
+	if !got[0].Vlans[0].PVID || !got[0].Vlans[1].Tagged || got[0].Vlans[1].ID != 20 {
+		t.Errorf("flags: %+v", got[0].Vlans)
+	}
+	// The payload the registry holds is not written to.
+	if ports[0].Vlans[0].Name != "" {
+		t.Fatalf("the payload was mutated: %+v", ports[0].Vlans)
+	}
+
+	// Without labels there is nothing to join, so the same slice comes back.
+	if same := vlanPortsWithNames(ports, nil); &same[0] != &ports[0] {
+		t.Error("no labels: a copy was made for nothing")
+	}
+	if same := vlanPortsWithNames(ports, &probe.LuCILabels{}); &same[0] != &ports[0] {
+		t.Error("empty labels: a copy was made for nothing")
+	}
+}