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"
|
2022-01-06 08:54:14 +00:00
|
|
|
"github.com/cuigh/swirl/misc"
|
2021-12-06 12:24:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NetworkHandler encapsulates network related handlers.
|
|
|
|
type NetworkHandler struct {
|
|
|
|
Search web.HandlerFunc `path:"/search" auth:"network.view" desc:"search networks"`
|
|
|
|
Find web.HandlerFunc `path:"/find" auth:"network.view" desc:"find network by name"`
|
|
|
|
Delete web.HandlerFunc `path:"/delete" method:"post" auth:"network.delete" desc:"delete network"`
|
|
|
|
Save web.HandlerFunc `path:"/save" method:"post" auth:"network.edit" desc:"create or update network"`
|
|
|
|
Disconnect web.HandlerFunc `path:"/disconnect" method:"post" auth:"network.disconnect" desc:"disconnect container from network"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNetwork creates an instance of NetworkHandler
|
|
|
|
func NewNetwork(nb biz.NetworkBiz) *NetworkHandler {
|
|
|
|
return &NetworkHandler{
|
|
|
|
Search: networkSearch(nb),
|
|
|
|
Find: networkFind(nb),
|
|
|
|
Delete: networkDelete(nb),
|
|
|
|
Save: networkSave(nb),
|
|
|
|
Disconnect: networkDisconnect(nb),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func networkSearch(nb biz.NetworkBiz) web.HandlerFunc {
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) error {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
networks, err := nb.Search(ctx)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return success(c, networks)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func networkFind(nb biz.NetworkBiz) web.HandlerFunc {
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) error {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
name := c.Query("name")
|
|
|
|
network, raw, err := nb.Find(ctx, name)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return success(c, data.Map{"network": network, "raw": raw})
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func networkDelete(nb biz.NetworkBiz) web.HandlerFunc {
|
|
|
|
type Args struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
|
|
|
|
return func(c web.Context) (err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
args := &Args{}
|
2022-01-06 08:54:14 +00:00
|
|
|
if err = c.Bind(args); err == nil {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err = nb.Delete(ctx, args.ID, args.Name, c.User())
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return ajax(c, err)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func networkSave(nb biz.NetworkBiz) web.HandlerFunc {
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) error {
|
2021-12-06 12:24:22 +00:00
|
|
|
n := &biz.Network{}
|
2022-01-06 08:54:14 +00:00
|
|
|
err := c.Bind(n, true)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err == nil {
|
2022-01-06 08:54:14 +00:00
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err = nb.Create(ctx, n, c.User())
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return ajax(c, err)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func networkDisconnect(nb biz.NetworkBiz) web.HandlerFunc {
|
|
|
|
type Args struct {
|
|
|
|
NetworkID string `json:"networkId"`
|
|
|
|
NetworkName string `json:"networkName"`
|
|
|
|
Container string `json:"container"`
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
|
|
|
|
return func(c web.Context) error {
|
2021-12-06 12:24:22 +00:00
|
|
|
args := &Args{}
|
2022-01-06 08:54:14 +00:00
|
|
|
err := c.Bind(args, true)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err == nil {
|
2022-01-06 08:54:14 +00:00
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err = nb.Disconnect(ctx, args.NetworkID, args.NetworkName, args.Container, c.User())
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return ajax(c, err)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|