From 4d4d062e73d6db8bc3a572d0851f5492a8ab872e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E9=9B=A8?= Date: Mon, 18 Nov 2019 10:41:19 +0800 Subject: [PATCH] fix(region): Set name and status when creating and syncing snapshotpolicycache That snapshotpolicycaches has same name is ok. Set name and status directly when creating and syncing snapshotpolicycache. --- pkg/compute/models/snapshotpolicy.go | 44 +++++++++++++++-------- pkg/compute/models/snapshotpolicycache.go | 21 ++--------- 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/pkg/compute/models/snapshotpolicy.go b/pkg/compute/models/snapshotpolicy.go index 18aa85536d..b0a0f9f305 100644 --- a/pkg/compute/models/snapshotpolicy.go +++ b/pkg/compute/models/snapshotpolicy.go @@ -379,8 +379,10 @@ func (manager *SSnapshotPolicyManager) SyncSnapshotPolicies(ctx context.Context, // start sync // add forsnapshotpolicy and cache - // delete forsnapshotpolicy cache + // delete for snapshotpolicy cache added := make([]cloudprovider.ICloudSnapshotPolicy, 0, 1) + commonext := make([]cloudprovider.ICloudSnapshotPolicy, 0, 1) + commondb := make([]*SSnapshotPolicyCache, 0, 1) removed := make([]*SSnapshotPolicyCache, 0, 1) for _, cloudSP := range cloudSPs { spCache, ok := spCacheSet[cloudSP.GetGlobalId()] @@ -392,7 +394,15 @@ func (manager *SSnapshotPolicyManager) SyncSnapshotPolicies(ctx context.Context, if !snapshotPolicy.Equals(cloudSP) { removed = append(removed, spCache) added = append(added, cloudSP) + } else { + commondb = append(commondb, spCache) + commonext = append(commonext, cloudSP) } + delete(spCacheSet, cloudSP.GetGlobalId()) + } + + for _, v := range spCacheSet { + removed = append(removed, v) } for i := range removed { @@ -412,6 +422,23 @@ func (manager *SSnapshotPolicyManager) SyncSnapshotPolicies(ctx context.Context, syncResult.Add() } } + + for i := range commondb { + _, err = db.Update(commondb[i], func() error { + commondb[i].Status = api.SNAPSHOT_POLICY_CACHE_STATUS_READY + if len(commonext[i].GetName()) == 0 { + commondb[i].Name = commonext[i].GetId() + } else { + commondb[i].Name = commonext[i].GetName() + } + return nil + }) + if err != nil { + syncResult.UpdateError(err) + } else { + syncResult.Update() + } + } return syncResult } @@ -452,7 +479,7 @@ func (manager *SSnapshotPolicyManager) newFromCloudSnapshotPolicy( // add cache _, err = SnapshotPolicyCacheManager.NewCacheWithExternalId(ctx, userCred, snapshotPolicy.GetId(), - ext.GetGlobalId(), region.GetId(), provider.GetId()) + ext.GetGlobalId(), region.GetId(), provider.GetId(), ext.GetName()) if err != nil { //snapshotpolicy has been exist so that created is successful although cache created is fail. // disk will be sync aftersnapshotpolicy sync, cache must be right so that this sync is fail @@ -496,19 +523,6 @@ func (manager *SSnapshotPolicyManager) getProviderSnapshotPolicies(region *SClou return snapshotPolicies, nil } -func (sp *SSnapshotPolicy) syncRemoveCloudSnapshot(ctx context.Context, userCred mcclient.TokenCredential) error { - lockman.LockObject(ctx, sp) - defer lockman.ReleaseObject(ctx, sp) - - err := sp.ValidateDeleteCondition(ctx) - if err != nil { - err = sp.SetStatus(userCred, api.SNAPSHOT_POLICY_UNKNOWN, "sync to delete") - } else { - err = sp.RealDelete(ctx, userCred) - } - return err -} - func (sp *SSnapshotPolicy) Delete(ctx context.Context, userCred mcclient.TokenCredential) error { return nil } diff --git a/pkg/compute/models/snapshotpolicycache.go b/pkg/compute/models/snapshotpolicycache.go index 04a3880229..dc0918774f 100644 --- a/pkg/compute/models/snapshotpolicycache.go +++ b/pkg/compute/models/snapshotpolicycache.go @@ -163,12 +163,13 @@ func (spcm *SSnapshotPolicyCacheManager) NewCache(ctx context.Context, userCred } func (spcm *SSnapshotPolicyCacheManager) NewCacheWithExternalId(ctx context.Context, userCred mcclient.TokenCredential, - snapshotPolicyId, externalId, regionId, providerId string) (*SSnapshotPolicyCache, error) { + snapshotPolicyId, externalId, regionId, providerId string, name string) (*SSnapshotPolicyCache, error) { snapshotPolicyCache := NewSSnapshotPolicyCache(snapshotPolicyId, regionId, externalId) snapshotPolicyCache.ManagerId = providerId snapshotPolicyCache.Status = api.SNAPSHOT_POLICY_CACHE_STATUS_READY + snapshotPolicyCache.Name = name // should have lock if err := spcm.TableSpec().Insert(&snapshotPolicyCache); err != nil { return nil, errors.Wrapf(err, "insert snapshotpolicycache failed") @@ -197,23 +198,6 @@ func (spcm *SSnapshotPolicyCacheManager) Register(ctx context.Context, userCred return spcm.NewCache(ctx, userCred, snapshotPolicyId, regionId, providerId) } -func (spcm *SSnapshotPolicyCacheManager) RegisterWithExternalID(ctx context.Context, userCred mcclient.TokenCredential, - snapshotPolicyId, externalId, regionId, providerId string) (*SSnapshotPolicyCache, error) { - - snapshotPolicyCache, err := spcm.FetchSnapshotPolicyCache(snapshotPolicyId, regionId, providerId) - // error - if err != nil { - return nil, err - } - - // no cache - if snapshotPolicyCache != nil { - return snapshotPolicyCache, nil - } - - return spcm.NewCacheWithExternalId(ctx, userCred, snapshotPolicyId, externalId, regionId, providerId) -} - // ==================================================== fetch ========================================================= func (spcm *SSnapshotPolicyCacheManager) FetchSnapshotpolicyCaheById(cacheId string) (*SSnapshotPolicyCache, error) { @@ -366,6 +350,7 @@ func (spc *SSnapshotPolicyCache) CreateCloudSnapshotPolicy() error { return errors.Wrap(err, "createsnapshotpolicy failed") } spc.ExternalId = externalId + spc.Name = snapshotPolicy.Name iPolicy, err := iregion.GetISnapshotPolicyById(externalId) if err != nil {