187 lines
4.7 KiB
Go
187 lines
4.7 KiB
Go
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
//go:build plus
|
|
|
|
package domains
|
|
|
|
import (
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/dns/domains/domainutils"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
"github.com/iwind/TeaGo/actions"
|
|
"github.com/iwind/TeaGo/lists"
|
|
"strings"
|
|
)
|
|
|
|
type CreateAction struct {
|
|
actionutils.ParentAction
|
|
}
|
|
|
|
func (this *CreateAction) Init() {
|
|
this.Nav("", "", "create")
|
|
}
|
|
|
|
func (this *CreateAction) RunGet(params struct{}) {
|
|
// 集群数量
|
|
countClustersResp, err := this.RPC().NSClusterRPC().CountAllNSClusters(this.AdminContext(), &pb.CountAllNSClustersRequest{})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
this.Data["countClusters"] = countClustersResp.Count
|
|
|
|
this.Show()
|
|
}
|
|
|
|
func (this *CreateAction) RunPost(params struct {
|
|
AddingType string
|
|
|
|
Name string
|
|
Names string // 如果批量的话
|
|
ClusterId int64
|
|
UserId int64
|
|
GroupId int64
|
|
|
|
Must *actions.Must
|
|
CSRF *actionutils.CSRF
|
|
}) {
|
|
var groupIds = []int64{}
|
|
if params.GroupId > 0 {
|
|
groupIds = append(groupIds, params.GroupId)
|
|
}
|
|
|
|
if params.AddingType == "batch" { // 批量添加
|
|
defer this.CreateLogInfo(codes.NS_LogCreateNSDomainsBatch)
|
|
|
|
params.Must.
|
|
Field("names", params.Names).
|
|
Require("请输入域名").
|
|
Field("clusterId", params.ClusterId).
|
|
Gt(0, "请选择所属集群")
|
|
|
|
var names = []string{}
|
|
for _, name := range strings.Split(params.Names, "\n") {
|
|
name = strings.TrimSpace(name)
|
|
if len(name) == 0 {
|
|
continue
|
|
}
|
|
if !domainutils.ValidateDomainFormat(name) {
|
|
this.Fail("域名 '" + name + "' 格式不正确")
|
|
}
|
|
if !lists.ContainsString(names, name) {
|
|
names = append(names, name)
|
|
}
|
|
}
|
|
|
|
if len(names) == 0 {
|
|
this.FailField("names", "请输入域名")
|
|
return
|
|
}
|
|
|
|
// 检查域名是否已经存在
|
|
{
|
|
existResp, err := this.RPC().NSDomainRPC().ExistNSDomains(this.AdminContext(), &pb.ExistNSDomainsRequest{
|
|
Names: names,
|
|
UserId: params.UserId,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
if len(existResp.ExistingNames) > 0 {
|
|
this.Fail("域名 " + strings.Join(existResp.ExistingNames, ", ") + " 已经存在,无法重复添加")
|
|
return
|
|
}
|
|
}
|
|
{
|
|
existResp, err := this.RPC().NSDomainRPC().ExistVerifiedNSDomains(this.AdminContext(), &pb.ExistVerifiedNSDomainsRequest{
|
|
Names: names,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
if len(existResp.ExistingNames) > 0 {
|
|
this.Fail("域名 " + strings.Join(existResp.ExistingNames, ", ") + " 已经由别的用户添加,无法重复添加")
|
|
return
|
|
}
|
|
}
|
|
|
|
_, err := this.RPC().NSDomainRPC().CreateNSDomains(this.AdminContext(), &pb.CreateNSDomainsRequest{
|
|
NsClusterId: params.ClusterId,
|
|
UserId: params.UserId,
|
|
Names: names,
|
|
NsDomainGroupIds: groupIds,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
this.Success()
|
|
} else { // 单个添加
|
|
var domainId int64
|
|
defer func() {
|
|
this.CreateLogInfo(codes.NSDomain_LogCreateNSDomain, domainId)
|
|
}()
|
|
|
|
params.Name = strings.ToLower(params.Name)
|
|
|
|
params.Must.
|
|
Field("name", params.Name).
|
|
Require("请输入域名").
|
|
Expect(func() (message string, success bool) {
|
|
success = domainutils.ValidateDomainFormat(params.Name)
|
|
if !success {
|
|
message = "请输入正确的域名"
|
|
}
|
|
return
|
|
}).
|
|
Field("clusterId", params.ClusterId).
|
|
Gt(0, "请选择所属集群")
|
|
|
|
// 检查域名是否已经存在
|
|
{
|
|
existResp, err := this.RPC().NSDomainRPC().ExistNSDomains(this.AdminContext(), &pb.ExistNSDomainsRequest{
|
|
Names: []string{params.Name},
|
|
UserId: params.UserId,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
if len(existResp.ExistingNames) > 0 {
|
|
this.Fail("域名 " + strings.Join(existResp.ExistingNames, ", ") + " 已经存在,无法重复添加")
|
|
return
|
|
}
|
|
}
|
|
{
|
|
existResp, err := this.RPC().NSDomainRPC().ExistVerifiedNSDomains(this.AdminContext(), &pb.ExistVerifiedNSDomainsRequest{
|
|
Names: []string{params.Name},
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
if len(existResp.ExistingNames) > 0 {
|
|
this.Fail("域名 " + strings.Join(existResp.ExistingNames, ", ") + " 已经由别的用户添加,无法重复添加")
|
|
return
|
|
}
|
|
}
|
|
|
|
createResp, err := this.RPC().NSDomainRPC().CreateNSDomain(this.AdminContext(), &pb.CreateNSDomainRequest{
|
|
NsClusterId: params.ClusterId,
|
|
UserId: params.UserId,
|
|
Name: params.Name,
|
|
NsDomainGroupIds: groupIds,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
domainId = createResp.NsDomainId
|
|
|
|
this.Success()
|
|
}
|
|
}
|