[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)
}
}
@ -124,14 +124,14 @@ func (g *GatewayStruct) updateHttpRule(w http.ResponseWriter, r *http.Request) {
httpRule, err := h.UpdateHttpRule(&req, serviceID, tx)
if err != nil {
tx.Rollback()
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while " +
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while "+
"updating http rule: %v", err))
return
}
if err := h.AddCertificate(&req, tx); err != nil {
tx.Rollback()
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while " +
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while "+
"updating certificate: %v", err))
return
}
@ -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,13 +70,45 @@ 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{
UUID: req.CertificateID,
UUID: req.CertificateID,
CertificateName: req.CertificateName,
Certificate: req.Certificate,
PrivateKey: req.PrivateKey,
Certificate: req.Certificate,
PrivateKey: req.PrivateKey,
}
return g.dbmanager.CertificateDaoTransactions(tx).AddModel(cert)

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

@ -88,7 +88,7 @@ func (c *RuleExtensionDaoImpl) AddModel(mo model.Interface) error {
}
var old model.RuleExtension
if ok := c.DB.Where("rule_id = ? and value = ?", re.RuleID, re.Value).Find(&old).RecordNotFound(); ok {
return c.DB.Create(re).Error
return c.DB.Create(re).Error
} else {
return fmt.Errorf("RuleExtension already exists based on RuleID(%s) and Value(%s)",
re.RuleID, re.Value)
@ -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
}