Skip to content

Commit

Permalink
add netbox version compatibility check
Browse files Browse the repository at this point in the history
  • Loading branch information
xoc committed Sep 11, 2024
1 parent c3b8b6f commit 6863919
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ coverage.out
config.yml
netbox_sd
bin/
.testing
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = v1.0.0
VERSION = v1.0.1
COMMIT = $(shell git rev-list -1 HEAD | cut -c1-10)
DIRTY = $(shell git diff --quiet || echo '-dirty')
DATE = $(shell date -u +%a,\ %d\ %b\ %Y\ %H:%M:%S\ %Z)
Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,6 @@ metrics (additional to golang specific ones) are exposed:
- netbox_sd_api_status (200, 403, etc)
- netbox_sd_api_duration_seconds

## Changelog
### v1.0.0
- initial open source release

## Noteworthy Mention
Special thanks goes out to [WIIT AG](https://www.wiit.cloud/en/) for open sourcing netbox_sd and netbox-go. This tool
has been developed and used in production for multiple years now internally and WIIT AG was kind enough to release this
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
)

require (
github.com/Masterminds/semver v1.5.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/kr/text v0.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
Expand Down
2 changes: 2 additions & 0 deletions pkg/netbox/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ const (
StatusIPDeprecated string = "DEPRECATED"
StatusIPDHCP string = "DHCP"
StatusIPSLAAC string = "SLAAC"

compatibleNetboxVersion string = ">=3.4.5,<4.0.0"
)
22 changes: 19 additions & 3 deletions pkg/netbox/netbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ package netbox

import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"log"
Expand Down Expand Up @@ -99,6 +100,11 @@ type Name struct {
Name string `json:"name"`
}

// NetboxStatus contains details about a Netbox installation.
type netboxStatus struct {
Version string `json:"netbox-version"`
}

// New creates a new Client to interact with a netbox API. baseURL must point to a valid Netbox installation (without
// /api or /graphql at the end) while token must be a valid Netbox API key. WithTLS enabled TLS for HTTP transport while
// tlsInsecure can be set to allow any certificate to be accepted.
Expand Down Expand Up @@ -191,11 +197,12 @@ func New(baseURL, token, promNamespace string, withTLS bool, tlsInsecure bool) (
// token. If connection and token are okay, nil is returned.
func (client *Client) VerifyConnectivity() error {
var (
resp response
err error
resp response
err error
status netboxStatus
)

resp, err = client.get("/dcim/devices/?limit=1")
resp, err = client.get("/api/status/")
if err != nil {
return fmt.Errorf("failed to query api: %w", err)
}
Expand All @@ -206,6 +213,15 @@ func (client *Client) VerifyConnectivity() error {
return ErrInvalidToken
}

err = json.Unmarshal(resp.RawBody().Bytes(), &status)
if err != nil {
return fmt.Errorf("failed to unmarshal json from response body buffer: %w", err)
}

if !netboxIsCompatible(status.Version) {
return fmt.Errorf("detected incompatible Netbox version: v%s", status.Version)
}

return nil
}

Expand Down
28 changes: 28 additions & 0 deletions pkg/netbox/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package netbox

import (
"fmt"

"github.com/Masterminds/semver"
)

// netboxIsCompatible returns true when a version string of Netbox is supported.
func netboxIsCompatible(version string) bool {
var (
compatibleVersion *semver.Constraints
givenVersion *semver.Version
err error
)

compatibleVersion, err = semver.NewConstraint(compatibleNetboxVersion)
if err != nil {
panic(fmt.Sprintf("could not parse Netbox version constraint '%s'", compatibleNetboxVersion))
}

givenVersion, err = semver.NewVersion(version)
if err != nil {
panic(fmt.Sprintf("could not parse given Netbox version '%s'", version))
}

return compatibleVersion.Check(givenVersion)
}

0 comments on commit 6863919

Please sign in to comment.