From cd47b45f571af4a5beb89b708c97edf84d2ab17a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Wed, 28 Feb 2024 16:49:48 +0800 Subject: [PATCH] fix(region): remove useless code (#19567) --- pkg/apis/compute/loadbalancerbackendgroup.go | 1 + pkg/compute/models/buckets.go | 6 +-- pkg/compute/models/disks.go | 8 +--- pkg/compute/models/elasticcache_accounts.go | 4 -- pkg/compute/models/elasticcache_acls.go | 4 -- pkg/compute/models/elasticcache_backups.go | 6 +-- pkg/compute/models/elasticcache_instances.go | 4 -- pkg/compute/models/elasticcache_parameters.go | 4 -- pkg/compute/models/guest_actions.go | 2 +- pkg/compute/models/guests.go | 4 -- pkg/compute/models/hosts.go | 6 +-- .../models/loadbalancerbackendgroups.go | 47 ++++++++++--------- pkg/keystone/models/identity_provider.go | 2 +- 13 files changed, 34 insertions(+), 64 deletions(-) diff --git a/pkg/apis/compute/loadbalancerbackendgroup.go b/pkg/apis/compute/loadbalancerbackendgroup.go index 8eff69921b..2c95b052bd 100644 --- a/pkg/apis/compute/loadbalancerbackendgroup.go +++ b/pkg/apis/compute/loadbalancerbackendgroup.go @@ -24,6 +24,7 @@ type LoadbalancerBackendGroupDetails struct { LbListenerCount int `json:"lb_listener_count"` + IsDefault bool `json:"is_default"` ProjectId string `json:"tenant_id"` } diff --git a/pkg/compute/models/buckets.go b/pkg/compute/models/buckets.go index 7dd461d236..e3beb7f044 100644 --- a/pkg/compute/models/buckets.go +++ b/pkg/compute/models/buckets.go @@ -1138,10 +1138,6 @@ func (bucket *SBucket) PerformSync( return nil, nil } -func (bucket *SBucket) ValidatePurgeCondition(ctx context.Context) error { - return bucket.SSharableVirtualResourceBase.ValidateDeleteCondition(ctx, nil) -} - func (bucket *SBucket) ValidateDeleteCondition(ctx context.Context, info jsonutils.JSONObject) error { if bucket.Status == api.BUCKET_STATUS_UNKNOWN { return bucket.SSharableVirtualResourceBase.ValidateDeleteCondition(ctx, nil) @@ -1149,7 +1145,7 @@ func (bucket *SBucket) ValidateDeleteCondition(ctx context.Context, info jsonuti if bucket.ObjectCnt > 0 { return httperrors.NewNotEmptyError("Buckets that are not empty do not support this operation") } - return bucket.ValidatePurgeCondition(ctx) + return bucket.SSharableVirtualResourceBase.ValidateDeleteCondition(ctx, info) } // 获取对象或bucket的ACL diff --git a/pkg/compute/models/disks.go b/pkg/compute/models/disks.go index f7f0cdccb3..46ef326e54 100644 --- a/pkg/compute/models/disks.go +++ b/pkg/compute/models/disks.go @@ -1225,10 +1225,6 @@ func (self *SDisk) ValidateDeleteCondition(ctx context.Context, info api.DiskDet return self.SVirtualResourceBase.ValidateDeleteCondition(ctx, nil) } -func (self *SDisk) ValidatePurgeCondition(ctx context.Context) error { - return self.validateDeleteCondition(ctx, true) -} - func (self *SDisk) validateDeleteCondition(ctx context.Context, isPurge bool) error { if !isPurge { storage, _ := self.GetStorage() @@ -1621,7 +1617,7 @@ func (self *SDisk) syncRemoveCloudDisk(ctx context.Context, userCred mcclient.To return err } - err = self.ValidatePurgeCondition(ctx) + err = self.validateDeleteCondition(ctx, true) if err != nil { self.SetStatus(ctx, userCred, api.DISK_UNKNOWN, "missing original disk after sync") return err @@ -2219,7 +2215,7 @@ func (self *SDisk) PerformSyncstatus(ctx context.Context, userCred mcclient.Toke } func (self *SDisk) PerformPurge(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { - err := self.ValidatePurgeCondition(ctx) + err := self.validateDeleteCondition(ctx, true) if err != nil { return nil, err } diff --git a/pkg/compute/models/elasticcache_accounts.go b/pkg/compute/models/elasticcache_accounts.go index a93c229197..221085d60a 100644 --- a/pkg/compute/models/elasticcache_accounts.go +++ b/pkg/compute/models/elasticcache_accounts.go @@ -508,10 +508,6 @@ func (self *SElasticcacheAccount) GetDetailsLoginInfo(ctx context.Context, userC return ret, nil } -func (self *SElasticcacheAccount) ValidatePurgeCondition(ctx context.Context) error { - return nil -} - // 弹性缓存账号列表 func (manager *SElasticcacheAccountManager) ListItemFilter( ctx context.Context, diff --git a/pkg/compute/models/elasticcache_acls.go b/pkg/compute/models/elasticcache_acls.go index 457c3538e5..7205da15ba 100644 --- a/pkg/compute/models/elasticcache_acls.go +++ b/pkg/compute/models/elasticcache_acls.go @@ -306,10 +306,6 @@ func (self *SElasticcacheAcl) ValidateDeleteCondition(ctx context.Context, info return nil } -func (self *SElasticcacheAcl) ValidatePurgeCondition(ctx context.Context) error { - return nil -} - func (self *SElasticcacheAcl) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error { self.SetStatus(ctx, userCred, api.ELASTIC_CACHE_ACL_STATUS_DELETING, "") return self.StartDeleteElasticcacheAclTask(ctx, userCred, jsonutils.NewDict(), "") diff --git a/pkg/compute/models/elasticcache_backups.go b/pkg/compute/models/elasticcache_backups.go index 29b4b01829..75ae48e96d 100644 --- a/pkg/compute/models/elasticcache_backups.go +++ b/pkg/compute/models/elasticcache_backups.go @@ -333,11 +333,7 @@ func (self *SElasticcacheBackup) ValidateDeleteCondition(ctx context.Context, in return httperrors.NewUnsupportOperationError("unsupport delete %s backups", api.CLOUD_PROVIDER_ALIYUN) } - return self.ValidatePurgeCondition(ctx) -} - -func (self *SElasticcacheBackup) ValidatePurgeCondition(ctx context.Context) error { - return nil + return self.SStatusStandaloneResourceBase.ValidateDeleteCondition(ctx, info) } // 弹性缓存备份列表 diff --git a/pkg/compute/models/elasticcache_instances.go b/pkg/compute/models/elasticcache_instances.go index ac74eb5af7..cad2f66ad8 100644 --- a/pkg/compute/models/elasticcache_instances.go +++ b/pkg/compute/models/elasticcache_instances.go @@ -998,10 +998,6 @@ func (self *SElasticcache) ValidateDeleteCondition(ctx context.Context, info jso return httperrors.NewInvalidStatusError("Elastic cache is not expired, cannot delete") } - return self.ValidatePurgeCondition(ctx) -} - -func (self *SElasticcache) ValidatePurgeCondition(ctx context.Context) error { return self.SVirtualResourceBase.ValidateDeleteCondition(ctx, nil) } diff --git a/pkg/compute/models/elasticcache_parameters.go b/pkg/compute/models/elasticcache_parameters.go index c56a279c24..133cad83a0 100644 --- a/pkg/compute/models/elasticcache_parameters.go +++ b/pkg/compute/models/elasticcache_parameters.go @@ -246,10 +246,6 @@ func (self *SElasticcacheParameter) StartUpdateElasticcacheParameterTask(ctx con return nil } -func (self *SElasticcacheParameter) ValidatePurgeCondition(ctx context.Context) error { - return nil -} - // 列出弹性缓存参数 func (manager *SElasticcacheParameterManager) ListItemFilter( ctx context.Context, diff --git a/pkg/compute/models/guest_actions.go b/pkg/compute/models/guest_actions.go index 79f1433709..5fbf29523b 100644 --- a/pkg/compute/models/guest_actions.go +++ b/pkg/compute/models/guest_actions.go @@ -1548,7 +1548,7 @@ func (self *SGuest) StartDeleteGuestTask( } func (self *SGuest) PerformPurge(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { - err := self.ValidatePurgeCondition(ctx) + err := self.validateDeleteCondition(ctx, true) if err != nil { return nil, err } diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index b989b28ead..7216cdfbc6 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -924,10 +924,6 @@ func (guest *SGuest) validateDeleteCondition(ctx context.Context, isPurge bool) return guest.SVirtualResourceBase.ValidateDeleteCondition(ctx, nil) } -func (guest *SGuest) ValidatePurgeCondition(ctx context.Context) error { - return guest.validateDeleteCondition(ctx, true) -} - func (guest *SGuest) ValidateDeleteCondition(ctx context.Context, info *api.ServerDetails) error { if gotypes.IsNil(info) { info = &api.ServerDetails{} diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 9918b61dd1..cf0998d938 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -774,10 +774,6 @@ func (hh *SHost) ValidateDeleteCondition(ctx context.Context, info api.HostDetai return hh.SEnabledStatusInfrasResourceBase.ValidateDeleteCondition(ctx, nil) } -func (hh *SHost) ValidatePurgeCondition(ctx context.Context) error { - return hh.validateDeleteCondition(ctx, true) -} - func (hh *SHost) validateDeleteCondition(ctx context.Context, purge bool) error { if !purge && hh.IsBaremetal && hh.HostType != api.HOST_TYPE_BAREMETAL { return httperrors.NewInvalidStatusError("Host is a converted baremetal, should be unconverted before delete") @@ -1890,7 +1886,7 @@ func (hh *SHost) syncRemoveCloudHost(ctx context.Context, userCred mcclient.Toke lockman.LockObject(ctx, hh) defer lockman.ReleaseObject(ctx, hh) - err := hh.ValidatePurgeCondition(ctx) + err := hh.validateDeleteCondition(ctx, true) if err != nil { err = hh.purge(ctx, userCred) if err != nil { diff --git a/pkg/compute/models/loadbalancerbackendgroups.go b/pkg/compute/models/loadbalancerbackendgroups.go index f97c9e0b7f..16b4ff1c2c 100644 --- a/pkg/compute/models/loadbalancerbackendgroups.go +++ b/pkg/compute/models/loadbalancerbackendgroups.go @@ -22,8 +22,10 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" "yunion.io/x/pkg/errors" + "yunion.io/x/pkg/gotypes" "yunion.io/x/pkg/util/compare" "yunion.io/x/pkg/util/rbacscope" + "yunion.io/x/pkg/utils" "yunion.io/x/sqlchemy" api "yunion.io/x/onecloud/pkg/apis/compute" @@ -405,31 +407,21 @@ func (lbbg *SLoadbalancerBackendGroup) isDefault(ctx context.Context) (bool, err return count > 0, nil } -func (lbbg *SLoadbalancerBackendGroup) ValidateDeleteCondition(ctx context.Context, info jsonutils.JSONObject) error { - ok, err := lbbg.isDefault(ctx) - if err != nil { - return httperrors.NewInternalServerError("get isDefault fail %s", err.Error()) +func (lbbg *SLoadbalancerBackendGroup) ValidateDeleteCondition(ctx context.Context, info *api.LoadbalancerBackendGroupDetails) error { + if gotypes.IsNil(info) { + info = &api.LoadbalancerBackendGroupDetails{} + info.IsDefault, _ = lbbg.isDefault(ctx) + info.LbListenerCount, _ = lbbg.GetListenerCount() } - if ok { + if info.IsDefault { return httperrors.NewResourceBusyError("backend group %s is default backend group", lbbg.Id) } - return lbbg.ValidatePurgeCondition(ctx) -} - -func (lbbg *SLoadbalancerBackendGroup) ValidatePurgeCondition(ctx context.Context) error { - mans := lbbg.getRefManagers() - for _, m := range mans { - n, err := lbbg.refCount(m) - if err != nil { - return httperrors.NewInternalServerError("get refCount fail %s", err.Error()) - } - if n > 0 { - return httperrors.NewResourceBusyError("backend group %s is still referred by %d %s", - lbbg.Id, n, m.KeywordPlural()) - } + if info.LbListenerCount > 0 { + return httperrors.NewResourceBusyError("backend group %s is still referred by %d %s", + lbbg.Id, info.LbListenerCount, LoadbalancerListenerManager.KeywordPlural()) } - return nil + return lbbg.SStatusStandaloneResourceBase.ValidateDeleteCondition(ctx, nil) } func (man *SLoadbalancerBackendGroupManager) FetchCustomizeColumns( @@ -446,6 +438,7 @@ func (man *SLoadbalancerBackendGroupManager) FetchCustomizeColumns( lbRows := man.SLoadbalancerResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList) lbIds := make([]string, len(objs)) + lbbgIds := make([]string, len(objs)) for i := range rows { rows[i] = api.LoadbalancerBackendGroupDetails{ StatusStandaloneResourceDetails: stdRows[i], @@ -453,6 +446,7 @@ func (man *SLoadbalancerBackendGroupManager) FetchCustomizeColumns( } lbbg := objs[i].(*SLoadbalancerBackendGroup) lbIds[i] = lbbg.LoadbalancerId + lbbgIds[i] = lbbg.Id } lbs := map[string]SLoadbalancer{} @@ -461,16 +455,23 @@ func (man *SLoadbalancerBackendGroupManager) FetchCustomizeColumns( return rows } + defaultLbgIds := []string{} virObjs := make([]interface{}, len(objs)) for i := range rows { if lb, ok := lbs[lbIds[i]]; ok { virObjs[i] = &lb rows[i].ProjectId = lb.ProjectId + if !utils.IsInStringArray(lb.BackendGroupId, defaultLbgIds) { + defaultLbgIds = append(defaultLbgIds, lb.BackendGroupId) + } } } + for i := range rows { + rows[i].IsDefault = utils.IsInStringArray(lbbgIds[i], defaultLbgIds) + } for i := range objs { - q := LoadbalancerListenerManager.Query().Equals("backend_group_id", objs[i].(*SLoadbalancerBackendGroup).GetId()) + q := LoadbalancerListenerManager.Query().Equals("backend_group_id", lbbgIds[i]) ownerId, queryScope, err, _ := db.FetchCheckQueryOwnerScope(ctx, userCred, query, LoadbalancerListenerManager, policy.PolicyActionList, true) if err != nil { log.Errorf("FetchCheckQueryOwnerScope error: %v", err) @@ -566,6 +567,10 @@ func (lbbg *SLoadbalancerBackendGroup) GetListener() *SLoadbalancerListener { return ret } +func (lbbg *SLoadbalancerBackendGroup) GetListenerCount() (int, error) { + return LoadbalancerListenerManager.Query().Equals("backend_group_id", lbbg.Id).CountWithError() +} + func (lbbg *SLoadbalancerBackendGroup) GetBackendsParams() ([]cloudprovider.SLoadbalancerBackend, error) { backends, err := lbbg.GetBackends() if err != nil { diff --git a/pkg/keystone/models/identity_provider.go b/pkg/keystone/models/identity_provider.go index 17d006a70b..ad42e564f5 100644 --- a/pkg/keystone/models/identity_provider.go +++ b/pkg/keystone/models/identity_provider.go @@ -896,7 +896,7 @@ func (self *SIdentityProvider) Purge(ctx context.Context, userCred mcclient.Toke err = domains[i].ValidateDeleteCondition(ctx, nil) if err != nil { db.OpsLog.LogEvent(&domains[i], db.ACT_DELETE_FAIL, err, userCred) - return errors.Wrap(err, "domain.ValidatePurgeCondition") + return errors.Wrap(err, "domain.ValidateDeleteCondition") } err = domains[i].UnlinkIdp(self.Id) if err != nil {