[DEL] remove IPPort temporarily

This commit is contained in:
GLYASAI 2018-12-19 13:53:11 +08:00
parent 5ae1489948
commit ba298e3a39
2 changed files with 32 additions and 121 deletions

View File

@ -204,6 +204,11 @@ func (g *GatewayStruct) AddTCPRule(w http.ResponseWriter, r *http.Request) {
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)}
} 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 {
for _, re := range req.RuleExtensions {
@ -221,16 +226,6 @@ func (g *GatewayStruct) AddTCPRule(w http.ResponseWriter, r *http.Request) {
httputil.ReturnValidationError(r, w, values)
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)
if err != nil {
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()
// verify reqeust
values := url.Values{}
if req.Port == 0 {
values["port"] = []string{"The port field is required"}
} else if req.Port <= g.cfg.MinExtPort {
if req.Port != 0 && req.Port <= 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 {
for _, re := range req.RuleExtensions {
@ -279,21 +277,9 @@ func (g *GatewayStruct) updateTCPRule(w http.ResponseWriter, r *http.Request) {
httputil.ReturnValidationError(r, w, values)
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)
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 "+
"updating tcp rule: %v", err))
return
@ -344,33 +330,4 @@ func (g *GatewayStruct) GetAvailablePort(w http.ResponseWriter, r *http.Request)
}
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")
}

View File

@ -258,7 +258,7 @@ func (g *GatewayAction) UpdateCertificate(req apimodel.AddHTTPRuleStruct, httpRu
func (g *GatewayAction) AddTCPRule(req *apimodel.AddTCPRuleStruct) (string, error) {
// begin transaction
tx := db.GetManager().Begin()
// TODO: do not use LBMappingPort again
// add port
port := &model.TenantServiceLBMappingPort{
ServiceID: req.ServiceID,
Port: req.Port,
@ -269,17 +269,6 @@ func (g *GatewayAction) AddTCPRule(req *apimodel.AddTCPRuleStruct) (string, erro
tx.Rollback()
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
tcpRule := &model.TCPRule{
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) {
// begin transaction
tx := db.GetManager().Begin()
logrus.Debugf("begin transaction")
// get old tcp rule
tcpRule, err := g.dbmanager.TcpRuleDaoTransactions(tx).GetTcpRuleByID(req.TCPRuleID)
if err != nil {
tx.Rollback()
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 {
// delete old rule extensions
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
}
// add new rule extensions
logrus.Debugf("add new extensions.")
for _, ruleExtension := range req.RuleExtensions {
re := &model.RuleExtension{
UUID: util.NewUUID(),
@ -350,50 +332,34 @@ func (g *GatewayAction) UpdateTCPRule(req *apimodel.UpdateTCPRuleStruct, minPort
}
}
// update tcp rule
logrus.Debugf("update tcp rule")
if req.ServiceID != "" {
tcpRule.ServiceID = req.ServiceID
}
if req.ContainerPort != 0 {
tcpRule.ContainerPort = req.ContainerPort
}
// TODO: no longer use LBMappingPort
// get old port
logrus.Debugf("get old port")
port, err := g.dbmanager.TenantServiceLBMappingPortDaoTransactions(tx).GetLBMappingPortByServiceIDAndPort(
tcpRule.ServiceID, tcpRule.Port)
if err != nil {
tx.Rollback()
return "", err
if req.IP != "" {
tcpRule.IP = req.IP
}
// update port
logrus.Debugf("update port")
port.Port = req.Port
if err := g.dbmanager.TenantServiceLBMappingPortDaoTransactions(tx).UpdateModel(port); err != nil {
tx.Rollback()
return "", err
if req.Port > minPort {
// get old port
port, err := g.dbmanager.TenantServiceLBMappingPortDaoTransactions(tx).GetLBMappingPortByServiceIDAndPort(
tcpRule.ServiceID, tcpRule.Port)
if err != nil {
tx.Rollback()
return "", err
}
// check
// update port
port.Port = req.Port
if err := g.dbmanager.TenantServiceLBMappingPortDaoTransactions(tx).UpdateModel(port); err != nil {
tx.Rollback()
return "", err
}
tcpRule.Port = req.Port
} else {
logrus.Warningf("Expected external port > %d, but got %d", minPort, req.Port)
}
// 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.IP = req.IP
logrus.Debugf("update tcp rule")
if err := g.dbmanager.TcpRuleDaoTransactions(tx).UpdateModel(tcpRule); err != nil {
tx.Rollback()
return "", err
@ -415,10 +381,6 @@ func (g *GatewayAction) DeleteTCPRule(req *apimodel.DeleteTCPRuleStruct) (string
tx.Rollback()
return "", err
}
if tcpRule == nil {
tx.Rollback()
return "", fmt.Errorf("no TCPRule that matche ruleID(%s)", req.TCPRuleID)
}
// delete rule extensions
if err := db.GetManager().RuleExtensionDaoTransactions(tx).DeleteRuleExtensionByRuleID(tcpRule.UUID); err != nil {
tx.Rollback()
@ -436,14 +398,6 @@ func (g *GatewayAction) DeleteTCPRule(req *apimodel.DeleteTCPRuleStruct) (string
tx.Rollback()
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
if err := tx.Commit().Error; err != nil {
tx.Rollback()