nezha/cmd/dashboard/controller/server_group.go

229 lines
5.4 KiB
Go
Raw Normal View History

2024-10-20 11:23:04 -04:00
package controller
import (
"slices"
"strconv"
2024-10-20 11:23:04 -04:00
"github.com/gin-gonic/gin"
2024-10-21 11:00:51 -04:00
"gorm.io/gorm"
2024-10-21 04:22:30 -04:00
2024-11-28 06:38:54 -05:00
"github.com/nezhahq/nezha/model"
"github.com/nezhahq/nezha/service/singleton"
2024-10-20 11:23:04 -04:00
)
// List server group
// @Summary List server group
// @Schemes
// @Description List server group
// @Security BearerAuth
// @Tags common
// @Produce json
2024-10-21 11:00:51 -04:00
// @Success 200 {object} model.CommonResponse[[]model.ServerGroupResponseItem]
2024-10-20 11:23:04 -04:00
// @Router /server-group [get]
2024-12-16 06:40:37 -05:00
func listServerGroup(c *gin.Context) ([]*model.ServerGroupResponseItem, error) {
2024-10-20 11:23:04 -04:00
var sg []model.ServerGroup
if err := singleton.DB.Find(&sg).Error; err != nil {
2024-10-23 05:56:51 -04:00
return nil, err
}
2024-10-21 11:00:51 -04:00
groupServers := make(map[uint64][]uint64, 0)
var sgs []model.ServerGroupServer
if err := singleton.DB.Find(&sgs).Error; err != nil {
2024-10-23 05:56:51 -04:00
return nil, err
2024-10-21 11:00:51 -04:00
}
for _, s := range sgs {
if _, ok := groupServers[s.ServerGroupId]; !ok {
groupServers[s.ServerGroupId] = make([]uint64, 0)
}
groupServers[s.ServerGroupId] = append(groupServers[s.ServerGroupId], s.ServerId)
}
2024-12-16 06:40:37 -05:00
var sgRes []*model.ServerGroupResponseItem
2024-10-21 11:00:51 -04:00
for _, s := range sg {
2024-12-16 06:40:37 -05:00
sgRes = append(sgRes, &model.ServerGroupResponseItem{
2024-10-21 11:00:51 -04:00
Group: s,
Servers: groupServers[s.ID],
})
}
2024-10-23 05:56:51 -04:00
return sgRes, nil
2024-10-21 11:00:51 -04:00
}
// New server group
// @Summary New server group
// @Schemes
// @Description New server group
// @Security BearerAuth
// @Tags auth required
// @Accept json
// @Param body body model.ServerGroupForm true "ServerGroupForm"
// @Produce json
2024-10-23 05:56:51 -04:00
// @Success 200 {object} model.CommonResponse[uint64]
2024-10-21 11:00:51 -04:00
// @Router /server-group [post]
2024-10-23 05:56:51 -04:00
func createServerGroup(c *gin.Context) (uint64, error) {
2024-10-21 11:00:51 -04:00
var sgf model.ServerGroupForm
if err := c.ShouldBindJSON(&sgf); err != nil {
2024-10-23 05:56:51 -04:00
return 0, err
2024-10-21 11:00:51 -04:00
}
sgf.Servers = slices.Compact(sgf.Servers)
2024-10-21 11:00:51 -04:00
2024-12-17 01:09:49 -05:00
uid := getUid(c)
2024-10-21 11:00:51 -04:00
var sg model.ServerGroup
sg.Name = sgf.Name
2024-12-17 01:09:49 -05:00
sg.UserID = uid
2024-10-21 11:00:51 -04:00
var count int64
if err := singleton.DB.Model(&model.Server{}).Where("id in (?)", sgf.Servers).Count(&count).Error; err != nil {
return 0, newGormError("%v", err)
2024-10-21 11:00:51 -04:00
}
if count != int64(len(sgf.Servers)) {
2024-10-31 17:07:04 -04:00
return 0, singleton.Localizer.ErrorT("have invalid server id")
2024-10-21 11:00:51 -04:00
}
2024-10-23 05:56:51 -04:00
err := singleton.DB.Transaction(func(tx *gorm.DB) error {
2024-10-21 11:00:51 -04:00
if err := tx.Create(&sg).Error; err != nil {
return err
}
for _, s := range sgf.Servers {
if err := tx.Create(&model.ServerGroupServer{
2024-12-17 01:09:49 -05:00
Common: model.Common{
UserID: uid,
},
2024-10-21 11:00:51 -04:00
ServerGroupId: sg.ID,
ServerId: s,
}).Error; err != nil {
return err
}
}
return nil
})
2024-10-23 05:56:51 -04:00
if err != nil {
return 0, newGormError("%v", err)
}
2024-10-21 11:00:51 -04:00
2024-10-23 05:56:51 -04:00
return sg.ID, nil
2024-10-21 11:00:51 -04:00
}
// Edit server group
// @Summary Edit server group
// @Schemes
// @Description Edit server group
// @Security BearerAuth
// @Tags auth required
// @Accept json
2024-10-24 21:09:08 -04:00
// @Param id path uint true "ID"
2024-10-21 11:00:51 -04:00
// @Param body body model.ServerGroupForm true "ServerGroupForm"
// @Produce json
// @Success 200 {object} model.CommonResponse[any]
// @Router /server-group/{id} [patch]
2024-10-23 05:56:51 -04:00
func updateServerGroup(c *gin.Context) (any, error) {
idStr := c.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
return nil, err
}
2024-10-21 11:00:51 -04:00
var sg model.ServerGroupForm
if err := c.ShouldBindJSON(&sg); err != nil {
2024-10-23 05:56:51 -04:00
return nil, err
2024-10-21 11:00:51 -04:00
}
sg.Servers = slices.Compact(sg.Servers)
2024-10-21 11:00:51 -04:00
var sgDB model.ServerGroup
if err := singleton.DB.First(&sgDB, id).Error; err != nil {
2024-10-31 17:07:04 -04:00
return nil, singleton.Localizer.ErrorT("group id %d does not exist", id)
2024-10-21 11:00:51 -04:00
}
2024-12-17 01:09:49 -05:00
if !sgDB.HasPermission(c) {
return nil, singleton.Localizer.ErrorT("unauthorized")
}
2024-10-21 11:00:51 -04:00
sgDB.Name = sg.Name
var count int64
if err := singleton.DB.Model(&model.Server{}).Where("id in (?)", sg.Servers).Count(&count).Error; err != nil {
2024-10-23 05:56:51 -04:00
return nil, err
2024-10-21 11:00:51 -04:00
}
if count != int64(len(sg.Servers)) {
2024-10-31 17:07:04 -04:00
return nil, singleton.Localizer.ErrorT("have invalid server id")
2024-10-21 11:00:51 -04:00
}
2024-12-17 01:09:49 -05:00
uid := getUid(c)
err = singleton.DB.Transaction(func(tx *gorm.DB) error {
2024-10-21 11:00:51 -04:00
if err := tx.Save(&sgDB).Error; err != nil {
return err
}
if err := tx.Unscoped().Delete(&model.ServerGroupServer{}, "server_group_id = ?", id).Error; err != nil {
2024-10-21 11:00:51 -04:00
return err
}
for _, s := range sg.Servers {
if err := tx.Create(&model.ServerGroupServer{
2024-12-17 01:09:49 -05:00
Common: model.Common{
UserID: uid,
},
2024-10-21 11:00:51 -04:00
ServerGroupId: sgDB.ID,
ServerId: s,
}).Error; err != nil {
return err
}
}
return nil
})
if err != nil {
2024-10-23 05:56:51 -04:00
return nil, newGormError("%v", err)
2024-10-21 11:00:51 -04:00
}
2024-10-23 05:56:51 -04:00
return nil, nil
2024-10-21 11:00:51 -04:00
}
// Batch delete server group
// @Summary Batch delete server group
// @Security BearerAuth
// @Schemes
// @Description Batch delete server group
// @Tags auth required
// @Accept json
// @param request body []uint64 true "id list"
// @Produce json
// @Success 200 {object} model.CommonResponse[any]
// @Router /batch-delete/server-group [post]
2024-10-23 05:56:51 -04:00
func batchDeleteServerGroup(c *gin.Context) (any, error) {
2024-12-17 01:09:49 -05:00
var sgsr []uint64
if err := c.ShouldBindJSON(&sgsr); err != nil {
2024-10-23 05:56:51 -04:00
return nil, err
2024-10-21 11:00:51 -04:00
}
2024-12-17 01:09:49 -05:00
var sg []model.ServerGroup
if err := singleton.DB.Where("id in (?)", sgsr).Find(&sg).Error; err != nil {
return nil, err
}
var sgs []uint64
for _, s := range sg {
if !s.HasPermission(c) {
return nil, singleton.Localizer.ErrorT("permission denied")
}
sgs = append(sgs, s.ID)
}
2024-10-21 11:00:51 -04:00
err := singleton.DB.Transaction(func(tx *gorm.DB) error {
if err := tx.Unscoped().Delete(&model.ServerGroup{}, "id in (?)", sgs).Error; err != nil {
return err
}
if err := tx.Unscoped().Delete(&model.ServerGroupServer{}, "server_group_id in (?)", sgs).Error; err != nil {
return err
}
return nil
})
if err != nil {
2024-10-23 05:56:51 -04:00
return nil, newGormError("%v", err)
2024-10-21 11:00:51 -04:00
}
2024-10-23 05:56:51 -04:00
return nil, nil
2024-10-20 11:23:04 -04:00
}