mirror of
https://gitee.com/rainbond/Rainbond.git
synced 2024-12-04 04:38:04 +08:00
[DEL] remove IPPort temporarily
This commit is contained in:
parent
5ae1489948
commit
ba298e3a39
@ -204,6 +204,11 @@ func (g *GatewayStruct) AddTCPRule(w http.ResponseWriter, r *http.Request) {
|
|||||||
values["port"] = []string{"The port field is required"}
|
values["port"] = []string{"The port field is required"}
|
||||||
} else if req.Port <= g.cfg.MinExtPort {
|
} else if req.Port <= g.cfg.MinExtPort {
|
||||||
values["port"] = []string{fmt.Sprintf("The port field should be greater than %d", g.cfg.MinExtPort)}
|
values["port"] = []string{fmt.Sprintf("The port field should be greater than %d", g.cfg.MinExtPort)}
|
||||||
|
} else {
|
||||||
|
// check if the port exists
|
||||||
|
if h.PortExists(req.Port) {
|
||||||
|
values["port"] = []string{fmt.Sprintf("The port(%v) already exists", req.Port)}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if len(req.RuleExtensions) > 0 {
|
if len(req.RuleExtensions) > 0 {
|
||||||
for _, re := range req.RuleExtensions {
|
for _, re := range req.RuleExtensions {
|
||||||
@ -221,16 +226,6 @@ func (g *GatewayStruct) AddTCPRule(w http.ResponseWriter, r *http.Request) {
|
|||||||
httputil.ReturnValidationError(r, w, values)
|
httputil.ReturnValidationError(r, w, values)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if req.IP == "" {
|
|
||||||
req.IP = "0.0.0.0"
|
|
||||||
}
|
|
||||||
if !h.TCPAvailable(req.IP, req.Port, req.TCPRuleID) {
|
|
||||||
httputil.ReturnError(r, w, 500, fmt.Sprintf("%s:%d is not available, please change one",
|
|
||||||
req.IP, req.Port))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
sid, err := h.AddTCPRule(&req)
|
sid, err := h.AddTCPRule(&req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while "+
|
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while "+
|
||||||
@ -258,10 +253,13 @@ func (g *GatewayStruct) updateTCPRule(w http.ResponseWriter, r *http.Request) {
|
|||||||
h := handler.GetGatewayHandler()
|
h := handler.GetGatewayHandler()
|
||||||
// verify reqeust
|
// verify reqeust
|
||||||
values := url.Values{}
|
values := url.Values{}
|
||||||
if req.Port == 0 {
|
if req.Port != 0 && req.Port <= g.cfg.MinExtPort {
|
||||||
values["port"] = []string{"The port field is required"}
|
|
||||||
} else if req.Port <= g.cfg.MinExtPort {
|
|
||||||
values["port"] = []string{fmt.Sprintf("The port field should be greater than %d", g.cfg.MinExtPort)}
|
values["port"] = []string{fmt.Sprintf("The port field should be greater than %d", g.cfg.MinExtPort)}
|
||||||
|
} else {
|
||||||
|
// check if the port exists
|
||||||
|
if h.PortExists(req.Port) {
|
||||||
|
values["port"] = []string{fmt.Sprintf("The port(%v) already exists", req.Port)}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if len(req.RuleExtensions) > 0 {
|
if len(req.RuleExtensions) > 0 {
|
||||||
for _, re := range req.RuleExtensions {
|
for _, re := range req.RuleExtensions {
|
||||||
@ -279,21 +277,9 @@ func (g *GatewayStruct) updateTCPRule(w http.ResponseWriter, r *http.Request) {
|
|||||||
httputil.ReturnValidationError(r, w, values)
|
httputil.ReturnValidationError(r, w, values)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logrus.Debugf("request data is ok")
|
|
||||||
|
|
||||||
if req.IP == "" {
|
|
||||||
req.IP = "0.0.0.0"
|
|
||||||
}
|
|
||||||
if !h.TCPAvailable(req.IP, req.Port, req.TCPRuleID) {
|
|
||||||
httputil.ReturnError(r, w, 500, fmt.Sprintf("%s:%d is not available, please change one",
|
|
||||||
req.IP, req.Port))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logrus.Debugf("tcp available.")
|
|
||||||
|
|
||||||
sid, err := h.UpdateTCPRule(&req, g.cfg.MinExtPort)
|
sid, err := h.UpdateTCPRule(&req, g.cfg.MinExtPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("Unexpected error occorred while updating tcp rule: %v", err)
|
|
||||||
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while "+
|
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while "+
|
||||||
"updating tcp rule: %v", err))
|
"updating tcp rule: %v", err))
|
||||||
return
|
return
|
||||||
@ -345,32 +331,3 @@ func (g *GatewayStruct) GetAvailablePort(w http.ResponseWriter, r *http.Request)
|
|||||||
|
|
||||||
httputil.ReturnSuccess(r, w, res)
|
httputil.ReturnSuccess(r, w, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GatewayStruct) IPPool(w http.ResponseWriter, r *http.Request) {
|
|
||||||
switch r.Method {
|
|
||||||
case "POST":
|
|
||||||
g.AddIPPool(w, r)
|
|
||||||
case "PUT":
|
|
||||||
g.updateTCPRule(w, r)
|
|
||||||
case "DELETE":
|
|
||||||
g.deleteTCPRule(w, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GatewayStruct) AddIPPool(w http.ResponseWriter, r *http.Request) {
|
|
||||||
logrus.Debugf("add ip pool.")
|
|
||||||
var req api_model.IPPoolStruct
|
|
||||||
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
reqJSON, _ := json.Marshal(req)
|
|
||||||
logrus.Debugf("Request is : %s", string(reqJSON))
|
|
||||||
|
|
||||||
if err := handler.GetGatewayHandler().AddIPPool(&req); err != nil {
|
|
||||||
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while "+
|
|
||||||
"adding IPPool: %v", err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
httputil.ReturnSuccess(r, w, "success")
|
|
||||||
}
|
|
@ -258,7 +258,7 @@ func (g *GatewayAction) UpdateCertificate(req apimodel.AddHTTPRuleStruct, httpRu
|
|||||||
func (g *GatewayAction) AddTCPRule(req *apimodel.AddTCPRuleStruct) (string, error) {
|
func (g *GatewayAction) AddTCPRule(req *apimodel.AddTCPRuleStruct) (string, error) {
|
||||||
// begin transaction
|
// begin transaction
|
||||||
tx := db.GetManager().Begin()
|
tx := db.GetManager().Begin()
|
||||||
// TODO: do not use LBMappingPort again
|
// add port
|
||||||
port := &model.TenantServiceLBMappingPort{
|
port := &model.TenantServiceLBMappingPort{
|
||||||
ServiceID: req.ServiceID,
|
ServiceID: req.ServiceID,
|
||||||
Port: req.Port,
|
Port: req.Port,
|
||||||
@ -269,17 +269,6 @@ func (g *GatewayAction) AddTCPRule(req *apimodel.AddTCPRuleStruct) (string, erro
|
|||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
// IPPort
|
|
||||||
ipport := &model.IPPort{
|
|
||||||
UUID: util.NewUUID(),
|
|
||||||
IP: req.IP,
|
|
||||||
Port: req.Port,
|
|
||||||
}
|
|
||||||
err = g.dbmanager.IPPortDaoTransactions(tx).AddModel(ipport)
|
|
||||||
if err != nil {
|
|
||||||
tx.Rollback()
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
// add tcp rule
|
// add tcp rule
|
||||||
tcpRule := &model.TCPRule{
|
tcpRule := &model.TCPRule{
|
||||||
UUID: req.TCPRuleID,
|
UUID: req.TCPRuleID,
|
||||||
@ -317,18 +306,12 @@ func (g *GatewayAction) AddTCPRule(req *apimodel.AddTCPRuleStruct) (string, erro
|
|||||||
func (g *GatewayAction) UpdateTCPRule(req *apimodel.UpdateTCPRuleStruct, minPort int) (string, error) {
|
func (g *GatewayAction) UpdateTCPRule(req *apimodel.UpdateTCPRuleStruct, minPort int) (string, error) {
|
||||||
// begin transaction
|
// begin transaction
|
||||||
tx := db.GetManager().Begin()
|
tx := db.GetManager().Begin()
|
||||||
logrus.Debugf("begin transaction")
|
|
||||||
// get old tcp rule
|
// get old tcp rule
|
||||||
tcpRule, err := g.dbmanager.TcpRuleDaoTransactions(tx).GetTcpRuleByID(req.TCPRuleID)
|
tcpRule, err := g.dbmanager.TcpRuleDaoTransactions(tx).GetTcpRuleByID(req.TCPRuleID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if tcpRule == nil {
|
|
||||||
tx.Rollback()
|
|
||||||
return "", fmt.Errorf("no TCPRule that matche ruleID(%s)", req.TCPRuleID)
|
|
||||||
}
|
|
||||||
logrus.Debugf("rule extension.")
|
|
||||||
if len(req.RuleExtensions) > 0 {
|
if len(req.RuleExtensions) > 0 {
|
||||||
// delete old rule extensions
|
// delete old rule extensions
|
||||||
if err := g.dbmanager.RuleExtensionDaoTransactions(tx).DeleteRuleExtensionByRuleID(tcpRule.UUID); err != nil {
|
if err := g.dbmanager.RuleExtensionDaoTransactions(tx).DeleteRuleExtensionByRuleID(tcpRule.UUID); err != nil {
|
||||||
@ -336,7 +319,6 @@ func (g *GatewayAction) UpdateTCPRule(req *apimodel.UpdateTCPRuleStruct, minPort
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
// add new rule extensions
|
// add new rule extensions
|
||||||
logrus.Debugf("add new extensions.")
|
|
||||||
for _, ruleExtension := range req.RuleExtensions {
|
for _, ruleExtension := range req.RuleExtensions {
|
||||||
re := &model.RuleExtension{
|
re := &model.RuleExtension{
|
||||||
UUID: util.NewUUID(),
|
UUID: util.NewUUID(),
|
||||||
@ -350,50 +332,34 @@ func (g *GatewayAction) UpdateTCPRule(req *apimodel.UpdateTCPRuleStruct, minPort
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// update tcp rule
|
// update tcp rule
|
||||||
logrus.Debugf("update tcp rule")
|
|
||||||
if req.ServiceID != "" {
|
if req.ServiceID != "" {
|
||||||
tcpRule.ServiceID = req.ServiceID
|
tcpRule.ServiceID = req.ServiceID
|
||||||
}
|
}
|
||||||
if req.ContainerPort != 0 {
|
if req.ContainerPort != 0 {
|
||||||
tcpRule.ContainerPort = req.ContainerPort
|
tcpRule.ContainerPort = req.ContainerPort
|
||||||
}
|
}
|
||||||
// TODO: no longer use LBMappingPort
|
if req.IP != "" {
|
||||||
|
tcpRule.IP = req.IP
|
||||||
|
}
|
||||||
|
if req.Port > minPort {
|
||||||
// get old port
|
// get old port
|
||||||
logrus.Debugf("get old port")
|
|
||||||
port, err := g.dbmanager.TenantServiceLBMappingPortDaoTransactions(tx).GetLBMappingPortByServiceIDAndPort(
|
port, err := g.dbmanager.TenantServiceLBMappingPortDaoTransactions(tx).GetLBMappingPortByServiceIDAndPort(
|
||||||
tcpRule.ServiceID, tcpRule.Port)
|
tcpRule.ServiceID, tcpRule.Port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
// check
|
||||||
// update port
|
// update port
|
||||||
logrus.Debugf("update port")
|
|
||||||
port.Port = req.Port
|
port.Port = req.Port
|
||||||
if err := g.dbmanager.TenantServiceLBMappingPortDaoTransactions(tx).UpdateModel(port); err != nil {
|
if err := g.dbmanager.TenantServiceLBMappingPortDaoTransactions(tx).UpdateModel(port); err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
// IPPort
|
|
||||||
// get old IPPort
|
|
||||||
logrus.Debugf("get old IPPort")
|
|
||||||
ipport, err := g.dbmanager.IPPortDaoTransactions(tx).GetIPPortByIPAndPort(tcpRule.IP, tcpRule.Port)
|
|
||||||
if err != nil {
|
|
||||||
tx.Rollback()
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
// update
|
|
||||||
ipport.IP = req.IP
|
|
||||||
ipport.Port = req.Port
|
|
||||||
logrus.Debugf("update IPPort")
|
|
||||||
err = g.dbmanager.IPPortDaoTransactions(tx).UpdateModel(ipport)
|
|
||||||
if err != nil {
|
|
||||||
tx.Rollback()
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
// TCPRule
|
|
||||||
tcpRule.Port = req.Port
|
tcpRule.Port = req.Port
|
||||||
tcpRule.IP = req.IP
|
} else {
|
||||||
logrus.Debugf("update tcp rule")
|
logrus.Warningf("Expected external port > %d, but got %d", minPort, req.Port)
|
||||||
|
}
|
||||||
if err := g.dbmanager.TcpRuleDaoTransactions(tx).UpdateModel(tcpRule); err != nil {
|
if err := g.dbmanager.TcpRuleDaoTransactions(tx).UpdateModel(tcpRule); err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return "", err
|
return "", err
|
||||||
@ -415,10 +381,6 @@ func (g *GatewayAction) DeleteTCPRule(req *apimodel.DeleteTCPRuleStruct) (string
|
|||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if tcpRule == nil {
|
|
||||||
tx.Rollback()
|
|
||||||
return "", fmt.Errorf("no TCPRule that matche ruleID(%s)", req.TCPRuleID)
|
|
||||||
}
|
|
||||||
// delete rule extensions
|
// delete rule extensions
|
||||||
if err := db.GetManager().RuleExtensionDaoTransactions(tx).DeleteRuleExtensionByRuleID(tcpRule.UUID); err != nil {
|
if err := db.GetManager().RuleExtensionDaoTransactions(tx).DeleteRuleExtensionByRuleID(tcpRule.UUID); err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
@ -436,14 +398,6 @@ func (g *GatewayAction) DeleteTCPRule(req *apimodel.DeleteTCPRuleStruct) (string
|
|||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
// delete IPPort
|
|
||||||
if tcpRule.IP == "" {
|
|
||||||
tcpRule.IP = "0.0.0.0"
|
|
||||||
}
|
|
||||||
if err := db.GetManager().IPPortDao().DeleteByIPAndPort(tcpRule.IP, tcpRule.Port); err != nil {
|
|
||||||
tx.Rollback()
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
// end transaction
|
// end transaction
|
||||||
if err := tx.Commit().Error; err != nil {
|
if err := tx.Commit().Error; err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
|
Loading…
Reference in New Issue
Block a user