sync rule extensions

This commit is contained in:
GLYASAI 2021-07-30 16:17:33 +08:00
parent 501b588445
commit 4ee348bb22
3 changed files with 45 additions and 5 deletions

View File

@ -885,8 +885,9 @@ func (g *GatewayAction) listHTTPRuleIDs(componentID string, port int) ([]string,
// SyncHTTPRules - // SyncHTTPRules -
func (g *GatewayAction) SyncHTTPRules(tx *gorm.DB, components []*apimodel.Component) error { func (g *GatewayAction) SyncHTTPRules(tx *gorm.DB, components []*apimodel.Component) error {
var ( var (
componentIDs []string componentIDs []string
httpRules []*model.HTTPRule httpRules []*model.HTTPRule
ruleExtensions []*model.RuleExtension
) )
for _, component := range components { for _, component := range components {
if component.HTTPRules == nil { if component.HTTPRules == nil {
@ -895,14 +896,40 @@ func (g *GatewayAction) SyncHTTPRules(tx *gorm.DB, components []*apimodel.Compon
componentIDs = append(componentIDs, component.ComponentBase.ComponentID) componentIDs = append(componentIDs, component.ComponentBase.ComponentID)
for _, httpRule := range component.HTTPRules { for _, httpRule := range component.HTTPRules {
httpRules = append(httpRules, httpRule.DbModel(component.ComponentBase.ComponentID)) httpRules = append(httpRules, httpRule.DbModel(component.ComponentBase.ComponentID))
for _, ext := range httpRule.RuleExtensions {
ruleExtensions = append(ruleExtensions, &model.RuleExtension{
UUID: util.NewUUID(),
RuleID: httpRule.HTTPRuleID,
Key: ext.Key,
Value: ext.Value,
})
}
} }
} }
if err := g.syncRuleExtensions(tx, httpRules, ruleExtensions); err != nil {
return err
}
if err := db.GetManager().HTTPRuleDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil { if err := db.GetManager().HTTPRuleDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err return err
} }
return db.GetManager().HTTPRuleDaoTransactions(tx).CreateOrUpdateHTTPRuleInBatch(httpRules) return db.GetManager().HTTPRuleDaoTransactions(tx).CreateOrUpdateHTTPRuleInBatch(httpRules)
} }
func (g *GatewayAction) syncRuleExtensions(tx *gorm.DB, httpRules []*model.HTTPRule, exts []*model.RuleExtension) error {
var ruleIDs []string
for _, hr := range httpRules {
ruleIDs = append(ruleIDs, hr.UUID)
}
if err := db.GetManager().RuleExtensionDaoTransactions(tx).DeleteByRuleIDs(ruleIDs); err != nil {
return err
}
return db.GetManager().RuleExtensionDaoTransactions(tx).CreateOrUpdateRuleExtensionsInBatch(exts)
}
// SyncTCPRules - // SyncTCPRules -
func (g *GatewayAction) SyncTCPRules(tx *gorm.DB, components []*apimodel.Component) error { func (g *GatewayAction) SyncTCPRules(tx *gorm.DB, components []*apimodel.Component) error {
var ( var (

View File

@ -505,6 +505,7 @@ type RuleExtensionDao interface {
GetRuleExtensionByRuleID(ruleID string) ([]*model.RuleExtension, error) GetRuleExtensionByRuleID(ruleID string) ([]*model.RuleExtension, error)
DeleteRuleExtensionByRuleID(ruleID string) error DeleteRuleExtensionByRuleID(ruleID string) error
DeleteByRuleIDs(ruleIDs []string) error DeleteByRuleIDs(ruleIDs []string) error
CreateOrUpdateRuleExtensionsInBatch(exts []*model.RuleExtension) error
} }
// HTTPRuleDao - // HTTPRuleDao -

View File

@ -20,9 +20,9 @@ package dao
import ( import (
"fmt" "fmt"
gormbulkups "github.com/atcdot/gorm-bulk-upsert"
"reflect" "reflect"
gormbulkups "github.com/atcdot/gorm-bulk-upsert"
"github.com/goodrain/rainbond/api/util/bcode" "github.com/goodrain/rainbond/api/util/bcode"
"github.com/goodrain/rainbond/db/model" "github.com/goodrain/rainbond/db/model"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
@ -149,6 +149,18 @@ func (c *RuleExtensionDaoImpl) DeleteByRuleIDs(ruleIDs []string) error {
return nil return nil
} }
// RuleExtensionDaoImpl creates or updates rule extensions in batch.
func (c *RuleExtensionDaoImpl) CreateOrUpdateRuleExtensionsInBatch(exts []*model.RuleExtension) error {
var objects []interface{}
for _, ext := range exts {
objects = append(objects, *ext)
}
if err := gormbulkups.BulkUpsert(c.DB, objects, 2000); err != nil {
return errors.Wrap(err, "create or update rule extensions in batch")
}
return nil
}
//HTTPRuleDaoImpl http rule //HTTPRuleDaoImpl http rule
type HTTPRuleDaoImpl struct { type HTTPRuleDaoImpl struct {
DB *gorm.DB DB *gorm.DB
@ -276,7 +288,7 @@ func (h *HTTPRuleDaoImpl) ListByCertID(certID string) ([]*model.HTTPRule, error)
} }
//DeleteByComponentIDs delete http rule by component ids //DeleteByComponentIDs delete http rule by component ids
func (h *HTTPRuleDaoImpl) DeleteByComponentIDs(componentIDs []string) error{ func (h *HTTPRuleDaoImpl) DeleteByComponentIDs(componentIDs []string) error {
return h.DB.Where("service_id in (?) ", componentIDs).Delete(&model.HTTPRule{}).Error return h.DB.Where("service_id in (?) ", componentIDs).Delete(&model.HTTPRule{}).Error
} }
@ -407,7 +419,7 @@ func (t *TCPRuleDaoTmpl) ListByServiceID(serviceID string) ([]*model.TCPRule, er
} }
//DeleteByComponentIDs delete tcp rule by component ids //DeleteByComponentIDs delete tcp rule by component ids
func (t *TCPRuleDaoTmpl) DeleteByComponentIDs(componentIDs []string) error{ func (t *TCPRuleDaoTmpl) DeleteByComponentIDs(componentIDs []string) error {
return t.DB.Where("service_id in (?) ", componentIDs).Delete(&model.TCPRule{}).Error return t.DB.Where("service_id in (?) ", componentIDs).Delete(&model.TCPRule{}).Error
} }