swirl/controller/secret.go

100 lines
2.6 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"
"github.com/cuigh/swirl/model"
)
2017-10-11 03:26:03 +00:00
// SecretController is a controller of docker secret
2017-09-26 12:50:09 +00:00
type SecretController struct {
List web.HandlerFunc `path:"/" name:"secret.list" authorize:"!" desc:"secret list page"`
Delete web.HandlerFunc `path:"/delete" method:"post" name:"secret.delete" authorize:"!" desc:"delete secret"`
New web.HandlerFunc `path:"/new" name:"secret.new" authorize:"!" desc:"new secret page"`
Create web.HandlerFunc `path:"/new" method:"post" name:"secret.create" authorize:"!" desc:"create secret"`
2017-09-27 07:36:50 +00:00
Edit web.HandlerFunc `path:"/:id/edit" name:"secret.edit" authorize:"!" desc:"edit secret page"`
Update web.HandlerFunc `path:"/:id/update" method:"post" name:"secret.update" authorize:"!" desc:"update secret"`
2017-09-26 12:50:09 +00:00
}
2017-10-11 03:26:03 +00:00
// Secret creates an instance of SecretController
2017-09-26 12:50:09 +00:00
func Secret() (c *SecretController) {
2017-10-11 03:26:03 +00:00
return &SecretController{
List: secretList,
Delete: secretDelete,
New: secretNew,
Create: secretCreate,
Edit: secretEdit,
Update: secretUpdate,
2017-09-26 12:50:09 +00:00
}
2017-10-11 03:26:03 +00:00
}
func secretList(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
secrets, totalCount, err := docker.SecretList(name, page, model.PageSize)
if err != nil {
return err
}
m := newPagerModel(ctx, totalCount, model.PageSize, page).
2017-11-08 10:36:13 +00:00
Set("Name", name).
Set("Secrets", secrets)
2017-10-11 03:26:03 +00:00
return ctx.Render("secret/list", m)
}
2017-09-26 12:50:09 +00:00
2017-10-11 03:26:03 +00:00
func secretDelete(ctx web.Context) error {
ids := strings.Split(ctx.F("ids"), ",")
for _, id := range ids {
err := docker.SecretRemove(id)
if err != nil {
return ajaxResult(ctx, err)
} else {
// todo:
biz.Event.CreateSecret(model.EventActionDelete, id, 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 secretNew(ctx web.Context) error {
m := newModel(ctx)
return ctx.Render("secret/new", m)
}
2017-09-26 12:50:09 +00:00
2017-10-11 03:26:03 +00:00
func secretCreate(ctx web.Context) error {
v := &model.ConfigCreateInfo{}
err := ctx.Bind(v)
if err == nil {
err = docker.SecretCreate(v)
2017-09-26 12:50:09 +00:00
if err == nil {
2017-10-11 03:26:03 +00:00
biz.Event.CreateSecret(model.EventActionCreate, 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
2017-10-11 03:26:03 +00:00
func secretEdit(ctx web.Context) error {
id := ctx.P("id")
secret, _, err := docker.SecretInspect(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("Secret", secret)
2017-10-11 03:26:03 +00:00
return ctx.Render("secret/edit", m)
}
2017-09-27 07:36:50 +00:00
2017-10-11 03:26:03 +00:00
func secretUpdate(ctx web.Context) error {
v := &model.ConfigUpdateInfo{}
err := ctx.Bind(v)
if err == nil {
err = docker.SecretUpdate(v)
2017-09-27 07:36:50 +00:00
if err == nil {
2017-10-11 03:26:03 +00:00
biz.Event.CreateSecret(model.EventActionUpdate, 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-26 12:50:09 +00:00
}