diff --git a/Makefile b/Makefile index 06fc156072fc4102970d4c8ded93fa9c7e623f38..0823942920f906c3fb252cae2ab7fddd5197325b 100644 --- a/Makefile +++ b/Makefile @@ -3,4 +3,4 @@ generate: swagger generate client --target=./netbox --spec=./swagger.json --copyright-file=./copyright_header.txt clean: - rm -rf netbox/* \ No newline at end of file + rm -rf netbox/* diff --git a/README.md b/README.md index 93027bfd4be180eb8fbfb0bef38a02baa5cdcb4e..294150d3ae4d0b773772a8d96c3834b7abc80af7 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,55 @@ Package `netbox` provides an API 2.0 client for [DigitalOcean's NetBox](https:// IPAM and DCIM service. 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 +``` diff --git a/examples/customhost/customhost.go b/examples/customhost/customhost.go new file mode 100644 index 0000000000000000000000000000000000000000..850e1f933c4bf9e90009da89a15d1676c4808f76 --- /dev/null +++ b/examples/customhost/customhost.go @@ -0,0 +1,35 @@ +// 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)) +} diff --git a/examples/debugflag/debugflag.go b/examples/debugflag/debugflag.go new file mode 100644 index 0000000000000000000000000000000000000000..62c9d7fe343c5119cd1f9ac60b36498711e4e8fd --- /dev/null +++ b/examples/debugflag/debugflag.go @@ -0,0 +1,38 @@ +// 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)) +} diff --git a/examples/simple/simple.go b/examples/simple/simple.go new file mode 100644 index 0000000000000000000000000000000000000000..c2584ef21ef79e3b537579599918156943a7925b --- /dev/null +++ b/examples/simple/simple.go @@ -0,0 +1,35 @@ +// 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)) +} diff --git a/scripts/licensecheck.sh b/scripts/licensecheck.sh index 5c481376f69e08f48297a71bcc54bfb6d2d17843..bfa62411012a6688b7a79b596efb99cff256a3c3 100755 --- a/scripts/licensecheck.sh +++ b/scripts/licensecheck.sh @@ -3,8 +3,6 @@ # Verify that the correct license block is present in all Go source # files. IFS=$'\n' read -r -d '' -a EXPECTED <<EndOfLicense -// Code generated by go-swagger; DO NOT EDIT. - // Copyright 2018 The go-netbox Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,15 +24,17 @@ EXIT=0 GOFILES=$(find . -name "*.go") 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_block2=${BLOCK2[@]: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" EXIT=1 fi - if ! [[ "${BLOCK[1]}" =~ $AUTHOR_REGEX ]]; then + if ! [[ "${BLOCK[0]}" =~ $AUTHOR_REGEX || "${BLOCK2[0]}" =~ $AUTHOR_REGEX ]]; then echo "file missing author line: $FILE" EXIT=1 fi