提供检查域名是否重复接口

This commit is contained in:
刘祥超
2021-02-06 21:19:19 +08:00
parent 7e397671ec
commit 3252c08fb6
4 changed files with 72 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ func TestIPItemDAO_NotifyClustersUpdate(t *testing.T) {
dbs.NotifyReady() dbs.NotifyReady()
var tx *dbs.Tx var tx *dbs.Tx
err := SharedIPItemDAO.NotifyClustersUpdate(tx, 28, NodeTaskTypeIPItemChanged) err := SharedIPItemDAO.NotifyUpdate(tx, 28)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@@ -1319,6 +1319,20 @@ func (this *ServerDAO) CheckPortIsUsing(tx *dbs.Tx, clusterId int64, port int, e
Exist() Exist()
} }
// 检查ServerName是否已存在
func (this *ServerDAO) ExistServerNameInCluster(tx *dbs.Tx, clusterId int64, serverName string, excludeServerId int64) (bool, error) {
query := this.Query(tx).
Attr("clusterId", clusterId).
Where("(JSON_CONTAINS(serverNames, :jsonQuery1) OR JSON_CONTAINS(serverNames, :jsonQuery2))").
Param("jsonQuery1", maps.Map{"name": serverName}.AsJSON()).
Param("jsonQuery2", maps.Map{"subNames": serverName}.AsJSON())
if excludeServerId > 0 {
query.Neq("id", excludeServerId)
}
query.State(ServerStateEnabled)
return query.Exist()
}
// 生成DNS Name // 生成DNS Name
func (this *ServerDAO) GenDNSName(tx *dbs.Tx) (string, error) { func (this *ServerDAO) GenDNSName(tx *dbs.Tx) (string, error) {
for { for {

View File

@@ -106,3 +106,33 @@ func TestServerDAO_CheckPortIsUsing(t *testing.T) {
t.Log("isUsing:", isUsing) t.Log("isUsing:", isUsing)
} }
} }
func TestServerDAO_ExistServerNameInCluster(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
{
exist, err := SharedServerDAO.ExistServerNameInCluster(tx, 18, "hello.teaos.cn", 0)
if err != nil {
t.Fatal(err)
}
t.Log(exist)
}
{
exist, err := SharedServerDAO.ExistServerNameInCluster(tx, 18, "cdn.teaos.cn", 0)
if err != nil {
t.Fatal(err)
}
t.Log(exist)
}
{
exist, err := SharedServerDAO.ExistServerNameInCluster(tx, 18, "cdn.teaos.cn", 23)
if err != nil {
t.Fatal(err)
}
t.Log(exist)
}
}

View File

@@ -1355,3 +1355,30 @@ func (this *ServerService) UploadServerHTTPRequestStat(ctx context.Context, req
return this.Success() return this.Success()
} }
// 检查域名是否已经存在
func (this *ServerService) CheckServerNameDuplicationInNodeCluster(ctx context.Context, req *pb.CheckServerNameDuplicationInNodeClusterRequest) (*pb.CheckServerNameDuplicationInNodeClusterResponse, error) {
_, _, err := this.ValidateAdminAndUser(ctx, 0, 0)
if err != nil {
return nil, err
}
if len(req.ServerNames) == 0 {
return &pb.CheckServerNameDuplicationInNodeClusterResponse{DuplicatedServerNames: nil}, nil
}
var tx = this.NullTx()
duplicatedServerNames := []string{}
for _, serverName := range req.ServerNames {
exist, err := models.SharedServerDAO.ExistServerNameInCluster(tx, req.NodeClusterId, serverName, req.ExcludeServerId)
if err != nil {
return nil, err
}
if exist {
duplicatedServerNames = append(duplicatedServerNames, serverName)
}
}
return &pb.CheckServerNameDuplicationInNodeClusterResponse{DuplicatedServerNames: duplicatedServerNames}, nil
}