Files
EdgeAdmin/internal/web/actions/default/servers/server/settings/index.go

190 lines
4.3 KiB
Go
Raw Normal View History

2020-08-21 12:32:16 +08:00
package settings
2020-09-13 20:37:07 +08:00
import (
"errors"
2020-11-11 21:32:19 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
2020-09-13 20:37:07 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
2020-08-21 12:32:16 +08:00
2021-05-03 15:15:31 +08:00
// IndexAction 服务基本信息设置
2020-08-21 12:32:16 +08:00
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "setting", "")
this.SecondMenu("basic")
}
2020-09-13 20:37:07 +08:00
func (this *IndexAction) RunGet(params struct {
ServerId int64
}) {
// 所有集群
resp, err := this.RPC().NodeClusterRPC().FindAllEnabledNodeClusters(this.AdminContext(), &pb.FindAllEnabledNodeClustersRequest{})
if err != nil {
this.ErrorPage(err)
}
if err != nil {
this.ErrorPage(err)
return
}
clusterMaps := []maps.Map{}
2020-12-17 17:35:38 +08:00
for _, cluster := range resp.NodeClusters {
2020-09-13 20:37:07 +08:00
clusterMaps = append(clusterMaps, maps.Map{
"id": cluster.Id,
"name": cluster.Name,
})
}
this.Data["clusters"] = clusterMaps
// 当前服务信息
serverResp, err := this.RPC().ServerRPC().FindEnabledServer(this.AdminContext(), &pb.FindEnabledServerRequest{
2022-12-31 17:31:17 +08:00
ServerId: params.ServerId,
IgnoreSSLCerts: true,
})
2020-09-13 20:37:07 +08:00
if err != nil {
this.ErrorPage(err)
return
}
server := serverResp.Server
if server == nil {
2020-12-23 19:44:10 +08:00
this.NotFound("server", params.ServerId)
2020-09-13 20:37:07 +08:00
return
}
2020-12-23 19:44:10 +08:00
// 用户
if server.User != nil {
this.Data["user"] = maps.Map{
"id": server.User.Id,
"fullname": server.User.Fullname,
"username": server.User.Username,
}
} else {
this.Data["user"] = nil
}
2021-11-09 15:36:18 +08:00
// 套餐
2022-10-14 09:57:24 +08:00
this.initUserPlan(server)
2021-11-09 15:36:18 +08:00
2020-12-23 19:44:10 +08:00
// 集群
2020-09-13 20:37:07 +08:00
clusterId := int64(0)
2020-12-23 19:44:10 +08:00
this.Data["clusterName"] = ""
2020-12-17 17:35:38 +08:00
if server.NodeCluster != nil {
clusterId = server.NodeCluster.Id
2020-12-23 19:44:10 +08:00
this.Data["clusterName"] = server.NodeCluster.Name
2020-09-13 20:37:07 +08:00
}
2020-10-29 21:37:48 +08:00
// 分组
groupMaps := []maps.Map{}
2021-05-25 17:48:51 +08:00
if len(server.ServerGroups) > 0 {
for _, group := range server.ServerGroups {
2020-10-29 21:37:48 +08:00
groupMaps = append(groupMaps, maps.Map{
"id": group.Id,
"name": group.Name,
})
}
}
2020-09-13 20:37:07 +08:00
this.Data["server"] = maps.Map{
"id": server.Id,
"clusterId": clusterId,
"type": server.Type,
"name": server.Name,
"description": server.Description,
2020-09-28 16:25:26 +08:00
"isOn": server.IsOn,
2020-10-29 21:37:48 +08:00
"groups": groupMaps,
2020-09-13 20:37:07 +08:00
}
serverType := serverconfigs.FindServerType(server.Type)
if serverType == nil {
this.ErrorPage(errors.New("invalid server type '" + server.Type + "'"))
return
}
typeName := serverType.GetString("name")
this.Data["typeName"] = typeName
2021-05-03 15:15:31 +08:00
// 记录最近使用
_, err = this.RPC().LatestItemRPC().IncreaseLatestItem(this.AdminContext(), &pb.IncreaseLatestItemRequest{
ItemType: "server",
ItemId: params.ServerId,
})
if err != nil {
this.ErrorPage(err)
return
}
2020-08-21 12:32:16 +08:00
this.Show()
}
2020-09-13 20:37:07 +08:00
2021-05-03 15:15:31 +08:00
// RunPost 保存
2020-09-13 20:37:07 +08:00
func (this *IndexAction) RunPost(params struct {
ServerId int64
UserId int64
Name string
Description string
ClusterId int64
KeepOldConfigs bool
GroupIds []int64
IsOn bool
UserPlanId int64
2020-09-13 20:37:07 +08:00
Must *actions.Must
}) {
2020-11-11 21:32:19 +08:00
// 记录日志
2020-11-20 15:32:42 +08:00
defer this.CreateLog(oplogs.LevelInfo, "修改代理服务 %d 基本信息", params.ServerId)
2020-11-11 21:32:19 +08:00
2020-09-13 20:37:07 +08:00
params.Must.
Field("name", params.Name).
Require("请输入服务名称")
if params.ClusterId <= 0 {
this.Fail("请选择部署的集群")
}
2021-11-09 15:36:18 +08:00
// 修改基本信息
2020-09-13 20:37:07 +08:00
_, err := this.RPC().ServerRPC().UpdateServerBasic(this.AdminContext(), &pb.UpdateServerBasicRequest{
2021-05-25 17:48:51 +08:00
ServerId: params.ServerId,
Name: params.Name,
Description: params.Description,
NodeClusterId: params.ClusterId,
KeepOldConfigs: params.KeepOldConfigs,
2021-05-25 17:48:51 +08:00
IsOn: params.IsOn,
ServerGroupIds: params.GroupIds,
2020-09-13 20:37:07 +08:00
})
if err != nil {
this.ErrorPage(err)
return
}
// 修改用户
if params.UserId > 0 {
_, err = this.RPC().ServerRPC().UpdateServerUser(this.AdminContext(), &pb.UpdateServerUserRequest{
ServerId: params.ServerId,
UserId: params.UserId,
2021-11-09 15:36:18 +08:00
})
if err != nil {
this.ErrorPage(err)
return
}
} else {
// 修改套餐
if params.UserPlanId > 0 {
_, err = this.RPC().ServerRPC().UpdateServerUserPlan(this.AdminContext(), &pb.UpdateServerUserPlanRequest{
ServerId: params.ServerId,
UserPlanId: params.UserPlanId,
})
if err != nil {
this.ErrorPage(err)
return
}
}
2021-11-09 15:36:18 +08:00
}
2020-09-13 20:37:07 +08:00
this.Success()
}