mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-08 02:20:24 +08:00
增加独立的IP地址管理功能
This commit is contained in:
@@ -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