From 28f4907e7bf9465b1144d6db8a64d44f4ff8b5d5 Mon Sep 17 00:00:00 2001 From: Serge Bazanski Date: Sat, 6 Oct 2018 11:31:18 +0100 Subject: [PATCH] build graph from config --- graph/.graph.go.swp | Bin 0 -> 12288 bytes graph/graph.go | 123 ++++++++++++++++++++++++++++++++++++++++++++ main.go | 31 ++++++----- 3 files changed, 141 insertions(+), 13 deletions(-) create mode 100644 graph/.graph.go.swp create mode 100644 graph/graph.go diff --git a/graph/.graph.go.swp b/graph/.graph.go.swp new file mode 100644 index 0000000000000000000000000000000000000000..14109d85b526feabcaa5c7b06a5e1a03142fcf23 GIT binary patch literal 12288 zcmeI2O>7%Q6vwBaw%O1E0&YDpSx96@#x98jB@iSORj8r~RiULKio#|+Uc0e(HoI#~ z6qUjS!KI}Y7mi#?5e38z2~nvMmr5WJJ|x72pk6psLV+76{%>~HUORC8M}VrtJ&+T53$$Ej4h2XQtP&t@cm|OTPw!igu~K_ zP@WZT9F?k(TbIpp$&;lhbW5%*i(=Jr7eyEajw^~my+ro03}nd*ouHb$7CpIxP{zb4 zU=+wI(2OgO9XZIx#~xQp{SkYJKls3@tcCe(6fg=H1&jhl0i%FXz$jo8Fbe$76^Mg9 z>^#PJmmcSee&5scuD_Z$qkvJsC}0#Y3K#{90!9I&fKk9GU=%P47zOS?1swdJ|K{C{ zt?otg`2Tj2;J_T37$KY*n29&@7@Yg=* z3BCh2!4>cxI14Jk1VC_NVa zqojrurAHs=)^HX@uHgi;Q5@EMZ*D=#I@IkRD+%1kuvFqFl`dS?%P>+3N8IttJW}%| zqN18t%SzNq)0s`WgN5Quw~$e*I3*iFUBm+FR-Af7@+xh-y7Y?2{EDpP?B~jmTF2nS zOB}(fe3(-39S=Q$4CBZyz(NHm@VfAG_;P~U^B5u(R{vz87FnpM`qiR5CzM~Ebb(B) z)aNWk%nWBFaZd4|yrqg@zM${#lx$mu=_GNor>RPVt9&Om$UoZgo=@^f($k7$GmKi$ zQD;MrsZ!;liV?L693L^geYL<1x-QkKtTf`{(J+)Dra&};csZQ{S`IeJ3is_XQBHhv zlG`?gqh$aE(%y~+uJkKednRyC74rSy}+Nn6xxq}4O6 zt9@4YX0p>Q7|4!lsLTC^Leg=ztE5gAyDX)Q|L$7qcvMQzXmk@*l)kN}q_jb&uC!Uy zs}j6zU07+TY-Q;-t@MV{YIpb4md=5S3mX=;4(^Lq|J|y4ry^f{cE9c_?Qd9V`Yz9T zeDryG6gbb1cHd42A|);YbyBg(l&0joSXTjDb?N4CcBpIQe~DTCpI%ny2wiZ7#LB)tt7*o5v^Qa zYpI8mhQvg*kFrtS$gDI{S8vkD;^jba<((92y0rO{>}sWNas##FKx-dSbYL=dHgLKU zl@qT?gzenwEO+5o-_h-uR%kW@>&3V#!lO8cQyo+UHQXdI`o3cyom-NzWwOrr!2WN0 zIEb4IMOQZPaC{v<%Su$iHnWl%=k}J*$_Js0B_5}zwP8bN8wZZfYK?%B%`t|D>lbmp JbM;I;`wx2{x^Msh literal 0 HcmV?d00001 diff --git a/graph/graph.go b/graph/graph.go new file mode 100644 index 00000000..35b1f230 --- /dev/null +++ b/graph/graph.go @@ -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 + +} diff --git a/main.go b/main.go index 7d213f37..ce79ca78 100644 --- a/main.go +++ b/main.go @@ -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) }