mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	增加独立的IP地址管理功能
This commit is contained in:
		@@ -108,6 +108,19 @@ func (this *NodeDAO) FindEnabledNode(tx *dbs.Tx, id int64) (*Node, error) {
 | 
			
		||||
	return result.(*Node), err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindEnabledBasicNode 获取节点的基本信息
 | 
			
		||||
func (this *NodeDAO) FindEnabledBasicNode(tx *dbs.Tx, nodeId int64) (*Node, error) {
 | 
			
		||||
	one, err := this.Query(tx).
 | 
			
		||||
		State(NodeStateEnabled).
 | 
			
		||||
		Pk(nodeId).
 | 
			
		||||
		Result("id", "name", "clusterId", "isOn", "isUp").
 | 
			
		||||
		Find()
 | 
			
		||||
	if one == nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	return one.(*Node), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindNodeName 根据主键查找名称
 | 
			
		||||
func (this *NodeDAO) FindNodeName(tx *dbs.Tx, id int64) (string, error) {
 | 
			
		||||
	name, err := this.Query(tx).
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ package models
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	"github.com/iwind/TeaGo/Tea"
 | 
			
		||||
@@ -104,7 +105,7 @@ func (this *NodeIPAddressDAO) FindAddressName(tx *dbs.Tx, id int64) (string, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateAddress 创建IP地址
 | 
			
		||||
func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, nodeId int64, role nodeconfigs.NodeRole, name string, ip string, canAccess bool, thresholdsJSON []byte) (addressId int64, err error) {
 | 
			
		||||
func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, adminId int64, nodeId int64, role nodeconfigs.NodeRole, name string, ip string, canAccess bool, thresholdsJSON []byte) (addressId int64, err error) {
 | 
			
		||||
	if len(role) == 0 {
 | 
			
		||||
		role = nodeconfigs.NodeRoleNode
 | 
			
		||||
	}
 | 
			
		||||
@@ -123,7 +124,7 @@ func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, nodeId int64, role nodec
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	op.State = NodeIPAddressStateEnabled
 | 
			
		||||
	err = this.Save(tx, op)
 | 
			
		||||
	addressId, err = this.SaveInt64(tx, op)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -133,11 +134,17 @@ func (this *NodeIPAddressDAO) CreateAddress(tx *dbs.Tx, nodeId int64, role nodec
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return types.Int64(op.Id), nil
 | 
			
		||||
	// 创建日志
 | 
			
		||||
	err = SharedNodeIPAddressLogDAO.CreateLog(tx, adminId, addressId, "创建IP")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return addressId, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateAddress 修改IP地址
 | 
			
		||||
func (this *NodeIPAddressDAO) UpdateAddress(tx *dbs.Tx, addressId int64, name string, ip string, canAccess bool, isOn bool, thresholdsJSON []byte) (err error) {
 | 
			
		||||
func (this *NodeIPAddressDAO) UpdateAddress(tx *dbs.Tx, adminId int64, addressId int64, name string, ip string, canAccess bool, isOn bool, thresholdsJSON []byte) (err error) {
 | 
			
		||||
	if addressId <= 0 {
 | 
			
		||||
		return errors.New("invalid addressId")
 | 
			
		||||
	}
 | 
			
		||||
@@ -160,6 +167,13 @@ func (this *NodeIPAddressDAO) UpdateAddress(tx *dbs.Tx, addressId int64, name st
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 创建日志
 | 
			
		||||
	err = SharedNodeIPAddressLogDAO.CreateLog(tx, adminId, addressId, "修改IP")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return this.NotifyUpdate(tx, addressId)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -284,26 +298,29 @@ func (this *NodeIPAddressDAO) FireThresholds(tx *dbs.Tx, role nodeconfigs.NodeRo
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		var isOk = true
 | 
			
		||||
		var summary = []string{}
 | 
			
		||||
		var positiveSummarys = []string{}
 | 
			
		||||
		var negativeSummarys = []string{}
 | 
			
		||||
		for _, threshold := range thresholds {
 | 
			
		||||
			if threshold.Value <= 0 || threshold.Duration <= 0 {
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			var value = float64(0)
 | 
			
		||||
			var summary = ""
 | 
			
		||||
			var op = nodeconfigs.FindNodeValueOperatorName(threshold.Operator)
 | 
			
		||||
			switch threshold.Item {
 | 
			
		||||
			case "avgRequests":
 | 
			
		||||
				value, err = SharedNodeValueDAO.SumValues(tx, role, nodeId, nodeconfigs.NodeValueItemRequests, "total", nodeconfigs.NodeValueSumMethodAvg, types.Int32(threshold.Duration), threshold.DurationUnit)
 | 
			
		||||
				value = math.Round(value / 60)
 | 
			
		||||
				summary = append(summary, "平均请求数:"+types.String(value)+"/s")
 | 
			
		||||
				summary = "平均请求数:" + types.String(value) + "/s,阈值:" + op + " " + types.String(threshold.Value) + "/s"
 | 
			
		||||
			case "avgTrafficOut":
 | 
			
		||||
				value, err = SharedNodeValueDAO.SumValues(tx, role, nodeId, nodeconfigs.NodeValueItemTrafficOut, "total", nodeconfigs.NodeValueSumMethodAvg, types.Int32(threshold.Duration), threshold.DurationUnit)
 | 
			
		||||
				value = math.Round(value*100/1024/1024/60) / 100 // 100 = 两位小数
 | 
			
		||||
				summary = append(summary, "平均下行流量:"+types.String(value)+"MB/s")
 | 
			
		||||
				summary = "平均下行流量:" + types.String(value) + "MB/s,阈值:" + op + " " + types.String(threshold.Value) + "MB/s"
 | 
			
		||||
			case "avgTrafficIn":
 | 
			
		||||
				value, err = SharedNodeValueDAO.SumValues(tx, role, nodeId, nodeconfigs.NodeValueItemTrafficIn, "total", nodeconfigs.NodeValueSumMethodAvg, types.Int32(threshold.Duration), threshold.DurationUnit)
 | 
			
		||||
				value = math.Round(value*100/1024/1024/60) / 100 // 100 = 两位小数
 | 
			
		||||
				summary = append(summary, "平均上行流量:"+types.String(value)+"MB/s")
 | 
			
		||||
				summary = "平均上行流量:" + types.String(value) + "MB/s,阈值:" + op + " " + types.String(threshold.Value) + "MB/s"
 | 
			
		||||
			default:
 | 
			
		||||
				// TODO 支持更多
 | 
			
		||||
				err = errors.New("threshold item '" + threshold.Item + "' not supported")
 | 
			
		||||
@@ -313,6 +330,9 @@ func (this *NodeIPAddressDAO) FireThresholds(tx *dbs.Tx, role nodeconfigs.NodeRo
 | 
			
		||||
			}
 | 
			
		||||
			if !nodeconfigs.CompareNodeValue(threshold.Operator, value, float64(threshold.Value)) {
 | 
			
		||||
				isOk = false
 | 
			
		||||
				negativeSummarys = append(negativeSummarys, summary)
 | 
			
		||||
			} else {
 | 
			
		||||
				positiveSummarys = append(positiveSummarys, summary)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		if isOk && addr.IsUp == 0 { // 新上线
 | 
			
		||||
@@ -324,11 +344,24 @@ func (this *NodeIPAddressDAO) FireThresholds(tx *dbs.Tx, role nodeconfigs.NodeRo
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// 增加日志
 | 
			
		||||
			var description = ""
 | 
			
		||||
			if len(negativeSummarys) > 0 {
 | 
			
		||||
				description = "触发阈值:" + strings.Join(negativeSummarys, ",")
 | 
			
		||||
			} else {
 | 
			
		||||
				description = "触发阈值:" + strings.Join(positiveSummarys, ",")
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			err = SharedNodeIPAddressLogDAO.CreateLog(tx, 0, int64(addr.Id), description)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			clusterId, err := SharedNodeDAO.FindNodeClusterId(tx, nodeId)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			err = SharedMessageDAO.CreateNodeMessage(tx, role, clusterId, nodeId, MessageTypeIPAddrUp, MessageLevelSuccess, "节点IP'"+addr.Ip+"'因为达到阈值而上线", "节点IP'"+addr.Ip+"'因为达到阈值而上线。"+strings.Join(summary, ",") + "。", maps.Map{
 | 
			
		||||
			err = SharedMessageDAO.CreateNodeMessage(tx, role, clusterId, nodeId, MessageTypeIPAddrUp, MessageLevelSuccess, "节点IP'"+addr.Ip+"'因为达到阈值而上线", "节点IP'"+addr.Ip+"'因为达到阈值而上线。"+description+"。", maps.Map{
 | 
			
		||||
				"addrId": addr.Id,
 | 
			
		||||
			}.AsJSON())
 | 
			
		||||
			if err != nil {
 | 
			
		||||
@@ -348,11 +381,24 @@ func (this *NodeIPAddressDAO) FireThresholds(tx *dbs.Tx, role nodeconfigs.NodeRo
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// 增加日志
 | 
			
		||||
			var description = ""
 | 
			
		||||
			if len(negativeSummarys) > 0 {
 | 
			
		||||
				description = "触发阈值:" + strings.Join(negativeSummarys, ";")
 | 
			
		||||
			} else {
 | 
			
		||||
				description = "触发阈值:" + strings.Join(positiveSummarys, ";")
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			err = SharedNodeIPAddressLogDAO.CreateLog(tx, 0, int64(addr.Id), description)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			clusterId, err := SharedNodeDAO.FindNodeClusterId(tx, nodeId)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			err = SharedMessageDAO.CreateNodeMessage(tx, role, clusterId, nodeId, MessageTypeIPAddrDown, MessageLevelWarning, "节点IP'"+addr.Ip+"'因为达到阈值而下线", "节点IP'"+addr.Ip+"'因为达到阈值而下线。"+strings.Join(summary, ",") + "。", maps.Map{
 | 
			
		||||
			err = SharedMessageDAO.CreateNodeMessage(tx, role, clusterId, nodeId, MessageTypeIPAddrDown, MessageLevelWarning, "节点IP'"+addr.Ip+"'因为达到阈值而下线", "节点IP'"+addr.Ip+"'因为达到阈值而下线。"+description+"。", maps.Map{
 | 
			
		||||
				"addrId": addr.Id,
 | 
			
		||||
			}.AsJSON())
 | 
			
		||||
			if err != nil {
 | 
			
		||||
@@ -369,6 +415,77 @@ func (this *NodeIPAddressDAO) FireThresholds(tx *dbs.Tx, role nodeconfigs.NodeRo
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CountAllEnabledIPAddresses 计算IP地址数量
 | 
			
		||||
// TODO 目前支持边缘节点,将来支持NS节点
 | 
			
		||||
func (this *NodeIPAddressDAO) CountAllEnabledIPAddresses(tx *dbs.Tx, role string, nodeClusterId int64, upState configutils.BoolState, keyword string) (int64, error) {
 | 
			
		||||
	var query = this.Query(tx).
 | 
			
		||||
		State(NodeIPAddressStateEnabled).
 | 
			
		||||
		Attr("role", role)
 | 
			
		||||
 | 
			
		||||
	// 集群
 | 
			
		||||
	if nodeClusterId > 0 {
 | 
			
		||||
		query.Where("nodeId IN (SELECT id FROM "+SharedNodeDAO.Table+" WHERE (clusterId=:clusterId OR JSON_CONTAINS(secondaryClusterIds, :clusterIdString)) AND state=1)").
 | 
			
		||||
			Param("clusterId", nodeClusterId).
 | 
			
		||||
			Param("clusterIdString", types.String(nodeClusterId))
 | 
			
		||||
	} else {
 | 
			
		||||
		query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1 AND clusterId IN (SELECT id FROM " + SharedNodeClusterDAO.Table + " WHERE state=1))")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 在线状态
 | 
			
		||||
	switch upState {
 | 
			
		||||
	case configutils.BoolStateYes:
 | 
			
		||||
		query.Attr("isUp", 1)
 | 
			
		||||
	case configutils.BoolStateNo:
 | 
			
		||||
		query.Attr("isUp", 0)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 关键词
 | 
			
		||||
	if len(keyword) > 0 {
 | 
			
		||||
		query.Where("(ip LIKE :keyword OR name LIKE :keyword OR description LIKE :keyword OR nodeId IN (SELECT id FROM "+SharedNodeDAO.Table+" WHERE state=1 AND name LIKE :keyword))").
 | 
			
		||||
			Param("keyword", "%"+keyword+"%")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return query.Count()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ListEnabledIPAddresses 列出单页的IP地址
 | 
			
		||||
func (this *NodeIPAddressDAO) ListEnabledIPAddresses(tx *dbs.Tx, role string, nodeClusterId int64, upState configutils.BoolState, keyword string, offset int64, size int64) (result []*NodeIPAddress, err error) {
 | 
			
		||||
	var query = this.Query(tx).
 | 
			
		||||
		State(NodeIPAddressStateEnabled).
 | 
			
		||||
		Attr("role", role)
 | 
			
		||||
 | 
			
		||||
	// 集群
 | 
			
		||||
	if nodeClusterId > 0 {
 | 
			
		||||
		query.Where("nodeId IN (SELECT id FROM "+SharedNodeDAO.Table+" WHERE (clusterId=:clusterId OR JSON_CONTAINS(secondaryClusterIds, :clusterIdString)) AND state=1)").
 | 
			
		||||
			Param("clusterId", nodeClusterId).
 | 
			
		||||
			Param("clusterIdString", types.String(nodeClusterId))
 | 
			
		||||
	} else {
 | 
			
		||||
		query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1 AND clusterId IN (SELECT id FROM " + SharedNodeClusterDAO.Table + " WHERE state=1))")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 在线状态
 | 
			
		||||
	switch upState {
 | 
			
		||||
	case configutils.BoolStateYes:
 | 
			
		||||
		query.Attr("isUp", 1)
 | 
			
		||||
	case configutils.BoolStateNo:
 | 
			
		||||
		query.Attr("isUp", 0)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 关键词
 | 
			
		||||
	if len(keyword) > 0 {
 | 
			
		||||
		query.Where("(ip LIKE :keyword OR name LIKE :keyword OR description LIKE :keyword OR nodeId IN (SELECT id FROM "+SharedNodeDAO.Table+" WHERE state=1 AND name LIKE :keyword))").
 | 
			
		||||
			Param("keyword", "%"+keyword+"%")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = query.Offset(offset).
 | 
			
		||||
		Limit(size).
 | 
			
		||||
		Asc("isUp").
 | 
			
		||||
		Desc("nodeId").
 | 
			
		||||
		Slice(&result).
 | 
			
		||||
		FindAll()
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NotifyUpdate 通知更新
 | 
			
		||||
func (this *NodeIPAddressDAO) NotifyUpdate(tx *dbs.Tx, addressId int64) error {
 | 
			
		||||
	address, err := this.Query(tx).
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ import (
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	"github.com/iwind/TeaGo/Tea"
 | 
			
		||||
	"github.com/iwind/TeaGo/dbs"
 | 
			
		||||
	timeutil "github.com/iwind/TeaGo/utils/time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type NodeIPAddressLogDAO dbs.DAO
 | 
			
		||||
@@ -26,3 +27,47 @@ func init() {
 | 
			
		||||
		SharedNodeIPAddressLogDAO = NewNodeIPAddressLogDAO()
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateLog 创建日志
 | 
			
		||||
func (this *NodeIPAddressLogDAO) CreateLog(tx *dbs.Tx, adminId int64, addrId int64, description string) error {
 | 
			
		||||
	addr, err := SharedNodeIPAddressDAO.FindEnabledAddress(tx, addrId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	if addr == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var op = NewNodeIPAddressLogOperator()
 | 
			
		||||
	op.AdminId = adminId
 | 
			
		||||
	op.AddressId = addrId
 | 
			
		||||
	op.Description = description
 | 
			
		||||
	op.CanAccess = addr.CanAccess
 | 
			
		||||
	op.IsOn = addr.IsOn
 | 
			
		||||
	op.IsUp = addr.IsUp
 | 
			
		||||
	op.Day = timeutil.Format("Ymd")
 | 
			
		||||
	return this.Save(tx, op)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CountLogs 计算日志数量
 | 
			
		||||
func (this *NodeIPAddressLogDAO) CountLogs(tx *dbs.Tx, addrId int64) (int64, error) {
 | 
			
		||||
	var query = this.Query(tx)
 | 
			
		||||
	if addrId > 0 {
 | 
			
		||||
		query.Attr("addressId", addrId)
 | 
			
		||||
	}
 | 
			
		||||
	return query.Count()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ListLogs 列出单页日志
 | 
			
		||||
func (this *NodeIPAddressLogDAO) ListLogs(tx *dbs.Tx, addrId int64, offset int64, size int64) (result []*NodeIPAddressLog, err error) {
 | 
			
		||||
	var query = this.Query(tx)
 | 
			
		||||
	if addrId > 0 {
 | 
			
		||||
		query.Attr("addressId", addrId)
 | 
			
		||||
	}
 | 
			
		||||
	_, err = query.Offset(offset).
 | 
			
		||||
		Limit(size).
 | 
			
		||||
		DescPk().
 | 
			
		||||
		Slice(&result).
 | 
			
		||||
		FindAll()
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,9 @@ type NodeIPAddressLog struct {
 | 
			
		||||
	AdminId     uint32 `field:"adminId"`     // 管理员ID
 | 
			
		||||
	Description string `field:"description"` // 描述
 | 
			
		||||
	CreatedAt   uint64 `field:"createdAt"`   // 操作时间
 | 
			
		||||
	IsUp        uint8  `field:"isUp"`        // 是否在线
 | 
			
		||||
	IsOn        uint8  `field:"isOn"`        // 是否启用
 | 
			
		||||
	CanAccess   uint8  `field:"canAccess"`   // 是否可访问
 | 
			
		||||
	Day         string `field:"day"`         // YYYYMMDD,用来清理
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -16,6 +19,9 @@ type NodeIPAddressLogOperator struct {
 | 
			
		||||
	AdminId     interface{} // 管理员ID
 | 
			
		||||
	Description interface{} // 描述
 | 
			
		||||
	CreatedAt   interface{} // 操作时间
 | 
			
		||||
	IsUp        interface{} // 是否在线
 | 
			
		||||
	IsOn        interface{} // 是否启用
 | 
			
		||||
	CanAccess   interface{} // 是否可访问
 | 
			
		||||
	Day         interface{} // YYYYMMDD,用来清理
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -48,6 +48,11 @@ func (this *APINode) registerServices(server *grpc.Server) {
 | 
			
		||||
		pb.RegisterNodeIPAddressServiceServer(server, instance)
 | 
			
		||||
		this.rest(instance)
 | 
			
		||||
	}
 | 
			
		||||
	{
 | 
			
		||||
		instance := this.serviceInstance(&services.NodeIPAddressLogService{}).(*services.NodeIPAddressLogService)
 | 
			
		||||
		pb.RegisterNodeIPAddressLogServiceServer(server, instance)
 | 
			
		||||
		this.rest(instance)
 | 
			
		||||
	}
 | 
			
		||||
	{
 | 
			
		||||
		instance := this.serviceInstance(&services.APINodeService{}).(*services.APINodeService)
 | 
			
		||||
		pb.RegisterAPINodeServiceServer(server, instance)
 | 
			
		||||
 
 | 
			
		||||
@@ -634,6 +634,39 @@ func (this *NodeService) FindEnabledNode(ctx context.Context, req *pb.FindEnable
 | 
			
		||||
	}}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindEnabledBasicNode 获取单个节点基本信息
 | 
			
		||||
func (this *NodeService) FindEnabledBasicNode(ctx context.Context, req *pb.FindEnabledBasicNodeRequest) (*pb.FindEnabledBasicNodeResponse, error) {
 | 
			
		||||
	_, err := this.ValidateAdmin(ctx, 0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	tx := this.NullTx()
 | 
			
		||||
	node, err := models.SharedNodeDAO.FindEnabledBasicNode(tx, req.NodeId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if node == nil {
 | 
			
		||||
		return &pb.FindEnabledBasicNodeResponse{Node: nil}, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	clusterName, err := models.SharedNodeClusterDAO.FindNodeClusterName(tx, int64(node.ClusterId))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &pb.FindEnabledBasicNodeResponse{Node: &pb.BasicNode{
 | 
			
		||||
		Id:   int64(node.Id),
 | 
			
		||||
		Name: node.Name,
 | 
			
		||||
		IsOn: node.IsOn == 1,
 | 
			
		||||
		IsUp: node.IsUp == 1,
 | 
			
		||||
		NodeCluster: &pb.NodeCluster{
 | 
			
		||||
			Id:   int64(node.ClusterId),
 | 
			
		||||
			Name: clusterName,
 | 
			
		||||
		},
 | 
			
		||||
	}}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindCurrentNodeConfig 组合节点配置
 | 
			
		||||
func (this *NodeService) FindCurrentNodeConfig(ctx context.Context, req *pb.FindCurrentNodeConfigRequest) (*pb.FindCurrentNodeConfigResponse, error) {
 | 
			
		||||
	_ = req
 | 
			
		||||
@@ -1326,7 +1359,7 @@ func (this *NodeService) FindEnabledNodeDNS(ctx context.Context, req *pb.FindEna
 | 
			
		||||
// UpdateNodeDNS 修改节点的DNS解析信息
 | 
			
		||||
func (this *NodeService) UpdateNodeDNS(ctx context.Context, req *pb.UpdateNodeDNSRequest) (*pb.RPCSuccess, error) {
 | 
			
		||||
	// 校验请求
 | 
			
		||||
	_, err := this.ValidateAdmin(ctx, 0)
 | 
			
		||||
	adminId, err := this.ValidateAdmin(ctx, 0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -1378,7 +1411,7 @@ func (this *NodeService) UpdateNodeDNS(ctx context.Context, req *pb.UpdateNodeDN
 | 
			
		||||
				return nil, err
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			_, err = models.SharedNodeIPAddressDAO.CreateAddress(tx, req.NodeId, nodeconfigs.NodeRoleNode, "DNS IP", req.IpAddr, true, nil)
 | 
			
		||||
			_, err = models.SharedNodeIPAddressDAO.CreateAddress(tx, adminId, req.NodeId, nodeconfigs.NodeRoleNode, "DNS IP", req.IpAddr, true, nil)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return nil, err
 | 
			
		||||
			}
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/db/models"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/iwind/TeaGo/types"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type NodeIPAddressService struct {
 | 
			
		||||
@@ -13,32 +14,32 @@ type NodeIPAddressService struct {
 | 
			
		||||
// CreateNodeIPAddress 创建IP地址
 | 
			
		||||
func (this *NodeIPAddressService) CreateNodeIPAddress(ctx context.Context, req *pb.CreateNodeIPAddressRequest) (*pb.CreateNodeIPAddressResponse, error) {
 | 
			
		||||
	// 校验请求
 | 
			
		||||
	_, err := this.ValidateAdmin(ctx, 0)
 | 
			
		||||
	adminId, err := this.ValidateAdmin(ctx, 0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	tx := this.NullTx()
 | 
			
		||||
 | 
			
		||||
	addressId, err := models.SharedNodeIPAddressDAO.CreateAddress(tx, req.NodeId, req.Role, req.Name, req.Ip, req.CanAccess, req.ThresholdsJSON)
 | 
			
		||||
	addressId, err := models.SharedNodeIPAddressDAO.CreateAddress(tx, adminId, req.NodeId, req.Role, req.Name, req.Ip, req.CanAccess, req.ThresholdsJSON)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &pb.CreateNodeIPAddressResponse{AddressId: addressId}, nil
 | 
			
		||||
	return &pb.CreateNodeIPAddressResponse{NodeIPAddressId: addressId}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateNodeIPAddress 修改IP地址
 | 
			
		||||
func (this *NodeIPAddressService) UpdateNodeIPAddress(ctx context.Context, req *pb.UpdateNodeIPAddressRequest) (*pb.RPCSuccess, error) {
 | 
			
		||||
	// 校验请求
 | 
			
		||||
	_, err := this.ValidateAdmin(ctx, 0)
 | 
			
		||||
	adminId, err := this.ValidateAdmin(ctx, 0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	tx := this.NullTx()
 | 
			
		||||
 | 
			
		||||
	err = models.SharedNodeIPAddressDAO.UpdateAddress(tx, req.AddressId, req.Name, req.Ip, req.CanAccess, req.IsOn, req.ThresholdsJSON)
 | 
			
		||||
	err = models.SharedNodeIPAddressDAO.UpdateAddress(tx, adminId, req.NodeIPAddressId, req.Name, req.Ip, req.CanAccess, req.IsOn, req.ThresholdsJSON)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -56,7 +57,7 @@ func (this *NodeIPAddressService) UpdateNodeIPAddressNodeId(ctx context.Context,
 | 
			
		||||
 | 
			
		||||
	tx := this.NullTx()
 | 
			
		||||
 | 
			
		||||
	err = models.SharedNodeIPAddressDAO.UpdateAddressNodeId(tx, req.AddressId, req.NodeId)
 | 
			
		||||
	err = models.SharedNodeIPAddressDAO.UpdateAddressNodeId(tx, req.NodeIPAddressId, req.NodeId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -74,7 +75,7 @@ func (this *NodeIPAddressService) DisableNodeIPAddress(ctx context.Context, req
 | 
			
		||||
 | 
			
		||||
	tx := this.NullTx()
 | 
			
		||||
 | 
			
		||||
	err = models.SharedNodeIPAddressDAO.DisableAddress(tx, req.AddressId)
 | 
			
		||||
	err = models.SharedNodeIPAddressDAO.DisableAddress(tx, req.NodeIPAddressId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -110,7 +111,7 @@ func (this *NodeIPAddressService) FindEnabledNodeIPAddress(ctx context.Context,
 | 
			
		||||
 | 
			
		||||
	tx := this.NullTx()
 | 
			
		||||
 | 
			
		||||
	address, err := models.SharedNodeIPAddressDAO.FindEnabledAddress(tx, req.AddressId)
 | 
			
		||||
	address, err := models.SharedNodeIPAddressDAO.FindEnabledAddress(tx, req.NodeIPAddressId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -120,6 +121,7 @@ func (this *NodeIPAddressService) FindEnabledNodeIPAddress(ctx context.Context,
 | 
			
		||||
		result = &pb.NodeIPAddress{
 | 
			
		||||
			Id:             int64(address.Id),
 | 
			
		||||
			NodeId:         int64(address.NodeId),
 | 
			
		||||
			Role:           address.Role,
 | 
			
		||||
			Name:           address.Name,
 | 
			
		||||
			Ip:             address.Ip,
 | 
			
		||||
			Description:    address.Description,
 | 
			
		||||
@@ -132,7 +134,7 @@ func (this *NodeIPAddressService) FindEnabledNodeIPAddress(ctx context.Context,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &pb.FindEnabledNodeIPAddressResponse{IpAddress: result}, nil
 | 
			
		||||
	return &pb.FindEnabledNodeIPAddressResponse{NodeIPAddress: result}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindAllEnabledIPAddressesWithNodeId 查找节点的所有地址
 | 
			
		||||
@@ -155,6 +157,7 @@ func (this *NodeIPAddressService) FindAllEnabledIPAddressesWithNodeId(ctx contex
 | 
			
		||||
		result = append(result, &pb.NodeIPAddress{
 | 
			
		||||
			Id:             int64(address.Id),
 | 
			
		||||
			NodeId:         int64(address.NodeId),
 | 
			
		||||
			Role:           address.Role,
 | 
			
		||||
			Name:           address.Name,
 | 
			
		||||
			Ip:             address.Ip,
 | 
			
		||||
			Description:    address.Description,
 | 
			
		||||
@@ -169,3 +172,52 @@ func (this *NodeIPAddressService) FindAllEnabledIPAddressesWithNodeId(ctx contex
 | 
			
		||||
 | 
			
		||||
	return &pb.FindAllEnabledIPAddressesWithNodeIdResponse{Addresses: result}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CountAllEnabledIPAddresses 计算IP地址数量
 | 
			
		||||
func (this *NodeIPAddressService) CountAllEnabledIPAddresses(ctx context.Context, req *pb.CountAllEnabledIPAddressesRequest) (*pb.RPCCountResponse, error) {
 | 
			
		||||
	// 校验请求
 | 
			
		||||
	_, err := this.ValidateAdmin(ctx, 0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
	count, err := models.SharedNodeIPAddressDAO.CountAllEnabledIPAddresses(tx, req.Role, req.NodeClusterId, types.Int8(req.UpState), req.Keyword)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return this.SuccessCount(count)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ListEnabledIPAddresses 列出单页IP地址
 | 
			
		||||
func (this *NodeIPAddressService) ListEnabledIPAddresses(ctx context.Context, req *pb.ListEnabledIPAddressesRequest) (*pb.ListEnabledIPAddressesResponse, error) {
 | 
			
		||||
	// 校验请求
 | 
			
		||||
	_, err := this.ValidateAdmin(ctx, 0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
	addresses, err := models.SharedNodeIPAddressDAO.ListEnabledIPAddresses(tx, req.Role, req.NodeClusterId, types.Int8(req.UpState), req.Keyword, req.Offset, req.Size)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var pbAddrs = []*pb.NodeIPAddress{}
 | 
			
		||||
	for _, addr := range addresses {
 | 
			
		||||
		pbAddrs = append(pbAddrs, &pb.NodeIPAddress{
 | 
			
		||||
			Id:             int64(addr.Id),
 | 
			
		||||
			NodeId:         int64(addr.NodeId),
 | 
			
		||||
			Role:           addr.Role,
 | 
			
		||||
			Name:           addr.Name,
 | 
			
		||||
			Ip:             addr.Ip,
 | 
			
		||||
			Description:    addr.Description,
 | 
			
		||||
			CanAccess:      addr.CanAccess == 1,
 | 
			
		||||
			IsOn:           addr.IsOn == 1,
 | 
			
		||||
			IsUp:           addr.IsUp == 1,
 | 
			
		||||
			ThresholdsJSON: []byte(addr.Thresholds),
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
	return &pb.ListEnabledIPAddressesResponse{NodeIPAddresses: pbAddrs}, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										89
									
								
								internal/rpc/services/service_node_ip_address_log.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								internal/rpc/services/service_node_ip_address_log.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,89 @@
 | 
			
		||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
 | 
			
		||||
 | 
			
		||||
package services
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/db/models"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// NodeIPAddressLogService IP地址相关日志
 | 
			
		||||
type NodeIPAddressLogService struct {
 | 
			
		||||
	BaseService
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CountAllNodeIPAddressLogs 计算日志数量
 | 
			
		||||
func (this *NodeIPAddressLogService) CountAllNodeIPAddressLogs(ctx context.Context, req *pb.CountAllNodeIPAddressLogsRequest) (*pb.RPCCountResponse, error) {
 | 
			
		||||
	_, err := this.ValidateAdmin(ctx, 0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
	count, err := models.SharedNodeIPAddressLogDAO.CountLogs(tx, req.NodeIPAddressId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return this.SuccessCount(count)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ListNodeIPAddressLogs 列出单页日志
 | 
			
		||||
func (this *NodeIPAddressLogService) ListNodeIPAddressLogs(ctx context.Context, req *pb.ListNodeIPAddressLogsRequest) (*pb.ListNodeIPAddressLogsResponse, error) {
 | 
			
		||||
	_, err := this.ValidateAdmin(ctx, 0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
	logs, err := models.SharedNodeIPAddressLogDAO.ListLogs(tx, req.NodeIPAddressId, req.Offset, req.Size)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	var pbLogs = []*pb.NodeIPAddressLog{}
 | 
			
		||||
	for _, log := range logs {
 | 
			
		||||
		var pbAddr *pb.NodeIPAddress
 | 
			
		||||
		addr, err := models.SharedNodeIPAddressDAO.FindEnabledAddress(tx, int64(log.AddressId))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		if addr != nil {
 | 
			
		||||
			pbAddr = &pb.NodeIPAddress{
 | 
			
		||||
				Id:          int64(addr.Id),
 | 
			
		||||
				NodeId:      int64(addr.NodeId),
 | 
			
		||||
				Name:        addr.Name,
 | 
			
		||||
				Ip:          addr.Ip,
 | 
			
		||||
				Description: addr.Description,
 | 
			
		||||
				Role:        addr.Role,
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		var pbAdmin *pb.Admin
 | 
			
		||||
		if log.AdminId > 0 {
 | 
			
		||||
			admin, err := models.SharedAdminDAO.FindEnabledAdmin(tx, int64(log.AdminId))
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return nil, err
 | 
			
		||||
			}
 | 
			
		||||
			if admin != nil {
 | 
			
		||||
				pbAdmin = &pb.Admin{
 | 
			
		||||
					Id:       int64(admin.Id),
 | 
			
		||||
					Fullname: admin.Fullname,
 | 
			
		||||
					Username: admin.Username,
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		pbLogs = append(pbLogs, &pb.NodeIPAddressLog{
 | 
			
		||||
			Id:            int64(log.Id),
 | 
			
		||||
			Description:   log.Description,
 | 
			
		||||
			CreatedAt:     int64(log.CreatedAt),
 | 
			
		||||
			IsOn:          log.IsOn == 1,
 | 
			
		||||
			IsUp:          log.IsUp == 1,
 | 
			
		||||
			CanAccess:     log.CanAccess == 1,
 | 
			
		||||
			NodeIPAddress: pbAddr,
 | 
			
		||||
			Admin:         pbAdmin,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
	return &pb.ListNodeIPAddressLogsResponse{NodeIPAddressLogs: pbLogs}, nil
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user