From 5f822062da22c436e916f302862316f3b465e140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Sat, 17 Sep 2022 11:38:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A3=80=E6=9F=A5=E5=9F=9F=E5=90=8D=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E5=AD=98=E5=9C=A8=E6=97=B6=E5=90=8C=E6=97=B6=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E6=B3=9B=E5=9F=9F=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/db/models/server_dao.go | 37 +++++++++++++++++++++++-- internal/rpc/services/service_server.go | 4 +-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/internal/db/models/server_dao.go b/internal/db/models/server_dao.go index b3609f80..55313fa2 100644 --- a/internal/db/models/server_dao.go +++ b/internal/db/models/server_dao.go @@ -1880,8 +1880,8 @@ func (this *ServerDAO) CheckPortIsUsing(tx *dbs.Tx, clusterId int64, protocolFam } // ExistServerNameInCluster 检查ServerName是否已存在 -func (this *ServerDAO) ExistServerNameInCluster(tx *dbs.Tx, clusterId int64, serverName string, excludeServerId int64) (bool, error) { - query := this.Query(tx). +func (this *ServerDAO) ExistServerNameInCluster(tx *dbs.Tx, clusterId int64, serverName string, excludeServerId int64, supportWildcard bool) (bool, error) { + var query = this.Query(tx). Attr("clusterId", clusterId). Where("(JSON_CONTAINS(serverNames, :jsonQuery1) OR JSON_CONTAINS(serverNames, :jsonQuery2))"). Param("jsonQuery1", maps.Map{"name": serverName}.AsJSON()). @@ -1890,7 +1890,38 @@ func (this *ServerDAO) ExistServerNameInCluster(tx *dbs.Tx, clusterId int64, ser query.Neq("id", excludeServerId) } query.State(ServerStateEnabled) - return query.Exist() + exists, err := query.Exist() + if err != nil || exists { + return exists, err + } + + if supportWildcard { + var countPieces = strings.Count(serverName, ".") + for { + var index = strings.Index(serverName, ".") + if index > 0 { + serverName = serverName[index+1:] + var search = strings.Repeat("*.", countPieces-strings.Count(serverName, ".")) + serverName + var query = this.Query(tx). + Attr("clusterId", clusterId). + Where("(JSON_CONTAINS(serverNames, :jsonQuery1) OR JSON_CONTAINS(serverNames, :jsonQuery2))"). + Param("jsonQuery1", maps.Map{"name": search}.AsJSON()). + Param("jsonQuery2", maps.Map{"subNames": search}.AsJSON()) + if excludeServerId > 0 { + query.Neq("id", excludeServerId) + } + query.State(ServerStateEnabled) + exists, err = query.Exist() + if err != nil || exists { + return exists, err + } + } else { + break + } + } + } + + return false, nil } // GenDNSName 生成DNS Name diff --git a/internal/rpc/services/service_server.go b/internal/rpc/services/service_server.go index 8447570c..9d8386c8 100644 --- a/internal/rpc/services/service_server.go +++ b/internal/rpc/services/service_server.go @@ -1707,9 +1707,9 @@ func (this *ServerService) CheckServerNameDuplicationInNodeCluster(ctx context.C var tx = this.NullTx() - duplicatedServerNames := []string{} + var duplicatedServerNames = []string{} for _, serverName := range req.ServerNames { - exist, err := models.SharedServerDAO.ExistServerNameInCluster(tx, req.NodeClusterId, serverName, req.ExcludeServerId) + exist, err := models.SharedServerDAO.ExistServerNameInCluster(tx, req.NodeClusterId, serverName, req.ExcludeServerId, req.SupportWildcard) if err != nil { return nil, err }