swirl/api/volume.go

117 lines
2.8 KiB
Go
Raw Normal View History

2021-12-06 12:24:22 +00:00
package api
import (
"github.com/cuigh/auxo/data"
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/swirl/biz"
)
// VolumeHandler encapsulates volume related handlers.
type VolumeHandler struct {
Search web.HandlerFunc `path:"/search" auth:"volume.view" desc:"search volumes"`
Find web.HandlerFunc `path:"/find" auth:"volume.view" desc:"find volume by name"`
Delete web.HandlerFunc `path:"/delete" method:"post" auth:"volume.delete" desc:"delete volume"`
Save web.HandlerFunc `path:"/save" method:"post" auth:"volume.edit" desc:"create or update volume"`
Prune web.HandlerFunc `path:"/prune" method:"post" auth:"volume.delete" desc:"delete unused volumes"`
}
// NewVolume creates an instance of VolumeHandler
func NewVolume(b biz.VolumeBiz) *VolumeHandler {
return &VolumeHandler{
Search: volumeSearch(b),
Find: volumeFind(b),
Delete: volumeDelete(b),
Save: volumeSave(b),
Prune: volumePrune(b),
}
}
func volumeSearch(b biz.VolumeBiz) web.HandlerFunc {
type Args struct {
2021-12-20 06:28:43 +00:00
Node string `json:"node" bind:"node"`
2021-12-06 12:24:22 +00:00
Name string `json:"name" bind:"name"`
PageIndex int `json:"pageIndex" bind:"pageIndex"`
PageSize int `json:"pageSize" bind:"pageSize"`
}
return func(ctx web.Context) (err error) {
var (
args = &Args{}
volumes []*biz.Volume
total int
)
if err = ctx.Bind(args); err == nil {
2021-12-20 06:28:43 +00:00
volumes, total, err = b.Search(args.Node, args.Name, args.PageIndex, args.PageSize)
2021-12-06 12:24:22 +00:00
}
if err != nil {
return
}
return success(ctx, data.Map{
"items": volumes,
"total": total,
})
}
}
func volumeFind(b biz.VolumeBiz) web.HandlerFunc {
return func(ctx web.Context) error {
2021-12-20 06:28:43 +00:00
node := ctx.Query("node")
2021-12-06 12:24:22 +00:00
name := ctx.Query("name")
2021-12-20 06:28:43 +00:00
volume, raw, err := b.Find(node, name)
2021-12-06 12:24:22 +00:00
if err != nil {
return err
}
return success(ctx, data.Map{"volume": volume, "raw": raw})
}
}
func volumeDelete(b biz.VolumeBiz) web.HandlerFunc {
type Args struct {
2021-12-20 06:28:43 +00:00
Node string `json:"node"`
2021-12-06 12:24:22 +00:00
Name string `json:"name"`
}
return func(ctx web.Context) (err error) {
args := &Args{}
if err = ctx.Bind(args); err == nil {
2021-12-20 06:28:43 +00:00
err = b.Delete(args.Node, args.Name, ctx.User())
2021-12-06 12:24:22 +00:00
}
return ajax(ctx, err)
}
}
func volumeSave(b biz.VolumeBiz) web.HandlerFunc {
return func(ctx web.Context) error {
v := &biz.Volume{}
err := ctx.Bind(v, true)
if err == nil {
err = b.Create(v, ctx.User())
}
return ajax(ctx, err)
}
}
func volumePrune(b biz.VolumeBiz) web.HandlerFunc {
2021-12-20 06:28:43 +00:00
type Args struct {
Node string `json:"node"`
}
return func(ctx web.Context) (err error) {
args := &Args{}
if err = ctx.Bind(args); err != nil {
return err
}
deletedVolumes, reclaimedSpace, err := b.Prune(args.Node, ctx.User())
2021-12-06 12:24:22 +00:00
if err != nil {
return err
}
2021-12-20 06:28:43 +00:00
2021-12-06 12:24:22 +00:00
return success(ctx, data.Map{
"deletedVolumes": deletedVolumes,
"reclaimedSpace": reclaimedSpace,
})
}
}