2020-10-17 21:15:22 +08:00
|
|
|
package ipaddressutils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2021-12-07 18:22:46 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
|
2020-10-17 21:15:22 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
2021-05-26 14:43:29 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
2020-10-17 21:15:22 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
|
|
|
|
)
|
|
|
|
|
|
2021-05-26 14:43:29 +08:00
|
|
|
// UpdateNodeIPAddresses 保存一组IP地址
|
|
|
|
|
func UpdateNodeIPAddresses(parentAction *actionutils.ParentAction, nodeId int64, role nodeconfigs.NodeRole, ipAddressesJSON []byte) error {
|
2020-10-17 21:15:22 +08:00
|
|
|
addresses := []maps.Map{}
|
|
|
|
|
err := json.Unmarshal(ipAddressesJSON, &addresses)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
for _, addr := range addresses {
|
2021-12-07 18:22:46 +08:00
|
|
|
var resultAddrIds = []int64{}
|
2020-10-17 21:15:22 +08:00
|
|
|
addrId := addr.GetInt64("id")
|
|
|
|
|
if addrId > 0 {
|
2021-12-07 18:22:46 +08:00
|
|
|
resultAddrIds = append(resultAddrIds, addrId)
|
|
|
|
|
|
2021-08-18 09:24:03 +08:00
|
|
|
var isOn = false
|
|
|
|
|
if !addr.Has("isOn") { // 兼容老版本
|
|
|
|
|
isOn = true
|
|
|
|
|
} else {
|
|
|
|
|
isOn = addr.GetBool("isOn")
|
|
|
|
|
}
|
2021-08-18 16:19:07 +08:00
|
|
|
|
2020-10-17 21:15:22 +08:00
|
|
|
_, err = parentAction.RPC().NodeIPAddressRPC().UpdateNodeIPAddress(parentAction.AdminContext(), &pb.UpdateNodeIPAddressRequest{
|
2021-08-31 17:24:30 +08:00
|
|
|
NodeIPAddressId: addrId,
|
|
|
|
|
Ip: addr.GetString("ip"),
|
|
|
|
|
Name: addr.GetString("name"),
|
|
|
|
|
CanAccess: addr.GetBool("canAccess"),
|
|
|
|
|
IsOn: isOn,
|
2021-09-15 11:46:50 +08:00
|
|
|
IsUp: addr.GetBool("isUp"),
|
2020-10-17 21:15:22 +08:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-12-07 18:22:46 +08:00
|
|
|
var ipStrings = addr.GetString("ip")
|
|
|
|
|
result, _ := utils.ExtractIP(ipStrings)
|
|
|
|
|
|
|
|
|
|
if len(result) == 1 {
|
|
|
|
|
// 单个创建
|
|
|
|
|
createResp, err := parentAction.RPC().NodeIPAddressRPC().CreateNodeIPAddress(parentAction.AdminContext(), &pb.CreateNodeIPAddressRequest{
|
|
|
|
|
NodeId: nodeId,
|
|
|
|
|
Role: role,
|
|
|
|
|
Name: addr.GetString("name"),
|
|
|
|
|
Ip: result[0],
|
|
|
|
|
CanAccess: addr.GetBool("canAccess"),
|
|
|
|
|
IsUp: addr.GetBool("isUp"),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
addrId = createResp.NodeIPAddressId
|
|
|
|
|
resultAddrIds = append(resultAddrIds, addrId)
|
|
|
|
|
} else if len(result) > 1 {
|
|
|
|
|
// 批量创建
|
|
|
|
|
createResp, err := parentAction.RPC().NodeIPAddressRPC().CreateNodeIPAddresses(parentAction.AdminContext(), &pb.CreateNodeIPAddressesRequest{
|
|
|
|
|
NodeId: nodeId,
|
|
|
|
|
Role: role,
|
|
|
|
|
Name: addr.GetString("name"),
|
|
|
|
|
IpList: result,
|
|
|
|
|
CanAccess: addr.GetBool("canAccess"),
|
|
|
|
|
IsUp: addr.GetBool("isUp"),
|
|
|
|
|
GroupValue: ipStrings,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
resultAddrIds = append(resultAddrIds, createResp.NodeIPAddressIds...)
|
2021-09-12 20:21:32 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 保存阈值
|
|
|
|
|
var thresholds = addr.GetSlice("thresholds")
|
|
|
|
|
if len(thresholds) > 0 {
|
|
|
|
|
thresholdsJSON, err := json.Marshal(thresholds)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2021-12-07 18:22:46 +08:00
|
|
|
|
|
|
|
|
for _, addrId := range resultAddrIds {
|
|
|
|
|
_, err = parentAction.RPC().NodeIPAddressThresholdRPC().UpdateAllNodeIPAddressThresholds(parentAction.AdminContext(), &pb.UpdateAllNodeIPAddressThresholdsRequest{
|
|
|
|
|
NodeIPAddressId: addrId,
|
|
|
|
|
NodeIPAddressThresholdsJSON: thresholdsJSON,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2020-10-17 21:15:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-09-12 20:21:32 +08:00
|
|
|
|
|
|
|
|
// InitNodeIPAddressThresholds 初始化IP阈值
|
2021-09-14 19:39:41 +08:00
|
|
|
func InitNodeIPAddressThresholds(parentAction *actionutils.ParentAction, addrId int64) ([]*nodeconfigs.IPAddressThresholdConfig, error) {
|
2021-09-12 20:21:32 +08:00
|
|
|
thresholdsResp, err := parentAction.RPC().NodeIPAddressThresholdRPC().FindAllEnabledNodeIPAddressThresholds(parentAction.AdminContext(), &pb.FindAllEnabledNodeIPAddressThresholdsRequest{NodeIPAddressId: addrId})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-09-14 19:39:41 +08:00
|
|
|
var thresholds = []*nodeconfigs.IPAddressThresholdConfig{}
|
2021-09-12 20:21:32 +08:00
|
|
|
if len(thresholdsResp.NodeIPAddressThresholds) > 0 {
|
|
|
|
|
for _, pbThreshold := range thresholdsResp.NodeIPAddressThresholds {
|
2021-09-14 19:39:41 +08:00
|
|
|
var threshold = &nodeconfigs.IPAddressThresholdConfig{
|
2021-09-12 20:21:32 +08:00
|
|
|
Id: pbThreshold.Id,
|
2021-09-14 19:39:41 +08:00
|
|
|
Items: []*nodeconfigs.IPAddressThresholdItemConfig{},
|
|
|
|
|
Actions: []*nodeconfigs.IPAddressThresholdActionConfig{},
|
2021-09-12 20:21:32 +08:00
|
|
|
}
|
|
|
|
|
if len(pbThreshold.ItemsJSON) > 0 {
|
|
|
|
|
err = json.Unmarshal(pbThreshold.ItemsJSON, &threshold.Items)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(pbThreshold.ActionsJSON) > 0 {
|
|
|
|
|
err = json.Unmarshal(pbThreshold.ActionsJSON, &threshold.Actions)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
thresholds = append(thresholds, threshold)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return thresholds, nil
|
|
|
|
|
}
|