[ADD] add api for deleting http rule

This commit is contained in:
GLYASAI 2018-11-21 17:57:48 +08:00
parent c6d36e958f
commit 8d7d6e359a
5 changed files with 74 additions and 8 deletions

View File

@ -43,7 +43,7 @@ func (g *GatewayStruct) HttpRule(w http.ResponseWriter, r *http.Request) {
g.addHttpRule(w, r)
case "PUT":
g.updateHttpRule(w, r)
case "Delete":
case "DELETE":
g.deleteHttpRule(w, r)
}
}
@ -145,7 +145,7 @@ func (g *GatewayStruct) updateHttpRule(w http.ResponseWriter, r *http.Request) {
// end transaction
if err := tx.Commit().Error; err != nil {
tx.Rollback()
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while commit transaction: %v", err))
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while committing transaction: %v", err))
return
}
@ -153,5 +153,23 @@ func (g *GatewayStruct) updateHttpRule(w http.ResponseWriter, r *http.Request) {
}
func (g *GatewayStruct) deleteHttpRule(w http.ResponseWriter, r *http.Request) {
logrus.Debugf("delete http rule.")
var req api_model.HttpRuleStruct
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
if !ok {
return
}
reqJson, _ := json.Marshal(req)
logrus.Debugf("Request is : %s", string(reqJson))
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
h := handler.GetGatewayHandler()
err := h.DeleteHttpRule(&req, serviceID)
if err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while delete http rule: %v", err))
return
}
httputil.ReturnSuccess(r, w, "success")
}

View File

@ -70,6 +70,38 @@ func (g *GatewayAction) UpdateHttpRule(req *apimodel.HttpRuleStruct, serviceID s
return rule, g.dbmanager.HttpRuleDaoTransactions(tx).UpdateModel(rule)
}
// DeleteHttpRule deletes http rule, including certificate and rule extensions
func (g *GatewayAction) DeleteHttpRule(req *apimodel.HttpRuleStruct, serviceID string) error {
// begin transaction
tx := db.GetManager().Begin()
// delete http rule
httpRule, err := g.dbmanager.HttpRuleDaoTransactions(tx).DeleteHttpRuleByServiceIDAndContainerPort(serviceID,
req.ContainerPort)
if err != nil {
tx.Rollback()
return err
}
// delete certificate
if err := g.dbmanager.CertificateDaoTransactions(tx).DeleteCertificateByID(httpRule.CertificateID); err != nil {
tx.Rollback()
return err
}
// delete rule extension
if err := g.dbmanager.RuleExtensionDaoTransactions(tx).DeleteRuleExtensionByRuleID(httpRule.UUID); err != nil {
tx.Rollback()
return err
}
// end transaction
if err := tx.Commit().Error; err != nil {
return err
}
return nil
}
// AddCertificate adds certificate to db if it doesn't exists
func (g *GatewayAction) AddCertificate(req *apimodel.HttpRuleStruct, tx *gorm.DB) error {
cert := &model.Certificate{

View File

@ -27,6 +27,7 @@ import (
type GatewayHandler interface {
AddHttpRule(httpRule *dbmodel.HttpRule, tx *gorm.DB) error
UpdateHttpRule(req *apimodel.HttpRuleStruct, serviceID string, tx *gorm.DB) (*dbmodel.HttpRule, error)
DeleteHttpRule(req *apimodel.HttpRuleStruct, serviceID string) error
AddCertificate(req *apimodel.HttpRuleStruct, tx *gorm.DB) error
UpdateCertificate(req apimodel.HttpRuleStruct, httpRule *dbmodel.HttpRule, tx *gorm.DB) error
AddRuleExtensions(ruleID string, ruleExtensions []*apimodel.RuleExtensionStruct, tx *gorm.DB) error

View File

@ -423,6 +423,7 @@ type RuleExtensionDao interface {
type HttpRuleDao interface {
Dao
GetHttpRuleByServiceIDAndContainerPort(serviceID string, containerPort int) (*model.HttpRule, error)
DeleteHttpRuleByServiceIDAndContainerPort(serviceID string, containerPort int) (*model.HttpRule, error)
}
// TcpRuleDao -

View File

@ -163,6 +163,20 @@ func (h *HttpRuleDaoImpl) GetHttpRuleByServiceIDAndContainerPort(serviceID strin
return httpRule, nil
}
func (h *HttpRuleDaoImpl) DeleteHttpRuleByServiceIDAndContainerPort(serviceID string,
containerPort int) (*model.HttpRule, error) {
httpRule, err := h.GetHttpRuleByServiceIDAndContainerPort(serviceID, containerPort)
if err != nil {
return nil, err
}
if err := h.DB.Where("service_id = ? and container_port = ?", serviceID,
containerPort).Delete(httpRule).Error; err != nil {
return nil, err
}
return httpRule, nil
}
type TcpRuleDaoTmpl struct {
DB *gorm.DB
}