Files
EdgeAPI/internal/db/models/node_cluster_firewall_action_dao.go

196 lines
5.3 KiB
Go
Raw Permalink Normal View History

2021-02-06 17:38:04 +08:00
package models
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
2021-11-11 14:16:42 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/utils"
2021-02-06 17:38:04 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
2021-11-11 14:16:42 +08:00
"github.com/iwind/TeaGo/types"
2021-02-06 17:38:04 +08:00
)
const (
NodeClusterFirewallActionStateEnabled = 1 // 已启用
NodeClusterFirewallActionStateDisabled = 0 // 已禁用
)
type NodeClusterFirewallActionDAO dbs.DAO
func NewNodeClusterFirewallActionDAO() *NodeClusterFirewallActionDAO {
return dbs.NewDAO(&NodeClusterFirewallActionDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeNodeClusterFirewallActions",
Model: new(NodeClusterFirewallAction),
PkName: "id",
},
}).(*NodeClusterFirewallActionDAO)
}
var SharedNodeClusterFirewallActionDAO *NodeClusterFirewallActionDAO
func init() {
dbs.OnReady(func() {
SharedNodeClusterFirewallActionDAO = NewNodeClusterFirewallActionDAO()
})
}
2021-11-11 14:16:42 +08:00
// EnableFirewallAction 启用条目
2021-02-06 17:38:04 +08:00
func (this *NodeClusterFirewallActionDAO) EnableFirewallAction(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx).
Pk(id).
Set("state", NodeClusterFirewallActionStateEnabled).
Update()
return err
}
2021-11-11 14:16:42 +08:00
// DisableFirewallAction 禁用条目
2021-02-06 17:38:04 +08:00
func (this *NodeClusterFirewallActionDAO) DisableFirewallAction(tx *dbs.Tx, actionId int64) error {
_, err := this.Query(tx).
Pk(actionId).
Set("state", NodeClusterFirewallActionStateDisabled).
Update()
if err != nil {
return err
}
return this.NotifyUpdate(tx, actionId)
}
2021-11-11 14:16:42 +08:00
// FindEnabledFirewallAction 查找启用中的条目
2021-02-06 17:38:04 +08:00
func (this *NodeClusterFirewallActionDAO) FindEnabledFirewallAction(tx *dbs.Tx, actionId int64) (*NodeClusterFirewallAction, error) {
result, err := this.Query(tx).
Pk(actionId).
Attr("state", NodeClusterFirewallActionStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*NodeClusterFirewallAction), err
}
2021-11-11 14:16:42 +08:00
// FindFirewallActionName 根据主键查找名称
2021-02-06 17:38:04 +08:00
func (this *NodeClusterFirewallActionDAO) FindFirewallActionName(tx *dbs.Tx, id uint32) (string, error) {
return this.Query(tx).
Pk(id).
Result("name").
FindStringCol("")
}
2021-11-11 14:16:42 +08:00
// CreateFirewallAction 创建动作
2021-02-06 17:38:04 +08:00
func (this *NodeClusterFirewallActionDAO) CreateFirewallAction(tx *dbs.Tx, adminId int64, clusterId int64, name string, eventLevel, actionType firewallconfigs.FirewallActionType, params maps.Map) (int64, error) {
if params == nil {
params = maps.Map{}
}
var op = NewNodeClusterFirewallActionOperator()
2021-02-06 17:38:04 +08:00
op.AdminId = adminId
op.ClusterId = clusterId
op.Name = name
op.EventLevel = eventLevel
op.Type = actionType
op.Params = params.AsJSON()
op.State = NodeClusterFirewallActionStateEnabled
actionId, err := this.SaveInt64(tx, op)
if err != nil {
return 0, err
}
err = this.NotifyUpdate(tx, actionId)
if err != nil {
return 0, err
}
return actionId, nil
}
2021-11-11 14:16:42 +08:00
// UpdateFirewallAction 修改动作
2021-02-06 17:38:04 +08:00
func (this *NodeClusterFirewallActionDAO) UpdateFirewallAction(tx *dbs.Tx, actionId int64, name string, eventLevel string, actionType firewallconfigs.FirewallActionType, params maps.Map) error {
if actionId <= 0 {
return errors.New("invalid actionId")
}
if params == nil {
params = maps.Map{}
}
var op = NewNodeClusterFirewallActionOperator()
2021-02-06 17:38:04 +08:00
op.Id = actionId
op.Name = name
op.EventLevel = eventLevel
op.Type = actionType
op.Params = params.AsJSON()
_, err := this.SaveInt64(tx, op)
if err != nil {
return err
}
return this.NotifyUpdate(tx, actionId)
}
2021-11-11 14:16:42 +08:00
// FindAllEnabledFirewallActions 查找所有集群的动作
func (this *NodeClusterFirewallActionDAO) FindAllEnabledFirewallActions(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (result []*NodeClusterFirewallAction, err error) {
var cacheKey = this.Table + ":FindAllEnabledFirewallActions:" + types.String(clusterId)
if cacheMap != nil {
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.([]*NodeClusterFirewallAction), nil
}
}
2021-02-06 17:38:04 +08:00
_, err = this.Query(tx).
Attr("clusterId", clusterId).
State(NodeClusterFirewallActionStateEnabled).
Slice(&result).
FindAll()
2021-11-11 14:16:42 +08:00
if err != nil {
return nil, err
}
if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
2021-02-06 17:38:04 +08:00
return
}
2021-11-11 14:16:42 +08:00
// ComposeFirewallActionConfig 组合配置
2021-02-06 17:38:04 +08:00
func (this *NodeClusterFirewallActionDAO) ComposeFirewallActionConfig(tx *dbs.Tx, action *NodeClusterFirewallAction) (*firewallconfigs.FirewallActionConfig, error) {
if action == nil {
return nil, nil
}
config := &firewallconfigs.FirewallActionConfig{}
config.Id = int64(action.Id)
config.Type = action.Type
config.EventLevel = action.EventLevel
params, err := action.DecodeParams()
if err != nil {
return nil, err
}
config.Params = params
return config, nil
}
2021-11-11 14:16:42 +08:00
// CountAllEnabledFirewallActions 计算动作数量
2021-02-24 15:02:49 +08:00
func (this *NodeClusterFirewallActionDAO) CountAllEnabledFirewallActions(tx *dbs.Tx, clusterId int64) (int64, error) {
return this.Query(tx).
State(NodeClusterFirewallActionStateEnabled).
Attr("clusterId", clusterId).
Count()
}
2021-11-11 14:16:42 +08:00
// NotifyUpdate 通知更新
2021-02-06 17:38:04 +08:00
func (this *NodeClusterFirewallActionDAO) NotifyUpdate(tx *dbs.Tx, actionId int64) error {
clusterId, err := this.Query(tx).
Pk(actionId).
Result("clusterId").
FindInt64Col(0)
if err != nil {
return err
}
if clusterId > 0 {
return SharedNodeClusterDAO.NotifyUpdate(tx, clusterId)
}
return nil
}