add delete services interface

This commit is contained in:
yangk 2021-06-19 19:41:28 +08:00
parent 98779a9034
commit 0359b86178
5 changed files with 94 additions and 5 deletions

View File

@ -116,7 +116,7 @@ func (a *ApplicationController) SyncComponents(w http.ResponseWriter, r *http.Re
if !httputil.ValidatorRequestStructAndErrorResponse(r, w, &syncComponentReq, nil) {
return
}
err := handler.GetApplicationHandler().SyncComponents(app, syncComponentReq.Components)
err := handler.GetApplicationHandler().SyncComponents(app, syncComponentReq.Components, syncComponentReq.DeleteComponentIDs)
if err != nil {
httputil.ReturnBcodeError(r, w, err)
return

View File

@ -42,7 +42,7 @@ type ApplicationHandler interface {
DeleteConfigGroup(appID, configGroupName string) error
ListConfigGroups(appID string, page, pageSize int) (*model.ListApplicationConfigGroupResp, error)
SyncComponents(app *dbmodel.Application, components []*model.Component) error
SyncComponents(app *dbmodel.Application, components []*model.Component, deleteComponentIDs []string) error
SyncComponentConfigGroupRels(tx *gorm.DB, app *dbmodel.Application, components []*model.Component) error
SyncAppConfigGroups(app *dbmodel.Application, appConfigGroups []model.AppConfigGroup) error
}
@ -292,7 +292,7 @@ func (a *ApplicationAction) BatchBindService(appID string, req model.BindService
}
// SyncComponents -
func (a *ApplicationAction) SyncComponents(app *dbmodel.Application, components []*model.Component) error {
func (a *ApplicationAction) SyncComponents(app *dbmodel.Application, components []*model.Component, deleteComponentIDs []string) error {
return db.GetManager().DB().Transaction(func(tx *gorm.DB) error {
if err := GetServiceManager().SyncComponentBase(tx, app, components); err != nil {
return err
@ -336,6 +336,78 @@ func (a *ApplicationAction) SyncComponents(app *dbmodel.Application, components
if err := GetServiceManager().SyncComponentLabels(tx, components); err != nil {
return err
}
return GetServiceManager().SyncComponentScaleRules(tx, components)
if err := GetServiceManager().SyncComponentScaleRules(tx, components); err != nil {
return err
}
if len(deleteComponentIDs) != 0 {
return a.deleteByComponentIDs(tx, app, deleteComponentIDs)
}
return nil
})
}
func (a *ApplicationAction) deleteByComponentIDs(tx *gorm.DB, app *dbmodel.Application, componentIDs []string) error {
if err := db.GetManager().TenantServiceDaoTransactions(tx).DeleteByComponentIDs(app.TenantID, app.AppID, componentIDs); err != nil {
return err
}
if err := db.GetManager().HTTPRuleDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
if err := db.GetManager().TCPRuleDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
if err := db.GetManager().TenantServiceMonitorDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
if err := db.GetManager().TenantServicesStreamPluginPortDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
if err := db.GetManager().TenantPluginVersionConfigDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
if err := db.GetManager().TenantServicePluginRelationDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
if err := db.GetManager().TenantPluginVersionENVDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
if err := db.GetManager().TenantServicesPortDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
if err := db.GetManager().TenantServiceRelationDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
if err := db.GetManager().TenantServiceEnvVarDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
if err := db.GetManager().TenantServiceMountRelationDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
if err := db.GetManager().TenantServiceVolumeDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
if err := db.GetManager().TenantServiceConfigFileDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
if err := db.GetManager().ServiceProbeDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
if err := db.GetManager().AppConfigGroupServiceDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
if err := db.GetManager().TenantServiceLabelDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
autoScaleRules, err := db.GetManager().TenantServceAutoscalerRulesDaoTransactions(tx).ListByComponentIDs(componentIDs)
if err != nil {
return err
}
var autoScaleRuleIDs []string
for _, rule := range autoScaleRules {
autoScaleRuleIDs = append(autoScaleRuleIDs, rule.RuleID)
}
if err = db.GetManager().TenantServceAutoscalerRulesDaoTransactions(tx).DeleteByComponentIDs(componentIDs); err != nil {
return err
}
return db.GetManager().TenantServceAutoscalerRuleMetricsDaoTransactions(tx).DeleteByRuleIDs(autoScaleRuleIDs)
}

View File

@ -255,4 +255,5 @@ type Component struct {
// SyncComponentReq -
type SyncComponentReq struct {
Components []*Component `json:"components"`
DeleteComponentIDs []string `json:"delete_componentIDs"`
}

View File

@ -339,6 +339,7 @@ type TenantServiceVolumeDao interface {
DelShareableBySID(sid string) error
ListVolumesByComponentIDs(componentIDs []string) ([]*model.TenantServiceVolume, error)
DeleteByVolumeIDs(volumeIDs []uint) error
DeleteByComponentIDs(componentIDs []string) error
CreateOrUpdateVolumesInBatch(volumes []*model.TenantServiceVolume) error
}
@ -568,6 +569,7 @@ type TenantServceAutoscalerRulesDao interface {
GetByRuleID(ruleID string) (*model.TenantServiceAutoscalerRules, error)
ListByServiceID(serviceID string) ([]*model.TenantServiceAutoscalerRules, error)
ListEnableOnesByServiceID(serviceID string) ([]*model.TenantServiceAutoscalerRules, error)
ListByComponentIDs(componentIDs []string) ([]*model.TenantServiceAutoscalerRules, error)
DeleteByComponentIDs(componentIDs []string) error
CreateOrUpdateScaleRulesInBatch(rules []*model.TenantServiceAutoscalerRules) error
}

View File

@ -1247,6 +1247,11 @@ func (t *TenantServiceVolumeDaoImpl) DeleteByVolumeIDs(volumeIDs []uint) error {
return t.DB.Where("ID in (?)", volumeIDs).Delete(&model.TenantServiceVolume{}).Error
}
//DeleteByComponentIDs -
func (t *TenantServiceVolumeDaoImpl) DeleteByComponentIDs(componentIDs []string) error {
return t.DB.Where("service_id in (?)", componentIDs).Delete(&model.TenantServiceVolume{}).Error
}
// CreateOrUpdateVolumesInBatch -
func (t *TenantServiceVolumeDaoImpl) CreateOrUpdateVolumesInBatch(volumes []*model.TenantServiceVolume) error {
var objects []interface{}
@ -1824,6 +1829,15 @@ func (t *TenantServceAutoscalerRulesDaoImpl) ListEnableOnesByServiceID(serviceID
return rules, nil
}
// ListByComponentIDs -
func (t *TenantServceAutoscalerRulesDaoImpl) ListByComponentIDs(componentIDs []string) ([]*model.TenantServiceAutoscalerRules, error) {
var rules []*model.TenantServiceAutoscalerRules
if err := t.DB.Where("service_id in (?)", componentIDs).Find(&rules).Error; err != nil {
return nil, err
}
return rules, nil
}
// DeleteByComponentIDs deletes rule based on componentIDs
func (t *TenantServceAutoscalerRulesDaoImpl) DeleteByComponentIDs(componentIDs []string) error {
return t.DB.Where("service_id in (?)", componentIDs).Delete(&model.TenantServiceAutoscalerRules{}).Error