swirl/controller/config.go

93 lines
2.5 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"
2017-09-27 07:36:50 +00:00
"github.com/cuigh/swirl/biz"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/swirl/biz/docker"
"github.com/cuigh/swirl/model"
)
2017-10-11 03:26:03 +00:00
// ConfigController is a controller of docker config
2017-09-26 12:50:09 +00:00
type ConfigController struct {
List web.HandlerFunc `path:"/" name:"config.list" authorize:"!" desc:"config list page"`
Delete web.HandlerFunc `path:"/delete" method:"post" name:"config.delete" authorize:"!" desc:"delete config"`
New web.HandlerFunc `path:"/new" name:"config.new" authorize:"!" desc:"new config page"`
Create web.HandlerFunc `path:"/new" method:"post" name:"config.create" authorize:"!" desc:"create config"`
2017-09-27 07:36:50 +00:00
Edit web.HandlerFunc `path:"/:id/edit" name:"config.edit" authorize:"!" desc:"edit config page"`
Update web.HandlerFunc `path:"/:id/update" method:"post" name:"config.update" authorize:"!" desc:"update config"`
2017-09-26 12:50:09 +00:00
}
2017-10-11 03:26:03 +00:00
// Config creates an instance of ConfigController
2017-09-26 12:50:09 +00:00
func Config() (c *ConfigController) {
2017-10-11 03:26:03 +00:00
return &ConfigController{
List: configList,
Delete: configDelete,
New: configNew,
Create: configCreate,
Edit: configEdit,
Update: configUpdate,
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 configList(ctx web.Context) error {
name := ctx.Q("name")
2017-10-19 06:24:38 +00:00
page := cast.ToInt(ctx.Q("page"), 1)
2017-10-11 03:26:03 +00:00
configs, totalCount, err := docker.ConfigList(name, page, model.PageSize)
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).
2017-11-08 10:36:13 +00:00
Set("Name", name).
Set("Configs", configs)
2017-10-11 03:26:03 +00:00
return ctx.Render("config/list", m)
}
func configDelete(ctx web.Context) error {
ids := strings.Split(ctx.F("ids"), ",")
err := docker.ConfigRemove(ids)
return ajaxResult(ctx, err)
}
func configNew(ctx web.Context) error {
m := newModel(ctx)
return ctx.Render("config/new", m)
}
2017-09-26 12:50:09 +00:00
2017-10-11 03:26:03 +00:00
func configCreate(ctx web.Context) error {
v := &model.ConfigCreateInfo{}
err := ctx.Bind(v)
if err == nil {
err = docker.ConfigCreate(v)
2017-09-26 12:50:09 +00:00
if err == nil {
2017-10-11 03:26:03 +00:00
biz.Event.CreateConfig(model.EventActionCreate, v.Name, ctx.User())
2017-09-27 07:36:50 +00:00
}
}
2017-10-11 03:26:03 +00:00
return ajaxResult(ctx, err)
}
2017-09-27 07:36:50 +00:00
2017-10-11 03:26:03 +00:00
func configEdit(ctx web.Context) error {
id := ctx.P("id")
cfg, _, err := docker.ConfigInspect(id)
if err != nil {
return err
2017-09-27 07:36:50 +00:00
}
2017-11-08 10:36:13 +00:00
m := newModel(ctx).Set("Config", cfg)
2017-10-11 03:26:03 +00:00
return ctx.Render("config/edit", m)
}
2017-09-27 07:36:50 +00:00
2017-10-11 03:26:03 +00:00
func configUpdate(ctx web.Context) error {
v := &model.ConfigUpdateInfo{}
err := ctx.Bind(v)
if err == nil {
err = docker.ConfigUpdate(v)
2017-09-27 07:36:50 +00:00
if err == nil {
2017-10-11 03:26:03 +00:00
biz.Event.CreateConfig(model.EventActionUpdate, v.Name, ctx.User())
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
}