111 lines
3.0 KiB
Go
111 lines
3.0 KiB
Go
package apps
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
"github.com/iwind/TeaGo/actions"
|
|
"github.com/iwind/TeaGo/maps"
|
|
)
|
|
|
|
type CreateAction struct {
|
|
actionutils.ParentAction
|
|
}
|
|
|
|
func (this *CreateAction) Init() {
|
|
this.Nav("", "", "create")
|
|
}
|
|
|
|
func (this *CreateAction) RunGet(params struct{}) {
|
|
clusterResp, err := this.RPC().HTTPDNSClusterRPC().FindAllHTTPDNSClusters(this.AdminContext(), &pb.FindAllHTTPDNSClustersRequest{})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
clusters := make([]maps.Map, 0, len(clusterResp.GetClusters()))
|
|
for _, cluster := range clusterResp.GetClusters() {
|
|
clusters = append(clusters, maps.Map{
|
|
"id": cluster.GetId(),
|
|
"name": cluster.GetName(),
|
|
})
|
|
}
|
|
this.Data["clusters"] = clusters
|
|
|
|
defaultPrimaryClusterId := int64(0)
|
|
for _, cluster := range clusterResp.GetClusters() {
|
|
if cluster.GetIsDefault() {
|
|
defaultPrimaryClusterId = cluster.GetId()
|
|
break
|
|
}
|
|
}
|
|
if defaultPrimaryClusterId <= 0 && len(clusters) > 0 {
|
|
defaultPrimaryClusterId = clusters[0].GetInt64("id")
|
|
}
|
|
this.Data["defaultPrimaryClusterId"] = defaultPrimaryClusterId
|
|
|
|
defaultBackupClusterId := int64(0)
|
|
for _, cluster := range clusters {
|
|
clusterId := cluster.GetInt64("id")
|
|
if clusterId > 0 && clusterId != defaultPrimaryClusterId {
|
|
defaultBackupClusterId = clusterId
|
|
break
|
|
}
|
|
}
|
|
this.Data["defaultBackupClusterId"] = defaultBackupClusterId
|
|
|
|
usersResp, err := this.RPC().UserRPC().ListEnabledUsers(this.AdminContext(), &pb.ListEnabledUsersRequest{
|
|
Offset: 0,
|
|
Size: 10_000,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
users := make([]maps.Map, 0, len(usersResp.GetUsers()))
|
|
for _, user := range usersResp.GetUsers() {
|
|
users = append(users, maps.Map{
|
|
"id": user.GetId(),
|
|
"fullname": user.GetFullname(),
|
|
"username": user.GetUsername(),
|
|
})
|
|
}
|
|
this.Data["users"] = users
|
|
|
|
this.Show()
|
|
}
|
|
|
|
func (this *CreateAction) RunPost(params struct {
|
|
Name string
|
|
PrimaryClusterId int64
|
|
BackupClusterId int64
|
|
UserId int64
|
|
|
|
Must *actions.Must
|
|
CSRF *actionutils.CSRF
|
|
}) {
|
|
params.Must.Field("name", params.Name).Require("请输入应用名称")
|
|
params.Must.Field("primaryClusterId", params.PrimaryClusterId).Gt(0, "请输入主服务集群")
|
|
if params.BackupClusterId > 0 && params.BackupClusterId == params.PrimaryClusterId {
|
|
this.FailField("backupClusterId", "备用服务集群必须和主服务集群不一致")
|
|
}
|
|
|
|
createResp, err := this.RPC().HTTPDNSAppRPC().CreateHTTPDNSApp(this.AdminContext(), &pb.CreateHTTPDNSAppRequest{
|
|
Name: params.Name,
|
|
AppId: "app" + strconv.FormatInt(time.Now().UnixNano()%1_000_000_000_000, 36),
|
|
PrimaryClusterId: params.PrimaryClusterId,
|
|
BackupClusterId: params.BackupClusterId,
|
|
IsOn: true,
|
|
SignEnabled: true,
|
|
UserId: params.UserId,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
this.Data["appId"] = createResp.GetAppDbId()
|
|
this.Success()
|
|
}
|