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

feat: init diode-go-sdk (part 2) #2

Merged
merged 26 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
364e6ba
feat: regenerated diodepb package
mfiedorowicz Aug 20, 2024
852dd09
feat: load system root CAs, api key, parse target, tests
mfiedorowicz Aug 21, 2024
4a4c394
move client outside v1
mfiedorowicz Aug 21, 2024
1397ef3
bump indirect dependencies
mfiedorowicz Aug 21, 2024
2ceb593
ignore .coverage dir
mfiedorowicz Aug 21, 2024
94224f3
go mod tidy
mfiedorowicz Aug 21, 2024
f7f6171
tests tidy
mfiedorowicz Aug 21, 2024
b6eaf10
add Makefile
mfiedorowicz Aug 21, 2024
b338ced
golangci config fix
mfiedorowicz Aug 21, 2024
e4b6aaf
feat(gha): add code coverage as a comment
mfiedorowicz Aug 21, 2024
cdbe796
fix(gha): github outputs
mfiedorowicz Aug 21, 2024
b8e708f
set default logging level to info
mfiedorowicz Aug 21, 2024
180b1f5
allow to provide api key using optional WithAPIKey function
mfiedorowicz Aug 21, 2024
834359a
append metadata to outgoing context
mfiedorowicz Aug 21, 2024
5b279cf
test-coverage: exclude diodepb and examples
mfiedorowicz Aug 21, 2024
a6de7a5
fix test-coverage recipe
mfiedorowicz Aug 21, 2024
90d5a89
add codegen for ingester
mfiedorowicz Aug 27, 2024
cf1fc66
add diode pointer scalar type functions
mfiedorowicz Aug 27, 2024
6532aea
add generated ingester
mfiedorowicz Aug 27, 2024
e063b81
use generated types for ingester
mfiedorowicz Aug 27, 2024
7bc5550
fix GetTags method
mfiedorowicz Aug 27, 2024
d35ac7d
add unit tests for ingester
mfiedorowicz Aug 27, 2024
5426619
fix test-coverage recipe
mfiedorowicz Aug 27, 2024
c11ebca
add examples
mfiedorowicz Aug 27, 2024
e7a4fa8
add README
mfiedorowicz Aug 27, 2024
1d048d1
tidy up ingester
mfiedorowicz Aug 27, 2024
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: 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/[email protected]
- 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
Loading