90 lines
2.1 KiB
Go
90 lines
2.1 KiB
Go
package apps
|
|
|
|
import (
|
|
"encoding/json"
|
|
"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
|
|
|
|
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
|
|
ClusterId int64
|
|
UserId int64
|
|
|
|
Must *actions.Must
|
|
CSRF *actionutils.CSRF
|
|
}) {
|
|
params.Must.Field("name", params.Name).Require("请输入应用名称")
|
|
if params.ClusterId <= 0 {
|
|
this.FailField("clusterId", "请选择集群")
|
|
return
|
|
}
|
|
|
|
clusterIdsJSON, _ := json.Marshal([]int64{params.ClusterId})
|
|
|
|
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),
|
|
ClusterIdsJSON: clusterIdsJSON,
|
|
IsOn: true,
|
|
SignEnabled: true,
|
|
UserId: params.UserId,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
this.Data["appId"] = createResp.GetAppDbId()
|
|
this.Success()
|
|
}
|