Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Netbox to 3.4 #144

Merged
merged 6 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Docker
#USER_ID=1000
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ jobs:
strategy:
fail-fast: false
matrix:
go: ["1.17.x", "1.18.x"]
go: ["1.19", "1.18"]
steps:
- name: Checkout
uses: actions/checkout@v2

- name: License check
run: ./scripts/licensecheck.sh
run: ./scripts/license-check.sh

- name: Go installation
uses: actions/setup-go@v2
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.DS_Store
.env
.gobincache/

2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ Allan Liu <aliu@digitalocean.com>
Dave Cameron <dcameron@digitalocean.com>
Quan D. Hoang <hdquan2014@gmail.com>
David Dymko <dymkod@gmail.com>
Víctor Díaz <victor@brutalsys.com>
Víctor Díaz <victor@brutal.systems>
Hirano Yuki <yuuki0xff@gmail.com>
42 changes: 42 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
ARG GO_VERSION=1.19.5
ARG ALPINE_VERSION=3.17
ARG GOIMPORTS_VERSION=0.5.0
ARG DELVE_VERSION=1.20.1
ARG SWAGGER_VERSION=0.30.3


## Base image
FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS base

WORKDIR /go/src/app

ENV CGO_ENABLED=0


## Development image
FROM base AS development

ARG GOIMPORTS_VERSION
ARG DELVE_VERSION
ARG SWAGGER_VERSION

RUN apk add \
bash \
curl \
git \
jq \
python3 \
zsh \
&& go install golang.org/x/tools/cmd/goimports@v${GOIMPORTS_VERSION} \
&& go install github.com/go-delve/delve/cmd/dlv@v${DELVE_VERSION} \
&& go install github.com/go-swagger/go-swagger/cmd/swagger@v${SWAGGER_VERSION}

ARG USER_ID=1000
ENV USER_NAME=default

ENV PROMPT="%B%F{cyan}%n%f@%m:%F{yellow}%~%f %F{%(?.green.red[%?] )}>%f %b"

RUN adduser -D -u $USER_ID ${USER_NAME} \
&& chown -R ${USER_NAME}: /go/pkg

USER ${USER_NAME}
46 changes: 37 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
deps:
go mod download
go install github.com/go-swagger/go-swagger/cmd/swagger@latest
# Helpers
IS_DARWIN := $(filter Darwin,$(shell uname -s))

generate: deps
swagger generate client --target=./netbox --copyright-file=copyright_header.txt
define set_env
sed $(if $(IS_DARWIN),-i "",-i) -e "s/^#*\($(1)=\).*/$(if $(2),,#)\1$(2)/" .env
endef

clean:
rm -rf netbox/client netbox/models
EXEC := docker compose exec app

integration:
go test ./... -tags=integration
# Environment recipes
.PHONY: default
default: init up

.PHONY: init
init:
test -f .env || cp .env.example .env
$(call set_env,USER_ID,$(shell id -u))

.PHONY: up
up:
DOCKER_BUILDKIT=1 docker compose up -d --build

.PHONY: down
down:
docker compose down

.PHONY: shell
shell:
$(EXEC) zsh

# Project recipes
.PHONY: build
build:
$(EXEC) ./scripts/set-versions.sh $(NETBOX_VERSION) $(NETBOX_DOCKER_VERSION)
./scripts/fetch-spec.sh $$(cat api/netbox_version) $$(cat api/netbox_docker_version)
$(EXEC) ./scripts/generate-code.sh

.PHONY: test
test:
$(EXEC) go test ./... -tags=integration
67 changes: 51 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
go-netbox
=========
# go-netbox

[![GoDoc](http://godoc.org/github.com/netbox-community/go-netbox?status.svg)](http://godoc.org/github.com/netbox-community/go-netbox) [![Build Status](https://github.com/netbox-community/go-netbox/workflows/main/badge.svg?branch=master)](https://github.com/netbox-community/go-netbox/actions) [![Report Card](https://goreportcard.com/badge/github.com/netbox-community/go-netbox)](https://goreportcard.com/report/github.com/netbox-community/go-netbox)

Package `netbox` provides an API 3.2 client for [netbox-community's Netbox](https://github.com/netbox-community/netbox) IPAM and DCIM service.

This package assumes you are using Netbox 3.2, as the Netbox 1.0 API no longer exists. If you need support for previous Netbox versions, you can still import the corresponding release of the library. For example, run `go get github.com/netbox-community/go-netbox@netbox_v2.11` if you need compatibility with Netbox 2.11.

Using the client
================
## Using the client

The `github.com/netbox-community/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
Expand Down Expand Up @@ -68,16 +66,14 @@ func main() {
}
```

Go Module support
================
## Go Module support

Go 1.16+

`go get github.com/netbox-community/go-netbox`


More complex client configuration
=================================
## More complex client configuration

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
Expand All @@ -88,14 +84,53 @@ The [godocs for the go-openapi/runtime/client module](https://godoc.org/github.c
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
=======================
## Development

To regenerate the client with a new or different swagger schema, first clean the existing client, then replace
swagger.json and finally re-generate:
The project comes with a containerized development environment that can be used from any platform. It is only required to have [Git](https://git-scm.com) and [Docker Desktop](https://www.docker.com/products/docker-desktop/) (or, separately, [Docker](https://docs.docker.com/engine/install) and [Docker Compose](https://docs.docker.com/compose/install/)) installed on the machine.

To start the development environment, run the following command.

```bash
make
```

Then, to attach a shell in the container, run the command below.

```bash
make shell
```
make clean
./scripts/swagger_modifier.py new_swagger_file.json
mv swagger_transformed.json swagger.json
make generate

Finally, to stop the development environment, run the following command.

```bash
make down
```

### Considerations

The library is almost entirely generated from the Netbox [OpenAPI](https://www.openapis.org/) specification. Therefore, files under directories `netbox/client` and `netbox/models` should not be directly modified, as they will be overwritten in the next regeneration (see next section).

To fix issues in generated code, there are two options:

- Change the OpenAPI spec with pre-generation hooks (see [`scripts/pre-generation`](scripts/pre-generation)).
- If the above is not possible, change the generated code with post-generation hooks (see [`scripts/post-generation`](scripts/post-generation)).

### Regenerate the library

To update the OpenAPI specification to the latest Netbox version and regenerate the library, run the following command.

```bash
make build
```

If regeneration of the library is needed for a specific Netbox version other than the latest one, pass the corresponding argument.

```bash
make build NETBOX_VERSION=3.0.0
```

In order to obtain the OpenAPI specification, the version of _[netbox-docker](https://github.com/netbox-community/netbox-docker)_ corresponding to the given Netbox version is used. However, it is also possible to provide a specific version of _netbox-docker_.

```bash
make build NETBOX_VERSION=3.0.0 NETBOX_DOCKER_VERSION=1.3.1
```
1 change: 1 addition & 0 deletions api/netbox_docker_version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.4.0
1 change: 1 addition & 0 deletions api/netbox_version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.4.2
Loading