Skip to content

Commit

Permalink
Helper functions for most common configurations.
Browse files Browse the repository at this point in the history
  • Loading branch information
davcamer committed Mar 15, 2018
1 parent c01d6b1 commit ed36be4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 86 deletions.
6 changes: 4 additions & 2 deletions Makefile
@@ -1,6 +1,8 @@

generate:
swagger generate client --target=./netbox --spec=./swagger.json --copyright-file=./copyright_header.txt

clean:
rm -rf netbox/*
rm -rf netbox/client netbox/models

integration:
go test ./... -tags=integration
51 changes: 22 additions & 29 deletions README.md
Expand Up @@ -9,43 +9,36 @@ This package assumes you are using NetBox 2.0, as the NetBox 1.0 API no longer e
Using the client
================

The `github.com/go-netbox/netbox/client` package is the entry point for using the client. By default, the client will
connect to an api at `http://localhost:8000/api`. The simplest possible usage looks like:
The `github.com/go-netbox/netbox` package has some convenience functions for creating clients with the most common
configurations you are likely to 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.
```golang
// Passing nil to this method results in all default values
c := client.NewHTTPClient(nil)

... work with the returned client ...
import (
"github.com/digitalocean/go-netbox/netbox"
)
...
c := netbox.NewNetboxAt("your.netbox.host:8000")
// OR
c := netbox.NewNetboxWithAPIKey("your.netbox.host:8000", "your_netbox_token")
```

A more likely scenario is to connect to a remote netbox:
If you specify the API key, you do not need to pass an additional `authInfo` to operations that need authentication, and
can pass `nil`:
```golang
t := client.DefaultTransportConfig().WithHost("your.netbox.host")
c := client.NewHTTPClientWithConfig(nil, t)
c.Dcim.DcimDeviceTypesCreate(createRequest, nil)
```

The client is generated using [go-swagger](https://github.com/go-swagger/go-swagger). This means the generated client
makes use of [github.com/go-openapi/runtime/client](https://godoc.org/github.com/go-openapi/runtime/client). The [godocs
for that module](https://godoc.org/github.com/go-openapi/runtime/client) explain the client options in detail, including
different authentication and debugging options.
More complex client configuration
=================================

Setting the debug flag will print all requests and responses on standard out, which is great for debugging unexpected
results. It does require creating the client in the lower level `go-openapi` libraries:
```golang
import (
"github.com/digitalocean/go-netbox/netbox/client"
"github.com/go-openapi/strfmt"
runtimeclient "github.com/go-openapi/runtime/client"
)

func main() {
t := runtimeclient.New(client.DefaultHost, client.DefaultBasePath, client.DefaultSchemes)
t.SetDebug(true)
c := client.New(t, strfmt.Default)
The client is generated using [go-swagger](https://github.com/go-swagger/go-swagger). This means the generated client
makes use of [github.com/go-openapi/runtime/client](https://godoc.org/github.com/go-openapi/runtime/client). If you need
a more complex configuration, it is probably possible with a combination of this generated client and the runtime
options.

... work with c ...
)
```
The [godocs for the go-openapi/runtime/client module](https://godoc.org/github.com/go-openapi/runtime/client) explain
the client options in detail, including different authentication and debugging options. One thing I want to flag because
it is so useful: setting the `DEBUG` environment variable will dump all requests to standard out.

Regenerating the client
=======================
Expand Down
41 changes: 0 additions & 41 deletions examples/simple/simple.go

This file was deleted.

23 changes: 23 additions & 0 deletions netbox/netbox.go
@@ -0,0 +1,23 @@
package netbox

import (
"fmt"

"github.com/go-openapi/strfmt"
runtimeclient "github.com/go-openapi/runtime/client"

"github.com/digitalocean/go-netbox/netbox/client"
)

func NewNetboxAt(host string) *client.NetBox {
t := client.DefaultTransportConfig().WithHost(host)
return client.NewHTTPClientWithConfig(strfmt.Default, t)
}

const authHeaderName = "Authorization"
const authHeaderFormat = "Token %v"
func NewNetboxWithAPIKey(host string, apiToken string) *client.NetBox {
t := runtimeclient.New(host, client.DefaultBasePath, client.DefaultSchemes)
t.DefaultAuthentication = runtimeclient.APIKeyAuth(authHeaderName, "header", fmt.Sprintf(authHeaderFormat, apiToken))
return client.New(t, strfmt.Default)
}
19 changes: 5 additions & 14 deletions examples/simple/simple_test.go → netbox/netbox_test.go
Expand Up @@ -14,16 +14,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package netbox

import (
"testing"
"fmt"

"github.com/digitalocean/go-netbox/netbox/client/dcim"
"github.com/digitalocean/go-netbox/netbox/models"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/runtime"
"github.com/stretchr/testify/assert"
)

Expand All @@ -33,7 +31,7 @@ import (
// python3 netbox/manage.py runserver 0.0.0.0:8000 --insecure

func TestRetrieveDeviceList(t *testing.T) {
c := defaultClient()
c := NewNetboxAt("localhost:8000")

list, err := c.Dcim.DcimDevicesList(nil, nil)

Expand All @@ -42,7 +40,7 @@ func TestRetrieveDeviceList(t *testing.T) {
}

func TestSubdeviceRole(t *testing.T) {
c := defaultClient()
c := NewNetboxWithAPIKey("localhost:8000", "7b4e1ceaaf93528a41e64d048090f7fe13ed16f4")

role := true
manufacturerID := int64(1)
Expand All @@ -59,7 +57,7 @@ func TestSubdeviceRole(t *testing.T) {
assert.NoError(t, err)

createRequest := dcim.NewDcimDeviceTypesCreateParams().WithData(newDeviceType)
createResponse, err := c.Dcim.DcimDeviceTypesCreate(createRequest, runtime.ClientAuthInfoWriterFunc(SetAuthenticationHeader))
createResponse, err := c.Dcim.DcimDeviceTypesCreate(createRequest, nil)
assert.NoError(t, err)

newID := float64(createResponse.Payload.ID)
Expand All @@ -71,13 +69,6 @@ func TestSubdeviceRole(t *testing.T) {
assert.EqualValues(t, "Test device type", retrieveResponse.Payload.Results[0].Comments)

deleteRequest := dcim.NewDcimDeviceTypesDeleteParams().WithID(int64(newID))
_, err = c.Dcim.DcimDeviceTypesDelete(deleteRequest, runtime.ClientAuthInfoWriterFunc(SetAuthenticationHeader))
_, err = c.Dcim.DcimDeviceTypesDelete(deleteRequest, nil)
assert.NoError(t, err)
}

const apiToken = "7b4e1ceaaf93528a41e64d048090f7fe13ed16f4"
const authHeaderFormat = "Token %v"
func SetAuthenticationHeader(req runtime.ClientRequest, _ strfmt.Registry) error {
req.SetHeaderParam("Authorization", fmt.Sprintf(authHeaderFormat, apiToken))
return nil
}

0 comments on commit ed36be4

Please sign in to comment.