[REV] change label request from LabelStruct to LabelsStruct

This commit is contained in:
GLYASAI 2018-12-04 15:20:59 +08:00
parent 3d58194df9
commit dd3656b4f3
6 changed files with 151 additions and 100 deletions

View File

@ -36,7 +36,7 @@ import (
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
validator "github.com/thedevsaddam/govalidator" "github.com/thedevsaddam/govalidator"
"github.com/goodrain/rainbond/api/handler" "github.com/goodrain/rainbond/api/handler"
httputil "github.com/goodrain/rainbond/util/http" httputil "github.com/goodrain/rainbond/util/http"
@ -816,20 +816,20 @@ func (t *TenantStruct) AddServiceLabel(w http.ResponseWriter, r *http.Request) {
// schema: // schema:
// "$ref": "#/responses/commandResponse" // "$ref": "#/responses/commandResponse"
// description: 统一返回格式 // description: 统一返回格式
rules := validator.MapData{ //rules := validator.MapData{
"label_values": []string{"required"}, // "label_values": []string{"required"},
} //}
data, ok := httputil.ValidatorRequestMapAndErrorResponse(r, w, rules, nil) //data, ok := httputil.ValidatorRequestMapAndErrorResponse(r, w, rules, nil)
if !ok { //if !ok {
return // return
} //}
var valueList []string //var valueList []string
valueList = append(valueList, data["label_values"].(string)) //valueList = append(valueList, data["label_values"].(string))
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string) //serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
if err := handler.GetServiceManager().AddLabel("service", serviceID, valueList); err != nil { //if err := handler.GetServiceManager().AddLabel("service", serviceID, valueList); err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("add service label error, %v", err)) // httputil.ReturnError(r, w, 500, fmt.Sprintf("add service label error, %v", err))
return // return
} //}
httputil.ReturnSuccess(r, w, nil) httputil.ReturnSuccess(r, w, nil)
} }
@ -910,12 +910,12 @@ func (t *TenantStruct) AddNodeLabel(w http.ResponseWriter, r *http.Request) {
return return
} }
//logrus.Info(labels.Body.LabelValues) //logrus.Info(labels.Body.LabelValues)
valueList := labels.Body.LabelValues //valueList := labels.Body.LabelValues
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string) //serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
if err := handler.GetServiceManager().AddLabel("node", serviceID, valueList); err != nil { //if err := handler.GetServiceManager().AddLabel("node", serviceID, valueList); err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("add node label failure, %v", err)) // httputil.ReturnError(r, w, 500, fmt.Sprintf("add node label failure, %v", err))
return // return
} //}
httputil.ReturnSuccess(r, w, nil) httputil.ReturnSuccess(r, w, nil)
} }
@ -932,7 +932,7 @@ func (t *TenantStruct) Label(w http.ResponseWriter, r *http.Request) {
func (t *TenantStruct) AddLabel(w http.ResponseWriter, r *http.Request) { func (t *TenantStruct) AddLabel(w http.ResponseWriter, r *http.Request) {
logrus.Debugf("add label") logrus.Debugf("add label")
var req api_model.LabelStruct var req api_model.LabelsStruct
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil) ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
if !ok { if !ok {
return return
@ -941,7 +941,7 @@ func (t *TenantStruct) AddLabel(w http.ResponseWriter, r *http.Request) {
logrus.Debugf("Request is : %s", string(reqJSON)) logrus.Debugf("Request is : %s", string(reqJSON))
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string) serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
if err := handler.GetServiceManager().AddLabel(req.LabelKey, serviceID, req.LabelValues); err != nil { if err := handler.GetServiceManager().AddLabel(&req, serviceID); err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("add label error, %v", err)) httputil.ReturnError(r, w, 500, fmt.Sprintf("add label error, %v", err))
return return
} }
@ -950,7 +950,7 @@ func (t *TenantStruct) AddLabel(w http.ResponseWriter, r *http.Request) {
func (t *TenantStruct) DeleteLabel(w http.ResponseWriter, r *http.Request) { func (t *TenantStruct) DeleteLabel(w http.ResponseWriter, r *http.Request) {
logrus.Debugf("delete label") logrus.Debugf("delete label")
var req api_model.LabelStruct var req api_model.LabelsStruct
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil) ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
if !ok { if !ok {
return return
@ -959,7 +959,7 @@ func (t *TenantStruct) DeleteLabel(w http.ResponseWriter, r *http.Request) {
logrus.Debugf("Request is : %s", string(reqJSON)) logrus.Debugf("Request is : %s", string(reqJSON))
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string) serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
if err := handler.GetServiceManager().DeleteLabel(req.LabelKey, serviceID, req.LabelValues); err != nil { if err := handler.GetServiceManager().DeleteLabel(&req, serviceID); err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("delete node label failure, %v", err)) httputil.ReturnError(r, w, 500, fmt.Sprintf("delete node label failure, %v", err))
return return
} }
@ -968,7 +968,7 @@ func (t *TenantStruct) DeleteLabel(w http.ResponseWriter, r *http.Request) {
func (t *TenantStruct) UpdateLabel(w http.ResponseWriter, r *http.Request) { func (t *TenantStruct) UpdateLabel(w http.ResponseWriter, r *http.Request) {
logrus.Debugf("update label") logrus.Debugf("update label")
var req api_model.LabelStruct var req api_model.LabelsStruct
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil) ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
if !ok { if !ok {
return return
@ -977,7 +977,7 @@ func (t *TenantStruct) UpdateLabel(w http.ResponseWriter, r *http.Request) {
logrus.Debugf("Request is : %s", string(reqJSON)) logrus.Debugf("Request is : %s", string(reqJSON))
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string) serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
if err := handler.GetServiceManager().UpdateLabel(req.LabelKey, serviceID, req.LabelValues); err != nil { if err := handler.GetServiceManager().UpdateLabel(&req, serviceID); err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("error updating label: %v", err)) httputil.ReturnError(r, w, 500, fmt.Sprintf("error updating label: %v", err))
return return
} }
@ -1007,19 +1007,19 @@ func (t *TenantStruct) DeleteNodeLabel(w http.ResponseWriter, r *http.Request) {
// "$ref": "#/responses/commandResponse" // "$ref": "#/responses/commandResponse"
// description: 统一返回格式 // description: 统一返回格式
var labels api_model.AddNodeLabelStruct //var labels api_model.AddNodeLabelStruct
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &labels.Body, nil) //ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &labels.Body, nil)
if !ok { //if !ok {
return // return
} //}
//logrus.Info(labels.Body.LabelValues) ////logrus.Info(labels.Body.LabelValues)
valueList := labels.Body.LabelValues //valueList := labels.Body.LabelValues
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string) //serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
if err := handler.GetServiceManager().DeleteLabel("node", serviceID, valueList); err != nil { //if err := handler.GetServiceManager().DeleteLabel("node", serviceID, valueList); err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("delete node label failure, %v", err)) // httputil.ReturnError(r, w, 500, fmt.Sprintf("delete node label failure, %v", err))
return // return
} //}
httputil.ReturnSuccess(r, w, nil) //httputil.ReturnSuccess(r, w, nil)
} }
//StatusContainerID StatusContainerID //StatusContainerID StatusContainerID

View File

@ -29,9 +29,9 @@ import (
//ServiceHandler service handler //ServiceHandler service handler
type ServiceHandler interface { type ServiceHandler interface {
ServiceBuild(tenantID, serviceID string, r *api_model.BuildServiceStruct) error ServiceBuild(tenantID, serviceID string, r *api_model.BuildServiceStruct) error
AddLabel(kind, serviceID string, valueList []string) error AddLabel(l *api_model.LabelsStruct, serviceID string) error
DeleteLabel(kind, serviceID string, amp []string) error DeleteLabel(l *api_model.LabelsStruct, serviceID string) error
UpdateLabel(serviceID, kind string, amp []string) error UpdateLabel(l *api_model.LabelsStruct, serviceID string) error
UpdateServiceLabel(serviceID, value string) error UpdateServiceLabel(serviceID, value string) error
StartStopService(s *api_model.StartStopStruct) error StartStopService(s *api_model.StartStopStruct) error
ServiceVertical(v *model.VerticalScalingTaskBody) error ServiceVertical(v *model.VerticalScalingTaskBody) error

View File

@ -211,70 +211,81 @@ func (s *ServiceAction) buildFromSourceCode(r *api_model.BuildServiceStruct, ser
} }
//AddLabel add labels //AddLabel add labels
func (s *ServiceAction) AddLabel(kind, serviceID string, amp []string) error { func (s *ServiceAction) AddLabel(l *api_model.LabelsStruct, serviceID string) error {
for _, v := range amp { tx := db.GetManager().Begin()
var labelModel dbmodel.TenantServiceLable for _, label := range l.Labels {
switch kind { for _, v := range label.LabelValues {
case "service": var labelModel dbmodel.TenantServiceLable
labelModel.ServiceID = serviceID switch label.LabelKey {
labelModel.LabelKey = core_model.LabelKeyServiceType case "service":
v = chekeServiceLabel(v) labelModel.ServiceID = serviceID
labelModel.LabelValue = v labelModel.LabelKey = core_model.LabelKeyServiceType
case "node": v = chekeServiceLabel(v)
labelModel.ServiceID = serviceID labelModel.LabelValue = v
labelModel.LabelKey = v case "node":
labelModel.LabelValue = core_model.LabelKeyNodeSelector labelModel.ServiceID = serviceID
default: labelModel.LabelKey = v
labelModel.ServiceID = serviceID labelModel.LabelValue = core_model.LabelKeyNodeSelector
labelModel.LabelKey = kind default:
labelModel.LabelValue = v labelModel.ServiceID = serviceID
} labelModel.LabelKey = label.LabelKey
if err := db.GetManager().TenantServiceLabelDao().AddModel(&labelModel); err != nil { labelModel.LabelValue = v
return err }
if err := db.GetManager().TenantServiceLabelDaoTransactions(tx).AddModel(&labelModel); err != nil {
return err
}
} }
} }
if err := tx.Commit().Error; err != nil {
tx.Rollback()
return err
}
return nil return nil
} }
//UpdateLabel updates labels //UpdateLabel updates labels
func (s *ServiceAction) UpdateLabel(serviceID, kind string, amp []string) error { func (s *ServiceAction) UpdateLabel(l *api_model.LabelsStruct, serviceID string) error {
tx := db.GetManager().Begin() tx := db.GetManager().Begin()
// delete old labels for _, label := range l.Labels {
var err error // delete old labels
switch kind { var err error
case "node": switch label.LabelKey {
err = db.GetManager().TenantServiceLabelDaoTransactions(tx).DELTenantServiceLabelsByLabelvaluesAndServiceID(serviceID, amp)
default:
err = db.GetManager().TenantServiceLabelDaoTransactions(tx).DelTenantServiceLabels(serviceID, kind, amp)
}
if err != nil {
logrus.Errorf("error deleting old labels: %v", err)
tx.Rollback()
return err
}
// add new labels
for _, v := range amp {
var labelModel dbmodel.TenantServiceLable
switch kind {
case "service":
labelModel.ServiceID = serviceID
labelModel.LabelKey = core_model.LabelKeyServiceType
v = chekeServiceLabel(v)
labelModel.LabelValue = v
case "node": case "node":
labelModel.ServiceID = serviceID err = db.GetManager().TenantServiceLabelDaoTransactions(tx).
labelModel.LabelKey = v DelTenantServiceLabelsByLabelValuesAndServiceID(serviceID)
labelModel.LabelValue = core_model.LabelKeyNodeSelector
default: default:
labelModel.ServiceID = serviceID err = db.GetManager().TenantServiceLabelDaoTransactions(tx).
labelModel.LabelKey = kind DelTenantServiceLabelsByKey(serviceID, label.LabelKey)
labelModel.LabelValue = v
} }
if err := db.GetManager().TenantServiceLabelDaoTransactions(tx).AddModel(&labelModel); err != nil { if err != nil {
logrus.Errorf("error adding new labels: %v", err) logrus.Errorf("error deleting old labels: %v", err)
tx.Rollback() tx.Rollback()
return err return err
} }
// add new labels
for _, v := range label.LabelValues {
var labelModel dbmodel.TenantServiceLable
switch label.LabelKey {
case "service":
labelModel.ServiceID = serviceID
labelModel.LabelKey = core_model.LabelKeyServiceType
v = chekeServiceLabel(v)
labelModel.LabelValue = v
case "node":
labelModel.ServiceID = serviceID
labelModel.LabelKey = v
labelModel.LabelValue = core_model.LabelKeyNodeSelector
default:
labelModel.ServiceID = serviceID
labelModel.LabelKey = label.LabelKey
labelModel.LabelValue = v
}
if err := db.GetManager().TenantServiceLabelDaoTransactions(tx).AddModel(&labelModel); err != nil {
logrus.Errorf("error adding new labels: %v", err)
tx.Rollback()
return err
}
}
} }
if err := tx.Commit().Error; err != nil { if err := tx.Commit().Error; err != nil {
tx.Rollback() tx.Rollback()
@ -284,13 +295,29 @@ func (s *ServiceAction) UpdateLabel(serviceID, kind string, amp []string) error
} }
//DeleteLabel deletes label //DeleteLabel deletes label
func (s *ServiceAction) DeleteLabel(kind, serviceID string, amp []string) error { func (s *ServiceAction) DeleteLabel(l *api_model.LabelsStruct, serviceID string) error {
switch kind { tx := db.GetManager().Begin()
case "node": var err error
return db.GetManager().TenantServiceLabelDao().DELTenantServiceLabelsByLabelvaluesAndServiceID(serviceID, amp) for _, label := range l.Labels {
default: switch label.LabelKey {
return db.GetManager().TenantServiceLabelDao().DelTenantServiceLabels(serviceID, kind, amp) case "node":
err = db.GetManager().TenantServiceLabelDaoTransactions(tx).
DELTenantServiceLabelsByLabelvaluesAndServiceID(serviceID, label.LabelValues)
default:
err = db.GetManager().TenantServiceLabelDaoTransactions(tx).
DelTenantServiceLabels(serviceID, label.LabelKey, label.LabelValues)
}
if err != nil {
logrus.Errorf("error deleting label: %v", err)
tx.Rollback()
return err
}
} }
if err := tx.Commit().Error; err != nil {
tx.Rollback()
return err
}
return nil
} }
//UpdateServiceLabel UpdateLabel //UpdateServiceLabel UpdateLabel

View File

@ -803,6 +803,10 @@ type AddNodeLabelStruct struct {
} }
} }
type LabelsStruct struct {
Labels []LabelStruct `json:"labels"`
}
// LabelStruct holds info for adding, updating or deleting label // LabelStruct holds info for adding, updating or deleting label
type LabelStruct struct { type LabelStruct struct {
LabelKey string `json:"label_key" validate:"label_key|required"` LabelKey string `json:"label_key" validate:"label_key|required"`

View File

@ -245,7 +245,9 @@ type TenantServiceLabelDao interface {
GetTenantServiceAffinityLabel(serviceID string) ([]*model.TenantServiceLable, error) GetTenantServiceAffinityLabel(serviceID string) ([]*model.TenantServiceLable, error)
GetTenantServiceTypeLabel(serviceID string) (*model.TenantServiceLable, error) GetTenantServiceTypeLabel(serviceID string) (*model.TenantServiceLable, error)
DELTenantServiceLabelsByLabelvaluesAndServiceID(serviceID string, labelValues []string) error DELTenantServiceLabelsByLabelvaluesAndServiceID(serviceID string, labelValues []string) error
DelTenantServiceLabelsByLabelValuesAndServiceID(serviceID string) error
DelTenantServiceLabels(serviceID string, labelKey string, labelValues []string) error DelTenantServiceLabels(serviceID string, labelKey string, labelValues []string) error
DelTenantServiceLabelsByKey(serviceID string, labelKey string) error
} }
//LocalSchedulerDao 本地调度信息 //LocalSchedulerDao 本地调度信息

View File

@ -1272,6 +1272,15 @@ func (t *ServiceLabelDaoImpl) DELTenantServiceLabelsByLabelvaluesAndServiceID(se
return nil return nil
} }
//DELTenantServiceLabelsByLabelvaluesAndServiceID DELTenantServiceLabelsByLabelvaluesAndServiceID
func (t *ServiceLabelDaoImpl) DelTenantServiceLabelsByLabelValuesAndServiceID(serviceID string) error {
var label model.TenantServiceLable
if err := t.DB.Where("service_id=? and label_value=?", serviceID, model.LabelKeyNodeSelector).Delete(&label).Error; err != nil {
return err
}
return nil
}
//DelTenantServiceLabels deletes labels //DelTenantServiceLabels deletes labels
func (t *ServiceLabelDaoImpl) DelTenantServiceLabels(serviceID string, labelKey string, labelValues []string) error { func (t *ServiceLabelDaoImpl) DelTenantServiceLabels(serviceID string, labelKey string, labelValues []string) error {
var label model.TenantServiceLable var label model.TenantServiceLable
@ -1281,3 +1290,12 @@ func (t *ServiceLabelDaoImpl) DelTenantServiceLabels(serviceID string, labelKey
} }
return nil return nil
} }
//DelTenantServiceLabelsByKey deletes labels by label key
func (t *ServiceLabelDaoImpl) DelTenantServiceLabelsByKey(serviceID string, labelKey string) error {
var label model.TenantServiceLable
if err := t.DB.Where("service_id=? and label_key=?", serviceID, labelKey).Delete(&label).Error; err != nil {
return err
}
return nil
}