Skip to content

Commit

Permalink
Add: Implement create and list endpoint (#11)
Browse files Browse the repository at this point in the history
Filtering and Sorting is not yet supported.

Note: The packages `errs` and `restErrorHandler` as well as the function
`PrepareResultSelector` was duplicated from the VI backend repo.
Ultimately this code should be moved to
[greenbone/opensight-golang-libraries](https://github.com/greenbone/opensight-golang-libraries).
  • Loading branch information
mgoetzegb authored May 13, 2024
2 parents f9185b5 + 7ae85d4 commit b4ae8be
Show file tree
Hide file tree
Showing 24 changed files with 1,076 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
name: OpenSight Notification Service
uses: greenbone/workflows/.github/workflows/helm-container-build-push-3rd-gen.yml@main
with:
# helm-chart: opensight-notification-service # TODO: reference relevant helm chart as soon as it exists
helm-chart: opensight-notification-service
image-url: ${{ github.repository }}
image-labels: |
org.opencontainers.image.vendor=Greenbone
Expand Down
10 changes: 10 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
with-expecter: true
dir: "{{.InterfaceDir}}/mocks"
outpkg: "mocks"
mockname: "{{.InterfaceName}}"
filename: "{{.InterfaceNameSnake}}.go"

packages:
github.com/greenbone/opensight-notification-service/pkg/port:
interfaces:
NotificationService:
8 changes: 7 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ COPY pkg/web pkg/web
COPY pkg/models pkg/models
RUN make api-docs

# copy rest of the source files and build
# copy rest of the source files
COPY cmd cmd
COPY pkg pkg

# (re)generate mocks
COPY .mockery.yaml .mockery.yaml
RUN make generate-code

# test and build
RUN make test
RUN make build

Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
all: api-docs build test

SWAG = github.com/swaggo/swag/cmd/[email protected]
MOCKERY = github.com/vektra/mockery/[email protected]
GOLANGCI-LINT = github.com/golangci/golangci-lint/cmd/golangci-lint@latest

.PHONY: lint
Expand All @@ -10,6 +11,11 @@ lint:
.PHONY: install-code-generation-tools
install-code-generation-tools:
go install $(SWAG)
go install $(MOCKERY)

.PHONY: generate-code
generate-code: # create mocks
go run $(MOCKERY)

.PHONY: api-docs
api-docs:
Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ module github.com/greenbone/opensight-notification-service
go 1.22.2

require (
github.com/gin-contrib/cors v1.7.2
github.com/gin-contrib/logger v1.1.2
github.com/gin-gonic/gin v1.10.0
github.com/golang-migrate/migrate/v4 v4.17.1
github.com/greenbone/opensight-golang-libraries v1.3.1
github.com/jmoiron/sqlx v1.4.0
github.com/rs/zerolog v1.32.0
github.com/samber/lo v1.39.0
github.com/stretchr/testify v1.9.0
github.com/swaggo/files v1.0.1
github.com/swaggo/gin-swagger v1.6.0
github.com/swaggo/swag v1.16.3
Expand All @@ -20,6 +23,7 @@ require (
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
Expand Down Expand Up @@ -51,13 +55,15 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.18.2 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gin-contrib/cors v1.7.2 h1:oLDHxdg8W/XDoN/8zamqk/Drgt4oVZDvaV0YmvVICQw=
github.com/gin-contrib/cors v1.7.2/go.mod h1:SUJVARKgQ40dmrzgXEVxj2m7Ig1v1qIboQkPDTQ9t2E=
github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4=
github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk=
github.com/gin-contrib/logger v1.1.2 h1:+y8VHqn5zAsAFnW6y/6GF93eXaCPFv/XPdqUCFeBMRg=
Expand Down Expand Up @@ -163,6 +165,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
Expand All @@ -176,6 +180,7 @@ github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMV
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
45 changes: 45 additions & 0 deletions pkg/errs/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-FileCopyrightText: 2024 Greenbone AG <https://greenbone.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package errs

import (
"errors"
"fmt"
)

// ErrItemNotFound is the error used when looking up an item by ID, e.g.
// OID for VTs, fails because the item cannot be found.
var ErrItemNotFound = errors.New("item not found")

// embed this error to mark an error as retryable
var ErrRetryable = errors.New("(retryable error)")

// ErrConflict indicates a conflict. If there are certain fields conflicting which are meaningful to the client,
// set the individual error message for a property via `Errors`, otherwise just set `Message`.
type ErrConflict struct {
Message string
Errors map[string]string // maps property to specific error message
}

func (e *ErrConflict) Error() string {
message := e.Message
if len(e.Errors) > 0 {
message += fmt.Sprintf(", specific errors: %v", e.Errors)
}
return message
}

type ErrValidation struct {
Message string
Errors map[string]string // maps property to specific error message
}

func (e *ErrValidation) Error() string {
message := e.Message
if len(e.Errors) > 0 {
message += fmt.Sprintf(", specific errors: %v", e.Errors)
}
return message
}
18 changes: 18 additions & 0 deletions pkg/helper/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-FileCopyrightText: 2024 Greenbone AG <https://greenbone.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package helper

func ToPtr[T any](val T) *T {
return &val
}

// SafeDereference return the value ptr points to. If ptr is nil, it returns the default value if the type instead.
func SafeDereference[T any](ptr *T) T {
var zeroT T
if ptr == nil {
return zeroT
}
return *ptr
}
43 changes: 43 additions & 0 deletions pkg/helper/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-FileCopyrightText: 2024 Greenbone AG <https://greenbone.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package helper

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSafeDereference(t *testing.T) {
type someStruct struct {
foo, bar string
}

zeroValue := someStruct{}
nonZeroValue := someStruct{foo: "aa", bar: "bb"}

tests := []struct {
name string
input *someStruct
want someStruct
}{
{
name: "yields zero value with nil pointer",
input: nil,
want: zeroValue,
},
{
name: "yields object pointed on for non-nil pointer",
input: &nonZeroValue,
want: nonZeroValue,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := SafeDereference(tt.input)
assert.Equal(t, tt.want, got)
})
}
}
16 changes: 8 additions & 8 deletions pkg/models/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
package models

type Notification struct {
Id string `json:"id" db:"id" readonly:"true"`
Origin string `json:"origin" db:"origin" binding:"required"`
OriginUri string `json:"originUri,omitempty" db:"origin"` // can be used to provide a link to the origin
Timestamp string `json:"timestamp" db:"timestamp" binding:"required" format:"date-time"`
Title string `json:"title" db:"title" binding:"required"` // can also be seen as the 'type'
Detail string `json:"detail" db:"detail" binding:"required"`
Level string `json:"level" db:"level" binding:"required" enums:"info,warning,error,critical"`
CustomFields map[string]any `json:"customFields,omitempty" db:"custom_fields"` // can contain arbitrary structured information about the notification
Id string `json:"id" readonly:"true"`
Origin string `json:"origin" binding:"required"`
OriginUri string `json:"originUri,omitempty"` // can be used to provide a link to the origin
Timestamp string `json:"timestamp" binding:"required" format:"date-time"`
Title string `json:"title" binding:"required"` // can also be seen as the 'type'
Detail string `json:"detail" binding:"required"`
Level string `json:"level" binding:"required" enums:"info,warning,error,critical"`
CustomFields map[string]any `json:"customFields,omitempty"` // can contain arbitrary structured information about the notification
}
Loading

0 comments on commit b4ae8be

Please sign in to comment.