2020-07-29 19:02:28 +08:00
|
|
|
package services
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2020-10-17 11:14:53 +08:00
|
|
|
"encoding/json"
|
2020-07-29 19:02:28 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
2020-10-17 11:14:53 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
2020-07-29 19:02:28 +08:00
|
|
|
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
2020-09-13 20:37:28 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2020-10-17 11:14:53 +08:00
|
|
|
"strconv"
|
2020-07-29 19:02:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type NodeClusterService struct {
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 16:12:00 +08:00
|
|
|
// 创建集群
|
|
|
|
|
func (this *NodeClusterService) CreateNodeCluster(ctx context.Context, req *pb.CreateNodeClusterRequest) (*pb.CreateNodeClusterResponse, error) {
|
|
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clusterId, err := models.SharedNodeClusterDAO.CreateCluster(req.Name, req.GrantId, req.InstallDir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &pb.CreateNodeClusterResponse{ClusterId: clusterId}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 修改集群
|
2020-09-17 10:16:00 +08:00
|
|
|
func (this *NodeClusterService) UpdateNodeCluster(ctx context.Context, req *pb.UpdateNodeClusterRequest) (*pb.RPCUpdateSuccess, error) {
|
2020-08-30 16:12:00 +08:00
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = models.SharedNodeClusterDAO.UpdateCluster(req.ClusterId, req.Name, req.GrantId, req.InstallDir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-17 10:16:00 +08:00
|
|
|
return &pb.RPCUpdateSuccess{}, nil
|
2020-08-30 16:12:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 禁用集群
|
|
|
|
|
func (this *NodeClusterService) DisableNodeCluster(ctx context.Context, req *pb.DisableNodeClusterRequest) (*pb.DisableNodeClusterResponse, error) {
|
|
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = models.SharedNodeClusterDAO.DisableNodeCluster(req.ClusterId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &pb.DisableNodeClusterResponse{}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查找单个集群
|
|
|
|
|
func (this *NodeClusterService) FindEnabledNodeCluster(ctx context.Context, req *pb.FindEnabledNodeClusterRequest) (*pb.FindEnabledNodeClusterResponse, error) {
|
|
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cluster, err := models.SharedNodeClusterDAO.FindEnabledNodeCluster(req.ClusterId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cluster == nil {
|
|
|
|
|
return &pb.FindEnabledNodeClusterResponse{}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &pb.FindEnabledNodeClusterResponse{Cluster: &pb.NodeCluster{
|
|
|
|
|
Id: int64(cluster.Id),
|
|
|
|
|
Name: cluster.Name,
|
|
|
|
|
CreatedAt: int64(cluster.CreatedAt),
|
|
|
|
|
InstallDir: cluster.InstallDir,
|
|
|
|
|
GrantId: int64(cluster.GrantId),
|
2020-10-17 11:14:53 +08:00
|
|
|
UniqueId: cluster.UniqueId,
|
|
|
|
|
Secret: cluster.Secret,
|
2020-08-30 16:12:00 +08:00
|
|
|
}}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 11:14:53 +08:00
|
|
|
// 查找集群的API节点信息
|
|
|
|
|
func (this *NodeClusterService) FindAPINodesWithNodeCluster(ctx context.Context, req *pb.FindAPINodesWithNodeClusterRequest) (*pb.FindAPINodesWithNodeClusterResponse, error) {
|
|
|
|
|
// 校验请求
|
|
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cluster, err := models.SharedNodeClusterDAO.FindEnabledNodeCluster(req.ClusterId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if cluster == nil {
|
|
|
|
|
return nil, errors.New("can not find cluster with id '" + strconv.FormatInt(req.ClusterId, 10) + "'")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result := &pb.FindAPINodesWithNodeClusterResponse{}
|
|
|
|
|
result.UseAllAPINodes = cluster.UseAllAPINodes == 1
|
|
|
|
|
|
|
|
|
|
apiNodeIds := []int64{}
|
|
|
|
|
if len(cluster.ApiNodes) > 0 && cluster.ApiNodes != "null" {
|
|
|
|
|
err = json.Unmarshal([]byte(cluster.ApiNodes), &apiNodeIds)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if len(apiNodeIds) > 0 {
|
|
|
|
|
apiNodes := []*pb.APINode{}
|
|
|
|
|
for _, apiNodeId := range apiNodeIds {
|
|
|
|
|
apiNode, err := models.SharedAPINodeDAO.FindEnabledAPINode(apiNodeId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
apiNodeAddrs, err := apiNode.DecodeAccessAddrStrings()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
apiNodes = append(apiNodes, &pb.APINode{
|
|
|
|
|
Id: int64(apiNode.Id),
|
|
|
|
|
IsOn: apiNode.IsOn == 1,
|
|
|
|
|
ClusterId: int64(apiNode.ClusterId),
|
|
|
|
|
Name: apiNode.Name,
|
|
|
|
|
Description: apiNode.Description,
|
|
|
|
|
AccessAddrs: apiNodeAddrs,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
result.ApiNodes = apiNodes
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 16:12:00 +08:00
|
|
|
// 查找所有可用的集群
|
|
|
|
|
func (this *NodeClusterService) FindAllEnabledNodeClusters(ctx context.Context, req *pb.FindAllEnabledNodeClustersRequest) (*pb.FindAllEnabledNodeClustersResponse, error) {
|
2020-10-17 11:14:53 +08:00
|
|
|
// 校验请求
|
2020-07-29 19:02:28 +08:00
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clusters, err := models.SharedNodeClusterDAO.FindAllEnableClusters()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result := []*pb.NodeCluster{}
|
|
|
|
|
for _, cluster := range clusters {
|
|
|
|
|
result = append(result, &pb.NodeCluster{
|
|
|
|
|
Id: int64(cluster.Id),
|
|
|
|
|
Name: cluster.Name,
|
|
|
|
|
CreatedAt: int64(cluster.CreatedAt),
|
2020-10-17 11:14:53 +08:00
|
|
|
UniqueId: cluster.UniqueId,
|
|
|
|
|
Secret: cluster.Secret,
|
2020-07-29 19:02:28 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &pb.FindAllEnabledNodeClustersResponse{
|
|
|
|
|
Clusters: result,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
2020-08-21 12:32:33 +08:00
|
|
|
|
|
|
|
|
// 查找所有变更的集群
|
2020-08-30 16:12:00 +08:00
|
|
|
func (this *NodeClusterService) FindAllChangedNodeClusters(ctx context.Context, req *pb.FindAllChangedNodeClustersRequest) (*pb.FindAllChangedNodeClustersResponse, error) {
|
2020-08-21 12:32:33 +08:00
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clusterIds, err := models.SharedNodeDAO.FindChangedClusterIds()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if len(clusterIds) == 0 {
|
2020-08-30 16:12:00 +08:00
|
|
|
return &pb.FindAllChangedNodeClustersResponse{
|
2020-08-21 12:32:33 +08:00
|
|
|
Clusters: []*pb.NodeCluster{},
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
result := []*pb.NodeCluster{}
|
|
|
|
|
for _, clusterId := range clusterIds {
|
|
|
|
|
cluster, err := models.SharedNodeClusterDAO.FindEnabledNodeCluster(clusterId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if cluster == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
result = append(result, &pb.NodeCluster{
|
|
|
|
|
Id: int64(cluster.Id),
|
|
|
|
|
Name: cluster.Name,
|
|
|
|
|
CreatedAt: int64(cluster.CreatedAt),
|
2020-10-17 11:14:53 +08:00
|
|
|
UniqueId: cluster.UniqueId,
|
|
|
|
|
Secret: cluster.Secret,
|
2020-08-21 12:32:33 +08:00
|
|
|
})
|
|
|
|
|
}
|
2020-08-30 16:12:00 +08:00
|
|
|
return &pb.FindAllChangedNodeClustersResponse{Clusters: result}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 计算所有集群数量
|
|
|
|
|
func (this *NodeClusterService) CountAllEnabledNodeClusters(ctx context.Context, req *pb.CountAllEnabledNodeClustersRequest) (*pb.CountAllEnabledNodeClustersResponse, error) {
|
|
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
count, err := models.SharedNodeClusterDAO.CountAllEnabledClusters()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &pb.CountAllEnabledNodeClustersResponse{Count: count}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 列出单页集群
|
|
|
|
|
func (this *NodeClusterService) ListEnabledNodeClusters(ctx context.Context, req *pb.ListEnabledNodeClustersRequest) (*pb.ListEnabledNodeClustersResponse, error) {
|
|
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clusters, err := models.SharedNodeClusterDAO.ListEnabledClusters(req.Offset, req.Size)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result := []*pb.NodeCluster{}
|
|
|
|
|
for _, cluster := range clusters {
|
|
|
|
|
result = append(result, &pb.NodeCluster{
|
|
|
|
|
Id: int64(cluster.Id),
|
|
|
|
|
Name: cluster.Name,
|
|
|
|
|
CreatedAt: int64(cluster.CreatedAt),
|
|
|
|
|
GrantId: int64(cluster.GrantId),
|
|
|
|
|
InstallDir: cluster.InstallDir,
|
2020-10-17 11:14:53 +08:00
|
|
|
UniqueId: cluster.UniqueId,
|
|
|
|
|
Secret: cluster.Secret,
|
2020-08-30 16:12:00 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &pb.ListEnabledNodeClustersResponse{Clusters: result}, nil
|
2020-08-21 12:32:33 +08:00
|
|
|
}
|