Get virtual interfaces
This commit is contained in:
parent
22017abc1b
commit
a286f94709
5 changed files with 83 additions and 16 deletions
32
internal/cache/datacache.go
vendored
32
internal/cache/datacache.go
vendored
|
@ -13,6 +13,7 @@ func NewDataCache() *DataCache {
|
|||
interfaces: make(map[int]*models.Interface),
|
||||
cables: make(map[int]*models.Cable),
|
||||
vms: make(map[int]*models.VM),
|
||||
vmInterfaces: make(map[int]*models.VMInterface),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,6 +24,7 @@ type DataCache struct {
|
|||
cables map[int]*models.Cable
|
||||
vms map[int]*models.VM
|
||||
arpRecords []*models.ARPRecord
|
||||
vmInterfaces map[int]*models.VMInterface
|
||||
}
|
||||
|
||||
func (d *DataCache) AddDevice(device models.Device) {
|
||||
|
@ -124,19 +126,47 @@ func (d *DataCache) ReconcileData() {
|
|||
for _, iif := range d.interfaces {
|
||||
if r.MacAddress == iif.MacAddress {
|
||||
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 {
|
||||
d.lock.Lock()
|
||||
defer d.lock.Unlock()
|
||||
|
||||
var res []models.UnmonitoredDevice
|
||||
|
||||
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})
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package models
|
|||
|
||||
type ARPRecord struct {
|
||||
Device *Device `json:"-"`
|
||||
VM *VM `json:"-"`
|
||||
Address string `json:"address"`
|
||||
MacAddress string `json:"mac-address"`
|
||||
}
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ const (
|
|||
cablesRoute = "/api/dcim/cables"
|
||||
vmsRoute = "/api/virtualization/virtual-machines"
|
||||
interfaceRoute = "/api/dcim/interfaces"
|
||||
vmInterfaceRoute = "/api/virtualization/interfaces"
|
||||
)
|
||||
|
||||
type vmsResponse struct {
|
||||
|
@ -31,6 +32,10 @@ type interfaceResponse struct {
|
|||
Results []models.Interface `json:"results"`
|
||||
}
|
||||
|
||||
type vmInterfaceResponse struct {
|
||||
Results []models.VMInterface `json:"results"`
|
||||
}
|
||||
|
||||
type NetboxClient struct {
|
||||
httpClt *http.Client
|
||||
netboxBaseURL string
|
||||
|
@ -115,6 +120,21 @@ func (c *NetboxClient) GetVMs() ([]models.VM, error) {
|
|||
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 {
|
||||
return &NetboxClient{
|
||||
httpClt: http.DefaultClient,
|
||||
|
|
9
main.go
9
main.go
|
@ -61,6 +61,15 @@ func main() {
|
|||
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")
|
||||
arpCache, err := mikrotikClt.GetARPRecords()
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in a new issue