2020-08-30 16:12:00 +08:00
package models
import (
2021-09-08 19:34:31 +08:00
"encoding/json"
2020-08-30 16:12:00 +08:00
"errors"
2021-08-18 09:24:18 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
2021-08-31 17:24:52 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
2021-05-26 14:40:05 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
2020-08-30 16:12:00 +08:00
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
)
const (
NodeIPAddressStateEnabled = 1 // 已启用
NodeIPAddressStateDisabled = 0 // 已禁用
)
type NodeIPAddressDAO dbs . DAO
func NewNodeIPAddressDAO ( ) * NodeIPAddressDAO {
return dbs . NewDAO ( & NodeIPAddressDAO {
DAOObject : dbs . DAOObject {
DB : Tea . Env ,
Table : "edgeNodeIPAddresses" ,
Model : new ( NodeIPAddress ) ,
PkName : "id" ,
} ,
} ) . ( * NodeIPAddressDAO )
}
2020-10-13 20:05:13 +08:00
var SharedNodeIPAddressDAO * NodeIPAddressDAO
func init ( ) {
dbs . OnReady ( func ( ) {
SharedNodeIPAddressDAO = NewNodeIPAddressDAO ( )
} )
}
2020-08-30 16:12:00 +08:00
2021-05-26 14:40:05 +08:00
// EnableAddress 启用条目
2021-08-18 09:24:18 +08:00
func ( this * NodeIPAddressDAO ) EnableAddress ( tx * dbs . Tx , addressId int64 ) ( err error ) {
2021-01-01 23:31:30 +08:00
_ , err = this . Query ( tx ) .
2021-08-18 09:24:18 +08:00
Pk ( addressId ) .
2020-08-30 16:12:00 +08:00
Set ( "state" , NodeIPAddressStateEnabled ) .
Update ( )
2021-08-18 09:24:18 +08:00
if err != nil {
return err
}
return this . NotifyUpdate ( tx , addressId )
2020-08-30 16:12:00 +08:00
}
2021-05-26 14:40:05 +08:00
// DisableAddress 禁用IP地址
2021-08-18 09:24:18 +08:00
func ( this * NodeIPAddressDAO ) DisableAddress ( tx * dbs . Tx , addressId int64 ) ( err error ) {
2021-01-01 23:31:30 +08:00
_ , err = this . Query ( tx ) .
2021-08-18 09:24:18 +08:00
Pk ( addressId ) .
2020-08-30 16:12:00 +08:00
Set ( "state" , NodeIPAddressStateDisabled ) .
Update ( )
2021-08-18 09:24:18 +08:00
if err != nil {
return err
}
return this . NotifyUpdate ( tx , addressId )
2020-08-30 16:12:00 +08:00
}
2021-05-26 14:40:05 +08:00
// DisableAllAddressesWithNodeId 禁用节点的所有的IP地址
func ( this * NodeIPAddressDAO ) DisableAllAddressesWithNodeId ( tx * dbs . Tx , nodeId int64 , role nodeconfigs . NodeRole ) error {
2020-08-30 16:12:00 +08:00
if nodeId <= 0 {
return errors . New ( "invalid nodeId" )
}
2021-05-26 14:40:05 +08:00
if len ( role ) == 0 {
role = nodeconfigs . NodeRoleNode
}
2021-01-01 23:31:30 +08:00
_ , err := this . Query ( tx ) .
2020-08-30 16:12:00 +08:00
Attr ( "nodeId" , nodeId ) .
2021-08-03 13:35:29 +08:00
Attr ( "role" , role ) .
2020-08-30 16:12:00 +08:00
Set ( "state" , NodeIPAddressStateDisabled ) .
Update ( )
2021-08-18 09:24:18 +08:00
if err != nil {
return err
}
return SharedNodeDAO . NotifyDNSUpdate ( tx , nodeId )
2020-08-30 16:12:00 +08:00
}
2021-05-26 14:40:05 +08:00
// FindEnabledAddress 查找启用中的IP地址
2021-01-01 23:31:30 +08:00
func ( this * NodeIPAddressDAO ) FindEnabledAddress ( tx * dbs . Tx , id int64 ) ( * NodeIPAddress , error ) {
result , err := this . Query ( tx ) .
2020-08-30 16:12:00 +08:00
Pk ( id ) .
Attr ( "state" , NodeIPAddressStateEnabled ) .
Find ( )
if result == nil {
return nil , err
}
return result . ( * NodeIPAddress ) , err
}
2021-05-26 14:40:05 +08:00
// FindAddressName 根据主键查找名称
2021-01-01 23:31:30 +08:00
func ( this * NodeIPAddressDAO ) FindAddressName ( tx * dbs . Tx , id int64 ) ( string , error ) {
return this . Query ( tx ) .
2020-08-30 16:12:00 +08:00
Pk ( id ) .
Result ( "name" ) .
FindStringCol ( "" )
}
2021-05-26 14:40:05 +08:00
// CreateAddress 创建IP地址
2021-09-12 20:21:42 +08:00
func ( this * NodeIPAddressDAO ) CreateAddress ( tx * dbs . Tx , adminId int64 , nodeId int64 , role nodeconfigs . NodeRole , name string , ip string , canAccess bool ) ( addressId int64 , err error ) {
2021-05-26 14:40:05 +08:00
if len ( role ) == 0 {
role = nodeconfigs . NodeRoleNode
}
2020-08-30 16:12:00 +08:00
op := NewNodeIPAddressOperator ( )
op . NodeId = nodeId
2021-05-26 14:40:05 +08:00
op . Role = role
2020-08-30 16:12:00 +08:00
op . Name = name
2020-10-17 21:15:31 +08:00
op . Ip = ip
op . CanAccess = canAccess
2021-08-18 16:19:16 +08:00
2020-08-30 16:12:00 +08:00
op . State = NodeIPAddressStateEnabled
2021-08-31 17:24:52 +08:00
addressId , err = this . SaveInt64 ( tx , op )
2020-08-30 16:12:00 +08:00
if err != nil {
return 0 , err
}
2021-01-27 23:00:02 +08:00
err = SharedNodeDAO . NotifyDNSUpdate ( tx , nodeId )
if err != nil {
return 0 , err
}
2021-08-31 17:24:52 +08:00
// 创建日志
err = SharedNodeIPAddressLogDAO . CreateLog ( tx , adminId , addressId , "创建IP" )
if err != nil {
return 0 , err
}
return addressId , nil
2020-08-30 16:12:00 +08:00
}
2021-05-26 14:40:05 +08:00
// UpdateAddress 修改IP地址
2021-09-12 20:21:42 +08:00
func ( this * NodeIPAddressDAO ) UpdateAddress ( tx * dbs . Tx , adminId int64 , addressId int64 , name string , ip string , canAccess bool , isOn bool ) ( err error ) {
2020-08-30 16:12:00 +08:00
if addressId <= 0 {
return errors . New ( "invalid addressId" )
}
op := NewNodeIPAddressOperator ( )
op . Id = addressId
op . Name = name
2020-10-17 21:15:31 +08:00
op . Ip = ip
op . CanAccess = canAccess
2021-08-18 09:24:18 +08:00
op . IsOn = isOn
2021-08-18 16:19:16 +08:00
2020-10-17 21:15:31 +08:00
op . State = NodeIPAddressStateEnabled // 恢复状态
2021-01-01 23:31:30 +08:00
err = this . Save ( tx , op )
2021-08-18 09:24:18 +08:00
if err != nil {
return err
}
2021-08-31 17:24:52 +08:00
// 创建日志
err = SharedNodeIPAddressLogDAO . CreateLog ( tx , adminId , addressId , "修改IP" )
if err != nil {
return err
}
2021-08-18 09:24:18 +08:00
return this . NotifyUpdate ( tx , addressId )
2020-08-30 16:12:00 +08:00
}
2021-05-26 14:40:05 +08:00
// UpdateAddressIP 修改IP地址中的IP
2021-01-01 23:31:30 +08:00
func ( this * NodeIPAddressDAO ) UpdateAddressIP ( tx * dbs . Tx , addressId int64 , ip string ) error {
2020-11-14 09:41:58 +08:00
if addressId <= 0 {
return errors . New ( "invalid addressId" )
}
op := NewNodeIPAddressOperator ( )
op . Id = addressId
op . Ip = ip
2021-01-01 23:31:30 +08:00
err := this . Save ( tx , op )
2021-08-18 09:24:18 +08:00
if err != nil {
return err
}
return this . NotifyUpdate ( tx , addressId )
2020-11-14 09:41:58 +08:00
}
2021-05-26 14:40:05 +08:00
// UpdateAddressNodeId 修改IP地址所属节点
2021-01-01 23:31:30 +08:00
func ( this * NodeIPAddressDAO ) UpdateAddressNodeId ( tx * dbs . Tx , addressId int64 , nodeId int64 ) error {
_ , err := this . Query ( tx ) .
2020-08-30 16:12:00 +08:00
Pk ( addressId ) .
Set ( "nodeId" , nodeId ) .
Set ( "state" , NodeIPAddressStateEnabled ) . // 恢复状态
Update ( )
2021-01-27 23:00:02 +08:00
if err != nil {
return err
}
err = SharedNodeDAO . NotifyDNSUpdate ( tx , nodeId )
if err != nil {
return err
}
return nil
2020-08-30 16:12:00 +08:00
}
2021-05-26 14:40:05 +08:00
// FindAllEnabledAddressesWithNode 查找节点的所有的IP地址
func ( this * NodeIPAddressDAO ) FindAllEnabledAddressesWithNode ( tx * dbs . Tx , nodeId int64 , role nodeconfigs . NodeRole ) ( result [ ] * NodeIPAddress , err error ) {
if len ( role ) == 0 {
role = nodeconfigs . NodeRoleNode
}
2021-01-01 23:31:30 +08:00
_ , err = this . Query ( tx ) .
2020-08-30 16:12:00 +08:00
Attr ( "nodeId" , nodeId ) .
2021-05-26 14:40:05 +08:00
Attr ( "role" , role ) .
2020-08-30 16:12:00 +08:00
State ( NodeIPAddressStateEnabled ) .
Desc ( "order" ) .
AscPk ( ) .
Slice ( & result ) .
FindAll ( )
return
}
2020-11-13 18:22:22 +08:00
2021-05-26 14:40:05 +08:00
// FindFirstNodeAccessIPAddress 查找节点的第一个可访问的IP地址
func ( this * NodeIPAddressDAO ) FindFirstNodeAccessIPAddress ( tx * dbs . Tx , nodeId int64 , role nodeconfigs . NodeRole ) ( string , error ) {
if len ( role ) == 0 {
role = nodeconfigs . NodeRoleNode
}
2021-01-01 23:31:30 +08:00
return this . Query ( tx ) .
2020-11-13 18:22:22 +08:00
Attr ( "nodeId" , nodeId ) .
2021-05-26 14:40:05 +08:00
Attr ( "role" , role ) .
2020-11-13 18:22:22 +08:00
State ( NodeIPAddressStateEnabled ) .
Attr ( "canAccess" , true ) .
Desc ( "order" ) .
AscPk ( ) .
Result ( "ip" ) .
FindStringCol ( "" )
}
2020-11-14 09:41:58 +08:00
2021-05-26 14:40:05 +08:00
// FindFirstNodeAccessIPAddressId 查找节点的第一个可访问的IP地址ID
func ( this * NodeIPAddressDAO ) FindFirstNodeAccessIPAddressId ( tx * dbs . Tx , nodeId int64 , role nodeconfigs . NodeRole ) ( int64 , error ) {
if len ( role ) == 0 {
role = nodeconfigs . NodeRoleNode
}
2021-01-01 23:31:30 +08:00
return this . Query ( tx ) .
2020-11-14 09:41:58 +08:00
Attr ( "nodeId" , nodeId ) .
2021-05-26 14:40:05 +08:00
Attr ( "role" , role ) .
2020-11-14 09:41:58 +08:00
State ( NodeIPAddressStateEnabled ) .
Attr ( "canAccess" , true ) .
Desc ( "order" ) .
AscPk ( ) .
Result ( "id" ) .
FindInt64Col ( 0 )
}
2021-01-27 23:00:02 +08:00
2021-08-18 09:24:18 +08:00
// FindNodeAccessAndUpIPAddresses 查找节点所有的可访问的IP地址
func ( this * NodeIPAddressDAO ) FindNodeAccessAndUpIPAddresses ( tx * dbs . Tx , nodeId int64 , role nodeconfigs . NodeRole ) ( result [ ] * NodeIPAddress , err error ) {
2021-05-26 14:40:05 +08:00
if len ( role ) == 0 {
role = nodeconfigs . NodeRoleNode
}
2021-01-27 23:00:02 +08:00
_ , err = this . Query ( tx ) .
2021-06-27 21:59:37 +08:00
Attr ( "role" , role ) .
2021-01-27 23:00:02 +08:00
Attr ( "nodeId" , nodeId ) .
State ( NodeIPAddressStateEnabled ) .
Attr ( "canAccess" , true ) .
2021-08-18 09:24:18 +08:00
Attr ( "isOn" , true ) .
Attr ( "isUp" , true ) .
2021-01-27 23:00:02 +08:00
Desc ( "order" ) .
AscPk ( ) .
Slice ( & result ) .
FindAll ( )
return
}
2021-08-18 09:24:18 +08:00
2021-08-31 17:24:52 +08:00
// CountAllEnabledIPAddresses 计算IP地址数量
// TODO 目前支持边缘节点, 将来支持NS节点
func ( this * NodeIPAddressDAO ) CountAllEnabledIPAddresses ( tx * dbs . Tx , role string , nodeClusterId int64 , upState configutils . BoolState , keyword string ) ( int64 , error ) {
var query = this . Query ( tx ) .
State ( NodeIPAddressStateEnabled ) .
Attr ( "role" , role )
// 集群
if nodeClusterId > 0 {
query . Where ( "nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE (clusterId=:clusterId OR JSON_CONTAINS(secondaryClusterIds, :clusterIdString)) AND state=1)" ) .
Param ( "clusterId" , nodeClusterId ) .
Param ( "clusterIdString" , types . String ( nodeClusterId ) )
} else {
query . Where ( "nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE state=1 AND clusterId IN (SELECT id FROM " + SharedNodeClusterDAO . Table + " WHERE state=1))" )
}
// 在线状态
switch upState {
case configutils . BoolStateYes :
query . Attr ( "isUp" , 1 )
case configutils . BoolStateNo :
query . Attr ( "isUp" , 0 )
}
// 关键词
if len ( keyword ) > 0 {
query . Where ( "(ip LIKE :keyword OR name LIKE :keyword OR description LIKE :keyword OR nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE state=1 AND name LIKE :keyword))" ) .
Param ( "keyword" , "%" + keyword + "%" )
}
return query . Count ( )
}
// ListEnabledIPAddresses 列出单页的IP地址
func ( this * NodeIPAddressDAO ) ListEnabledIPAddresses ( tx * dbs . Tx , role string , nodeClusterId int64 , upState configutils . BoolState , keyword string , offset int64 , size int64 ) ( result [ ] * NodeIPAddress , err error ) {
var query = this . Query ( tx ) .
State ( NodeIPAddressStateEnabled ) .
Attr ( "role" , role )
// 集群
if nodeClusterId > 0 {
query . Where ( "nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE (clusterId=:clusterId OR JSON_CONTAINS(secondaryClusterIds, :clusterIdString)) AND state=1)" ) .
Param ( "clusterId" , nodeClusterId ) .
Param ( "clusterIdString" , types . String ( nodeClusterId ) )
} else {
query . Where ( "nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE state=1 AND clusterId IN (SELECT id FROM " + SharedNodeClusterDAO . Table + " WHERE state=1))" )
}
// 在线状态
switch upState {
case configutils . BoolStateYes :
query . Attr ( "isUp" , 1 )
case configutils . BoolStateNo :
query . Attr ( "isUp" , 0 )
}
// 关键词
if len ( keyword ) > 0 {
query . Where ( "(ip LIKE :keyword OR name LIKE :keyword OR description LIKE :keyword OR nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE state=1 AND name LIKE :keyword))" ) .
Param ( "keyword" , "%" + keyword + "%" )
}
_ , err = query . Offset ( offset ) .
Limit ( size ) .
Asc ( "isUp" ) .
Desc ( "nodeId" ) .
Slice ( & result ) .
FindAll ( )
return
}
2021-09-08 19:34:31 +08:00
// FindAllAccessibleIPAddressesWithClusterId 列出所有的正在启用的IP地址
func ( this * NodeIPAddressDAO ) FindAllAccessibleIPAddressesWithClusterId ( tx * dbs . Tx , role string , clusterId int64 ) ( result [ ] * NodeIPAddress , err error ) {
2021-09-06 08:12:48 +08:00
_ , err = this . Query ( tx ) .
State ( NodeIPAddressStateEnabled ) .
Attr ( "role" , role ) .
Attr ( "isOn" , true ) .
2021-09-08 19:34:31 +08:00
Attr ( "canAccess" , true ) .
2021-09-06 08:12:48 +08:00
Where ( "nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE state=1 AND clusterId=:clusterId)" ) .
Param ( "clusterId" , clusterId ) .
Slice ( & result ) .
FindAll ( )
return
}
2021-09-08 19:34:31 +08:00
// CountAllAccessibleIPAddressesWithClusterId 计算集群中的可用IP地址数量
func ( this * NodeIPAddressDAO ) CountAllAccessibleIPAddressesWithClusterId ( tx * dbs . Tx , role string , clusterId int64 ) ( count int64 , err error ) {
return this . Query ( tx ) .
State ( NodeIPAddressStateEnabled ) .
Attr ( "role" , role ) .
Attr ( "isOn" , true ) .
Attr ( "canAccess" , true ) .
Where ( "nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE state=1 AND clusterId=:clusterId)" ) .
Param ( "clusterId" , clusterId ) .
Count ( )
}
// ListAccessibleIPAddressesWithClusterId 列出单页集群中的可用IP地址
func ( this * NodeIPAddressDAO ) ListAccessibleIPAddressesWithClusterId ( tx * dbs . Tx , role string , clusterId int64 , offset int64 , size int64 ) ( result [ ] * NodeIPAddress , err error ) {
_ , err = this . Query ( tx ) .
State ( NodeIPAddressStateEnabled ) .
Attr ( "role" , role ) .
Attr ( "isOn" , true ) .
Attr ( "canAccess" , true ) .
Where ( "nodeId IN (SELECT id FROM " + SharedNodeDAO . Table + " WHERE state=1 AND clusterId=:clusterId)" ) .
Param ( "clusterId" , clusterId ) .
Offset ( offset ) .
Limit ( size ) .
Slice ( & result ) .
FindAll ( )
return
}
// UpdateAddressConnectivity 设置连通性数据
func ( this * NodeIPAddressDAO ) UpdateAddressConnectivity ( tx * dbs . Tx , addrId int64 , connectivity * nodeconfigs . Connectivity ) error {
connectivityJSON , err := json . Marshal ( connectivity )
if err != nil {
return err
}
return this . Query ( tx ) .
Pk ( addrId ) .
Set ( "connectivity" , connectivityJSON ) .
UpdateQuickly ( )
}
2021-09-12 20:21:42 +08:00
// UpdateAddressIsUp 设置IP地址在线状态
func ( this * NodeIPAddressDAO ) UpdateAddressIsUp ( tx * dbs . Tx , addressId int64 , isUp bool ) error {
var err = this . Query ( tx ) .
Pk ( addressId ) .
Set ( "isUp" , isUp ) .
UpdateQuickly ( )
if err != nil {
return err
}
return this . NotifyUpdate ( tx , addressId )
}
2021-08-18 09:24:18 +08:00
// NotifyUpdate 通知更新
func ( this * NodeIPAddressDAO ) NotifyUpdate ( tx * dbs . Tx , addressId int64 ) error {
address , err := this . Query ( tx ) .
Pk ( addressId ) .
Result ( "nodeId" , "role" ) .
Find ( )
if err != nil {
return err
}
if address == nil {
return nil
}
var nodeId = int64 ( address . ( * NodeIPAddress ) . NodeId )
if nodeId == 0 {
return nil
}
var role = address . ( * NodeIPAddress ) . Role
switch role {
case nodeconfigs . NodeRoleNode :
err = dns . SharedDNSTaskDAO . CreateNodeTask ( tx , nodeId , dns . DNSTaskTypeNodeChange )
}
if err != nil {
return err
}
return nil
}