From 481fa8cd2dc326a0c4f59e877e06bdc210ee395c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Thu, 4 Aug 2022 16:25:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9F=A5=E6=89=BE=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=9F=90=E4=B8=AA=E8=AF=81=E4=B9=A6=E7=9A=84NS?= =?UTF-8?q?=E9=9B=86=E7=BE=A4=E6=95=B0=E9=87=8F=E7=9A=84API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/db/models/ns_cluster_dao.go | 18 ++++++++++++++ .../nameservers/service_ns_cluster.go | 24 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/internal/db/models/ns_cluster_dao.go b/internal/db/models/ns_cluster_dao.go index 44918b32..efee6b83 100644 --- a/internal/db/models/ns_cluster_dao.go +++ b/internal/db/models/ns_cluster_dao.go @@ -8,6 +8,8 @@ import ( _ "github.com/go-sql-driver/mysql" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/dbs" + "strconv" + "strings" ) const ( @@ -307,6 +309,22 @@ func (this *NSClusterDAO) UpdateClusterUDP(tx *dbs.Tx, clusterId int64, udpConfi return this.NotifyUpdate(tx, clusterId) } +// CountAllClustersWithSSLPolicyIds 计算使用SSL策略的所有NS集群数量 +func (this *NSClusterDAO) CountAllClustersWithSSLPolicyIds(tx *dbs.Tx, sslPolicyIds []int64) (count int64, err error) { + if len(sslPolicyIds) == 0 { + return + } + policyStringIds := []string{} + for _, policyId := range sslPolicyIds { + policyStringIds = append(policyStringIds, strconv.FormatInt(policyId, 10)) + } + return this.Query(tx). + State(NSClusterStateEnabled). + Where("(FIND_IN_SET(JSON_EXTRACT(tls, '$.sslPolicyRef.sslPolicyId'), :policyIds)) "). + Param("policyIds", strings.Join(policyStringIds, ",")). + Count() +} + // NotifyUpdate 通知更改 func (this *NSClusterDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error { return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, 0, NSNodeTaskTypeConfigChanged) diff --git a/internal/rpc/services/nameservers/service_ns_cluster.go b/internal/rpc/services/nameservers/service_ns_cluster.go index 2eeaa3af..e29408c6 100644 --- a/internal/rpc/services/nameservers/service_ns_cluster.go +++ b/internal/rpc/services/nameservers/service_ns_cluster.go @@ -340,3 +340,27 @@ func (this *NSClusterService) UpdateNSClusterUDP(ctx context.Context, req *pb.Up return this.Success() } + +// CountAllNSClustersWithSSLCertId 计算使用某个SSL证书的集群数量 +func (this *NSClusterService) CountAllNSClustersWithSSLCertId(ctx context.Context, req *pb.CountAllNSClustersWithSSLCertIdRequest) (*pb.RPCCountResponse, error) { + _, err := this.ValidateAdmin(ctx) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + policyIds, err := models.SharedSSLPolicyDAO.FindAllEnabledPolicyIdsWithCertId(tx, req.SslCertId) + if err != nil { + return nil, err + } + if len(policyIds) == 0 { + return this.SuccessCount(0) + } + + count, err := models.SharedNSClusterDAO.CountAllClustersWithSSLPolicyIds(tx, policyIds) + if err != nil { + return nil, err + } + + return this.SuccessCount(count) +}