diff --git a/.github/ISSUE_TEMPLATE/bug.md b/.github/ISSUE_TEMPLATE/bug.md new file mode 100644 index 00000000..7d523074 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug.md @@ -0,0 +1,49 @@ +--- +name: Bug Report +about: Create a bug report to help us improve this project +title: "[Main title for your issue here]" +labels: bug +assignees: smutel +--- + + + + +## Summary + + +## Version + +### Netbox version + +```paste below + +``` + +### go-netbox version + +```paste below + +``` + +## Issue details + +### Piece of code using go-netbox wich is not working + + +### Affected library call + + +### Program output + +```paste below + +``` + +## Behaviors + +### Actual Behavior + + +### Expected Behavior + diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 00000000..3ba13e0c --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1 @@ +blank_issues_enabled: false diff --git a/.github/ISSUE_TEMPLATE/feature.md b/.github/ISSUE_TEMPLATE/feature.md new file mode 100644 index 00000000..a8f945a2 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature.md @@ -0,0 +1,18 @@ +--- +name: New feature +about: Need a new feature not yet implemented +title: "[Main title for your issue here]" +labels: enhancement +assignees: smutel +--- + + + +## Summary + + +## Example + + +## Usage + diff --git a/.github/ISSUE_TEMPLATE/help.md b/.github/ISSUE_TEMPLATE/help.md new file mode 100644 index 00000000..16c5458d --- /dev/null +++ b/.github/ISSUE_TEMPLATE/help.md @@ -0,0 +1,34 @@ +--- +name: Help Needed +about: I need some help to use this project +title: "[I am not able to do ...]" +labels: help wanted +assignees: smutel +--- + + + +## Issue description + +### What I want to do + + +### What I already test + +1. `Read the README` + +### Other important information + +## Version + +### Netbox version + +```paste below + +``` + +### go-netbox version + +```paste below + +``` diff --git a/.github/auto_assign.yml b/.github/auto_assign.yml new file mode 100644 index 00000000..5aceade1 --- /dev/null +++ b/.github/auto_assign.yml @@ -0,0 +1,18 @@ +# Set to true to add reviewers to pull requests +addReviewers: true + +# Set to true to add assignees to pull requests +addAssignees: true + +# A list of reviewers to be added to pull requests (GitHub user name) +reviewers: + - smutel + +# A list of keywords to be skipped the process that add reviewers if pull requests include it +skipKeywords: + - wip + - draft + +# A number of reviewers added to the pull request +# Set 0 to add all the reviewers (default: 0) +numberOfReviewers: 0 diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml new file mode 100644 index 00000000..91cd17d1 --- /dev/null +++ b/.github/workflows/master.yml @@ -0,0 +1,62 @@ +--- +name: Master + +on: + push: + branches: + - master + +jobs: + Build: + runs-on: ubuntu-latest + strategy: + fail-fast: false + steps: + - name: Checkout + uses: actions/checkout@v2.3.4 + + - name: Unshallow + run: git fetch --prune --unshallow + + - name: Lint + uses: reviewdog/action-golangci-lint@v1.17.0 + with: + github_token: ${{ secrets.github_token }} + golangci_lint_flags: "--config=.golangci.yml" + + - name: Setup env + run: | + echo "GOPATH=$(dirname $GITHUB_WORKSPACE)" >> $GITHUB_ENV + echo "$(dirname $GITHUB_WORKSPACE)/bin" >> $GITHUB_PATH + mkdir -p "$(dirname $GITHUB_WORKSPACE)/src/github.com/smutel" + ln -sf "$GITHUB_WORKSPACE/" "$(dirname $GITHUB_WORKSPACE)/src/github.com/smutel/go-netbox" + shell: bash + + - name: Swagger and patchs + run: | + NETBOX_MAJOR_VERSION=$(cat netbox_major_version) + echo "NETBOX_MAJOR_VERSION=${NETBOX_MAJOR_VERSION}" + LAST_NETBOX_VERSION="$(./utils/netbox_get_last_version ${NETBOX_MAJOR_VERSION})" + echo "LAST_NETBOX_VERSION=${LAST_NETBOX_VERSION}" + export VERSION=${LAST_NETBOX_VERSION} + git clone https://github.com/netbox-community/netbox-docker.git + cd netbox-docker + mv docker-compose.override.yml.example docker-compose.override.yml + docker-compose up -d + cd .. + while ! curl -s http://127.0.0.1:8000/api/swagger.json -o swagger.json 2> /dev/null; do sleep 1; done + curl -sL https://github.com/go-swagger/go-swagger/releases/download/v0.27.0/swagger_linux_amd64 -o swagger + chmod 755 swagger + rm -rf netbox && mkdir netbox && touch netbox/.gitkeep + ./swagger generate client -f swagger.json -A go-netbox -t "$(dirname $GITHUB_WORKSPACE)/src/github.com/smutel/go-netbox/netbox" --copyright-file="$(dirname $GITHUB_WORKSPACE)/src/github.com/smutel/go-netbox/LICENSE" + find patchs -type f -name "*.patch" | xargs patch -p0 + shell: bash + + - name: Commit library + uses: EndBug/add-and-commit@v7 + with: + add: 'netbox' + author_name: smutel + default_author: github_actor + message: 'ci: Autogeneration of go-netbox library' + pull_strategy: '--rebase' diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml new file mode 100644 index 00000000..fcb92f88 --- /dev/null +++ b/.github/workflows/pr.yml @@ -0,0 +1,54 @@ +--- +name: PR + +on: + pull_request: + +jobs: + Build: + runs-on: ubuntu-latest + strategy: + fail-fast: false + steps: + - name: Check conventional commits in PR + uses: Namchee/conventional-pr@v0.4.1 + with: + access_token: ${{ secrets.github_token }} + label: "no-conventional-commits" + close: false + template: "Thank you for your contribution attempt in this repository!\n\nUnfortunately, this pull request doesn't meet our standards by reason we stated below.\nFor future pull requests, please use conventional commits.\nPlease update the commits in this PR with conventional commits rules." + strict: false + + - name: Checkout + uses: actions/checkout@v2.3.4 + + - name: Unshallow + run: git fetch --prune --unshallow + + - name: Setup env + run: | + echo "GOPATH=$(dirname $GITHUB_WORKSPACE)" >> $GITHUB_ENV + echo "$(dirname $GITHUB_WORKSPACE)/bin" >> $GITHUB_PATH + mkdir -p "$(dirname $GITHUB_WORKSPACE)/src/github.com/smutel" + ln -sf "$GITHUB_WORKSPACE/" "$(dirname $GITHUB_WORKSPACE)/src/github.com/smutel/go-netbox" + shell: bash + + - name: Swagger and patchs + run: | + NETBOX_MAJOR_VERSION=$(cat netbox_major_version) + echo "NETBOX_MAJOR_VERSION=${NETBOX_MAJOR_VERSION}" + LAST_NETBOX_VERSION="$(./utils/netbox_get_last_version ${NETBOX_MAJOR_VERSION})" + echo "LAST_NETBOX_VERSION=${LAST_NETBOX_VERSION}" + export VERSION=${LAST_NETBOX_VERSION} + git clone https://github.com/netbox-community/netbox-docker.git + cd netbox-docker + mv docker-compose.override.yml.example docker-compose.override.yml + docker-compose up -d + cd .. + while ! curl -s http://127.0.0.1:8000/api/swagger.json -o swagger.json 2> /dev/null; do sleep 1; done + curl -sL https://github.com/go-swagger/go-swagger/releases/download/v0.27.0/swagger_linux_amd64 -o swagger + chmod 755 swagger + rm -rf netbox && mkdir netbox && touch netbox/.gitkeep + ./swagger generate client -f swagger.json -A go-netbox -t "$(dirname $GITHUB_WORKSPACE)/src/github.com/smutel/go-netbox/netbox" --copyright-file="$(dirname $GITHUB_WORKSPACE)/src/github.com/smutel/go-netbox/LICENSE" + find patchs -type f -name "*.patch" | xargs patch -p0 + shell: bash diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000..b8309b78 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,44 @@ +--- +name: Release + +on: + push: + tags: + - "v*" + +jobs: + Release: + runs-on: ubuntu-latest + strategy: + fail-fast: false + steps: + - name: Checkout + uses: actions/checkout@v2.3.4 + + - name: Unshallow + run: git fetch --prune --unshallow + + - name: Import GPG key + id: import_gpg + uses: crazy-max/ghaction-import-gpg@v3.1.0 + with: + gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }} + passphrase: ${{ secrets.PASSPHRASE }} + + - name: Get tag + id: tag + uses: dawidd6/action-get-tag@v1 + + - name: Generate CHANGELOG for this version + run: ./utils/getChangelogRelease CHANGELOG.md ${{ steps.tag.outputs.tag }} > /tmp/RELEASE_CHANGELOG.md + + - name: Release + uses: actions/create-release@v1 + with: + draft: true + prerelease: false + release_name: ${{ steps.tag.outputs.tag }} + tag_name: ${{ github.ref }} + body_path: /tmp/RELEASE_CHANGELOG.md + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..d40ca465 --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ +Copyright (c) 2020 Samuel Mutel <12967891+smutel@users.noreply.github.com> + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/NOTE b/NOTE new file mode 100644 index 00000000..de0184f7 --- /dev/null +++ b/NOTE @@ -0,0 +1,8 @@ +git clone https://github.com/netbox-community/netbox-docker.git +mv docker-compose.override.yml.example docker-compose.override.yml +docker-compose up +curl http://127.0.0.1:8000/api/swagger.json -o swagger.json +curl -L https://github.com/go-swagger/go-swagger/releases/download/v0.27.0/swagger_linux_amd64 -o swagger +chmod 755 swagger +mkdir -p ~/go/src/github.com/smutel/go-netbox +./swagger generate client -f swagger.json -A go-netbox --with-flatten=full -t ~/go/src/github.com/smutel/go-netbox diff --git a/README.md b/README.md index 05e3d389..a727aa17 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,131 @@ # go-netbox + +[![Lisence](https://img.shields.io/badge/license-ISC-informational?style=flat-square)](https://github.com/smutel/go-netbox/blob/master/LICENSE) +[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-informational.svg?style=flat-square&logo=git)](https://conventionalcommits.org) +[![Build Status](https://img.shields.io/github/workflow/status/smutel/go-netbox/Master/master?style=flat-square&logo=github-actions)](https://github.com/smutel/go-netbox/actions) + Go library to interact with NetBox IPAM and DCIM service. + +## Compatibility with Netbox + +The version for Netbox and go-netbox will the same except for the last digit. + +Example: +* go-netbox v1.10.x is working with Netbox v1.10 +* go-netbox v1.11.x is working with Netbox v1.11 + +## Using the client + +The `github.com/smutel/go-netbox/netbox` package has some convenience functions for creating clients with the most common +configurations you likely 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. + +```go +import ( + "github.com/smutel/go-netbox/netbox" +) +... + c := netbox.NewNetboxAt("your.netbox.host:8000") + // OR + c := netbox.NewNetboxWithAPIKey("your.netbox.host:8000", "your_netbox_token") +``` + +If you specify the API key, you do not need to pass an additional `authInfo` to operations that need authentication, and +can pass `nil`: +```go + c.Dcim.DcimDeviceTypesCreate(createRequest, nil) +``` + +If you connect to netbox via HTTPS you have to create an HTTPS configured transport: +```go +package main + +import ( + "os" + + httptransport "github.com/go-openapi/runtime/client" + "github.com/smutel/go-netbox/netbox/client" + "github.com/smutel/go-netbox/netbox/client/dcim" + + log "github.com/sirupsen/logrus" +) + +func main() { + token := os.Getenv("NETBOX_TOKEN") + if token == "" { + log.Fatalf("Please provide netbox API token via env var NETBOX_TOKEN") + } + + netboxHost := os.Getenv("NETBOX_HOST") + if netboxHost == "" { + log.Fatalf("Please provide netbox host via env var NETBOX_HOST") + } + + transport := httptransport.New(netboxHost, client.DefaultBasePath, []string{"https"}) + transport.DefaultAuthentication = httptransport.APIKeyAuth("Authorization", "header", "Token "+token) + + c := client.New(transport, nil) + + req := dcim.NewDcimSitesListParams() + res, err := c.Dcim.DcimSitesList(req, nil) + if err != nil { + log.Fatalf("Cannot get sites list: %v", err) + } + log.Infof("res: %v", res) +} +``` + +## How to contribute to this project + +* To contribute to this project, please follow the [conventional commits](https://www.conventionalcommits.org/en/v1.0.0-beta.2/) rules. +* Most of the code of this project will be generated using the swagger spec of Netbox and the [go-swagger](https://github.com/go-swagger/go-swagger) program. +* You can change the behavior of the generated library by pushing patchs in the `patchs` folder. +* The best is to see if the bug is due to a wrong swagger definition and to report this bug to the [Netbox](https://github.com/netbox-community/netbox) project. +* If the bug is due to the go-swagger program the best is to create a bug here [go-swagger](https://github.com/go-swagger/go-swagger). + +## How to test your work locally + +### Requirements + +* docker +* docker-compose +* [swagger](https://github.com/go-swagger/go-swagger) installed somewhere (/usr/local/bin) +* [netbox-docker](https://github.com/netbox-community/netbox-docker.git) project installed somewhere + +### Installing the go-netbox + +```sh +$ mkdir -p ~/go/src/github.com/smutel +$ cd ~/go/src/github.com/smutel +$ git clone git@github.com:smutel/go-netbox.git +``` + +### Starting netbox and getting the swagger definitions + +You can do this by executing the commands below: +```sh +$ cd ~/go/src/github.com/smutel/go-netbox +$ NETBOX_MAJOR_VERSION=$(cat netbox_major_version) +$ LAST_NETBOX_VERSION="$(./utils/netbox_get_last_version ${NETBOX_MAJOR_VERSION})" +$ export VERSION=${LAST_NETBOX_VERSION} +$ cd +$ mv docker-compose.override.yml.example docker-compose.override.yml +$ docker-compose up -d +$ while ! curl -s http://127.0.0.1:8000/api/swagger.json -o /tmp/swagger.json 2> /dev/null; do sleep 1; done +``` + +### Regenerating the library + +```sh +$ cd ~/go/src/github.com/smutel/go-netbox +$ rm -rf netbox && mkdir netbox && touch netbox/.gitkeep +$ swagger generate client -f /tmp/swagger.json -A go-netbox -t ~/src/github.com/smutel/go-netbox/netbox +``` + +### Trying to apply patchs + +```sh +$ cd ~/go/src/github.com/smutel/go-netbox +$ for p in patchs/*; do patch -p0 < $p; done +``` + diff --git a/netbox/.gitkeep b/netbox/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/netbox_major_version b/netbox_major_version new file mode 100644 index 00000000..9fda0b17 --- /dev/null +++ b/netbox_major_version @@ -0,0 +1 @@ +v2.11 diff --git a/package.json b/package.json new file mode 100644 index 00000000..5ef662de --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "name": "go-netbox", + "version": "1.10.0", + "description": "Go library to interact with NetBox IPAM and DCIM service" +} diff --git a/patchs/.gitkeep b/patchs/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/utils/getChangelogRelease b/utils/getChangelogRelease new file mode 100755 index 00000000..8df3d58a --- /dev/null +++ b/utils/getChangelogRelease @@ -0,0 +1,13 @@ +#!/bin/bash + +CHANGELOG_FILE="$1" +VERSION="$2" + +if [[ ${VERSION} =~ v.* ]]; then + VERSION=${VERSION:1} +fi + +START=$(grep -n "## \[${VERSION}\]" "${CHANGELOG_FILE}" | cut -d : -f 1) +END_VERSION=$(grep "## \[" "${CHANGELOG_FILE}" | cut -d"[" -f2 | cut -d"]" -f1 | grep -v ${VERSION} | head -1) +END=$(grep -n "## \[${END_VERSION}\]" ${CHANGELOG_FILE} | cut -d : -f 1) +cat "${CHANGELOG_FILE}" | tail -n +"$((START + 3))" | head -n "$((END - 9))" diff --git a/utils/netbox_get_last_version b/utils/netbox_get_last_version new file mode 100755 index 00000000..31b439a0 --- /dev/null +++ b/utils/netbox_get_last_version @@ -0,0 +1,33 @@ +#!/bin/bash + +VERSION="$1" +TAGS="" +TAGS_LIST="" +NEXT="" +MAX=0 + +while [ "$NEXT" != "null" ] +do + i=$((i+1)) + JSON=$(curl https://registry.hub.docker.com/v2/repositories/netboxcommunity/netbox/tags/?page=$i 2>/dev/null) + TAGS="$TAGS $(echo $JSON | jq '."results"[]["name"]')" + NEXT=$(echo $JSON | jq '."next"') +done + +for t in $TAGS; do + tag="$(echo $t | xargs)" + if [ "$(echo $tag | grep "$VERSION")" == "$tag" -a "$(echo $tag | grep -v "ldap")" == "$tag" -a "$(echo $tag | grep -v "develop")" == "$tag" ]; then + TAGS_LIST="$TAGS_LIST $tag" + fi +done + +for t in $TAGS_LIST; do + MAJOR=$(echo $t | cut -d"." -f3) + if [ "$MAJOR" != "" ]; then + if [ $MAJOR -gt $MAX ]; then + MAX=$MAJOR + fi + fi +done + +echo "$VERSION.$MAX"