mirror of
https://gitee.com/rainbond/Rainbond.git
synced 2024-12-01 11:18:08 +08:00
[ADD] add api for updating tcp rule
This commit is contained in:
parent
1e4e5b0f37
commit
aa98f5fd55
@ -201,7 +201,23 @@ func (g *GatewayStruct) addTcpRule(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (g *GatewayStruct) updateTcpRule(w http.ResponseWriter, r *http.Request) {
|
||||
logrus.Debugf("add tcp rule.")
|
||||
var req api_model.TcpRuleStruct
|
||||
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
reqJson, _ := json.Marshal(req)
|
||||
logrus.Debugf("Request is : %s", string(reqJson))
|
||||
|
||||
h := handler.GetGatewayHandler()
|
||||
if err := h.UpdateTcpRule(&req); err != nil {
|
||||
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while " +
|
||||
"updating tcp rule: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
httputil.ReturnSuccess(r, w, "success")
|
||||
}
|
||||
|
||||
func (g *GatewayStruct) deleteTcpRule(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -51,7 +51,7 @@ func (g *GatewayAction) UpdateHttpRule(req *apimodel.HttpRuleStruct,
|
||||
return nil, err
|
||||
}
|
||||
if rule == nil {
|
||||
return nil, fmt.Errorf("HttpRule dosen't exist based on ServiceID(%s) " +
|
||||
return nil, fmt.Errorf("HttpRule dosen't exist based on ServiceID(%s) "+
|
||||
"and ContainerPort(%v)", req.ServiceID, req.ContainerPort)
|
||||
}
|
||||
// delete old Certificate
|
||||
@ -149,6 +149,7 @@ func (g *GatewayAction) AddTcpRule(req *apimodel.TcpRuleStruct) error {
|
||||
tx := db.GetManager().Begin()
|
||||
// add tcp rule
|
||||
if err := g.dbmanager.TcpRuleDaoTransactions(tx).AddModel(tcpRule); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
@ -160,6 +161,7 @@ func (g *GatewayAction) AddTcpRule(req *apimodel.TcpRuleStruct) error {
|
||||
Value: ruleExtension.Value,
|
||||
}
|
||||
if err := g.dbmanager.RuleExtensionDaoTransactions(tx).AddModel(re); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -172,6 +174,51 @@ func (g *GatewayAction) AddTcpRule(req *apimodel.TcpRuleStruct) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *GatewayAction) UpdateTcpRule(req *apimodel.TcpRuleStruct) error {
|
||||
// begin transaction
|
||||
tx := db.GetManager().Begin()
|
||||
// get old tcp rule
|
||||
tcpRule, err := g.dbmanager.TcpRuleDaoTransactions(tx).GetTcpRuleByServiceIDAndContainerPort(req.ServiceID,
|
||||
req.ContainerPort)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
// delete old rule extensions
|
||||
if err := g.dbmanager.RuleExtensionDaoTransactions(tx).DeleteRuleExtensionByRuleID(tcpRule.UUID); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
// update tcp rule
|
||||
tcpRule.IP = req.IP
|
||||
tcpRule.Port = req.Port
|
||||
tcpRule.LoadBalancerType = req.LoadBalancerType
|
||||
if err := g.dbmanager.TcpRuleDaoTransactions(tx).UpdateModel(tcpRule); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
// add new rule extensions
|
||||
for _, ruleExtension := range req.RuleExtensions {
|
||||
re := &model.RuleExtension{
|
||||
UUID: util.NewUUID(),
|
||||
RuleID: tcpRule.UUID,
|
||||
Value: ruleExtension.Value,
|
||||
}
|
||||
if err := g.dbmanager.RuleExtensionDaoTransactions(tx).AddModel(re); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// end transaction
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddRuleExtensions adds rule extensions to db if any of they doesn't exists
|
||||
func (g *GatewayAction) AddRuleExtensions(ruleID string, ruleExtensions []*apimodel.RuleExtensionStruct,
|
||||
tx *gorm.DB) error {
|
||||
|
@ -33,6 +33,7 @@ type GatewayHandler interface {
|
||||
UpdateCertificate(req apimodel.HttpRuleStruct, httpRule *dbmodel.HttpRule, tx *gorm.DB) error
|
||||
|
||||
AddTcpRule(req *apimodel.TcpRuleStruct) error
|
||||
UpdateTcpRule(req *apimodel.TcpRuleStruct) error
|
||||
|
||||
AddRuleExtensions(ruleID string, ruleExtensions []*apimodel.RuleExtensionStruct, tx *gorm.DB) error
|
||||
}
|
||||
|
@ -196,8 +196,15 @@ func (t *TcpRuleDaoTmpl) AddModel(mo model.Interface) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *TcpRuleDaoTmpl) UpdateModel(model.Interface) error {
|
||||
return nil
|
||||
func (t *TcpRuleDaoTmpl) UpdateModel(mo model.Interface) error {
|
||||
tr, ok := mo.(*model.TcpRule)
|
||||
if !ok {
|
||||
return fmt.Errorf("Failed to convert %s to *model.TcpRule", reflect.TypeOf(mo).String())
|
||||
}
|
||||
|
||||
return t.DB.Table(tr.TableName()).
|
||||
Where("uuid = ?", tr.UUID).
|
||||
Update(tr).Error
|
||||
}
|
||||
|
||||
// GetTcpRuleByServiceIDAndContainerPort gets a TcpRule based on serviceID and containerPort
|
||||
|
Loading…
Reference in New Issue
Block a user