Merge pull request #3746 from rainzm/bugfix/snapshotpolicycache

fix(region): Set name and status when creating and syncing snapshotpolicycache
This commit is contained in:
yunion-ci-robot
2019-11-18 16:15:23 +08:00
committed by GitHub
2 changed files with 32 additions and 33 deletions
+29 -15
View File
@@ -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
}
+3 -18
View File
@@ -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 {