mirror of
https://gitee.com/rainbond/Rainbond.git
synced 2024-11-30 02:38:17 +08:00
[REV] change label request from LabelStruct to LabelsStruct
This commit is contained in:
parent
3d58194df9
commit
dd3656b4f3
@ -36,7 +36,7 @@ import (
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/jinzhu/gorm"
|
||||
validator "github.com/thedevsaddam/govalidator"
|
||||
"github.com/thedevsaddam/govalidator"
|
||||
|
||||
"github.com/goodrain/rainbond/api/handler"
|
||||
httputil "github.com/goodrain/rainbond/util/http"
|
||||
@ -816,20 +816,20 @@ func (t *TenantStruct) AddServiceLabel(w http.ResponseWriter, r *http.Request) {
|
||||
// schema:
|
||||
// "$ref": "#/responses/commandResponse"
|
||||
// description: 统一返回格式
|
||||
rules := validator.MapData{
|
||||
"label_values": []string{"required"},
|
||||
}
|
||||
data, ok := httputil.ValidatorRequestMapAndErrorResponse(r, w, rules, nil)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var valueList []string
|
||||
valueList = append(valueList, data["label_values"].(string))
|
||||
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
|
||||
if err := handler.GetServiceManager().AddLabel("service", serviceID, valueList); err != nil {
|
||||
httputil.ReturnError(r, w, 500, fmt.Sprintf("add service label error, %v", err))
|
||||
return
|
||||
}
|
||||
//rules := validator.MapData{
|
||||
// "label_values": []string{"required"},
|
||||
//}
|
||||
//data, ok := httputil.ValidatorRequestMapAndErrorResponse(r, w, rules, nil)
|
||||
//if !ok {
|
||||
// return
|
||||
//}
|
||||
//var valueList []string
|
||||
//valueList = append(valueList, data["label_values"].(string))
|
||||
//serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
|
||||
//if err := handler.GetServiceManager().AddLabel("service", serviceID, valueList); err != nil {
|
||||
// httputil.ReturnError(r, w, 500, fmt.Sprintf("add service label error, %v", err))
|
||||
// return
|
||||
//}
|
||||
httputil.ReturnSuccess(r, w, nil)
|
||||
}
|
||||
|
||||
@ -910,12 +910,12 @@ func (t *TenantStruct) AddNodeLabel(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
//logrus.Info(labels.Body.LabelValues)
|
||||
valueList := labels.Body.LabelValues
|
||||
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
|
||||
if err := handler.GetServiceManager().AddLabel("node", serviceID, valueList); err != nil {
|
||||
httputil.ReturnError(r, w, 500, fmt.Sprintf("add node label failure, %v", err))
|
||||
return
|
||||
}
|
||||
//valueList := labels.Body.LabelValues
|
||||
//serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
|
||||
//if err := handler.GetServiceManager().AddLabel("node", serviceID, valueList); err != nil {
|
||||
// httputil.ReturnError(r, w, 500, fmt.Sprintf("add node label failure, %v", err))
|
||||
// return
|
||||
//}
|
||||
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) {
|
||||
logrus.Debugf("add label")
|
||||
var req api_model.LabelStruct
|
||||
var req api_model.LabelsStruct
|
||||
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
|
||||
if !ok {
|
||||
return
|
||||
@ -941,7 +941,7 @@ func (t *TenantStruct) AddLabel(w http.ResponseWriter, r *http.Request) {
|
||||
logrus.Debugf("Request is : %s", string(reqJSON))
|
||||
|
||||
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))
|
||||
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) {
|
||||
logrus.Debugf("delete label")
|
||||
var req api_model.LabelStruct
|
||||
var req api_model.LabelsStruct
|
||||
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
|
||||
if !ok {
|
||||
return
|
||||
@ -959,7 +959,7 @@ func (t *TenantStruct) DeleteLabel(w http.ResponseWriter, r *http.Request) {
|
||||
logrus.Debugf("Request is : %s", string(reqJSON))
|
||||
|
||||
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))
|
||||
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) {
|
||||
logrus.Debugf("update label")
|
||||
var req api_model.LabelStruct
|
||||
var req api_model.LabelsStruct
|
||||
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
|
||||
if !ok {
|
||||
return
|
||||
@ -977,7 +977,7 @@ func (t *TenantStruct) UpdateLabel(w http.ResponseWriter, r *http.Request) {
|
||||
logrus.Debugf("Request is : %s", string(reqJSON))
|
||||
|
||||
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))
|
||||
return
|
||||
}
|
||||
@ -1007,19 +1007,19 @@ func (t *TenantStruct) DeleteNodeLabel(w http.ResponseWriter, r *http.Request) {
|
||||
// "$ref": "#/responses/commandResponse"
|
||||
// description: 统一返回格式
|
||||
|
||||
var labels api_model.AddNodeLabelStruct
|
||||
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &labels.Body, nil)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
//logrus.Info(labels.Body.LabelValues)
|
||||
valueList := labels.Body.LabelValues
|
||||
serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
|
||||
if err := handler.GetServiceManager().DeleteLabel("node", serviceID, valueList); err != nil {
|
||||
httputil.ReturnError(r, w, 500, fmt.Sprintf("delete node label failure, %v", err))
|
||||
return
|
||||
}
|
||||
httputil.ReturnSuccess(r, w, nil)
|
||||
//var labels api_model.AddNodeLabelStruct
|
||||
//ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &labels.Body, nil)
|
||||
//if !ok {
|
||||
// return
|
||||
//}
|
||||
////logrus.Info(labels.Body.LabelValues)
|
||||
//valueList := labels.Body.LabelValues
|
||||
//serviceID := r.Context().Value(middleware.ContextKey("service_id")).(string)
|
||||
//if err := handler.GetServiceManager().DeleteLabel("node", serviceID, valueList); err != nil {
|
||||
// httputil.ReturnError(r, w, 500, fmt.Sprintf("delete node label failure, %v", err))
|
||||
// return
|
||||
//}
|
||||
//httputil.ReturnSuccess(r, w, nil)
|
||||
}
|
||||
|
||||
//StatusContainerID StatusContainerID
|
||||
|
@ -29,9 +29,9 @@ import (
|
||||
//ServiceHandler service handler
|
||||
type ServiceHandler interface {
|
||||
ServiceBuild(tenantID, serviceID string, r *api_model.BuildServiceStruct) error
|
||||
AddLabel(kind, serviceID string, valueList []string) error
|
||||
DeleteLabel(kind, serviceID string, amp []string) error
|
||||
UpdateLabel(serviceID, kind string, amp []string) error
|
||||
AddLabel(l *api_model.LabelsStruct, serviceID string) error
|
||||
DeleteLabel(l *api_model.LabelsStruct, serviceID string) error
|
||||
UpdateLabel(l *api_model.LabelsStruct, serviceID string) error
|
||||
UpdateServiceLabel(serviceID, value string) error
|
||||
StartStopService(s *api_model.StartStopStruct) error
|
||||
ServiceVertical(v *model.VerticalScalingTaskBody) error
|
||||
|
@ -211,10 +211,12 @@ func (s *ServiceAction) buildFromSourceCode(r *api_model.BuildServiceStruct, ser
|
||||
}
|
||||
|
||||
//AddLabel add labels
|
||||
func (s *ServiceAction) AddLabel(kind, serviceID string, amp []string) error {
|
||||
for _, v := range amp {
|
||||
func (s *ServiceAction) AddLabel(l *api_model.LabelsStruct, serviceID string) error {
|
||||
tx := db.GetManager().Begin()
|
||||
for _, label := range l.Labels {
|
||||
for _, v := range label.LabelValues {
|
||||
var labelModel dbmodel.TenantServiceLable
|
||||
switch kind {
|
||||
switch label.LabelKey {
|
||||
case "service":
|
||||
labelModel.ServiceID = serviceID
|
||||
labelModel.LabelKey = core_model.LabelKeyServiceType
|
||||
@ -226,26 +228,34 @@ func (s *ServiceAction) AddLabel(kind, serviceID string, amp []string) error {
|
||||
labelModel.LabelValue = core_model.LabelKeyNodeSelector
|
||||
default:
|
||||
labelModel.ServiceID = serviceID
|
||||
labelModel.LabelKey = kind
|
||||
labelModel.LabelKey = label.LabelKey
|
||||
labelModel.LabelValue = v
|
||||
}
|
||||
if err := db.GetManager().TenantServiceLabelDao().AddModel(&labelModel); err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
//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()
|
||||
for _, label := range l.Labels {
|
||||
// delete old labels
|
||||
var err error
|
||||
switch kind {
|
||||
switch label.LabelKey {
|
||||
case "node":
|
||||
err = db.GetManager().TenantServiceLabelDaoTransactions(tx).DELTenantServiceLabelsByLabelvaluesAndServiceID(serviceID, amp)
|
||||
err = db.GetManager().TenantServiceLabelDaoTransactions(tx).
|
||||
DelTenantServiceLabelsByLabelValuesAndServiceID(serviceID)
|
||||
default:
|
||||
err = db.GetManager().TenantServiceLabelDaoTransactions(tx).DelTenantServiceLabels(serviceID, kind, amp)
|
||||
err = db.GetManager().TenantServiceLabelDaoTransactions(tx).
|
||||
DelTenantServiceLabelsByKey(serviceID, label.LabelKey)
|
||||
}
|
||||
if err != nil {
|
||||
logrus.Errorf("error deleting old labels: %v", err)
|
||||
@ -253,9 +263,9 @@ func (s *ServiceAction) UpdateLabel(serviceID, kind string, amp []string) error
|
||||
return err
|
||||
}
|
||||
// add new labels
|
||||
for _, v := range amp {
|
||||
for _, v := range label.LabelValues {
|
||||
var labelModel dbmodel.TenantServiceLable
|
||||
switch kind {
|
||||
switch label.LabelKey {
|
||||
case "service":
|
||||
labelModel.ServiceID = serviceID
|
||||
labelModel.LabelKey = core_model.LabelKeyServiceType
|
||||
@ -267,7 +277,7 @@ func (s *ServiceAction) UpdateLabel(serviceID, kind string, amp []string) error
|
||||
labelModel.LabelValue = core_model.LabelKeyNodeSelector
|
||||
default:
|
||||
labelModel.ServiceID = serviceID
|
||||
labelModel.LabelKey = kind
|
||||
labelModel.LabelKey = label.LabelKey
|
||||
labelModel.LabelValue = v
|
||||
}
|
||||
if err := db.GetManager().TenantServiceLabelDaoTransactions(tx).AddModel(&labelModel); err != nil {
|
||||
@ -276,6 +286,7 @@ func (s *ServiceAction) UpdateLabel(serviceID, kind string, amp []string) error
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
@ -284,13 +295,29 @@ func (s *ServiceAction) UpdateLabel(serviceID, kind string, amp []string) error
|
||||
}
|
||||
|
||||
//DeleteLabel deletes label
|
||||
func (s *ServiceAction) DeleteLabel(kind, serviceID string, amp []string) error {
|
||||
switch kind {
|
||||
func (s *ServiceAction) DeleteLabel(l *api_model.LabelsStruct, serviceID string) error {
|
||||
tx := db.GetManager().Begin()
|
||||
var err error
|
||||
for _, label := range l.Labels {
|
||||
switch label.LabelKey {
|
||||
case "node":
|
||||
return db.GetManager().TenantServiceLabelDao().DELTenantServiceLabelsByLabelvaluesAndServiceID(serviceID, amp)
|
||||
err = db.GetManager().TenantServiceLabelDaoTransactions(tx).
|
||||
DELTenantServiceLabelsByLabelvaluesAndServiceID(serviceID, label.LabelValues)
|
||||
default:
|
||||
return db.GetManager().TenantServiceLabelDao().DelTenantServiceLabels(serviceID, kind, amp)
|
||||
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
|
||||
|
@ -803,6 +803,10 @@ type AddNodeLabelStruct struct {
|
||||
}
|
||||
}
|
||||
|
||||
type LabelsStruct struct {
|
||||
Labels []LabelStruct `json:"labels"`
|
||||
}
|
||||
|
||||
// LabelStruct holds info for adding, updating or deleting label
|
||||
type LabelStruct struct {
|
||||
LabelKey string `json:"label_key" validate:"label_key|required"`
|
||||
|
@ -245,7 +245,9 @@ type TenantServiceLabelDao interface {
|
||||
GetTenantServiceAffinityLabel(serviceID string) ([]*model.TenantServiceLable, error)
|
||||
GetTenantServiceTypeLabel(serviceID string) (*model.TenantServiceLable, error)
|
||||
DELTenantServiceLabelsByLabelvaluesAndServiceID(serviceID string, labelValues []string) error
|
||||
DelTenantServiceLabelsByLabelValuesAndServiceID(serviceID string) error
|
||||
DelTenantServiceLabels(serviceID string, labelKey string, labelValues []string) error
|
||||
DelTenantServiceLabelsByKey(serviceID string, labelKey string) error
|
||||
}
|
||||
|
||||
//LocalSchedulerDao 本地调度信息
|
||||
|
@ -1272,6 +1272,15 @@ func (t *ServiceLabelDaoImpl) DELTenantServiceLabelsByLabelvaluesAndServiceID(se
|
||||
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
|
||||
func (t *ServiceLabelDaoImpl) DelTenantServiceLabels(serviceID string, labelKey string, labelValues []string) error {
|
||||
var label model.TenantServiceLable
|
||||
@ -1281,3 +1290,12 @@ func (t *ServiceLabelDaoImpl) DelTenantServiceLabels(serviceID string, labelKey
|
||||
}
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user