Skip to content

Commit

Permalink
Add device registration endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bkuen committed Dec 26, 2023
1 parent cbf5545 commit 3984883
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ tasks:
cmd: goose -dir ./migrations up
gen-http:
cmds:
- oapi-codegen -package wizard -generate types,server ./api/openapi/wizard/wizard.yml > ./api/openapi/wizard/wizard.gen.go
- oapi-codegen -package wizard -generate types,server,strict-server ./api/openapi/wizard/wizard.yml > ./api/openapi/wizard/wizard.gen.go
194 changes: 184 additions & 10 deletions api/openapi/wizard/wizard.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions api/openapi/wizard/wizard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ paths:
/devices:
get:
description: Returns a list of devices
operationId: findDevices
responses:
'200':
description: Successfully returned a list of devices
Expand All @@ -29,6 +30,14 @@ paths:
$ref: "#/components/schemas/Device"
post:
description: Registers a new device
operationId: registerDevice
requestBody:
required: true
description: Name, kind and IP addresses of the device to be registered
content:
'application/json':
schema:
$ref: "#/components/schemas/RegisterDeviceRequest"
responses:
'200':
description: Successfully registered a new device and returned it
Expand Down Expand Up @@ -72,6 +81,20 @@ paths:

components:
schemas:
RegisterDeviceRequest:
type: object
properties:
name:
type: string
kind:
$ref: "#/components/schemas/DeviceKind"
ips:
type: array
items:
type: string
required:
- kind
- ips
ProtectionStatus:
type: string
enum:
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/wizardapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func StartAPIServer(app application.Application) {
e.Use(echoMiddleware.Recover())
//e.Use(middleware.OapiRequestValidator())

api.RegisterHandlers(e, handler)
strictAPIHandler := api.NewStrictHandler(handler, nil)
api.RegisterHandlers(e, strictAPIHandler)

e.Logger.Fatal(e.Start(net.JoinHostPort("0.0.0.0", *port)))
}
32 changes: 24 additions & 8 deletions internal/pkg/wizardapi/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,49 @@ package wizardapi
import (
"bwizard/api/openapi/wizard"
"bwizard/internal/pkg/wizard/application"
"github.com/labstack/echo/v4"
openapiTypes "github.com/oapi-codegen/runtime/types"
deviceValue "bwizard/internal/pkg/wizard/domain/valueobject/device"
"bwizard/internal/pkg/wizardapi/mappings"
"context"
)

type Handler struct {
app application.Application
}

var _ wizard.ServerInterface = &Handler{}
var _ wizard.StrictServerInterface = &Handler{}

func NewHandler(app application.Application) *Handler {
return &Handler{
app: app,
}
}

func (h *Handler) GetDevices(ctx echo.Context) error {
func (h *Handler) FindDevices(ctx context.Context, request wizard.FindDevicesRequestObject) (wizard.FindDevicesResponseObject, error) {
//TODO implement me
panic("implement me")
}

func (h *Handler) PostDevices(ctx echo.Context) error {
//TODO implement me
panic("implement me")
func (h *Handler) RegisterDevice(ctx context.Context, request wizard.RegisterDeviceRequestObject) (wizard.RegisterDeviceResponseObject, error) {
ips := make([]deviceValue.IPAddress, 0)
for _, ip := range ips {
ips = append(ips, ip)
}

device, err := h.app.SetupDevice(ips, deviceValue.Kind(request.Body.Kind), request.Body.Name)
if err != nil {
// TODO: Add error handling here
return nil, err
}

mappedDevice := mappings.MapDevice(device)

return wizard.RegisterDevice200JSONResponse{
Success: true,
Data: mappedDevice,
}, nil
}

func (h *Handler) GetDevicesId(ctx echo.Context, id openapiTypes.UUID) error {
func (h *Handler) GetDevicesId(ctx context.Context, request wizard.GetDevicesIdRequestObject) (wizard.GetDevicesIdResponseObject, error) {
//TODO implement me
panic("implement me")
}
23 changes: 23 additions & 0 deletions internal/pkg/wizardapi/mappings/device.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package mappings

import (
"bwizard/api/openapi/wizard"
"bwizard/internal/pkg/wizard/domain/entity/device"
)

func MapDevice(device *device.Device) wizard.Device {
ips := make([]string, 0, len(device.IPs))
for _, ip := range ips {
ips = append(ips, ip)
}

return wizard.Device{
Id: device.ID,
Name: device.Name,
Kind: wizard.DeviceKind(device.Kind),
Protection: wizard.ProtectionStatus(device.Protection),
Agent: device.Agent,
Ips: ips,
LastBackup: device.LastBackup,
}
}

0 comments on commit 3984883

Please sign in to comment.