增加独立的IP地址管理功能

This commit is contained in:
GoEdgeLab
2021-08-31 17:24:30 +08:00
parent 06d7f8d754
commit 31aa942c0f
31 changed files with 822 additions and 50 deletions

View File

@@ -171,8 +171,8 @@ func (this *CreateNodeAction) RunPost(params struct {
addressId := address.GetInt64("id")
if addressId > 0 {
_, err = this.RPC().NodeIPAddressRPC().UpdateNodeIPAddressNodeId(this.AdminContext(), &pb.UpdateNodeIPAddressNodeIdRequest{
AddressId: addressId,
NodeId: nodeId,
NodeIPAddressId: addressId,
NodeId: nodeId,
})
} else {
var thresholdsJSON = []byte{}

View File

@@ -0,0 +1,26 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package addr
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
type DeleteAction struct {
actionutils.ParentAction
}
func (this *DeleteAction) RunPost(params struct {
AddrId int64
}) {
defer this.CreateLogInfo("删除IP地址 %d", params.AddrId)
_, err := this.RPC().NodeIPAddressRPC().DisableNodeIPAddress(this.AdminContext(), &pb.DisableNodeIPAddressRequest{NodeIPAddressId: params.AddrId})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,66 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package addr
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/ip-addrs/ipaddrutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "addr")
}
func (this *IndexAction) RunGet(params struct {
AddrId int64
}) {
addr, err := ipaddrutils.InitIPAddr(this.Parent(), params.AddrId)
if err != nil {
this.ErrorPage(err)
return
}
var thresholds = []*nodeconfigs.NodeValueThresholdConfig{}
if len(addr.ThresholdsJSON) > 0 {
err = json.Unmarshal(addr.ThresholdsJSON, &thresholds)
if err != nil {
this.ErrorPage(err)
return
}
}
nodeResp, err := this.RPC().NodeRPC().FindEnabledBasicNode(this.AdminContext(), &pb.FindEnabledBasicNodeRequest{NodeId: addr.NodeId})
if err != nil {
this.ErrorPage(err)
return
}
var node = nodeResp.Node
if node == nil || node.NodeCluster == nil {
this.ErrorPage(errors.New("node or cluster is not available"))
return
}
this.Data["addr"] = maps.Map{
"id": addr.Id,
"name": addr.Name,
"description": addr.Description,
"ip": addr.Ip,
"canAccess": addr.CanAccess,
"isOn": addr.IsOn,
"isUp": addr.IsUp,
"thresholds": thresholds,
"node": maps.Map{"id": node.Id, "name": node.Name},
"cluster": maps.Map{"id": node.NodeCluster.Id, "name": node.NodeCluster.Name},
}
this.Show()
}

View File

@@ -0,0 +1,75 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package addr
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/ip-addrs/ipaddrutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
)
type LogsAction struct {
actionutils.ParentAction
}
func (this *LogsAction) Init() {
this.Nav("", "", "log")
}
func (this *LogsAction) RunGet(params struct {
AddrId int64
}) {
_, err := ipaddrutils.InitIPAddr(this.Parent(), params.AddrId)
if err != nil {
this.ErrorPage(err)
return
}
countResp, err := this.RPC().NodeIPAddressLogRPC().CountAllNodeIPAddressLogs(this.AdminContext(), &pb.CountAllNodeIPAddressLogsRequest{NodeIPAddressId: params.AddrId})
if err != nil {
this.ErrorPage(err)
return
}
var page = this.NewPage(countResp.Count)
this.Data["page"] = page.AsHTML()
logsResp, err := this.RPC().NodeIPAddressLogRPC().ListNodeIPAddressLogs(this.AdminContext(), &pb.ListNodeIPAddressLogsRequest{
NodeIPAddressId: params.AddrId,
Offset: page.Offset,
Size: page.Size,
})
if err != nil {
this.ErrorPage(err)
return
}
var logMaps = []maps.Map{}
for _, log := range logsResp.NodeIPAddressLogs {
var adminMap maps.Map
if log.Admin != nil {
adminMap = maps.Map{
"id": log.Admin.Id,
"name": log.Admin.Fullname,
}
} else {
adminMap = maps.Map{
"id": 0,
"name": "[系统]",
}
}
logMaps = append(logMaps, maps.Map{
"id": log.Id,
"description": log.Description,
"createdTime": timeutil.FormatTime("Y-m-d H:i:s", log.CreatedAt),
"isUp": log.IsUp,
"isOn": log.IsOn,
"canAccess": log.CanAccess,
"admin": adminMap,
})
}
this.Data["logs"] = logMaps
this.Show()
}

View File

@@ -0,0 +1,92 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package addr
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/ip-addrs/ipaddrutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
"net"
)
type UpdateAction struct {
actionutils.ParentAction
}
func (this *UpdateAction) Init() {
this.Nav("", "", "update")
}
func (this *UpdateAction) RunGet(params struct {
AddrId int64
}) {
addr, err := ipaddrutils.InitIPAddr(this.Parent(), params.AddrId)
if err != nil {
this.ErrorPage(err)
return
}
var thresholds = []*nodeconfigs.NodeValueThresholdConfig{}
if len(addr.ThresholdsJSON) > 0 {
err = json.Unmarshal(addr.ThresholdsJSON, &thresholds)
if err != nil {
this.ErrorPage(err)
return
}
}
this.Data["supportThresholds"] = true
this.Data["addr"] = maps.Map{
"id": addr.Id,
"name": addr.Name,
"description": addr.Description,
"ip": addr.Ip,
"canAccess": addr.CanAccess,
"isOn": addr.IsOn,
"isUp": addr.IsUp,
"thresholds": thresholds,
}
this.Show()
}
func (this *UpdateAction) RunPost(params struct {
AddrId int64
IP string `alias:"ip"`
Name string
CanAccess bool
IsOn bool
ThresholdsJSON []byte
Must *actions.Must
CSRF *actionutils.CSRF
}) {
params.Must.
Field("ip", params.IP).
Require("请输入IP地址")
ip := net.ParseIP(params.IP)
if len(ip) == 0 {
this.Fail("请输入正确的IP")
}
_, err := this.RPC().NodeIPAddressRPC().UpdateNodeIPAddress(this.AdminContext(), &pb.UpdateNodeIPAddressRequest{
NodeIPAddressId: params.AddrId,
Name: params.Name,
Ip: params.IP,
CanAccess: params.CanAccess,
IsOn: params.IsOn,
ThresholdsJSON: params.ThresholdsJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,94 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package ipaddrs
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "index")
}
func (this *IndexAction) RunGet(params struct {
ClusterId int64
UpState int8
Keyword string
}) {
this.Data["clusterId"] = params.ClusterId
this.Data["upState"] = params.UpState
this.Data["keyword"] = params.Keyword
countResp, err := this.RPC().NodeIPAddressRPC().CountAllEnabledIPAddresses(this.AdminContext(), &pb.CountAllEnabledIPAddressesRequest{
NodeClusterId: params.ClusterId,
Role: nodeconfigs.NodeRoleNode,
UpState: int32(params.UpState),
Keyword: params.Keyword,
})
if err != nil {
this.ErrorPage(err)
return
}
var count = countResp.Count
var page = this.NewPage(count)
this.Data["page"] = page.AsHTML()
addrsResp, err := this.RPC().NodeIPAddressRPC().ListEnabledIPAddresses(this.AdminContext(), &pb.ListEnabledIPAddressesRequest{
NodeClusterId: params.ClusterId,
Role: nodeconfigs.NodeRoleNode,
UpState: int32(params.UpState),
Keyword: params.Keyword,
Offset: page.Offset,
Size: page.Size,
})
if err != nil {
this.ErrorPage(err)
return
}
var addrMaps = []maps.Map{}
for _, addr := range addrsResp.NodeIPAddresses {
var thresholds = []*nodeconfigs.NodeValueThresholdConfig{}
if len(addr.ThresholdsJSON) > 0 {
err = json.Unmarshal(addr.ThresholdsJSON, &thresholds)
if err != nil {
this.ErrorPage(err)
return
}
}
nodeResp, err := this.RPC().NodeRPC().FindEnabledBasicNode(this.AdminContext(), &pb.FindEnabledBasicNodeRequest{NodeId: addr.NodeId})
if err != nil {
this.ErrorPage(err)
return
}
var node = nodeResp.Node
if node == nil || node.NodeCluster == nil {
continue
}
addrMaps = append(addrMaps, maps.Map{
"id": addr.Id,
"name": addr.Name,
"description": addr.Description,
"ip": addr.Ip,
"canAccess": addr.CanAccess,
"isOn": addr.IsOn,
"isUp": addr.IsUp,
"hasThresholds": len(thresholds) > 0,
"node": maps.Map{"id": node.Id, "name": node.Name},
"cluster": maps.Map{"id": node.NodeCluster.Id, "name": node.NodeCluster.Name},
})
}
this.Data["addrs"] = addrMaps
this.Show()
}

View File

@@ -0,0 +1,28 @@
package ipaddrs
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/clusterutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/ip-addrs/addr"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeNode)).
Helper(clusterutils.NewClustersHelper()).
Data("teaSubMenu", "ipAddr").
Prefix("/clusters/ip-addrs").
Get("", new(IndexAction)).
Get("/logs", new(LogsAction)).
// 单个地址操作
Post("/addr/delete", new(addr.DeleteAction)).
Get("/addr", new(addr.IndexAction)).
GetPost("/addr/update", new(addr.UpdateAction)).
Get("/addr/logs", new(addr.LogsAction)).
EndAll()
})
}

View File

@@ -0,0 +1,30 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package ipaddrutils
import (
"errors"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
)
func InitIPAddr(parent *actionutils.ParentAction, addrId int64) (*pb.NodeIPAddress, error) {
addrResp, err := parent.RPC().NodeIPAddressRPC().FindEnabledNodeIPAddress(parent.AdminContext(), &pb.FindEnabledNodeIPAddressRequest{NodeIPAddressId: addrId})
if err != nil {
return nil, err
}
var addr = addrResp.NodeIPAddress
if addr == nil {
return nil, errors.New("nodeIPAddress with id '" + types.String(addrId) + "' not found")
}
parent.Data["addr"] = maps.Map{
"id": addr.Id,
"name": addr.Name,
"ip": addr.Ip,
}
return addr, nil
}

View File

@@ -0,0 +1,78 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package ipaddrs
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
)
type LogsAction struct {
actionutils.ParentAction
}
func (this *LogsAction) Init() {
this.Nav("", "", "log")
}
func (this *LogsAction) RunGet(params struct {
}) {
countResp, err := this.RPC().NodeIPAddressLogRPC().CountAllNodeIPAddressLogs(this.AdminContext(), &pb.CountAllNodeIPAddressLogsRequest{})
if err != nil {
this.ErrorPage(err)
return
}
var page = this.NewPage(countResp.Count)
this.Data["page"] = page.AsHTML()
logsResp, err := this.RPC().NodeIPAddressLogRPC().ListNodeIPAddressLogs(this.AdminContext(), &pb.ListNodeIPAddressLogsRequest{
Offset: page.Offset,
Size: page.Size,
})
if err != nil {
this.ErrorPage(err)
return
}
var logMaps = []maps.Map{}
for _, log := range logsResp.NodeIPAddressLogs {
var adminMap maps.Map
if log.Admin != nil {
adminMap = maps.Map{
"id": log.Admin.Id,
"name": log.Admin.Fullname,
}
} else {
adminMap = maps.Map{
"id": 0,
"name": "[系统]",
}
}
var addrMap maps.Map
if log.NodeIPAddress != nil {
var addr = log.NodeIPAddress
if addr != nil {
addrMap = maps.Map{
"id": addr.Id,
"ip": addr.Ip,
}
}
}
logMaps = append(logMaps, maps.Map{
"id": log.Id,
"description": log.Description,
"createdTime": timeutil.FormatTime("Y-m-d H:i:s", log.CreatedAt),
"isUp": log.IsUp,
"isOn": log.IsOn,
"canAccess": log.CanAccess,
"admin": adminMap,
"addr": addrMap,
})
}
this.Data["logs"] = logMaps
this.Show()
}