Add topology endpoint and checklist tracking in spec.

Expose nodes/edges topology view and convert technical spec progress items to checkbox status.

Made-with: Cursor
This commit is contained in:
Andrey Lutsenko
2026-04-09 22:02:33 +10:00
parent 9e678fcfc7
commit ab94409a52
3 changed files with 128 additions and 29 deletions
+91
View File
@@ -25,6 +25,20 @@ type LinkView struct {
ResolvedBy string `json:"resolved_by"`
}
type TopologyNode struct {
ID string `json:"id"`
IP string `json:"ip"`
Name string `json:"name,omitempty"`
}
type TopologyEdge struct {
SourceIP string `json:"source_ip"`
SourcePort string `json:"source_port,omitempty"`
TargetIP string `json:"target_ip,omitempty"`
TargetName string `json:"target_name,omitempty"`
TargetPort string `json:"target_port,omitempty"`
}
func NewHandler(store scans.Store, run *scans.Runner) *Handler {
return &Handler{store: store, run: run}
}
@@ -40,6 +54,7 @@ func (h *Handler) Routes() http.Handler {
mux.HandleFunc("GET /api/scans/{id}/snmp", h.getScanSNMP)
mux.HandleFunc("GET /api/scans/{id}/lldp", h.getScanLLDP)
mux.HandleFunc("GET /api/scans/{id}/links", h.getScanLinks)
mux.HandleFunc("GET /api/scans/{id}/topology", h.getScanTopology)
return withJSON(mux)
}
@@ -276,6 +291,82 @@ func (h *Handler) getScanLinks(w http.ResponseWriter, r *http.Request) {
})
}
func (h *Handler) getScanTopology(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if id == "" {
writeError(w, http.StatusBadRequest, "scan id is required")
return
}
if _, ok := h.store.GetScan(id); !ok {
writeError(w, http.StatusNotFound, "scan not found")
return
}
limit := 5000
if raw := r.URL.Query().Get("limit"); raw != "" {
n, err := strconv.Atoi(raw)
if err != nil || n <= 0 || n > 50000 {
writeError(w, http.StatusBadRequest, "limit must be between 1 and 50000")
return
}
limit = n
}
snmpItems := h.store.ListSNMPResults(id, 10000)
lldpItems := h.store.ListLLDPResults(id, limit)
sysNameToIP := make(map[string]string, len(snmpItems))
nodesByIP := make(map[string]TopologyNode, len(snmpItems))
for _, s := range snmpItems {
nameNorm := strings.TrimSpace(strings.ToLower(s.SysName))
if nameNorm != "" {
if _, exists := sysNameToIP[nameNorm]; !exists {
sysNameToIP[nameNorm] = s.IP
}
}
nodesByIP[s.IP] = TopologyNode{
ID: s.IP,
IP: s.IP,
Name: s.SysName,
}
}
edges := make([]TopologyEdge, 0, len(lldpItems))
for _, l := range lldpItems {
targetIP := ""
targetNameNorm := strings.TrimSpace(strings.ToLower(l.RemoteSysName))
if targetNameNorm != "" {
targetIP = sysNameToIP[targetNameNorm]
}
if _, exists := nodesByIP[l.IP]; !exists {
nodesByIP[l.IP] = TopologyNode{ID: l.IP, IP: l.IP}
}
if targetIP != "" {
if _, exists := nodesByIP[targetIP]; !exists {
nodesByIP[targetIP] = TopologyNode{ID: targetIP, IP: targetIP, Name: l.RemoteSysName}
}
}
edges = append(edges, TopologyEdge{
SourceIP: l.IP,
SourcePort: l.LocalPortNum,
TargetIP: targetIP,
TargetName: l.RemoteSysName,
TargetPort: l.RemotePortID,
})
}
nodes := make([]TopologyNode, 0, len(nodesByIP))
for _, n := range nodesByIP {
nodes = append(nodes, n)
}
writeJSON(w, http.StatusOK, map[string]any{
"nodes": nodes,
"edges": edges,
})
}
func withJSON(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")