Skip to content
Snippets Groups Projects
Commit 0ad0d8f2 authored by Dave Cameron's avatar Dave Cameron
Browse files

Include simple examples.

Tweaking licensecheck.sh again since the examples are not generated.
parent 2b52398a
Branches
No related tags found
No related merge requests found
...@@ -3,4 +3,4 @@ generate: ...@@ -3,4 +3,4 @@ generate:
swagger generate client --target=./netbox --spec=./swagger.json --copyright-file=./copyright_header.txt swagger generate client --target=./netbox --spec=./swagger.json --copyright-file=./copyright_header.txt
clean: clean:
rm -rf netbox/* rm -rf netbox/*
\ No newline at end of file
...@@ -5,3 +5,55 @@ Package `netbox` provides an API 2.0 client for [DigitalOcean's NetBox](https:// ...@@ -5,3 +5,55 @@ Package `netbox` provides an API 2.0 client for [DigitalOcean's NetBox](https://
IPAM and DCIM service. IPAM and DCIM service.
This package assumes you are using NetBox 2.0, as the NetBox 1.0 API no longer exists. This package assumes you are using NetBox 2.0, as the NetBox 1.0 API no longer exists.
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:
```golang
// Passing nil to this method results in all default values
c := client.NewHTTPClient(nil)
... work with the returned client ...
```
A more likely scenario is to connect to a remote netbox:
```golang
t := client.DefaultTransportConfig().WithHost("your.netbox.host")
c := client.NewHTTPClientWithConfig(nil, t)
```
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.
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)
... work with c ...
)
```
Regenerating the client
=======================
To regenerate the client with a new or different swagger schema, first clean the existing client, then replace
swagger.json and finally re-generate:
```
make clean
cp new_swagger_file.json swagger.json
make generate
```
// Copyright 2018 The go-netbox Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"os"
"github.com/digitalocean/go-netbox/netbox/client"
)
func main() {
t := client.DefaultTransportConfig().WithHost("your.netbox.host")
c := client.NewHTTPClientWithConfig(nil, t)
rs, err := c.Dcim.DcimRacksList(nil, nil)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
fmt.Printf("%v\n", *(rs.Payload.Count))
}
// Copyright 2018 The go-netbox Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"os"
"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)
rs, err := c.Dcim.DcimRacksList(nil, nil)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
fmt.Printf("%v\n", *(rs.Payload.Count))
}
// Copyright 2018 The go-netbox Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"os"
"github.com/digitalocean/go-netbox/netbox/client"
)
func main() {
// Passing nil to this method results in all default values
c := client.NewHTTPClient(nil)
rs, err := c.Dcim.DcimRacksList(nil, nil)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
fmt.Printf("%v\n", *(rs.Payload.Count))
}
...@@ -3,8 +3,6 @@ ...@@ -3,8 +3,6 @@
# Verify that the correct license block is present in all Go source # Verify that the correct license block is present in all Go source
# files. # files.
IFS=$'\n' read -r -d '' -a EXPECTED <<EndOfLicense IFS=$'\n' read -r -d '' -a EXPECTED <<EndOfLicense
// Code generated by go-swagger; DO NOT EDIT.
// Copyright 2018 The go-netbox Authors. // Copyright 2018 The go-netbox Authors.
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
...@@ -26,15 +24,17 @@ EXIT=0 ...@@ -26,15 +24,17 @@ EXIT=0
GOFILES=$(find . -name "*.go") GOFILES=$(find . -name "*.go")
for FILE in $GOFILES; do for FILE in $GOFILES; do
IFS=$'\n' read -r -d '' -a BLOCK < <(head -n 16 $FILE) IFS=$'\n' read -r -d '' -a BLOCK < <(tail -n +3 $FILE | head -n 14)
IFS=$'\n' read -r -d '' -a BLOCK2 < <(head -n 14 $FILE)
tmp_block=${BLOCK[@]:1} tmp_block=${BLOCK[@]:1}
tmp_block2=${BLOCK2[@]:1}
tmp_expected=${EXPECTED[@]:1} tmp_expected=${EXPECTED[@]:1}
if [[ $tmp_block != $tmp_expected ]]; then if [[ $tmp_block != $tmp_expected && $tmp_block2 != $tmp_expected ]]; then
echo "file missing license: $FILE" echo "file missing license: $FILE"
EXIT=1 EXIT=1
fi fi
if ! [[ "${BLOCK[1]}" =~ $AUTHOR_REGEX ]]; then if ! [[ "${BLOCK[0]}" =~ $AUTHOR_REGEX || "${BLOCK2[0]}" =~ $AUTHOR_REGEX ]]; then
echo "file missing author line: $FILE" echo "file missing author line: $FILE"
EXIT=1 EXIT=1
fi fi
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment