[REV] change router of "http-rule"

This commit is contained in:
GLYASAI 2018-11-21 18:16:41 +08:00
parent 8d7d6e359a
commit f2c5b22c2d
5 changed files with 21 additions and 30 deletions

View File

@ -105,6 +105,12 @@ func (v2 *V2) tenantNameRouter() chi.Router {
//团队资源限制
r.Post("/limit_memory", controller.GetManager().LimitTenantMemory)
r.Get("/limit_memory", controller.GetManager().TenantResourcesStatus)
// Gateway
r.Post("/http-rule", controller.GetManager().HttpRule)
r.Delete("/http-rule", controller.GetManager().HttpRule)
r.Put("/http-rule", controller.GetManager().HttpRule)
return r
}
@ -196,24 +202,12 @@ func (v2 *V2) serviceRouter() chi.Router {
r.Post("/node-label", controller.GetManager().NodeLabel)
r.Delete("/node-label", controller.GetManager().NodeLabel)
//获取租户所有域名
r.Get("/get-domains", controller.GetManager().Entrance)
//租户域名 增加 删除(sources)
r.Post("/domains", controller.GetManager().Entrance)
r.Delete("/domains/{domain_name}", controller.GetManager().Entrance)
//插件
r.Mount("/plugin", v2.serviceRelatePluginRouter())
//rule
r.Mount("/net-rule", v2.rulesRouter())
// Gateway
r.Post("/http-rule", controller.GetManager().HttpRule)
r.Delete("/http-rule", controller.GetManager().HttpRule)
r.Put("/http-rule", controller.GetManager().HttpRule)
return r
}

View File

@ -23,7 +23,6 @@ import (
"fmt"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/api/handler"
"github.com/goodrain/rainbond/api/middleware"
api_model "github.com/goodrain/rainbond/api/model"
"github.com/goodrain/rainbond/db"
"github.com/goodrain/rainbond/db/model"
@ -58,12 +57,10 @@ func (g *GatewayStruct) addHttpRule(w http.ResponseWriter, r *http.Request) {
reqJson, _ := json.Marshal(req)
logrus.Debugf("Request is : %s", string(reqJson))
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
// TODO: shouldn't write the business logic here
httpRule := &model.HttpRule{
UUID: util.NewUUID(),
ServiceID: serviceID,
ServiceID: req.ServiceID,
ContainerPort: req.ContainerPort,
Domain: req.Domain,
Path: req.Path,
@ -115,13 +112,11 @@ func (g *GatewayStruct) updateHttpRule(w http.ResponseWriter, r *http.Request) {
reqJson, _ := json.Marshal(req)
logrus.Debugf("Request is : %s", string(reqJson))
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
// TODO: shouldn't write the business logic here
// begin transaction
tx := db.GetManager().Begin()
h := handler.GetGatewayHandler()
httpRule, err := h.UpdateHttpRule(&req, serviceID, tx)
httpRule, err := h.UpdateHttpRule(&req, tx)
if err != nil {
tx.Rollback()
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while "+
@ -162,10 +157,8 @@ func (g *GatewayStruct) deleteHttpRule(w http.ResponseWriter, r *http.Request) {
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)
err := h.DeleteHttpRule(&req)
if err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while delete http rule: %v", err))
return

View File

@ -43,13 +43,16 @@ func (g *GatewayAction) AddHttpRule(httpRule *model.HttpRule, tx *gorm.DB) error
return g.dbmanager.HttpRuleDaoTransactions(tx).AddModel(httpRule)
}
func (g *GatewayAction) UpdateHttpRule(req *apimodel.HttpRuleStruct, serviceID string, tx *gorm.DB) (httpRule *model.HttpRule, err error) {
rule, err := g.dbmanager.HttpRuleDaoTransactions(tx).GetHttpRuleByServiceIDAndContainerPort(serviceID, req.ContainerPort)
func (g *GatewayAction) UpdateHttpRule(req *apimodel.HttpRuleStruct,
tx *gorm.DB) (httpRule *model.HttpRule, err error) {
rule, err := g.dbmanager.HttpRuleDaoTransactions(tx).GetHttpRuleByServiceIDAndContainerPort(req.ServiceID,
req.ContainerPort)
if err != nil {
return nil, err
}
if rule == nil {
return nil, fmt.Errorf("HttpRule dosen't exist based on ServiceID(%s) and ContainerPort(%v)", serviceID, req.ContainerPort)
return nil, fmt.Errorf("HttpRule dosen't exist based on ServiceID(%s) " +
"and ContainerPort(%v)", req.ServiceID, req.ContainerPort)
}
// delete old Certificate
if err := g.dbmanager.CertificateDaoTransactions(tx).DeleteCertificateByID(rule.CertificateID); err != nil {
@ -71,12 +74,12 @@ func (g *GatewayAction) UpdateHttpRule(req *apimodel.HttpRuleStruct, serviceID s
}
// DeleteHttpRule deletes http rule, including certificate and rule extensions
func (g *GatewayAction) DeleteHttpRule(req *apimodel.HttpRuleStruct, serviceID string) error {
func (g *GatewayAction) DeleteHttpRule(req *apimodel.HttpRuleStruct) error {
// begin transaction
tx := db.GetManager().Begin()
// delete http rule
httpRule, err := g.dbmanager.HttpRuleDaoTransactions(tx).DeleteHttpRuleByServiceIDAndContainerPort(serviceID,
req.ContainerPort)
httpRule, err := g.dbmanager.HttpRuleDaoTransactions(tx).DeleteHttpRuleByServiceIDAndContainerPort(
req.ServiceID, req.ContainerPort)
if err != nil {
tx.Rollback()
return err

View File

@ -26,8 +26,8 @@ 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
UpdateHttpRule(req *apimodel.HttpRuleStruct, tx *gorm.DB) (*dbmodel.HttpRule, error)
DeleteHttpRule(req *apimodel.HttpRuleStruct) 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

@ -20,6 +20,7 @@ package model
//HttpRuleStruct -
type HttpRuleStruct struct {
ServiceID string `json:"service_id" validate:"service_id|required"`
ContainerPort int `json:"container_port" validate:"container_port|required"`
Domain string `json:"domain"`
Path string `json:"path"`