mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-01-03 13:06:35 +08:00
提供批量更新服务配置API(阶段性提交)
This commit is contained in:
@@ -2277,3 +2277,89 @@ func (this *ServerService) UpdateServerName(ctx context.Context, req *pb.UpdateS
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// CopyServerConfig 在服务之间复制配置
|
||||
func (this *ServerService) CopyServerConfig(ctx context.Context, req *pb.CopyServerConfigRequest) (*pb.RPCSuccess, error) {
|
||||
adminId, userId, err := this.ValidateAdminAndUser(ctx, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
|
||||
if req.ServerId <= 0 {
|
||||
return nil, errors.New("invalid 'serverId'")
|
||||
}
|
||||
|
||||
// 检查权限
|
||||
if userId > 0 {
|
||||
err = models.SharedServerDAO.CheckUserServer(tx, userId, req.ServerId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
switch req.TargetType {
|
||||
case "servers":
|
||||
// 检查权限
|
||||
if len(req.TargetServerIds) == 0 {
|
||||
return this.Success()
|
||||
}
|
||||
if userId > 0 {
|
||||
for _, targetServerId := range req.TargetServerIds {
|
||||
err = models.SharedServerDAO.CheckUserServer(tx, userId, targetServerId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
err = models.SharedServerDAO.CopyServerConfigToServers(tx, req.ServerId, req.TargetServerIds, req.ConfigCode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case "groups":
|
||||
// 检查权限
|
||||
if len(req.TargetServerGroupIds) == 0 {
|
||||
return this.Success()
|
||||
}
|
||||
if userId > 0 {
|
||||
for _, targetGroupId := range req.TargetServerGroupIds {
|
||||
err = models.SharedServerGroupDAO.CheckUserGroup(tx, userId, targetGroupId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
err = models.SharedServerDAO.CopyServerConfigToGroups(tx, req.ServerId, req.TargetServerGroupIds, req.ConfigCode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case "cluster":
|
||||
// 检查权限
|
||||
if adminId <= 0 {
|
||||
return nil, this.PermissionError()
|
||||
}
|
||||
if req.TargetClusterId <= 0 {
|
||||
return this.Success()
|
||||
}
|
||||
err = models.SharedServerDAO.CopyServerConfigToCluster(tx, req.ServerId, req.TargetClusterId, req.ConfigCode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case "user":
|
||||
if userId == 0 {
|
||||
userId, err = models.SharedServerDAO.FindServerUserId(tx, req.ServerId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 此时如果用户为0,则同步到未分配用户的服务
|
||||
}
|
||||
err = models.SharedServerDAO.CopyServerConfigToUser(tx, req.ServerId, req.TargetUserId, req.ConfigCode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
90
internal/rpc/services/service_updating_server_list.go
Normal file
90
internal/rpc/services/service_updating_server_list.go
Normal file
@@ -0,0 +1,90 @@
|
||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
)
|
||||
|
||||
// UpdatingServerListService 待更新服务列表服务
|
||||
type UpdatingServerListService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// FindUpdatingServerLists 查找要更新的服务配置
|
||||
func (this *UpdatingServerListService) FindUpdatingServerLists(ctx context.Context, req *pb.FindUpdatingServerListsRequest) (*pb.FindUpdatingServerListsResponse, error) {
|
||||
nodeId, err := this.ValidateNode(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
clusterIds, err := models.SharedNodeDAO.FindEnabledAndOnNodeClusterIds(tx, nodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
lists, err := models.SharedUpdatingServerListDAO.FindLists(tx, clusterIds, req.LastId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(lists) == 0 {
|
||||
return &pb.FindUpdatingServerListsResponse{
|
||||
MaxId: req.LastId,
|
||||
}, nil
|
||||
}
|
||||
|
||||
var serverIdMap = map[int64]bool{}
|
||||
var serverIds = []int64{}
|
||||
var maxId int64
|
||||
for _, list := range lists {
|
||||
if int64(list.Id) > maxId {
|
||||
maxId = int64(list.Id)
|
||||
}
|
||||
|
||||
for _, serverId := range list.DecodeServerIds() {
|
||||
if !serverIdMap[serverId] {
|
||||
serverIdMap[serverId] = true
|
||||
serverIds = append(serverIds, serverId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(serverIds) == 0 {
|
||||
return &pb.FindUpdatingServerListsResponse{
|
||||
MaxId: req.LastId,
|
||||
}, nil
|
||||
}
|
||||
|
||||
servers, err := models.SharedServerDAO.FindEnabledServersWithIds(tx, serverIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var serverConfigs = []*serverconfigs.ServerConfig{}
|
||||
var cacheMap = utils.NewCacheMap()
|
||||
for _, server := range servers {
|
||||
serverConfig, err := models.SharedServerDAO.ComposeServerConfig(tx, server, false, nil, cacheMap, true, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if serverConfig == nil {
|
||||
continue
|
||||
}
|
||||
serverConfigs = append(serverConfigs, serverConfig)
|
||||
}
|
||||
|
||||
serversJSON, err := json.Marshal(serverConfigs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.FindUpdatingServerListsResponse{
|
||||
ServersJSON: serversJSON,
|
||||
MaxId: maxId,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user