diff --git a/Gopkg.lock b/Gopkg.lock index cebc34e8..ff902dae 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -25,6 +25,14 @@ revision = "ccb8e960c48f04d6935e72476ae4a51028f9e22f" version = "v9" +[[projects]] + digest = "1:ffe9824d294da03b391f44e1ae8281281b4afc1bdaa9588c9097785e3af10cec" + name = "github.com/davecgh/go-spew" + packages = ["spew"] + pruneopts = "UT" + revision = "8991bc29aa16c548c550c7ff78260e27b9ab7c73" + version = "v1.1.1" + [[projects]] branch = "master" digest = "1:08aeb20c1d146f9254126139f272865927b6db288145f88f0a3baacc24ddfa24" @@ -293,7 +301,9 @@ analyzer-name = "dep" analyzer-version = 1 input-imports = [ + "github.com/davecgh/go-spew/spew", "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", diff --git a/graph/graph.go b/graph/graph.go index 35b1f230..1fdb5ab2 100644 --- a/graph/graph.go +++ b/graph/graph.go @@ -1,10 +1,15 @@ package graph import ( + "context" "fmt" - confpb "code.hackerspace.pl/q3k/topo/proto/config" + "github.com/davecgh/go-spew/spew" + "github.com/digitalocean/go-netbox/netbox/client" + "github.com/digitalocean/go-netbox/netbox/client/dcim" "github.com/golang/glog" + + confpb "code.hackerspace.pl/q3k/topo/proto/config" ) type MachinePort struct { @@ -60,6 +65,9 @@ func (g *Graph) LoadConfig(conf *confpb.Config) error { if machinepb.Name == "" { return fmt.Errorf("empty machine name") } + if loadedMachines[machinepb.Name] { + return fmt.Errorf("duplicate machine name: %v", machinepb.Name) + } machine, ok := g.Machines[machinepb.Name] if !ok { machine = &Machine{ @@ -81,6 +89,12 @@ func (g *Graph) LoadConfig(conf *confpb.Config) error { if switchpb.Name == "" { return fmt.Errorf("empty switch name") } + if loadedSwitches[switchpb.Name] { + return fmt.Errorf("duplicate switch name: %v", switchpb.Name) + } + if loadedMachines[switchpb.Name] { + return fmt.Errorf("switch name collides with machine name: %v", switchpb.Name) + } sw, ok := g.Switches[switchpb.Name] if !ok { sw = &Switch{ @@ -121,3 +135,20 @@ func (g *Graph) LoadConfig(conf *confpb.Config) error { return nil } + +func (g *Graph) FeedFromNetbox(ctx context.Context, nb *client.NetBox) error { + for _, machine := range g.Machines { + req := &dcim.DcimInterfaceConnectionsListParams{ + Device: &machine.Name, + Context: ctx, + } + res, err := nb.Dcim.DcimInterfaceConnectionsList(req, nil) + if err != nil { + return fmt.Errorf("while querying information about %q: %v", machine.Name, err) + } + for _, connection := range res.Payload.Results { + glog.Info(spew.Sdump(connection)) + } + } + return nil +} diff --git a/main.go b/main.go index ce79ca78..b97e1718 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,12 @@ 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/digitalocean/go-netbox/netbox" + "github.com/digitalocean/go-netbox/netbox/client" "github.com/golang/glog" "github.com/golang/protobuf/proto" @@ -27,10 +26,12 @@ func init() { func main() { flag.StringVar(&flagConfigPath, "config_path", "./topo.pb.text", "Text proto configuration of Topo (per config.proto)") - flag.StringVar(&flagNetboxHost, "netbox_host", "nebtox.bgp.wtf", "Netbox host") + flag.StringVar(&flagNetboxHost, "netbox_host", "netbox.bgp.wtf", "Netbox host") flag.StringVar(&flagNetboxAPIKey, "netbox_api_key", "", "Netbox API key") flag.Parse() + ctx := context.Background() + data, err := ioutil.ReadFile(flagConfigPath) if err != nil { glog.Exitf("Could not read config: %v", err) @@ -45,12 +46,10 @@ func main() { glog.Exitf("Initial config load failed: %v", 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) + client.DefaultSchemes = []string{"https"} + nb := netbox.NewNetboxWithAPIKey(flagNetboxHost, flagNetboxAPIKey) + err = gr.FeedFromNetbox(ctx, nb) + if err != nil { + glog.Exitf("Initial netbox feed failed: %v", err) + } }