// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. //go:build plus package clusters import ( "encoding/json" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/dns/domains/domainutils" "github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/langs/codes" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/iwind/TeaGo/actions" ) type CreateAction struct { actionutils.ParentAction } func (this *CreateAction) Init() { this.Nav("", "", "create") } func (this *CreateAction) RunGet(params struct{}) { // 默认的访问日志设置 this.Data["accessLogRef"] = &dnsconfigs.NSAccessLogRef{ IsOn: true, } this.Show() } func (this *CreateAction) RunPost(params struct { Name string AccessLogJSON []byte Hosts []string Email string Must *actions.Must CSRF *actionutils.CSRF }) { var clusterId int64 defer func() { this.CreateLogInfo(codes.NSCluster_LogCreateNSCluster, clusterId) }() params.Must. Field("name", params.Name). Require("请输入集群名称"). Field("email", params.Email). Require("请输入正确的管理员电子邮箱地址") // 校验主机域名 var hosts = []string{} for _, host := range params.Hosts { if !domainutils.ValidateDomainFormat(host) { this.Fail("错误的DNS主机地址 '" + host + "'") return } hosts = append(hosts, host) } // 校验访问日志设置 var ref = &dnsconfigs.NSAccessLogRef{} err := json.Unmarshal(params.AccessLogJSON, ref) if err != nil { this.Fail("数据格式错误:" + err.Error()) } err = ref.Init() if err != nil { this.Fail("数据格式错误:" + err.Error()) } resp, err := this.RPC().NSClusterRPC().CreateNSCluster(this.AdminContext(), &pb.CreateNSClusterRequest{ Name: params.Name, AccessLogJSON: params.AccessLogJSON, Hosts: hosts, Email: params.Email, }) if err != nil { this.ErrorPage(err) return } clusterId = resp.NsClusterId this.Success() }