mirror of https://gerrit.hackerspace.pl/hscloud
build graph from config
parent
88b572d74f
commit
28f4907e7b
Binary file not shown.
|
@ -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
31
main.go
|
@ -1,17 +1,17 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"flag"
|
"flag"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
||||||
"github.com/digitalocean/go-netbox/netbox"
|
//"github.com/digitalocean/go-netbox/netbox"
|
||||||
"github.com/digitalocean/go-netbox/netbox/client"
|
//"github.com/digitalocean/go-netbox/netbox/client"
|
||||||
"github.com/digitalocean/go-netbox/netbox/client/dcim"
|
//"github.com/digitalocean/go-netbox/netbox/client/dcim"
|
||||||
"github.com/go-openapi/swag"
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
|
|
||||||
|
"code.hackerspace.pl/q3k/topo/graph"
|
||||||
confpb "code.hackerspace.pl/q3k/topo/proto/config"
|
confpb "code.hackerspace.pl/q3k/topo/proto/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -38,14 +38,19 @@ func main() {
|
||||||
|
|
||||||
config := confpb.Config{}
|
config := confpb.Config{}
|
||||||
proto.UnmarshalText(string(data), &config)
|
proto.UnmarshalText(string(data), &config)
|
||||||
glog.Infof("%+v", config)
|
|
||||||
|
|
||||||
client.DefaultSchemes = []string{"https"}
|
gr := graph.New()
|
||||||
nb := netbox.NewNetboxWithAPIKey(flagNetboxHost, flagNetboxAPIKey)
|
err = gr.LoadConfig(&config)
|
||||||
req := &dcim.DcimInterfaceConnectionsListParams{
|
if err != nil {
|
||||||
Device: swag.String("bc01n01"),
|
glog.Exitf("Initial config load failed: %v", err)
|
||||||
Context: context.Background(),
|
|
||||||
}
|
}
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue