Fix IP deduplication and filter link interfaces in JSON
This commit is contained in:
23
link.go
23
link.go
@@ -1,6 +1,9 @@
|
||||
package tendrils
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Link struct {
|
||||
TypeID string `json:"typeid"`
|
||||
@@ -10,6 +13,24 @@ type Link struct {
|
||||
InterfaceB string `json:"interface_b,omitempty"`
|
||||
}
|
||||
|
||||
func (l *Link) MarshalJSON() ([]byte, error) {
|
||||
type linkJSON struct {
|
||||
TypeID string `json:"typeid"`
|
||||
NodeA interface{} `json:"node_a"`
|
||||
InterfaceA string `json:"interface_a,omitempty"`
|
||||
NodeB interface{} `json:"node_b"`
|
||||
InterfaceB string `json:"interface_b,omitempty"`
|
||||
}
|
||||
|
||||
return json.Marshal(linkJSON{
|
||||
TypeID: l.TypeID,
|
||||
NodeA: l.NodeA.WithInterface(l.InterfaceA),
|
||||
InterfaceA: l.InterfaceA,
|
||||
NodeB: l.NodeB.WithInterface(l.InterfaceB),
|
||||
InterfaceB: l.InterfaceB,
|
||||
})
|
||||
}
|
||||
|
||||
func (l *Link) String() string {
|
||||
nameA := l.NodeA.DisplayName()
|
||||
if nameA == "" {
|
||||
|
||||
4
nodes.go
4
nodes.go
@@ -308,6 +308,10 @@ func (n *Nodes) updateNodeInterface(node *Node, nodeID int, mac net.HardwareAddr
|
||||
}
|
||||
iface.IPs.Add(ip)
|
||||
n.ipIndex[ipKey] = nodeID
|
||||
|
||||
if ipOnlyIface, exists := node.Interfaces[ipKey]; exists && ipOnlyIface != iface {
|
||||
delete(node.Interfaces, ipKey)
|
||||
}
|
||||
}
|
||||
|
||||
return added
|
||||
|
||||
19
types.go
19
types.go
@@ -130,6 +130,25 @@ type Node struct {
|
||||
pollTrigger chan struct{}
|
||||
}
|
||||
|
||||
func (n *Node) WithInterface(ifaceKey string) *Node {
|
||||
if ifaceKey == "" {
|
||||
return n
|
||||
}
|
||||
iface, exists := n.Interfaces[ifaceKey]
|
||||
if !exists {
|
||||
return n
|
||||
}
|
||||
return &Node{
|
||||
TypeID: n.TypeID,
|
||||
Names: n.Names,
|
||||
Interfaces: InterfaceMap{ifaceKey: iface},
|
||||
MACTableSize: n.MACTableSize,
|
||||
PoEBudget: n.PoEBudget,
|
||||
IsDanteClockMaster: n.IsDanteClockMaster,
|
||||
DanteTxChannels: n.DanteTxChannels,
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Interface) String() string {
|
||||
var parts []string
|
||||
parts = append(parts, string(i.MAC))
|
||||
|
||||
Reference in New Issue
Block a user