forked from hswaw/hscloud
move netbox feed to graph
This commit is contained in:
parent
28f4907e7b
commit
2c6c6d16b9
3 changed files with 54 additions and 14 deletions
10
Gopkg.lock
generated
10
Gopkg.lock
generated
|
@ -25,6 +25,14 @@
|
||||||
revision = "ccb8e960c48f04d6935e72476ae4a51028f9e22f"
|
revision = "ccb8e960c48f04d6935e72476ae4a51028f9e22f"
|
||||||
version = "v9"
|
version = "v9"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:ffe9824d294da03b391f44e1ae8281281b4afc1bdaa9588c9097785e3af10cec"
|
||||||
|
name = "github.com/davecgh/go-spew"
|
||||||
|
packages = ["spew"]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "8991bc29aa16c548c550c7ff78260e27b9ab7c73"
|
||||||
|
version = "v1.1.1"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
branch = "master"
|
branch = "master"
|
||||||
digest = "1:08aeb20c1d146f9254126139f272865927b6db288145f88f0a3baacc24ddfa24"
|
digest = "1:08aeb20c1d146f9254126139f272865927b6db288145f88f0a3baacc24ddfa24"
|
||||||
|
@ -293,7 +301,9 @@
|
||||||
analyzer-name = "dep"
|
analyzer-name = "dep"
|
||||||
analyzer-version = 1
|
analyzer-version = 1
|
||||||
input-imports = [
|
input-imports = [
|
||||||
|
"github.com/davecgh/go-spew/spew",
|
||||||
"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/dcim",
|
"github.com/digitalocean/go-netbox/netbox/client/dcim",
|
||||||
"github.com/golang/glog",
|
"github.com/golang/glog",
|
||||||
"github.com/golang/protobuf/proto",
|
"github.com/golang/protobuf/proto",
|
||||||
|
|
|
@ -1,10 +1,15 @@
|
||||||
package graph
|
package graph
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"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"
|
"github.com/golang/glog"
|
||||||
|
|
||||||
|
confpb "code.hackerspace.pl/q3k/topo/proto/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MachinePort struct {
|
type MachinePort struct {
|
||||||
|
@ -60,6 +65,9 @@ func (g *Graph) LoadConfig(conf *confpb.Config) error {
|
||||||
if machinepb.Name == "" {
|
if machinepb.Name == "" {
|
||||||
return fmt.Errorf("empty machine 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]
|
machine, ok := g.Machines[machinepb.Name]
|
||||||
if !ok {
|
if !ok {
|
||||||
machine = &Machine{
|
machine = &Machine{
|
||||||
|
@ -81,6 +89,12 @@ func (g *Graph) LoadConfig(conf *confpb.Config) error {
|
||||||
if switchpb.Name == "" {
|
if switchpb.Name == "" {
|
||||||
return fmt.Errorf("empty switch 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]
|
sw, ok := g.Switches[switchpb.Name]
|
||||||
if !ok {
|
if !ok {
|
||||||
sw = &Switch{
|
sw = &Switch{
|
||||||
|
@ -121,3 +135,20 @@ func (g *Graph) LoadConfig(conf *confpb.Config) error {
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|
25
main.go
25
main.go
|
@ -1,13 +1,12 @@
|
||||||
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/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
|
|
||||||
|
@ -27,10 +26,12 @@ func init() {
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.StringVar(&flagConfigPath, "config_path", "./topo.pb.text", "Text proto configuration of Topo (per config.proto)")
|
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.StringVar(&flagNetboxAPIKey, "netbox_api_key", "", "Netbox API key")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
data, err := ioutil.ReadFile(flagConfigPath)
|
data, err := ioutil.ReadFile(flagConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Exitf("Could not read config: %v", err)
|
glog.Exitf("Could not read config: %v", err)
|
||||||
|
@ -45,12 +46,10 @@ func main() {
|
||||||
glog.Exitf("Initial config load failed: %v", err)
|
glog.Exitf("Initial config load failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//client.DefaultSchemes = []string{"https"}
|
client.DefaultSchemes = []string{"https"}
|
||||||
//nb := netbox.NewNetboxWithAPIKey(flagNetboxHost, flagNetboxAPIKey)
|
nb := netbox.NewNetboxWithAPIKey(flagNetboxHost, flagNetboxAPIKey)
|
||||||
//req := &dcim.DcimInterfaceConnectionsListParams{
|
err = gr.FeedFromNetbox(ctx, nb)
|
||||||
// Device: swag.String("bc01n01"),
|
if err != nil {
|
||||||
// Context: context.Background(),
|
glog.Exitf("Initial netbox feed failed: %v", err)
|
||||||
//}
|
}
|
||||||
//res, err := nb.Dcim.DcimInterfaceConnectionsList(req, nil)
|
|
||||||
//glog.Infof("%+v, %v", res, err)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue