实现节点分组管理

This commit is contained in:
刘祥超
2020-10-28 18:21:15 +08:00
parent 5810fd9144
commit 531e0fa0a7
6 changed files with 231 additions and 8 deletions

View File

@@ -191,7 +191,7 @@ func (this *NodeDAO) CountAllEnabledNodes() (int64, error) {
} }
// 列出单页节点 // 列出单页节点
func (this *NodeDAO) ListEnabledNodesMatch(offset int64, size int64, clusterId int64, installState configutils.BoolState, activeState configutils.BoolState) (result []*Node, err error) { func (this *NodeDAO) ListEnabledNodesMatch(offset int64, size int64, clusterId int64, installState configutils.BoolState, activeState configutils.BoolState, keyword string, groupId int64) (result []*Node, err error) {
query := this.Query(). query := this.Query().
State(NodeStateEnabled). State(NodeStateEnabled).
Offset(offset). Offset(offset).
@@ -224,6 +224,17 @@ func (this *NodeDAO) ListEnabledNodesMatch(offset int64, size int64, clusterId i
query.Where("(status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)") query.Where("(status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
} }
// 关键词
if len(keyword) > 0 {
query.Where("(name LIKE :keyword OR JSON_EXTRACT(status,'$.hostname') LIKE :keyword OR id IN (SELECT nodeId FROM "+SharedNodeIPAddressDAO.Table+" WHERE ip LIKE :keyword))").
Param("keyword", "%"+keyword+"%")
}
// 分组
if groupId > 0 {
query.Attr("groupId", groupId)
}
_, err = query.FindAll() _, err = query.FindAll()
return return
} }
@@ -309,7 +320,7 @@ func (this *NodeDAO) FindAllInactiveNodesWithClusterId(clusterId int64) (result
} }
// 计算节点数量 // 计算节点数量
func (this *NodeDAO) CountAllEnabledNodesMatch(clusterId int64, installState configutils.BoolState, activeState configutils.BoolState) (int64, error) { func (this *NodeDAO) CountAllEnabledNodesMatch(clusterId int64, installState configutils.BoolState, activeState configutils.BoolState, keyword string, groupId int64) (int64, error) {
query := this.Query() query := this.Query()
query.State(NodeStateEnabled) query.State(NodeStateEnabled)
@@ -338,6 +349,17 @@ func (this *NodeDAO) CountAllEnabledNodesMatch(clusterId int64, installState con
query.Where("(status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)") query.Where("(status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
} }
// 关键词
if len(keyword) > 0 {
query.Where("(name LIKE :keyword OR JSON_EXTRACT(status,'$.hostname') LIKE :keyword OR id IN (SELECT nodeId FROM "+SharedNodeIPAddressDAO.Table+" WHERE ip LIKE :keyword))").
Param("keyword", "%"+keyword+"%")
}
// 分组
if groupId > 0 {
query.Attr("groupId", groupId)
}
return query.Count() return query.Count()
} }
@@ -566,6 +588,14 @@ func (this *NodeDAO) FindAllLowerVersionNodesWithClusterId(clusterId int64, os s
return return
} }
// 查找某个节点分组下的所有节点数量
func (this *NodeDAO) CountAllEnabledNodesWithGroupId(groupId int64) (int64, error) {
return this.Query().
State(NodeStateEnabled).
Attr("groupId", groupId).
Count()
}
// 生成唯一ID // 生成唯一ID
func (this *NodeDAO) genUniqueId() (string, error) { func (this *NodeDAO) genUniqueId() (string, error) {
for { for {

View File

@@ -1,9 +1,11 @@
package models package models
import ( import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
) )
const ( const (
@@ -33,7 +35,7 @@ func init() {
} }
// 启用条目 // 启用条目
func (this *NodeGroupDAO) EnableNodeGroup(id uint32) (rowsAffected int64, err error) { func (this *NodeGroupDAO) EnableNodeGroup(id int64) (rowsAffected int64, err error) {
return this.Query(). return this.Query().
Pk(id). Pk(id).
Set("state", NodeGroupStateEnabled). Set("state", NodeGroupStateEnabled).
@@ -41,7 +43,7 @@ func (this *NodeGroupDAO) EnableNodeGroup(id uint32) (rowsAffected int64, err er
} }
// 禁用条目 // 禁用条目
func (this *NodeGroupDAO) DisableNodeGroup(id uint32) (rowsAffected int64, err error) { func (this *NodeGroupDAO) DisableNodeGroup(id int64) (rowsAffected int64, err error) {
return this.Query(). return this.Query().
Pk(id). Pk(id).
Set("state", NodeGroupStateDisabled). Set("state", NodeGroupStateDisabled).
@@ -49,7 +51,7 @@ func (this *NodeGroupDAO) DisableNodeGroup(id uint32) (rowsAffected int64, err e
} }
// 查找启用中的条目 // 查找启用中的条目
func (this *NodeGroupDAO) FindEnabledNodeGroup(id uint32) (*NodeGroup, error) { func (this *NodeGroupDAO) FindEnabledNodeGroup(id int64) (*NodeGroup, error) {
result, err := this.Query(). result, err := this.Query().
Pk(id). Pk(id).
Attr("state", NodeGroupStateEnabled). Attr("state", NodeGroupStateEnabled).
@@ -61,10 +63,61 @@ func (this *NodeGroupDAO) FindEnabledNodeGroup(id uint32) (*NodeGroup, error) {
} }
// 根据主键查找名称 // 根据主键查找名称
func (this *NodeGroupDAO) FindNodeGroupName(id uint32) (string, error) { func (this *NodeGroupDAO) FindNodeGroupName(id int64) (string, error) {
name, err := this.Query(). name, err := this.Query().
Pk(id). Pk(id).
Result("name"). Result("name").
FindCol("") FindCol("")
return name.(string), err return name.(string), err
} }
// 创建分组
func (this *NodeGroupDAO) CreateNodeGroup(clusterId int64, name string) (int64, error) {
op := NewNodeGroupOperator()
op.ClusterId = clusterId
op.Name = name
op.State = NodeGroupStateEnabled
_, err := this.Save(op)
if err != nil {
return 0, err
}
return types.Int64(op.Id), nil
}
// 修改分组
func (this *NodeGroupDAO) UpdateNodeGroup(groupId int64, name string) error {
if groupId <= 0 {
return errors.New("invalid groupId")
}
op := NewNodeGroupOperator()
op.Id = groupId
op.Name = name
_, err := this.Save(op)
return err
}
// 查询所有分组
func (this *NodeGroupDAO) FindAllEnabledGroupsWithClusterId(clusterId int64) (result []*NodeGroup, err error) {
_, err = this.Query().
State(NodeGroupStateEnabled).
Attr("clusterId", clusterId).
Desc("order").
AscPk().
Slice(&result).
FindAll()
return
}
// 保存排序
func (this *NodeGroupDAO) UpdateGroupOrders(groupIds []int64) error {
for index, groupId := range groupIds {
_, err := this.Query().
Pk(groupId).
Set("order", len(groupIds)-index).
Update()
if err != nil {
return err
}
}
return nil
}

View File

@@ -4,6 +4,7 @@ package models
type NodeGroup struct { type NodeGroup struct {
Id uint32 `field:"id"` // ID Id uint32 `field:"id"` // ID
Name string `field:"name"` // 名称 Name string `field:"name"` // 名称
ClusterId uint32 `field:"clusterId"` // 集群ID
Order uint32 `field:"order"` // 排序 Order uint32 `field:"order"` // 排序
CreatedAt uint64 `field:"createdAt"` // 创建时间 CreatedAt uint64 `field:"createdAt"` // 创建时间
State uint8 `field:"state"` // 状态 State uint8 `field:"state"` // 状态
@@ -12,6 +13,7 @@ type NodeGroup struct {
type NodeGroupOperator struct { type NodeGroupOperator struct {
Id interface{} // ID Id interface{} // ID
Name interface{} // 名称 Name interface{} // 名称
ClusterId interface{} // 集群ID
Order interface{} // 排序 Order interface{} // 排序
CreatedAt interface{} // 创建时间 CreatedAt interface{} // 创建时间
State interface{} // 状态 State interface{} // 状态

View File

@@ -171,6 +171,7 @@ func (this *APINode) listenRPC(listener net.Listener, tlsConfig *tls.Config) err
pb.RegisterNodeLogServiceServer(rpcServer, &services.NodeLogService{}) pb.RegisterNodeLogServiceServer(rpcServer, &services.NodeLogService{})
pb.RegisterHTTPAccessLogServiceServer(rpcServer, &services.HTTPAccessLogService{}) pb.RegisterHTTPAccessLogServiceServer(rpcServer, &services.HTTPAccessLogService{})
pb.RegisterMessageServiceServer(rpcServer, &services.MessageService{}) pb.RegisterMessageServiceServer(rpcServer, &services.MessageService{})
pb.RegisterNodeGroupServiceServer(rpcServer, &services.NodeGroupService{})
err := rpcServer.Serve(listener) err := rpcServer.Serve(listener)
if err != nil { if err != nil {
return errors.New("[API]start rpc failed: " + err.Error()) return errors.New("[API]start rpc failed: " + err.Error())

View File

@@ -102,7 +102,7 @@ func (this *NodeService) CountAllEnabledNodesMatch(ctx context.Context, req *pb.
if err != nil { if err != nil {
return nil, err return nil, err
} }
count, err := models.SharedNodeDAO.CountAllEnabledNodesMatch(req.ClusterId, configutils.ToBoolState(req.InstallState), configutils.ToBoolState(req.ActiveState)) count, err := models.SharedNodeDAO.CountAllEnabledNodesMatch(req.ClusterId, configutils.ToBoolState(req.InstallState), configutils.ToBoolState(req.ActiveState), req.Keyword, req.GroupId)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -115,7 +115,7 @@ func (this *NodeService) ListEnabledNodesMatch(ctx context.Context, req *pb.List
if err != nil { if err != nil {
return nil, err return nil, err
} }
nodes, err := models.SharedNodeDAO.ListEnabledNodesMatch(req.Offset, req.Size, req.ClusterId, configutils.ToBoolState(req.InstallState), configutils.ToBoolState(req.ActiveState)) nodes, err := models.SharedNodeDAO.ListEnabledNodesMatch(req.Offset, req.Size, req.ClusterId, configutils.ToBoolState(req.InstallState), configutils.ToBoolState(req.ActiveState), req.Keyword, req.GroupId)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -790,3 +790,18 @@ func (this *NodeService) UpdateNodeLogin(ctx context.Context, req *pb.UpdateNode
return rpcutils.RPCUpdateSuccess() return rpcutils.RPCUpdateSuccess()
} }
// 计算某个节点分组内的节点数量
func (this *NodeService) CountAllEnabledNodesWithGroupId(ctx context.Context, req *pb.CountAllEnabledNodesWithGroupIdRequest) (*pb.CountAllEnabledNodesWithGroupIdResponse, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
count, err := models.SharedNodeDAO.CountAllEnabledNodesWithGroupId(req.GroupId)
if err != nil {
return nil, err
}
return &pb.CountAllEnabledNodesWithGroupIdResponse{Count: count}, nil
}

View File

@@ -0,0 +1,122 @@
package services
import (
"context"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
// 节点分组相关服务
type NodeGroupService struct {
}
// 创建分组
func (this *NodeGroupService) CreateNodeGroup(ctx context.Context, req *pb.CreateNodeGroupRequest) (*pb.CreateNodeGroupResponse, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
groupId, err := models.SharedNodeGroupDAO.CreateNodeGroup(req.ClusterId, req.Name)
if err != nil {
return nil, err
}
return &pb.CreateNodeGroupResponse{GroupId: groupId}, nil
}
// 修改分组
func (this *NodeGroupService) UpdateNodeGroup(ctx context.Context, req *pb.UpdateNodeGroupRequest) (*pb.RPCUpdateSuccess, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
err = models.SharedNodeGroupDAO.UpdateNodeGroup(req.GroupId, req.Name)
if err != nil {
return nil, err
}
return rpcutils.RPCUpdateSuccess()
}
// 删除分组
func (this *NodeGroupService) DeleteNodeGroup(ctx context.Context, req *pb.DeleteNodeGroupRequest) (*pb.RPCDeleteSuccess, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
_, err = models.SharedNodeGroupDAO.DisableNodeGroup(req.GroupId)
if err != nil {
return nil, err
}
return rpcutils.RPCDeleteSuccess()
}
// 查询所有分组
func (this *NodeGroupService) FindAllEnabledNodeGroupsWithClusterId(ctx context.Context, req *pb.FindAllEnabledNodeGroupsWithClusterIdRequest) (*pb.FindAllEnabledNodeGroupsWithClusterIdResponse, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
groups, err := models.SharedNodeGroupDAO.FindAllEnabledGroupsWithClusterId(req.ClusterId)
if err != nil {
return nil, err
}
result := []*pb.NodeGroup{}
for _, group := range groups {
result = append(result, &pb.NodeGroup{
Id: int64(group.Id),
Name: group.Name,
})
}
return &pb.FindAllEnabledNodeGroupsWithClusterIdResponse{Groups: result}, nil
}
// 修改分组排序
func (this *NodeGroupService) UpdateNodeGroupOrders(ctx context.Context, req *pb.UpdateNodeGroupOrdersRequest) (*pb.RPCUpdateSuccess, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
err = models.SharedNodeGroupDAO.UpdateGroupOrders(req.GroupIds)
if err != nil {
return nil, err
}
return rpcutils.RPCUpdateSuccess()
}
// 查找单个分组信息
func (this *NodeGroupService) FindEnabledNodeGroup(ctx context.Context, req *pb.FindEnabledNodeGroupRequest) (*pb.FindEnabledNodeGroupResponse, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
group, err := models.SharedNodeGroupDAO.FindEnabledNodeGroup(req.GroupId)
if err != nil {
return nil, err
}
if group == nil {
return &pb.FindEnabledNodeGroupResponse{
Group: nil,
}, nil
}
return &pb.FindEnabledNodeGroupResponse{
Group: &pb.NodeGroup{
Id: int64(group.Id),
Name: group.Name,
},
}, nil
}