mirror of
				https://github.com/TeaOSLab/EdgeAdmin.git
				synced 2025-11-04 13:10:26 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			102 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			102 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package cluster
 | 
						||
 | 
						||
import (
 | 
						||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
						||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
						||
	"github.com/iwind/TeaGo/actions"
 | 
						||
	"github.com/iwind/TeaGo/lists"
 | 
						||
	"github.com/iwind/TeaGo/maps"
 | 
						||
	"net"
 | 
						||
	"strconv"
 | 
						||
	"strings"
 | 
						||
)
 | 
						||
 | 
						||
type CreateBatchAction struct {
 | 
						||
	actionutils.ParentAction
 | 
						||
}
 | 
						||
 | 
						||
func (this *CreateBatchAction) Init() {
 | 
						||
	this.Nav("", "node", "create")
 | 
						||
	this.SecondMenu("nodes")
 | 
						||
}
 | 
						||
 | 
						||
func (this *CreateBatchAction) RunGet(params struct {
 | 
						||
	ClusterId int64
 | 
						||
}) {
 | 
						||
	leftMenuItems := []maps.Map{
 | 
						||
		{
 | 
						||
			"name":     "单个创建",
 | 
						||
			"url":      "/clusters/cluster/createNode?clusterId=" + strconv.FormatInt(params.ClusterId, 10),
 | 
						||
			"isActive": false,
 | 
						||
		},
 | 
						||
		{
 | 
						||
			"name":     "批量创建",
 | 
						||
			"url":      "/clusters/cluster/createBatch?clusterId=" + strconv.FormatInt(params.ClusterId, 10),
 | 
						||
			"isActive": true,
 | 
						||
		},
 | 
						||
	}
 | 
						||
	this.Data["leftMenuItems"] = leftMenuItems
 | 
						||
 | 
						||
	this.Show()
 | 
						||
}
 | 
						||
 | 
						||
func (this *CreateBatchAction) RunPost(params struct {
 | 
						||
	ClusterId int64
 | 
						||
	GroupId   int64
 | 
						||
	IpList    string
 | 
						||
 | 
						||
	Must *actions.Must
 | 
						||
	CSRF *actionutils.CSRF
 | 
						||
}) {
 | 
						||
	if params.ClusterId <= 0 {
 | 
						||
		this.Fail("请选择正确的集群")
 | 
						||
	}
 | 
						||
 | 
						||
	// 校验
 | 
						||
	// TODO 支持IP范围,比如:192.168.1.[100-105]
 | 
						||
	realIPList := []string{}
 | 
						||
	for _, ip := range strings.Split(params.IpList, "\n") {
 | 
						||
		ip = strings.TrimSpace(ip)
 | 
						||
		if len(ip) == 0 {
 | 
						||
			continue
 | 
						||
		}
 | 
						||
		ip = strings.ReplaceAll(ip, " ", "")
 | 
						||
 | 
						||
		if net.ParseIP(ip) == nil {
 | 
						||
			this.Fail("发现错误的IP地址:" + ip)
 | 
						||
		}
 | 
						||
 | 
						||
		if lists.ContainsString(realIPList, ip) {
 | 
						||
			continue
 | 
						||
		}
 | 
						||
		realIPList = append(realIPList, ip)
 | 
						||
	}
 | 
						||
 | 
						||
	// 保存
 | 
						||
	for _, ip := range realIPList {
 | 
						||
		resp, err := this.RPC().NodeRPC().CreateNode(this.AdminContext(), &pb.CreateNodeRequest{
 | 
						||
			Name:      ip,
 | 
						||
			ClusterId: params.ClusterId,
 | 
						||
			GroupId:   params.GroupId,
 | 
						||
			Login:     nil,
 | 
						||
		})
 | 
						||
		if err != nil {
 | 
						||
			this.ErrorPage(err)
 | 
						||
			return
 | 
						||
		}
 | 
						||
		nodeId := resp.NodeId
 | 
						||
		_, err = this.RPC().NodeIPAddressRPC().CreateNodeIPAddress(this.AdminContext(), &pb.CreateNodeIPAddressRequest{
 | 
						||
			NodeId:    nodeId,
 | 
						||
			Name:      "IP地址",
 | 
						||
			Ip:        ip,
 | 
						||
			CanAccess: true,
 | 
						||
		})
 | 
						||
		if err != nil {
 | 
						||
			this.ErrorPage(err)
 | 
						||
			return
 | 
						||
		}
 | 
						||
	}
 | 
						||
 | 
						||
	this.Success()
 | 
						||
}
 |