2020-10-17 21:15:22 +08:00
|
|
|
package ipaddressutils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"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-08-18 16:19:07 +08:00
|
|
|
var thresholdsJSON = []byte{}
|
|
|
|
|
var thresholds = addr.GetSlice("thresholds")
|
|
|
|
|
if len(thresholds) > 0 {
|
|
|
|
|
thresholdsJSON, _ = json.Marshal(thresholds)
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 21:15:22 +08:00
|
|
|
addrId := addr.GetInt64("id")
|
|
|
|
|
if addrId > 0 {
|
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,
|
|
|
|
|
ThresholdsJSON: thresholdsJSON,
|
2020-10-17 21:15:22 +08:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
_, err = parentAction.RPC().NodeIPAddressRPC().CreateNodeIPAddress(parentAction.AdminContext(), &pb.CreateNodeIPAddressRequest{
|
2021-08-18 16:19:07 +08:00
|
|
|
NodeId: nodeId,
|
|
|
|
|
Role: role,
|
|
|
|
|
Name: addr.GetString("name"),
|
|
|
|
|
Ip: addr.GetString("ip"),
|
|
|
|
|
CanAccess: addr.GetBool("canAccess"),
|
|
|
|
|
ThresholdsJSON: thresholdsJSON,
|
2020-10-17 21:15:22 +08:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|