Files
terminal/server/api/ssh_gateway_api.go
T

111 lines
2.6 KiB
Go

package api
import (
"context"
"strconv"
"next-terminal/server/common/maps"
"next-terminal/server/model"
"next-terminal/server/repository"
"next-terminal/server/utils"
"github.com/labstack/echo/v4"
)
type SshGatewayApi struct{}
func (api SshGatewayApi) AllEndpoint(c echo.Context) error {
items, err := repository.SshGatewayRepository.FindAll(context.TODO())
if err != nil {
return err
}
return Success(c, items)
}
func (api SshGatewayApi) PagingEndpoint(c echo.Context) error {
pageIndex, _ := strconv.Atoi(c.QueryParam("pageIndex"))
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
name := c.QueryParam("name")
items, total, err := repository.SshGatewayRepository.Find(context.TODO(), pageIndex, pageSize, name)
if err != nil {
return err
}
return Success(c, maps.Map{
"total": total,
"items": items,
})
}
func (api SshGatewayApi) CreateEndpoint(c echo.Context) error {
var item model.SshGateway
if err := c.Bind(&item); err != nil {
return err
}
item.ID = utils.UUID()
item.Type = "ssh"
item.Status = "unknown"
if err := repository.SshGatewayRepository.Create(context.TODO(), &item); err != nil {
return err
}
return Success(c, "")
}
func (api SshGatewayApi) UpdateEndpoint(c echo.Context) error {
id := c.Param("id")
var item model.SshGateway
if err := c.Bind(&item); err != nil {
return err
}
if err := repository.SshGatewayRepository.UpdateById(context.TODO(), &item, id); err != nil {
return err
}
return Success(c, nil)
}
func (api SshGatewayApi) DeleteEndpoint(c echo.Context) error {
id := c.Param("id")
if err := repository.SshGatewayRepository.DeleteById(context.TODO(), id); err != nil {
return err
}
return Success(c, nil)
}
func (api SshGatewayApi) GetEndpoint(c echo.Context) error {
id := c.Param("id")
item, err := repository.SshGatewayRepository.FindById(context.TODO(), id)
if err != nil {
return err
}
return Success(c, item)
}
func (api SshGatewayApi) DecryptedEndpoint(c echo.Context) error {
id := c.Param("id")
item, err := repository.SshGatewayRepository.FindById(context.TODO(), id)
if err != nil {
return err
}
return Success(c, item)
}
func (api SshGatewayApi) AvailableForGatewayEndpoint(c echo.Context) error {
assets, err := repository.SshGatewayRepository.FindAvailableAssets(context.TODO())
if err != nil {
return err
}
result := make([]maps.Map, len(assets))
for i, a := range assets {
result[i] = maps.Map{
"id": a.ID,
"name": a.Name,
"ip": a.IP,
"port": a.Port,
"canBeGateway": true,
"disableReason": "",
}
}
return Success(c, result)
}