Files
EdgeAPI/internal/rpc/services/service_monitor_node.go

234 lines
6.2 KiB
Go
Raw Normal View History

2021-04-05 20:48:33 +08:00
package services
import (
"context"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"google.golang.org/grpc/metadata"
)
type MonitorNodeService struct {
BaseService
}
2021-07-11 18:05:57 +08:00
// CreateMonitorNode 创建监控节点
2021-04-05 20:48:33 +08:00
func (this *MonitorNodeService) CreateMonitorNode(ctx context.Context, req *pb.CreateMonitorNodeRequest) (*pb.CreateMonitorNodeResponse, error) {
2021-07-11 18:05:57 +08:00
_, err := this.ValidateAdmin(ctx, 0)
2021-04-05 20:48:33 +08:00
if err != nil {
return nil, err
}
tx := this.NullTx()
nodeId, err := models.SharedMonitorNodeDAO.CreateMonitorNode(tx, req.Name, req.Description, req.IsOn)
if err != nil {
return nil, err
}
2021-11-24 12:00:38 +08:00
return &pb.CreateMonitorNodeResponse{MonitorNodeId: nodeId}, nil
2021-04-05 20:48:33 +08:00
}
2021-07-11 18:05:57 +08:00
// UpdateMonitorNode 修改监控节点
2021-04-05 20:48:33 +08:00
func (this *MonitorNodeService) UpdateMonitorNode(ctx context.Context, req *pb.UpdateMonitorNodeRequest) (*pb.RPCSuccess, error) {
2021-07-11 18:05:57 +08:00
_, err := this.ValidateAdmin(ctx, 0)
2021-04-05 20:48:33 +08:00
if err != nil {
return nil, err
}
tx := this.NullTx()
2021-11-24 12:00:38 +08:00
err = models.SharedMonitorNodeDAO.UpdateMonitorNode(tx, req.MonitorNodeId, req.Name, req.Description, req.IsOn)
2021-04-05 20:48:33 +08:00
if err != nil {
return nil, err
}
return this.Success()
}
2021-07-11 18:05:57 +08:00
// DeleteMonitorNode 删除监控节点
2021-04-05 20:48:33 +08:00
func (this *MonitorNodeService) DeleteMonitorNode(ctx context.Context, req *pb.DeleteMonitorNodeRequest) (*pb.RPCSuccess, error) {
2021-07-11 18:05:57 +08:00
_, err := this.ValidateAdmin(ctx, 0)
2021-04-05 20:48:33 +08:00
if err != nil {
return nil, err
}
tx := this.NullTx()
2021-11-24 12:00:38 +08:00
err = models.SharedMonitorNodeDAO.DisableMonitorNode(tx, req.MonitorNodeId)
2021-04-05 20:48:33 +08:00
if err != nil {
return nil, err
}
return this.Success()
}
2021-07-11 18:05:57 +08:00
// FindAllEnabledMonitorNodes 列出所有可用监控节点
2021-04-05 20:48:33 +08:00
func (this *MonitorNodeService) FindAllEnabledMonitorNodes(ctx context.Context, req *pb.FindAllEnabledMonitorNodesRequest) (*pb.FindAllEnabledMonitorNodesResponse, error) {
2021-07-11 18:05:57 +08:00
_, err := this.ValidateAdmin(ctx, 0)
2021-04-05 20:48:33 +08:00
if err != nil {
return nil, err
}
tx := this.NullTx()
nodes, err := models.SharedMonitorNodeDAO.FindAllEnabledMonitorNodes(tx)
if err != nil {
return nil, err
}
result := []*pb.MonitorNode{}
for _, node := range nodes {
result = append(result, &pb.MonitorNode{
Id: int64(node.Id),
IsOn: node.IsOn == 1,
UniqueId: node.UniqueId,
Secret: node.Secret,
Name: node.Name,
Description: node.Description,
})
}
2021-11-24 12:00:38 +08:00
return &pb.FindAllEnabledMonitorNodesResponse{MonitorNodes: result}, nil
2021-04-05 20:48:33 +08:00
}
2021-07-11 18:05:57 +08:00
// CountAllEnabledMonitorNodes 计算监控节点数量
2021-04-05 20:48:33 +08:00
func (this *MonitorNodeService) CountAllEnabledMonitorNodes(ctx context.Context, req *pb.CountAllEnabledMonitorNodesRequest) (*pb.RPCCountResponse, error) {
2021-07-11 18:05:57 +08:00
_, err := this.ValidateAdmin(ctx, 0)
2021-04-05 20:48:33 +08:00
if err != nil {
return nil, err
}
tx := this.NullTx()
count, err := models.SharedMonitorNodeDAO.CountAllEnabledMonitorNodes(tx)
if err != nil {
return nil, err
}
return this.SuccessCount(count)
}
2021-07-11 18:05:57 +08:00
// ListEnabledMonitorNodes 列出单页的监控节点
2021-04-05 20:48:33 +08:00
func (this *MonitorNodeService) ListEnabledMonitorNodes(ctx context.Context, req *pb.ListEnabledMonitorNodesRequest) (*pb.ListEnabledMonitorNodesResponse, error) {
2021-07-11 18:05:57 +08:00
_, err := this.ValidateAdmin(ctx, 0)
2021-04-05 20:48:33 +08:00
if err != nil {
return nil, err
}
tx := this.NullTx()
nodes, err := models.SharedMonitorNodeDAO.ListEnabledMonitorNodes(tx, req.Offset, req.Size)
if err != nil {
return nil, err
}
result := []*pb.MonitorNode{}
for _, node := range nodes {
result = append(result, &pb.MonitorNode{
Id: int64(node.Id),
IsOn: node.IsOn == 1,
UniqueId: node.UniqueId,
Secret: node.Secret,
Name: node.Name,
Description: node.Description,
StatusJSON: []byte(node.Status),
})
}
2021-11-24 12:00:38 +08:00
return &pb.ListEnabledMonitorNodesResponse{MonitorNodes: result}, nil
2021-04-05 20:48:33 +08:00
}
2021-07-11 18:05:57 +08:00
// FindEnabledMonitorNode 根据ID查找节点
2021-04-05 20:48:33 +08:00
func (this *MonitorNodeService) FindEnabledMonitorNode(ctx context.Context, req *pb.FindEnabledMonitorNodeRequest) (*pb.FindEnabledMonitorNodeResponse, error) {
2021-07-11 18:05:57 +08:00
_, err := this.ValidateAdmin(ctx, 0)
2021-04-05 20:48:33 +08:00
if err != nil {
return nil, err
}
tx := this.NullTx()
2021-11-24 12:00:38 +08:00
node, err := models.SharedMonitorNodeDAO.FindEnabledMonitorNode(tx, req.MonitorNodeId)
2021-04-05 20:48:33 +08:00
if err != nil {
return nil, err
}
if node == nil {
2021-11-24 12:00:38 +08:00
return &pb.FindEnabledMonitorNodeResponse{MonitorNode: nil}, nil
2021-04-05 20:48:33 +08:00
}
result := &pb.MonitorNode{
Id: int64(node.Id),
IsOn: node.IsOn == 1,
UniqueId: node.UniqueId,
Secret: node.Secret,
Name: node.Name,
Description: node.Description,
}
2021-11-24 12:00:38 +08:00
return &pb.FindEnabledMonitorNodeResponse{MonitorNode: result}, nil
2021-04-05 20:48:33 +08:00
}
2021-07-11 18:05:57 +08:00
// FindCurrentMonitorNode 获取当前监控节点的版本
2021-04-05 20:48:33 +08:00
func (this *MonitorNodeService) FindCurrentMonitorNode(ctx context.Context, req *pb.FindCurrentMonitorNodeRequest) (*pb.FindCurrentMonitorNodeResponse, error) {
2021-07-22 18:42:57 +08:00
_, err := this.ValidateMonitorNode(ctx)
2021-04-05 20:48:33 +08:00
if err != nil {
return nil, err
}
tx := this.NullTx()
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, errors.New("context: need 'nodeId'")
}
nodeIds := md.Get("nodeid")
if len(nodeIds) == 0 {
return nil, errors.New("invalid 'nodeId'")
}
nodeId := nodeIds[0]
node, err := models.SharedMonitorNodeDAO.FindEnabledMonitorNodeWithUniqueId(tx, nodeId)
if err != nil {
return nil, err
}
if node == nil {
2021-11-24 12:00:38 +08:00
return &pb.FindCurrentMonitorNodeResponse{MonitorNode: nil}, nil
2021-04-05 20:48:33 +08:00
}
result := &pb.MonitorNode{
Id: int64(node.Id),
IsOn: node.IsOn == 1,
UniqueId: node.UniqueId,
Secret: node.Secret,
Name: node.Name,
Description: node.Description,
}
2021-11-24 12:00:38 +08:00
return &pb.FindCurrentMonitorNodeResponse{MonitorNode: result}, nil
2021-04-05 20:48:33 +08:00
}
2021-07-11 18:05:57 +08:00
// UpdateMonitorNodeStatus 更新节点状态
2021-04-05 20:48:33 +08:00
func (this *MonitorNodeService) UpdateMonitorNodeStatus(ctx context.Context, req *pb.UpdateMonitorNodeStatusRequest) (*pb.RPCSuccess, error) {
// 校验节点
_, nodeId, err := this.ValidateNodeId(ctx, rpcutils.UserTypeMonitor)
if err != nil {
return nil, err
}
2021-11-24 12:00:38 +08:00
if req.MonitorNodeId > 0 {
nodeId = req.MonitorNodeId
2021-04-05 20:48:33 +08:00
}
if nodeId <= 0 {
return nil, errors.New("'nodeId' should be greater than 0")
}
tx := this.NullTx()
err = models.SharedMonitorNodeDAO.UpdateNodeStatus(tx, nodeId, req.StatusJSON)
if err != nil {
return nil, err
}
return this.Success()
}