[ADD] add change tanent lb port api

This commit is contained in:
barnettZQG 2018-06-07 13:02:24 +08:00
parent a56fd2aa93
commit 8fdd679ad6
9 changed files with 105 additions and 2 deletions

View File

@ -55,6 +55,7 @@ type ServiceInterface interface {
PutPorts(w http.ResponseWriter, r *http.Request)
PortOuterController(w http.ResponseWriter, r *http.Request)
PortInnerController(w http.ResponseWriter, r *http.Request)
ChangeLBPort(w http.ResponseWriter, r *http.Request)
RollBack(w http.ResponseWriter, r *http.Request)
AddVolume(w http.ResponseWriter, r *http.Request)
DeleteVolume(w http.ResponseWriter, r *http.Request)

View File

@ -152,6 +152,7 @@ func (v2 *V2) serviceRouter() chi.Router {
r.Delete("/ports/{port}", controller.GetManager().Ports)
r.Put("/ports/{port}/outer", controller.GetManager().PortOuterController)
r.Put("/ports/{port}/inner", controller.GetManager().PortInnerController)
r.Put("/ports/{port}/changelbport", controller.GetManager().ChangeLBPort)
//应用版本回滚(act)
r.Post("/rollback", controller.GetManager().RollBack)

View File

@ -1536,6 +1536,29 @@ func (t *TenantStruct) PortInnerController(w http.ResponseWriter, r *http.Reques
httputil.ReturnSuccess(r, w, nil)
}
//ChangeLBPort change lb mapping port
//only support change to existing port in this tenants
func (t *TenantStruct) ChangeLBPort(w http.ResponseWriter, r *http.Request) {
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
tenantID := r.Context().Value(middleware.ContextKey("tenant_id")).(string)
portStr := chi.URLParam(r, "port")
containerPort, err := strconv.Atoi(portStr)
if err != nil {
httputil.ReturnError(r, w, 400, "port must be a number")
return
}
var data api_model.ServiceLBPortChange
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &(data.Body), nil) {
return
}
mapport, errc := handler.GetServiceManager().ChangeLBPort(tenantID, serviceID, containerPort, data.Body.ChangePort)
if errc != nil {
errc.Handle(r, w)
return
}
httputil.ReturnSuccess(r, w, mapport)
}
//Pods pods
// swagger:operation GET /v2/tenants/{tenant_name}/services/{service_alias}/pods v2 getPodsInfo
//

View File

@ -49,6 +49,7 @@ type ServiceHandler interface {
PortVar(action string, tenantID, serviceID string, vp *api_model.ServicePorts, oldPort int) error
PortOuter(tenantName, serviceID, operation string, port int) (*dbmodel.TenantServiceLBMappingPort, string, error)
PortInner(tenantName, serviceID, operation string, port int) error
ChangeLBPort(tenantID, serviceID string, containerPort, changelbPort int) (*dbmodel.TenantServiceLBMappingPort, *util.APIHandleError)
VolumnVar(tsv *dbmodel.TenantServiceVolume, tenantID, action string) *util.APIHandleError
VolumeDependency(tsr *dbmodel.TenantServiceMountRelation, action string) *util.APIHandleError
GetDepVolumes(serviceID string) ([]*dbmodel.TenantServiceMountRelation, *util.APIHandleError)

View File

@ -1456,6 +1456,42 @@ func (s *ServiceAction) PortInner(tenantName, serviceID, operation string, port
return nil
}
//ChangeLBPort change lb mapping port
//only support change to existing port in this tenants
func (s *ServiceAction) ChangeLBPort(tenantID, serviceID string, containerPort, changelbPort int) (*dbmodel.TenantServiceLBMappingPort, *util.APIHandleError) {
oldmapport, err := db.GetManager().TenantServiceLBMappingPortDao().GetLBPortByTenantAndPort(tenantID, changelbPort)
if err != nil {
logrus.Errorf("change lb port check error, %s", err.Error())
return nil, util.CreateAPIHandleErrorFromDBError("change lb port", err)
}
mapport, err := db.GetManager().TenantServiceLBMappingPortDao().GetTenantServiceLBMappingPort(serviceID, containerPort)
if err != nil {
logrus.Errorf("change lb port get error, %s", err.Error())
return nil, util.CreateAPIHandleErrorFromDBError("change lb port", err)
}
port := oldmapport.Port
oldmapport.Port = mapport.Port
mapport.Port = port
tx := db.GetManager().Begin()
if err := db.GetManager().TenantServiceLBMappingPortDaoTransactions(tx).DELServiceLBMappingPortByServiceIDAndPort(oldmapport.ServiceID, port); err != nil {
tx.Rollback()
return nil, util.CreateAPIHandleErrorFromDBError("change lb port", err)
}
if err := db.GetManager().TenantServiceLBMappingPortDaoTransactions(tx).UpdateModel(mapport); err != nil {
tx.Rollback()
return nil, util.CreateAPIHandleErrorFromDBError("change lb port", err)
}
if err := db.GetManager().TenantServiceLBMappingPortDaoTransactions(tx).AddModel(oldmapport); err != nil {
tx.Rollback()
return nil, util.CreateAPIHandleErrorFromDBError("change lb port", err)
}
if err := tx.Commit().Error; err != nil {
tx.Rollback()
return nil, util.CreateAPIHandleErrorFromDBError("change lb port", err)
}
return mapport, nil
}
//VolumnVar var volumn
func (s *ServiceAction) VolumnVar(tsv *dbmodel.TenantServiceVolume, tenantID, action string) *util.APIHandleError {
localPath := os.Getenv("LOCAL_DATA_PATH")

View File

@ -23,8 +23,9 @@ import (
"time"
"fmt"
dbmodel "github.com/goodrain/rainbond/db/model"
"strings"
dbmodel "github.com/goodrain/rainbond/db/model"
)
//ServiceGetCommon path参数
@ -460,6 +461,26 @@ type ServicePortInnerOrOuter struct {
}
}
// ServiceLBPortChange change lb port
// swagger:parameters changelbport
type ServiceLBPortChange struct {
// in: path
// required: true
TenantName string `json:"tenant_name"`
// in: path
// required: true
ServiceAlias string `json:"service_alias"`
// in: path
// required: true
Port int `json:"port"`
//in: body
Body struct {
// in: body
// required: true
ChangePort int `json:"change_port" validate:"change_port|required"`
}
}
//RollbackStruct struct
type RollbackStruct struct {
TenantID string `json:"tenant_id"`

View File

@ -226,6 +226,8 @@ type TenantServiceLBMappingPortDao interface {
GetTenantServiceLBMappingPortByService(serviceID string) ([]*model.TenantServiceLBMappingPort, error)
CreateTenantServiceLBMappingPort(serviceID string, containerPort int) (*model.TenantServiceLBMappingPort, error)
DELServiceLBMappingPortByServiceID(serviceID string) error
DELServiceLBMappingPortByServiceIDAndPort(serviceID string, lbPort int) error
GetLBPortByTenantAndPort(tenantID string, lbport int) (*model.TenantServiceLBMappingPort, error)
}
//TenantServiceLabelDao TenantServiceLabelDao

View File

@ -115,7 +115,7 @@ func (t *TenantPluginBuildVersion) CreateShareImage(hubURL, namespace string) (s
if namespace != "" {
image.Namespace = namespace
}
image.Name = image.Name + "_" + t.VersionID + ":" + t.DeployVersion
image.Name = image.Name + "_" + t.VersionID
return image.String(), nil
}

View File

@ -1012,6 +1012,24 @@ func (t *TenantServiceLBMappingPortDaoImpl) DELServiceLBMappingPortByServiceID(s
return nil
}
//DELServiceLBMappingPortByServiceIDAndPort DELServiceLBMappingPortByServiceIDAndPort
func (t *TenantServiceLBMappingPortDaoImpl) DELServiceLBMappingPortByServiceIDAndPort(serviceID string, lbport int) error {
var mapPorts model.TenantServiceLBMappingPort
if err := t.DB.Where("service_id=? and port=?", serviceID, lbport).Delete(&mapPorts).Error; err != nil {
return err
}
return nil
}
// GetLBPortByTenantAndPort GetLBPortByTenantAndPort
func (t *TenantServiceLBMappingPortDaoImpl) GetLBPortByTenantAndPort(tenantID string, lbport int) (*model.TenantServiceLBMappingPort, error) {
var mapPort model.TenantServiceLBMappingPort
if err := t.DB.Raw("select * from tenant_lb_mapping_port where port=? and service_id in(select service_id from tenant_services where tenant_id=?)", lbport, tenantID).Scan(&mapPort).Error; err != nil {
return nil, err
}
return &mapPort, nil
}
//ServiceLabelDaoImpl ServiceLabelDaoImpl
type ServiceLabelDaoImpl struct {
DB *gorm.DB