Get virtual interfaces

This commit is contained in:
Melora Hugues 2024-09-23 17:34:40 +02:00
parent 22017abc1b
commit a286f94709
5 changed files with 83 additions and 16 deletions

View file

@ -13,6 +13,7 @@ func NewDataCache() *DataCache {
interfaces: make(map[int]*models.Interface), interfaces: make(map[int]*models.Interface),
cables: make(map[int]*models.Cable), cables: make(map[int]*models.Cable),
vms: make(map[int]*models.VM), vms: make(map[int]*models.VM),
vmInterfaces: make(map[int]*models.VMInterface),
} }
} }
@ -23,6 +24,7 @@ type DataCache struct {
cables map[int]*models.Cable cables map[int]*models.Cable
vms map[int]*models.VM vms map[int]*models.VM
arpRecords []*models.ARPRecord arpRecords []*models.ARPRecord
vmInterfaces map[int]*models.VMInterface
} }
func (d *DataCache) AddDevice(device models.Device) { func (d *DataCache) AddDevice(device models.Device) {
@ -124,19 +126,47 @@ func (d *DataCache) ReconcileData() {
for _, iif := range d.interfaces { for _, iif := range d.interfaces {
if r.MacAddress == iif.MacAddress { if r.MacAddress == iif.MacAddress {
r.Device = &iif.Device r.Device = &iif.Device
break
}
}
for _, iif := range d.vmInterfaces {
if r.MacAddress == iif.MacAddress {
r.VM = &iif.VM
break
} }
} }
} }
} }
func (d *DataCache) GetUnmonitoredMachines() []models.UnmonitoredDevice { func (d *DataCache) GetUnmonitoredMachines() []models.UnmonitoredDevice {
d.lock.Lock()
defer d.lock.Unlock()
var res []models.UnmonitoredDevice var res []models.UnmonitoredDevice
for _, r := range d.arpRecords { for _, r := range d.arpRecords {
if r.Device == nil { if r.Device == nil && r.VM == nil {
res = append(res, models.UnmonitoredDevice{Address: r.Address, MacAddress: r.MacAddress}) res = append(res, models.UnmonitoredDevice{Address: r.Address, MacAddress: r.MacAddress})
} }
} }
return res return res
} }
func (d *DataCache) AddVMInterface(iface models.VMInterface) {
d.lock.Lock()
defer d.lock.Unlock()
d.vmInterfaces[iface.ID] = &iface
}
func (d *DataCache) GetVMInterfaces() []*models.VMInterface {
d.lock.Lock()
defer d.lock.Unlock()
res := []*models.VMInterface{}
for _, iif := range d.vmInterfaces {
res = append(res, iif)
}
return res
}

View file

@ -2,6 +2,7 @@ package models
type ARPRecord struct { type ARPRecord struct {
Device *Device `json:"-"` Device *Device `json:"-"`
VM *VM `json:"-"`
Address string `json:"address"` Address string `json:"address"`
MacAddress string `json:"mac-address"` MacAddress string `json:"mac-address"`
} }

View file

@ -143,3 +143,10 @@ func (v VM) Elements() []Element {
}, },
} }
} }
type VMInterface struct {
ID int `json:"id"`
Name string `json:"name"`
MacAddress string `json:"mac_address"`
VM VM `json:"virtual_machine"`
}

View file

@ -13,6 +13,7 @@ const (
cablesRoute = "/api/dcim/cables" cablesRoute = "/api/dcim/cables"
vmsRoute = "/api/virtualization/virtual-machines" vmsRoute = "/api/virtualization/virtual-machines"
interfaceRoute = "/api/dcim/interfaces" interfaceRoute = "/api/dcim/interfaces"
vmInterfaceRoute = "/api/virtualization/interfaces"
) )
type vmsResponse struct { type vmsResponse struct {
@ -31,6 +32,10 @@ type interfaceResponse struct {
Results []models.Interface `json:"results"` Results []models.Interface `json:"results"`
} }
type vmInterfaceResponse struct {
Results []models.VMInterface `json:"results"`
}
type NetboxClient struct { type NetboxClient struct {
httpClt *http.Client httpClt *http.Client
netboxBaseURL string netboxBaseURL string
@ -115,6 +120,21 @@ func (c *NetboxClient) GetVMs() ([]models.VM, error) {
return res.Results, nil return res.Results, nil
} }
func (c *NetboxClient) GetVMInterfaces() ([]models.VMInterface, error) {
var res vmInterfaceResponse
respBody, err := c.queryAPI(vmInterfaceRoute)
if err != nil {
return nil, err
}
if err := json.Unmarshal(respBody, &res); err != nil {
return nil, err
}
return res.Results, nil
}
func NewClient(baseURL, token string) *NetboxClient { func NewClient(baseURL, token string) *NetboxClient {
return &NetboxClient{ return &NetboxClient{
httpClt: http.DefaultClient, httpClt: http.DefaultClient,

View file

@ -61,6 +61,15 @@ func main() {
dataCache.AddInterface(i) dataCache.AddInterface(i)
} }
fmt.Println("Getting virtual interfaces")
vmInterfaces, err := netboxClt.GetVMInterfaces()
if err != nil {
panic(err)
}
for _, i := range vmInterfaces {
dataCache.AddVMInterface(i)
}
fmt.Println("Getting data from ARP cache") fmt.Println("Getting data from ARP cache")
arpCache, err := mikrotikClt.GetARPRecords() arpCache, err := mikrotikClt.GetARPRecords()
if err != nil { if err != nil {