Files
EdgeAdmin/internal/web/actions/default/dns/issues/updateNodePopup.go

123 lines
3.1 KiB
Go
Raw Normal View History

2020-11-14 09:42:21 +08:00
package issues
import (
2020-11-16 13:03:45 +08:00
"encoding/json"
2020-11-14 09:42:21 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
2020-11-16 13:03:45 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/dns/domains/domainutils"
2020-11-14 09:42:21 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
2020-11-15 11:57:43 +08:00
"github.com/iwind/TeaGo/maps"
2020-11-14 09:42:21 +08:00
"net"
)
type UpdateNodePopupAction struct {
actionutils.ParentAction
}
func (this *UpdateNodePopupAction) Init() {
this.Nav("", "", "")
}
func (this *UpdateNodePopupAction) RunGet(params struct {
2021-07-31 22:23:07 +08:00
ClusterId int64
NodeId int64
IpAddrId int64
2020-11-14 09:42:21 +08:00
}) {
this.Data["nodeId"] = params.NodeId
2021-07-31 22:23:07 +08:00
dnsInfoResp, err := this.RPC().NodeRPC().FindEnabledNodeDNS(this.AdminContext(), &pb.FindEnabledNodeDNSRequest{
NodeId: params.NodeId,
NodeClusterId: params.ClusterId,
NodeIPAddrId: params.IpAddrId,
2021-07-31 22:23:07 +08:00
})
2020-11-14 09:42:21 +08:00
if err != nil {
this.ErrorPage(err)
return
}
dnsInfo := dnsInfoResp.Node
if dnsInfo == nil {
this.NotFound("node", params.NodeId)
return
}
this.Data["ipAddr"] = dnsInfo.IpAddr
this.Data["ipAddrId"] = dnsInfo.NodeIPAddressId
2021-07-31 22:23:07 +08:00
this.Data["routes"] = domainutils.ConvertRoutesToMaps(dnsInfo)
2020-11-14 09:42:21 +08:00
this.Data["domainId"] = dnsInfo.DnsDomainId
2020-11-14 21:28:14 +08:00
this.Data["domainName"] = dnsInfo.DnsDomainName
2020-11-14 09:42:21 +08:00
// 读取所有线路
var allRouteMaps = []maps.Map{}
2020-11-14 09:42:21 +08:00
if dnsInfo.DnsDomainId > 0 {
routesResp, err := this.RPC().DNSDomainRPC().FindAllDNSDomainRoutes(this.AdminContext(), &pb.FindAllDNSDomainRoutesRequest{DnsDomainId: dnsInfo.DnsDomainId})
if err != nil {
this.ErrorPage(err)
return
}
if len(routesResp.Routes) > 0 {
2020-11-15 11:57:43 +08:00
for _, route := range routesResp.Routes {
2020-11-16 13:03:45 +08:00
allRouteMaps = append(allRouteMaps, maps.Map{
2021-07-31 22:23:07 +08:00
"name": route.Name,
"code": route.Code,
"domainName": dnsInfo.DnsDomainName,
"domainId": dnsInfo.DnsDomainId,
2020-11-15 11:57:43 +08:00
})
}
2020-11-14 09:42:21 +08:00
2020-11-16 13:03:45 +08:00
// 筛选
2021-07-31 22:23:07 +08:00
var routes = domainutils.FilterRoutes(dnsInfo.Routes, routesResp.Routes)
dnsInfo.Routes = routes
this.Data["routes"] = domainutils.ConvertRoutesToMaps(dnsInfo)
2020-11-15 11:57:43 +08:00
}
2020-11-14 09:42:21 +08:00
}
2020-11-16 13:03:45 +08:00
this.Data["allRoutes"] = allRouteMaps
2020-11-14 09:42:21 +08:00
this.Show()
}
func (this *UpdateNodePopupAction) RunPost(params struct {
2020-11-16 13:03:45 +08:00
NodeId int64
IpAddr string
IpAddrId int64
2020-11-16 13:03:45 +08:00
DomainId int64
DnsRoutesJSON []byte
2020-11-14 09:42:21 +08:00
Must *actions.Must
CSRF *actionutils.CSRF
}) {
// 操作日志
2020-11-20 15:32:42 +08:00
defer this.CreateLog(oplogs.LevelInfo, "修改节点 %d 的DNS设置", params.NodeId)
2020-11-14 09:42:21 +08:00
var routes = []string{}
if len(params.DnsRoutesJSON) > 0 {
err := json.Unmarshal(params.DnsRoutesJSON, &routes)
if err != nil {
this.ErrorPage(err)
return
}
2020-11-16 13:03:45 +08:00
}
2020-11-14 09:42:21 +08:00
params.Must.
Field("ipAddr", params.IpAddr).
Require("请输入IP地址")
if net.ParseIP(params.IpAddr) == nil {
this.FailField("ipAddr", "请输入正确的IP地址")
}
// 执行修改
_, err := this.RPC().NodeRPC().UpdateNodeDNS(this.AdminContext(), &pb.UpdateNodeDNSRequest{
NodeId: params.NodeId,
IpAddr: params.IpAddr,
NodeIPAddressId: params.IpAddrId,
DnsDomainId: params.DomainId,
Routes: routes,
2020-11-14 09:42:21 +08:00
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}