diff --git a/api/controller/gateway.go b/api/controller/gateway.go index bb08884f1..4249fb3ea 100644 --- a/api/controller/gateway.go +++ b/api/controller/gateway.go @@ -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") } diff --git a/api/handler/gateway.go b/api/handler/gateway.go index 4d796e90d..ca027e9fb 100644 --- a/api/handler/gateway.go +++ b/api/handler/gateway.go @@ -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) diff --git a/api/handler/gateway_handler.go b/api/handler/gateway_handler.go index cbbb7bce9..0fab1de1e 100644 --- a/api/handler/gateway_handler.go +++ b/api/handler/gateway_handler.go @@ -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 diff --git a/db/dao/dao.go b/db/dao/dao.go index 5cc313013..cce6e7a39 100644 --- a/db/dao/dao.go +++ b/db/dao/dao.go @@ -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 - diff --git a/db/mysql/dao/gateway.go b/db/mysql/dao/gateway.go index 722daa4df..1b36550f8 100644 --- a/db/mysql/dao/gateway.go +++ b/db/mysql/dao/gateway.go @@ -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 }