Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 11 additions & 1 deletion internal/api/compute/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"net/http"

appcompute "github.com/JSYoo5B/SandStack/internal/app/compute"
"github.com/JSYoo5B/SandStack/internal/platform/clock"
"github.com/JSYoo5B/SandStack/internal/platform/config"
"github.com/JSYoo5B/SandStack/internal/platform/idgen"
storecompute "github.com/JSYoo5B/SandStack/internal/store/compute"
"github.com/go-chi/chi/v5"
)

Expand All @@ -25,7 +28,14 @@ func NewRouterWithService(
}

func NewHandler(cfg config.Config) Handler {
return NewHandlerWithService(cfg, appcompute.NewService())
return NewHandlerWithService(
cfg,
appcompute.NewServiceWithRuntime(
storecompute.NewMemoryServerRepository(),
clock.Wall(),
idgen.Random(),
),
)
}

func NewHandlerWithService(
Expand Down
26 changes: 26 additions & 0 deletions internal/api/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ func (h Handler) getImage(w http.ResponseWriter, r *http.Request) {
respond.JSON(w, http.StatusOK, toImageDocument(image))
}

func (h Handler) updateImage(w http.ResponseWriter, r *http.Request) {
var operations []imagePatchOperation
if err := json.NewDecoder(r.Body).Decode(&operations); err != nil {
respond.Error(w, http.StatusBadRequest, "invalid JSON request body")
return
}

patch, err := toPatchImage(operations)
if err != nil {
respond.Error(w, http.StatusBadRequest, "invalid image patch")
return
}

image, err := h.service.Update(chi.URLParam(r, "image_id"), patch)
if errors.Is(err, appimage.ErrImageNotFound) {
respond.Error(w, http.StatusNotFound, "image not found")
return
}
if err != nil {
respond.Error(w, http.StatusInternalServerError, "image update failed")
return
}

respond.JSON(w, http.StatusOK, toImageDocument(image))
}

func (h Handler) deleteImage(w http.ResponseWriter, r *http.Request) {
err := h.service.Delete(chi.URLParam(r, "image_id"))
if errors.Is(err, appimage.ErrImageNotFound) {
Expand Down
22 changes: 22 additions & 0 deletions internal/api/image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ func (s *ImageSuite) TestGetImage() {
s.Assert().Equal("ubuntu", found.Name)
}

func (s *ImageSuite) TestUpdateImage() {
created := s.createImage("ubuntu")

updated, err := images.Update(
s.T().Context(),
testhelper.ServiceClient(s.server.URL),
created.ID,
images.UpdateOpts{
images.ReplaceImageName{NewName: "ubuntu-updated"},
images.ReplaceImageMinDisk{NewMinDisk: 2},
images.ReplaceImageTags{NewTags: []string{"linux", "test"}},
},
).Extract()
s.Require().NoError(err)
s.Require().NotNil(updated)

s.Assert().Equal(created.ID, updated.ID)
s.Assert().Equal("ubuntu-updated", updated.Name)
s.Assert().Equal(2, updated.MinDiskGigabytes)
s.Assert().Equal([]string{"linux", "test"}, updated.Tags)
}

func (s *ImageSuite) TestDeleteImage() {
created := s.createImage("ubuntu")

Expand Down
13 changes: 12 additions & 1 deletion internal/api/image/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"net/http"

appimage "github.com/JSYoo5B/SandStack/internal/app/image"
"github.com/JSYoo5B/SandStack/internal/platform/clock"
"github.com/JSYoo5B/SandStack/internal/platform/config"
"github.com/JSYoo5B/SandStack/internal/platform/idgen"
storeimage "github.com/JSYoo5B/SandStack/internal/store/image"
"github.com/go-chi/chi/v5"
)

Expand All @@ -25,7 +28,14 @@ func NewRouterWithService(
}

func NewHandler(cfg config.Config) Handler {
return NewHandlerWithService(cfg, appimage.NewService())
return NewHandlerWithService(
cfg,
appimage.NewServiceWithRuntime(
storeimage.NewMemoryRepository(),
clock.Wall(),
idgen.Random(),
),
)
}

func NewHandlerWithService(
Expand All @@ -44,6 +54,7 @@ func (h Handler) Router() http.Handler {
router.Get("/images", h.listImages)
router.Post("/images", h.createImage)
router.Get("/images/{image_id}", h.getImage)
router.Patch("/images/{image_id}", h.updateImage)
router.Delete("/images/{image_id}", h.deleteImage)

return router
Expand Down
78 changes: 78 additions & 0 deletions internal/api/image/update_dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package image

import (
"encoding/json"
"fmt"

appimage "github.com/JSYoo5B/SandStack/internal/app/image"
)

type imagePatchOperation struct {
Op string `json:"op"`
Path string `json:"path"`
Value json.RawMessage `json:"value"`
}

func toPatchImage(operations []imagePatchOperation) (appimage.PatchImage, error) {
var patch appimage.PatchImage
for _, operation := range operations {
if operation.Op != "replace" {
return appimage.PatchImage{}, fmt.Errorf("unsupported patch operation")
}

if err := applyPatchOperation(&patch, operation); err != nil {
return appimage.PatchImage{}, err
}
}

return patch, nil
}

func applyPatchOperation(
patch *appimage.PatchImage,
operation imagePatchOperation,
) error {
switch operation.Path {
case "/name":
var value string
if err := json.Unmarshal(operation.Value, &value); err != nil {
return err
}
patch.Name = &value
case "/min_disk":
var value int
if err := json.Unmarshal(operation.Value, &value); err != nil {
return err
}
patch.MinDisk = &value
case "/min_ram":
var value int
if err := json.Unmarshal(operation.Value, &value); err != nil {
return err
}
patch.MinRAM = &value
case "/protected":
var value bool
if err := json.Unmarshal(operation.Value, &value); err != nil {
return err
}
patch.Protected = &value
case "/visibility":
var value string
if err := json.Unmarshal(operation.Value, &value); err != nil {
return err
}
patch.Visibility = &value
case "/tags":
var value []string
if err := json.Unmarshal(operation.Value, &value); err != nil {
return err
}
patch.Tags = value
patch.HasTags = true
default:
return fmt.Errorf("unsupported patch path")
}

return nil
}
12 changes: 11 additions & 1 deletion internal/api/network/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

appnetwork "github.com/JSYoo5B/SandStack/internal/app/network"
"github.com/JSYoo5B/SandStack/internal/platform/config"
"github.com/JSYoo5B/SandStack/internal/platform/idgen"
storenetwork "github.com/JSYoo5B/SandStack/internal/store/network"
"github.com/go-chi/chi/v5"
)

Expand All @@ -25,7 +27,15 @@ func NewRouterWithService(
}

func NewHandler(cfg config.Config) Handler {
return NewHandlerWithService(cfg, appnetwork.NewService())
return NewHandlerWithService(
cfg,
appnetwork.NewServiceWithRepositories(
storenetwork.NewMemoryNetworkRepository(),
storenetwork.NewMemorySubnetRepository(),
storenetwork.NewMemoryPortRepository(),
idgen.Random(),
),
)
}

func NewHandlerWithService(
Expand Down
31 changes: 27 additions & 4 deletions internal/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import (
appnetwork "github.com/JSYoo5B/SandStack/internal/app/network"
"github.com/JSYoo5B/SandStack/internal/app/requestlog"
appvolume "github.com/JSYoo5B/SandStack/internal/app/volume"
"github.com/JSYoo5B/SandStack/internal/platform/clock"
"github.com/JSYoo5B/SandStack/internal/platform/config"
"github.com/JSYoo5B/SandStack/internal/platform/idgen"
storecompute "github.com/JSYoo5B/SandStack/internal/store/compute"
storeimage "github.com/JSYoo5B/SandStack/internal/store/image"
storenetwork "github.com/JSYoo5B/SandStack/internal/store/network"
storevolume "github.com/JSYoo5B/SandStack/internal/store/volume"
"github.com/go-chi/chi/v5"
)

Expand All @@ -25,10 +31,27 @@ func NewRouter(cfg config.Config) http.Handler {
requests := requestlog.NewService()
router.Use(recordRequests(requests))
identityHandler := identity.NewHandler(cfg)
computeService := appcompute.NewService()
imageService := appimage.NewService()
networkService := appnetwork.NewService()
volumeService := appvolume.NewService()
computeService := appcompute.NewServiceWithRuntime(

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This router now acts as the composition root for default in-memory services. The important dependency direction is API -> app interfaces plus store implementations supplied here, not app -> store concrete types.

storecompute.NewMemoryServerRepository(),
clock.Wall(),
idgen.Random(),
)
imageService := appimage.NewServiceWithRuntime(
storeimage.NewMemoryRepository(),
clock.Wall(),
idgen.Random(),
)
networkService := appnetwork.NewServiceWithRepositories(

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Network has multiple repositories earlier than the other services because networks, subnets, and ports already have independent state. This is the representative wiring pattern for services that grow more than one resource store.

storenetwork.NewMemoryNetworkRepository(),
storenetwork.NewMemorySubnetRepository(),
storenetwork.NewMemoryPortRepository(),
idgen.Random(),
)
volumeService := appvolume.NewServiceWithRuntime(
storevolume.NewMemoryRepository(),
clock.Wall(),
idgen.Random(),
)

router.Mount("/_sandstack", admin.NewRouterWithState(func() {
computeService.Reset()
Expand Down
13 changes: 12 additions & 1 deletion internal/api/volume/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"net/http"

appvolume "github.com/JSYoo5B/SandStack/internal/app/volume"
"github.com/JSYoo5B/SandStack/internal/platform/clock"
"github.com/JSYoo5B/SandStack/internal/platform/config"
"github.com/JSYoo5B/SandStack/internal/platform/idgen"
storevolume "github.com/JSYoo5B/SandStack/internal/store/volume"
"github.com/go-chi/chi/v5"
)

Expand All @@ -25,7 +28,14 @@ func NewRouterWithService(
}

func NewHandler(cfg config.Config) Handler {
return NewHandlerWithService(cfg, appvolume.NewService())
return NewHandlerWithService(
cfg,
appvolume.NewServiceWithRuntime(
storevolume.NewMemoryRepository(),
clock.Wall(),
idgen.Random(),
),
)
}

func NewHandlerWithService(
Expand All @@ -45,6 +55,7 @@ func (h Handler) Router() http.Handler {
router.Get("/{project_id}/volumes/detail", h.listVolumes)
router.Post("/{project_id}/volumes", h.createVolume)
router.Get("/{project_id}/volumes/{volume_id}", h.getVolume)
router.Put("/{project_id}/volumes/{volume_id}", h.updateVolume)
router.Delete("/{project_id}/volumes/{volume_id}", h.deleteVolume)
router.Get("/{project_id}/types", h.listVolumeTypes)
router.Get("/{project_id}/types/{type_id}", h.getVolumeType)
Expand Down
25 changes: 25 additions & 0 deletions internal/api/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@ func (h Handler) getVolume(w http.ResponseWriter, r *http.Request) {
})
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update endpoints follow the existing handler convention rather than introducing a generic patch layer. That keeps this slice scoped to the compatibility calls currently needed.


func (h Handler) updateVolume(w http.ResponseWriter, r *http.Request) {
var request updateVolumeRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
respond.Error(w, http.StatusBadRequest, "invalid JSON request body")
return
}

volume, err := h.service.Update(
chi.URLParam(r, "volume_id"),
request.updateVolume(),
)
if errors.Is(err, appvolume.ErrVolumeNotFound) {
respond.Error(w, http.StatusNotFound, "volume not found")
return
}
if err != nil {
respond.Error(w, http.StatusInternalServerError, "volume update failed")
return
}

respond.JSON(w, http.StatusOK, volumeResponse{
Volume: toVolumeDocument(volume),
})
}

func (h Handler) deleteVolume(w http.ResponseWriter, r *http.Request) {
err := h.service.Delete(chi.URLParam(r, "volume_id"))
if errors.Is(err, appvolume.ErrVolumeNotFound) {
Expand Down
16 changes: 16 additions & 0 deletions internal/api/volume/volume_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ type createVolumeRequest struct {
} `json:"volume"`
}

type updateVolumeRequest struct {
Volume struct {
Name *string `json:"name"`
Description *string `json:"description"`
Metadata map[string]string `json:"metadata"`
} `json:"volume"`
}

func (r createVolumeRequest) createVolume() appvolume.CreateVolume {
return appvolume.CreateVolume{
Size: r.Volume.Size,
Expand All @@ -22,6 +30,14 @@ func (r createVolumeRequest) createVolume() appvolume.CreateVolume {
}
}

func (r updateVolumeRequest) updateVolume() appvolume.UpdateVolume {
return appvolume.UpdateVolume{
Name: r.Volume.Name,
Description: r.Volume.Description,
Metadata: r.Volume.Metadata,
}
}

type volumeListResponse struct {
Volumes []volumeDocument `json:"volumes"`
}
Expand Down
Loading
Loading