mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	增加批量增加节点IP接口
This commit is contained in:
		@@ -119,7 +119,7 @@ func (this *NodeIPAddressDAO) FindAddressIsHealthy(tx *dbs.Tx, addressId int64)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateAddress 创建IP地址
 | 
			
		||||
func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, adminId int64, nodeId int64, role nodeconfigs.NodeRole, name string, ip string, canAccess bool, isUp bool) (addressId int64, err error) {
 | 
			
		||||
func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, adminId int64, nodeId int64, role nodeconfigs.NodeRole, name string, ip string, canAccess bool, isUp bool, groupId int64) (addressId int64, err error) {
 | 
			
		||||
	if len(role) == 0 {
 | 
			
		||||
		role = nodeconfigs.NodeRoleNode
 | 
			
		||||
	}
 | 
			
		||||
@@ -131,6 +131,7 @@ func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, adminId int64, nodeId in
 | 
			
		||||
	op.Ip = ip
 | 
			
		||||
	op.CanAccess = canAccess
 | 
			
		||||
	op.IsUp = isUp
 | 
			
		||||
	op.GroupId = groupId
 | 
			
		||||
 | 
			
		||||
	op.State = NodeIPAddressStateEnabled
 | 
			
		||||
	addressId, err = this.SaveInt64(tx, op)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										44
									
								
								internal/db/models/node_ip_address_group_dao.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								internal/db/models/node_ip_address_group_dao.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	"github.com/iwind/TeaGo/Tea"
 | 
			
		||||
	"github.com/iwind/TeaGo/dbs"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type NodeIPAddressGroupDAO dbs.DAO
 | 
			
		||||
 | 
			
		||||
func NewNodeIPAddressGroupDAO() *NodeIPAddressGroupDAO {
 | 
			
		||||
	return dbs.NewDAO(&NodeIPAddressGroupDAO{
 | 
			
		||||
		DAOObject: dbs.DAOObject{
 | 
			
		||||
			DB:     Tea.Env,
 | 
			
		||||
			Table:  "edgeNodeIPAddressGroups",
 | 
			
		||||
			Model:  new(NodeIPAddressGroup),
 | 
			
		||||
			PkName: "id",
 | 
			
		||||
		},
 | 
			
		||||
	}).(*NodeIPAddressGroupDAO)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var SharedNodeIPAddressGroupDAO *NodeIPAddressGroupDAO
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	dbs.OnReady(func() {
 | 
			
		||||
		SharedNodeIPAddressGroupDAO = NewNodeIPAddressGroupDAO()
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindNodeIPAddressGroupName 根据主键查找名称
 | 
			
		||||
func (this *NodeIPAddressGroupDAO) FindNodeIPAddressGroupName(tx *dbs.Tx, id uint32) (string, error) {
 | 
			
		||||
	return this.Query(tx).
 | 
			
		||||
		Pk(id).
 | 
			
		||||
		Result("name").
 | 
			
		||||
		FindStringCol("")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateGroup 创建分组
 | 
			
		||||
func (this *NodeIPAddressGroupDAO) CreateGroup(tx *dbs.Tx, name string, value string) (int64, error) {
 | 
			
		||||
	var op = NewNodeIPAddressGroupOperator()
 | 
			
		||||
	op.Name = name
 | 
			
		||||
	op.Value = value
 | 
			
		||||
	return this.SaveInt64(tx, op)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										6
									
								
								internal/db/models/node_ip_address_group_dao_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								internal/db/models/node_ip_address_group_dao_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	_ "github.com/iwind/TeaGo/bootstrap"
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										18
									
								
								internal/db/models/node_ip_address_group_model.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								internal/db/models/node_ip_address_group_model.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
// NodeIPAddressGroup IP地址分组
 | 
			
		||||
type NodeIPAddressGroup struct {
 | 
			
		||||
	Id    uint32 `field:"id"`    // ID
 | 
			
		||||
	Name  string `field:"name"`  // 分组名
 | 
			
		||||
	Value string `field:"value"` // IP值
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type NodeIPAddressGroupOperator struct {
 | 
			
		||||
	Id    interface{} // ID
 | 
			
		||||
	Name  interface{} // 分组名
 | 
			
		||||
	Value interface{} // IP值
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewNodeIPAddressGroupOperator() *NodeIPAddressGroupOperator {
 | 
			
		||||
	return &NodeIPAddressGroupOperator{}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1
									
								
								internal/db/models/node_ip_address_group_model_ext.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								internal/db/models/node_ip_address_group_model_ext.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
package models
 | 
			
		||||
@@ -5,6 +5,7 @@ type NodeIPAddress struct {
 | 
			
		||||
	Id                uint32 `field:"id"`                // ID
 | 
			
		||||
	NodeId            uint32 `field:"nodeId"`            // 节点ID
 | 
			
		||||
	Role              string `field:"role"`              // 节点角色
 | 
			
		||||
	GroupId           uint32 `field:"groupId"`           // 所属分组ID
 | 
			
		||||
	Name              string `field:"name"`              // 名称
 | 
			
		||||
	Ip                string `field:"ip"`                // IP地址
 | 
			
		||||
	Description       string `field:"description"`       // 描述
 | 
			
		||||
@@ -26,6 +27,7 @@ type NodeIPAddressOperator struct {
 | 
			
		||||
	Id                interface{} // ID
 | 
			
		||||
	NodeId            interface{} // 节点ID
 | 
			
		||||
	Role              interface{} // 节点角色
 | 
			
		||||
	GroupId           interface{} // 所属分组ID
 | 
			
		||||
	Name              interface{} // 名称
 | 
			
		||||
	Ip                interface{} // IP地址
 | 
			
		||||
	Description       interface{} // 描述
 | 
			
		||||
 
 | 
			
		||||
@@ -1402,7 +1402,7 @@ func (this *NodeService) UpdateNodeDNS(ctx context.Context, req *pb.UpdateNodeDN
 | 
			
		||||
				return nil, err
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			_, err = models.SharedNodeIPAddressDAO.CreateAddress(tx, adminId, req.NodeId, nodeconfigs.NodeRoleNode, "DNS IP", req.IpAddr, true, true)
 | 
			
		||||
			_, err = models.SharedNodeIPAddressDAO.CreateAddress(tx, adminId, req.NodeId, nodeconfigs.NodeRoleNode, "DNS IP", req.IpAddr, true, true, 0)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return nil, err
 | 
			
		||||
			}
 | 
			
		||||
 
 | 
			
		||||
@@ -21,7 +21,7 @@ func (this *NodeIPAddressService) CreateNodeIPAddress(ctx context.Context, req *
 | 
			
		||||
 | 
			
		||||
	tx := this.NullTx()
 | 
			
		||||
 | 
			
		||||
	addressId, err := models.SharedNodeIPAddressDAO.CreateAddress(tx, adminId, req.NodeId, req.Role, req.Name, req.Ip, req.CanAccess, req.IsUp)
 | 
			
		||||
	addressId, err := models.SharedNodeIPAddressDAO.CreateAddress(tx, adminId, req.NodeId, req.Role, req.Name, req.Ip, req.CanAccess, req.IsUp, 0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -29,6 +29,34 @@ func (this *NodeIPAddressService) CreateNodeIPAddress(ctx context.Context, req *
 | 
			
		||||
	return &pb.CreateNodeIPAddressResponse{NodeIPAddressId: addressId}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateNodeIPAddresses 批量创建IP地址
 | 
			
		||||
func (this *NodeIPAddressService) CreateNodeIPAddresses(ctx context.Context, req *pb.CreateNodeIPAddressesRequest) (*pb.CreateNodeIPAddressesResponse, error) {
 | 
			
		||||
	// 校验请求
 | 
			
		||||
	adminId, err := this.ValidateAdmin(ctx, 0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	tx := this.NullTx()
 | 
			
		||||
 | 
			
		||||
	// 创建分组
 | 
			
		||||
	groupId, err := models.SharedNodeIPAddressGroupDAO.CreateGroup(tx, req.GroupValue, req.GroupValue)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var result = []int64{}
 | 
			
		||||
	for _, ip := range req.IpList {
 | 
			
		||||
		addressId, err := models.SharedNodeIPAddressDAO.CreateAddress(tx, adminId, req.NodeId, req.Role, req.Name, ip, req.CanAccess, req.IsUp, groupId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		result = append(result, addressId)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &pb.CreateNodeIPAddressesResponse{NodeIPAddressIds: result}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateNodeIPAddress 修改IP地址
 | 
			
		||||
func (this *NodeIPAddressService) UpdateNodeIPAddress(ctx context.Context, req *pb.UpdateNodeIPAddressRequest) (*pb.RPCSuccess, error) {
 | 
			
		||||
	// 校验请求
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user