build graph from config

master
Serge Bazanski 2018-10-06 11:31:18 +01:00
parent 88b572d74f
commit 28f4907e7b
3 changed files with 141 additions and 13 deletions

BIN
graph/.graph.go.swp Normal file

Binary file not shown.

123
graph/graph.go Normal file
View File

@ -0,0 +1,123 @@
package graph
import (
"fmt"
confpb "code.hackerspace.pl/q3k/topo/proto/config"
"github.com/golang/glog"
)
type MachinePort struct {
OtherEnd *SwitchPort
Name string
}
type SwitchPort struct {
OtherEnd *MachinePort
Name string
}
type Machine struct {
Name string
Complete bool
Ports map[string]*MachinePort
}
type Switch struct {
Name string
Complete bool
Ports map[string]*SwitchPort
}
type Graph struct {
Switches map[string]*Switch
Machines map[string]*Machine
}
func New() *Graph {
return &Graph{
Switches: make(map[string]*Switch),
Machines: make(map[string]*Machine),
}
}
func (g *Graph) RemoveMachine(name string) {
glog.Infof("Removed machine %q", name)
}
func (g *Graph) RemoveSwitch(name string) {
glog.Infof("Removed switch %q", name)
}
func (g *Graph) LoadConfig(conf *confpb.Config) error {
loadedMachines := make(map[string]bool)
loadedSwitches := make(map[string]bool)
// Add new machines and switches.
for _, machinepb := range conf.Machine {
if machinepb.Name == "" {
return fmt.Errorf("empty machine name")
}
machine, ok := g.Machines[machinepb.Name]
if !ok {
machine = &Machine{
Name: machinepb.Name,
Ports: make(map[string]*MachinePort),
}
for _, portpb := range machinepb.ManagedPort {
machine.Ports[portpb.Name] = &MachinePort{
Name: portpb.Name,
}
}
g.Machines[machinepb.Name] = machine
glog.Infof("Added machine %q with %d managed ports", machine.Name, len(machine.Ports))
}
machine.Complete = false
loadedMachines[machinepb.Name] = true
}
for _, switchpb := range conf.Switch {
if switchpb.Name == "" {
return fmt.Errorf("empty switch name")
}
sw, ok := g.Switches[switchpb.Name]
if !ok {
sw = &Switch{
Name: switchpb.Name,
Ports: make(map[string]*SwitchPort),
}
for _, portpb := range switchpb.ManagedPort {
sw.Ports[portpb.Name] = &SwitchPort{
Name: portpb.Name,
}
}
g.Switches[switchpb.Name] = sw
glog.Infof("Added switch %q with %d managed ports", sw.Name, len(sw.Ports))
}
sw.Complete = false
loadedSwitches[switchpb.Name] = true
}
// Remove old machines and switches.
removeMachines := make(map[string]bool)
removeSwitches := make(map[string]bool)
for name, _ := range g.Switches {
if !loadedSwitches[name] {
removeSwitches[name] = true
}
}
for name, _ := range g.Machines {
if !loadedMachines[name] {
removeMachines[name] = true
}
}
for name, _ := range removeMachines {
g.RemoveMachine(name)
}
for name, _ := range removeSwitches {
g.RemoveSwitch(name)
}
return nil
}

31
main.go
View File

@ -1,17 +1,17 @@
package main
import (
"context"
"flag"
"io/ioutil"
"github.com/digitalocean/go-netbox/netbox"
"github.com/digitalocean/go-netbox/netbox/client"
"github.com/digitalocean/go-netbox/netbox/client/dcim"
"github.com/go-openapi/swag"
//"github.com/digitalocean/go-netbox/netbox"
//"github.com/digitalocean/go-netbox/netbox/client"
//"github.com/digitalocean/go-netbox/netbox/client/dcim"
"github.com/golang/glog"
"github.com/golang/protobuf/proto"
"code.hackerspace.pl/q3k/topo/graph"
confpb "code.hackerspace.pl/q3k/topo/proto/config"
)
@ -38,14 +38,19 @@ func main() {
config := confpb.Config{}
proto.UnmarshalText(string(data), &config)
glog.Infof("%+v", config)
client.DefaultSchemes = []string{"https"}
nb := netbox.NewNetboxWithAPIKey(flagNetboxHost, flagNetboxAPIKey)
req := &dcim.DcimInterfaceConnectionsListParams{
Device: swag.String("bc01n01"),
Context: context.Background(),
gr := graph.New()
err = gr.LoadConfig(&config)
if err != nil {
glog.Exitf("Initial config load failed: %v", err)
}
res, err := nb.Dcim.DcimInterfaceConnectionsList(req, nil)
glog.Infof("%+v, %v", res, err)
//client.DefaultSchemes = []string{"https"}
//nb := netbox.NewNetboxWithAPIKey(flagNetboxHost, flagNetboxAPIKey)
//req := &dcim.DcimInterfaceConnectionsListParams{
// Device: swag.String("bc01n01"),
// Context: context.Background(),
//}
//res, err := nb.Dcim.DcimInterfaceConnectionsList(req, nil)
//glog.Infof("%+v, %v", res, err)
}