Skip to content
/ go-netbox Public
forked from smutel/go-netbox

Go library to interact with NetBox IPAM and DCIM service.

License

Notifications You must be signed in to change notification settings

amhn/go-netbox

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-netbox

Lisence Conventional Commits Build Status

Go library to interact with NetBox IPAM and DCIM service.

Compatibility with Netbox

The version for Netbox and go-netbox will the same except for the last digit.

Example:

  • go-netbox v1.10.x is working with Netbox v1.10
  • go-netbox v1.11.x is working with Netbox v1.11

Using the client

The github.com/smutel/go-netbox/v3/netbox package has some convenience functions for creating clients with the most common configurations you likely need while connecting to NetBox. NewNetboxAt allows you to specify a hostname (including port, if you need it), and NewNetboxWithAPIKey allows you to specify both a hostname:port and API token.

import (
    "github.com/smutel/go-netbox/v3/netbox"
)
...
    c := netbox.NewNetboxAt("your.netbox.host:8000")
    // OR
    c := netbox.NewNetboxWithAPIKey("your.netbox.host:8000", "your_netbox_token")

If you specify the API key, you do not need to pass an additional authInfo to operations that need authentication, and can pass nil:

    c.Dcim.DcimDeviceTypesCreate(createRequest, nil)

If you connect to netbox via HTTPS you have to create an HTTPS configured transport:

package main

import (
	"os"

	httptransport "github.com/go-openapi/runtime/client"
	"github.com/smutel/go-netbox/v3/netbox/client"
	"github.com/smutel/go-netbox/v3/netbox/client/dcim"

	log "github.com/sirupsen/logrus"
)

func main() {
	token := os.Getenv("NETBOX_TOKEN")
	if token == "" {
		log.Fatalf("Please provide netbox API token via env var NETBOX_TOKEN")
	}

	netboxHost := os.Getenv("NETBOX_HOST")
	if netboxHost == "" {
		log.Fatalf("Please provide netbox host via env var NETBOX_HOST")
	}

	transport := httptransport.New(netboxHost, client.DefaultBasePath, []string{"https"})
	transport.DefaultAuthentication = httptransport.APIKeyAuth("Authorization", "header", "Token "+token)

	c := client.New(transport, nil)

	req := dcim.NewDcimSitesListParams()
	res, err := c.Dcim.DcimSitesList(req, nil)
	if err != nil {
		log.Fatalf("Cannot get sites list: %v", err)
	}
	log.Infof("res: %v", res)
}

How to contribute to this project

  • To contribute to this project, please follow the conventional commits rules.
  • Most of the code of this project will be generated using the swagger spec of Netbox and the go-swagger program.
  • You can change the behavior of the generated library by pushing patchs in the patchs folder.
  • The best is to see if the bug is due to a wrong swagger definition and to report this bug to the Netbox project.
  • If the bug is due to the go-swagger program the best is to create a bug here go-swagger.

How to test your work locally

Requirements

  • docker
  • docker-compose
  • swagger installed somewhere (/usr/local/bin)
  • netbox-docker project installed somewhere

Installing the go-netbox

$ # Install jsonlint
$ apt-get install python3-demjson
$ mkdir -p ~/go/src/github.com/smutel
$ cd ~/go/src/github.com/smutel
$ git clone git@github.com:smutel/go-netbox.git
$ export GITHUB_WORKSPACE=~/go/src

Regenerating the library

$ cd ~/go/src/github.com/smutel/go-netbox/utils
$ ./netbox_generate_client

About

Go library to interact with NetBox IPAM and DCIM service.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell 100.0%