Skip to content

Commit

Permalink
feat: init diode-go-sdk (part 2) (#2)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com>
  • Loading branch information
mfiedorowicz authored Aug 27, 2024
1 parent 6a34790 commit 64f0fde
Show file tree
Hide file tree
Showing 21 changed files with 4,479 additions and 939 deletions.
2 changes: 1 addition & 1 deletion .github/golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ run:
modules-download-mode: readonly

output:
formats: github-actions
formats: colored-line-number

linters:
enable:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ jobs:
id: get-next-version
- name: Set short sha output
id: short-sha
run: echo "::set-output name=short-sha::${GITHUB_SHA::7}"
run: echo "short-sha=${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"
- name: Set release version
id: release-version
run: |
echo "::set-output name=release-version::`echo ${{ steps.get-next-version.outputs.new-release-version }} | sed 's/v//g'`"
echo "release-version=`echo ${{ steps.get-next-version.outputs.new-release-version }} | sed 's/v//g'`" >> "$GITHUB_OUTPUT"
outputs:
new-release-published: ${{ steps.get-next-version.outputs.new-release-published }}
new-release-version: ${{ steps.release-version.outputs.release-version }}
Expand Down
33 changes: 32 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ concurrency:
group: ${{ github.workflow }}
cancel-in-progress: false

permissions:
contents: write
pull-requests: write

jobs:
go-test:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -22,5 +27,31 @@ jobs:
check-latest: true
- name: Run go build
run: go build ./...
- name: Install additional dependencies
run: |
go install github.com/mfridman/tparse@v0.14.0
- name: Run go test
run: go test -race ./...
id: go-test
run: |
make test-coverage
echo 'coverage-report<<EOF' >> $GITHUB_OUTPUT
cat .coverage/test-report.md >> $GITHUB_OUTPUT
echo 'EOF' >> $GITHUB_OUTPUT
echo "coverage-total=$(cat .coverage/coverage.txt)" >> $GITHUB_OUTPUT
- name: Find comment
uses: peter-evans/find-comment@v3
id: existing-comment
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: Go test coverage
- name: Post comment
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ steps.existing-comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
Go test coverage
${{ steps.go-test.outputs.coverage-report }}
Total coverage: ${{ steps.go-test.outputs.coverage-total }}%
edit-mode: replace
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@

# Go
coverage.txt
.coverage/
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.PHONY: deps
deps:
@go mod tidy

.PHONY: lint
lint:
@golangci-lint run ./... --config .github/golangci.yaml

.PHONY: test
test:
@go test -race ./...

.PHONY: test-coverage
test-coverage:
@mkdir -p .coverage
@go test `go list ./... | grep -Ev "diodepb|examples|internal"` -race -cover -json -coverprofile=.coverage/cover.out.tmp ./... | tparse -format=markdown > .coverage/test-report.md
@cat .coverage/cover.out.tmp > .coverage/cover.out
@go tool cover -func=.coverage/cover.out | grep total | awk '{print substr($$3, 1, length($$3)-1)}' > .coverage/coverage.txt

.PHONY: codegen
codegen:
@go run internal/cmd/codegen/main.go | gofmt > ./diode/ingester.go
141 changes: 139 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,140 @@
# diode-sdk-go
# Diode SDK Go

TBD
Diode SDK Go is a Go library for interacting with the Diode ingestion service utilizing gRPC.

Diode is a new [NetBox](https://netboxlabs.com/oss/netbox/) ingestion service that greatly simplifies and enhances the
process to add and update network data
in NetBox, ensuring your network source of truth is always accurate and can be trusted to power your network automation
pipelines.

More information about Diode can be found
at [https://netboxlabs.com/blog/introducing-diode-streamlining-data-ingestion-in-netbox/](https://netboxlabs.com/blog/introducing-diode-streamlining-data-ingestion-in-netbox/).

## Installation

```bash
go install github.com/netboxlabs/diode-sdk-go
```

## Usage

### Environment variables

* `DIODE_API_KEY` - API key for the Diode service
* `DIODE_SDK_LOG_LEVEL` - Log level for the SDK (default: `INFO`)

### Example

* `target` should be the address of the Diode service, e.g. `grpc://localhost:8081` for insecure connection
or `grpcs://example.com` for secure connection.

```go
package main

import (
"context"
"log"

"github.com/netboxlabs/diode-sdk-go/diode"
)

func main() {
client, err := diode.NewClient(
"grpc://localhost:8080/diode",
"example-app",
"0.1.0",
diode.WithAPIKey("YOUR_API_KEY"),
)
if err != nil {
log.Fatal(err)
}

// Create a device
deviceEntity := &diode.Device{
Name: diode.String("Device A"),
DeviceType: &diode.DeviceType{
Model: diode.String("Device Type A"),
Manufacturer: &diode.Manufacturer{
Name: diode.String("Manufacturer A"),
},
},
Platform: &diode.Platform{
Name: diode.String("Platform A"),
Manufacturer: &diode.Manufacturer{
Name: diode.String("Manufacturer A"),
},
},
Site: &diode.Site{
Name: diode.String("Site ABC"),
},
Role: &diode.Role{
Name: diode.String("Role ABC"),
Tags: []*diode.Tag{
{
Name: diode.String("tag 1"),
},
{
Name: diode.String("tag 2"),
},
},
},
Serial: diode.String("123456"),
AssetTag: diode.String("123456"),
Status: diode.String("active"),
Comments: diode.String("Lorem ipsum dolor sit amet"),
Tags: []*diode.Tag{
{
Name: diode.String("tag 1"),
},
{
Name: diode.String("tag 3"),
},
},
}

entities := []diode.Entity{
deviceEntity,
}

resp, err := client.Ingest(context.Background(), entities)
if err != nil {
log.Fatal(err)
}
if resp != nil && resp.Errors != nil {
log.Printf("Errors: %v\n", resp.Errors)
} else {
log.Printf("Success\n")
}

}
```

See all [examples](./examples/main.go) for reference.

## Supported entities (object types)

* Device
* Device Type
* IP Address
* Interface
* Manufacturer
* Platform
* Prefix
* Role
* Site

#### Linting

```shell
make list
```

#### Testing

```shell
make test
```

## License

Distributed under the Apache 2.0 License. See [LICENSE.txt](./LICENSE.txt) for more information.
Loading

0 comments on commit 64f0fde

Please sign in to comment.