2020-10-29 20:53:40 +08:00
|
|
|
package services
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
|
|
|
)
|
|
|
|
|
|
2021-05-25 17:48:55 +08:00
|
|
|
// ServerGroupService 服务分组相关服务
|
2020-10-29 20:53:40 +08:00
|
|
|
type ServerGroupService struct {
|
2020-11-24 15:02:44 +08:00
|
|
|
BaseService
|
2020-10-29 20:53:40 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-25 17:48:55 +08:00
|
|
|
// CreateServerGroup 创建分组
|
2020-10-29 20:53:40 +08:00
|
|
|
func (this *ServerGroupService) CreateServerGroup(ctx context.Context, req *pb.CreateServerGroupRequest) (*pb.CreateServerGroupResponse, error) {
|
|
|
|
|
// 校验请求
|
2021-07-11 18:05:57 +08:00
|
|
|
_, err := this.ValidateAdmin(ctx, 0)
|
2020-10-29 20:53:40 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
|
|
|
|
groupId, err := models.SharedServerGroupDAO.CreateGroup(tx, req.Name)
|
2020-10-29 20:53:40 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-05-25 17:48:55 +08:00
|
|
|
return &pb.CreateServerGroupResponse{ServerGroupId: groupId}, nil
|
2020-10-29 20:53:40 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-25 17:48:55 +08:00
|
|
|
// UpdateServerGroup 修改分组
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *ServerGroupService) UpdateServerGroup(ctx context.Context, req *pb.UpdateServerGroupRequest) (*pb.RPCSuccess, error) {
|
2020-10-29 20:53:40 +08:00
|
|
|
// 校验请求
|
2021-07-11 18:05:57 +08:00
|
|
|
_, err := this.ValidateAdmin(ctx, 0)
|
2020-10-29 20:53:40 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-05-25 17:48:55 +08:00
|
|
|
err = models.SharedServerGroupDAO.UpdateGroup(tx, req.ServerGroupId, req.Name)
|
2020-10-29 20:53:40 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-24 15:02:44 +08:00
|
|
|
return this.Success()
|
2020-10-29 20:53:40 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-25 17:48:55 +08:00
|
|
|
// DeleteServerGroup 删除分组
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *ServerGroupService) DeleteServerGroup(ctx context.Context, req *pb.DeleteServerGroupRequest) (*pb.RPCSuccess, error) {
|
2020-10-29 20:53:40 +08:00
|
|
|
// 校验请求
|
2021-07-11 18:05:57 +08:00
|
|
|
_, err := this.ValidateAdmin(ctx, 0)
|
2020-10-29 20:53:40 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-05-25 17:48:55 +08:00
|
|
|
err = models.SharedServerGroupDAO.DisableServerGroup(tx, req.ServerGroupId)
|
2020-10-29 20:53:40 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-24 15:02:44 +08:00
|
|
|
return this.Success()
|
2020-10-29 20:53:40 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-25 17:48:55 +08:00
|
|
|
// FindAllEnabledServerGroups 查询所有分组
|
2020-10-29 20:53:40 +08:00
|
|
|
func (this *ServerGroupService) FindAllEnabledServerGroups(ctx context.Context, req *pb.FindAllEnabledServerGroupsRequest) (*pb.FindAllEnabledServerGroupsResponse, error) {
|
|
|
|
|
// 校验请求
|
2021-07-11 18:05:57 +08:00
|
|
|
_, err := this.ValidateAdmin(ctx, 0)
|
2020-10-29 20:53:40 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
|
|
|
|
groups, err := models.SharedServerGroupDAO.FindAllEnabledGroups(tx)
|
2020-10-29 20:53:40 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
result := []*pb.ServerGroup{}
|
|
|
|
|
for _, group := range groups {
|
|
|
|
|
result = append(result, &pb.ServerGroup{
|
|
|
|
|
Id: int64(group.Id),
|
|
|
|
|
Name: group.Name,
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-05-25 17:48:55 +08:00
|
|
|
return &pb.FindAllEnabledServerGroupsResponse{ServerGroups: result}, nil
|
2020-10-29 20:53:40 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-25 17:48:55 +08:00
|
|
|
// UpdateServerGroupOrders 修改分组排序
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *ServerGroupService) UpdateServerGroupOrders(ctx context.Context, req *pb.UpdateServerGroupOrdersRequest) (*pb.RPCSuccess, error) {
|
2020-10-29 20:53:40 +08:00
|
|
|
// 校验请求
|
2021-07-11 18:05:57 +08:00
|
|
|
_, err := this.ValidateAdmin(ctx, 0)
|
2020-10-29 20:53:40 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-05-25 17:48:55 +08:00
|
|
|
err = models.SharedServerGroupDAO.UpdateGroupOrders(tx, req.ServerGroupIds)
|
2020-10-29 20:53:40 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-11-24 15:02:44 +08:00
|
|
|
return this.Success()
|
2020-10-29 20:53:40 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-25 17:48:55 +08:00
|
|
|
// FindEnabledServerGroup 查找单个分组信息
|
2020-10-29 20:53:40 +08:00
|
|
|
func (this *ServerGroupService) FindEnabledServerGroup(ctx context.Context, req *pb.FindEnabledServerGroupRequest) (*pb.FindEnabledServerGroupResponse, error) {
|
|
|
|
|
// 校验请求
|
2021-07-11 18:05:57 +08:00
|
|
|
_, err := this.ValidateAdmin(ctx, 0)
|
2020-10-29 20:53:40 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-05-25 17:48:55 +08:00
|
|
|
group, err := models.SharedServerGroupDAO.FindEnabledServerGroup(tx, req.ServerGroupId)
|
2020-10-29 20:53:40 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if group == nil {
|
|
|
|
|
return &pb.FindEnabledServerGroupResponse{
|
2021-05-25 17:48:55 +08:00
|
|
|
ServerGroup: nil,
|
2020-10-29 20:53:40 +08:00
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &pb.FindEnabledServerGroupResponse{
|
2021-05-25 17:48:55 +08:00
|
|
|
ServerGroup: &pb.ServerGroup{
|
2020-10-29 20:53:40 +08:00
|
|
|
Id: int64(group.Id),
|
|
|
|
|
Name: group.Name,
|
|
|
|
|
},
|
|
|
|
|
}, nil
|
|
|
|
|
}
|