swirl/controller/volume.go

115 lines
3.0 KiB
Go
Raw Normal View History

2017-09-26 12:50:09 +00:00
package controller
import (
"strings"
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/auxo/util/cast"
"github.com/cuigh/swirl/biz"
"github.com/cuigh/swirl/biz/docker"
2017-10-10 07:26:07 +00:00
"github.com/cuigh/swirl/misc"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/swirl/model"
)
2017-10-11 03:26:03 +00:00
// VolumeController is a controller of docker volume
2017-09-26 12:50:09 +00:00
type VolumeController struct {
List web.HandlerFunc `path:"/" name:"volume.list" authorize:"!" desc:"volume list page"`
New web.HandlerFunc `path:"/new" name:"volume.new" authorize:"!" desc:"new volume page"`
Create web.HandlerFunc `path:"/create" method:"post" name:"volume.create" authorize:"!" desc:"create volume"`
Delete web.HandlerFunc `path:"/delete" method:"post" name:"volume.delete" authorize:"!" desc:"delete volume"`
Prune web.HandlerFunc `path:"/prune" method:"post" name:"volume.prune" authorize:"!" desc:"prune volume"`
Detail web.HandlerFunc `path:"/:name/detail" name:"volume.detail" authorize:"!" desc:"volume detail page"`
Raw web.HandlerFunc `path:"/:name/raw" name:"volume.raw" authorize:"!" desc:"volume raw page"`
}
2017-10-11 03:26:03 +00:00
// Volume creates an instance of VolumeController
2017-09-26 12:50:09 +00:00
func Volume() (c *VolumeController) {
2017-10-11 03:26:03 +00:00
return &VolumeController{
List: volumeList,
New: volumeNew,
Create: volumeCreate,
Delete: volumeDelete,
Prune: volumePrune,
Detail: volumeDetail,
Raw: volumeRaw,
2017-09-26 12:50:09 +00:00
}
2017-10-11 03:26:03 +00:00
}
2017-09-26 12:50:09 +00:00
2017-10-11 03:26:03 +00:00
func volumeList(ctx web.Context) error {
2018-04-16 10:49:33 +00:00
name := ctx.Q("name")
2017-10-19 06:24:38 +00:00
page := cast.ToInt(ctx.Q("page"), 1)
2018-04-16 10:49:33 +00:00
volumes, totalCount, err := docker.VolumeList(name, page, model.PageSize)
2017-10-11 03:26:03 +00:00
if err != nil {
return err
2017-09-26 12:50:09 +00:00
}
2017-10-11 03:26:03 +00:00
m := newPagerModel(ctx, totalCount, model.PageSize, page).
2018-04-16 10:49:33 +00:00
Set("Name", name).
2017-11-08 10:36:13 +00:00
Set("Volumes", volumes)
2017-10-11 03:26:03 +00:00
return ctx.Render("volume/list", m)
}
2017-09-26 12:50:09 +00:00
2017-10-11 03:26:03 +00:00
func volumeNew(ctx web.Context) error {
m := newModel(ctx)
return ctx.Render("/volume/new", m)
}
func volumeCreate(ctx web.Context) error {
info := &model.VolumeCreateInfo{}
2017-11-17 04:31:19 +00:00
err := ctx.Bind(info, true)
2017-10-11 03:26:03 +00:00
if err != nil {
return err
2017-09-26 12:50:09 +00:00
}
2017-10-11 03:26:03 +00:00
err = docker.VolumeCreate(info)
if err == nil {
biz.Event.CreateVolume(model.EventActionCreate, info.Name, ctx.User())
}
return ajaxResult(ctx, err)
}
2017-09-26 12:50:09 +00:00
2017-10-11 03:26:03 +00:00
func volumeDelete(ctx web.Context) error {
names := strings.Split(ctx.F("names"), ",")
for _, name := range names {
if err := docker.VolumeRemove(name); err != nil {
return ajaxResult(ctx, err)
} else {
biz.Event.CreateVolume(model.EventActionDelete, name, ctx.User())
2017-09-26 12:50:09 +00:00
}
}
2017-10-11 03:26:03 +00:00
return ajaxSuccess(ctx, nil)
}
2017-09-26 12:50:09 +00:00
2017-10-11 03:26:03 +00:00
func volumePrune(ctx web.Context) error {
report, err := docker.VolumePrune()
if err == nil {
return ajaxSuccess(ctx, report)
2017-09-26 12:50:09 +00:00
}
2017-10-11 03:26:03 +00:00
return ajaxResult(ctx, err)
}
2017-09-26 12:50:09 +00:00
2017-10-11 03:26:03 +00:00
func volumeDetail(ctx web.Context) error {
name := ctx.P("name")
volume, _, err := docker.VolumeInspectRaw(name)
if err != nil {
return err
}
2017-11-08 10:36:13 +00:00
m := newModel(ctx).Set("Volume", volume)
2017-10-11 03:26:03 +00:00
return ctx.Render("volume/detail", m)
}
2017-09-26 12:50:09 +00:00
2017-10-11 03:26:03 +00:00
func volumeRaw(ctx web.Context) error {
name := ctx.P("name")
_, raw, err := docker.VolumeInspectRaw(name)
if err != nil {
return err
}
2017-09-26 12:50:09 +00:00
2017-10-11 03:26:03 +00:00
j, err := misc.JSONIndent(raw)
if err != nil {
return err
2017-09-26 12:50:09 +00:00
}
2017-11-08 10:36:13 +00:00
m := newModel(ctx).Set("Volume", name).Set("Raw", j)
2017-10-11 03:26:03 +00:00
return ctx.Render("volume/raw", m)
2017-09-26 12:50:09 +00:00
}