Skip to content

Commit

Permalink
main,netbox: address linter concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
the-maldridge committed Jan 17, 2023
1 parent 0687332 commit 43a771e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
6 changes: 2 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ import (
"github.com/resinstack/netbox-dnsmasq/netbox"
)

// DHCPHost contains the information required to generate a matching
// line in a dhcp configuration daemon.
type DHCPHost struct {
DeviceID int64
HWAddr []string
Name string
Addr string
}

func strPtr(s string) *string {
return &s
}

func main() {
if _, verbose := os.LookupEnv("VERBOSE"); !verbose {
log.SetOutput(io.Discard)
Expand Down
9 changes: 9 additions & 0 deletions netbox/netbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import (
"time"
)

// Client is a minimal client to the netbox API suitable for pulling
// information about interfaces only.
type Client struct {
baseURL *url.URL
httpClient *http.Client

token string
}

// NewClient creates a client connected to the specified netbox
// server.
func NewClient(opts ...Option) (*Client, error) {
x := Client{
baseURL: &url.URL{
Expand All @@ -34,6 +38,7 @@ func NewClient(opts ...Option) (*Client, error) {
return &x, nil
}

// ListDevices searches for netboxes that match the given options.
func (nb *Client) ListDevices(site string) ([]Device, error) {
queryURL := *nb.baseURL
queryURL.Path = "/api/dcim/devices/"
Expand Down Expand Up @@ -87,6 +92,9 @@ func (nb *Client) ListDevices(site string) ([]Device, error) {
return devices, nil
}

// ListInterfaces returns a list of interfaces for a given device that
// have a non-null MAC address, and that are not management-only
// interfaces.
func (nb *Client) ListInterfaces(deviceID int64) ([]Interface, error) {
queryURL := *nb.baseURL
queryURL.Path = "/api/dcim/interfaces/"
Expand All @@ -98,6 +106,7 @@ func (nb *Client) ListInterfaces(deviceID int64) ([]Interface, error) {
queryVals := url.Values{}
queryVals.Add("device_id", fmt.Sprintf("%d", deviceID))
queryVals.Add("mac_address__n", "null")
queryVals.Add("mgmt_only", "false")

queryURL.RawQuery = queryVals.Encode()

Expand Down
7 changes: 7 additions & 0 deletions netbox/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import (
"net/url"
)

// Option handles variadic configuration parameters passed to the
// client initializer.
type Option func(*Client) error

// WithNetBoxURL sets the URL, including the protocol and port (if
// specified) for the netbox installation.
func WithNetBoxURL(netboxURL string) Option {
return func(c *Client) error {
u, err := url.Parse(netboxURL)
Expand All @@ -17,6 +21,9 @@ func WithNetBoxURL(netboxURL string) Option {
}
}

// WithToken sets the token used for requests. This should generally
// be a read-only token as there is no reason this needs any kind of
// elevated permissions.
func WithToken(token string) Option {
return func(c *Client) error {
c.token = token
Expand Down
8 changes: 8 additions & 0 deletions netbox/type.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package netbox

// Address is an embedded type for json deserialization from the
// netbox API.
type Address struct {
Address string
}

// Device represents the minimum information we wish to retreive from
// the netbox devices API as opposed to using the full fat OpenAPI
// client.
type Device struct {
ID int64
Name string `json:"name"`
PrimaryIPv4 Address `json:"primary_ip4"`
}

// Interface represents the minimum informatino we wish to retreive
// from the netbox interfaces API as opposed to using the full fat
// OpenAPI client.
type Interface struct {
ID int64
Name string
Expand Down

0 comments on commit 43a771e

Please sign in to comment.