Merge pull request #14141 from swordqiu/hotfix/qj-fail-delete-storage-with-fake-deleted-snapshots

fix: fail to delete storages with fake-deleted snapshots
This commit is contained in:
Zexi Li
2022-04-26 02:25:41 +08:00
committed by GitHub
2 changed files with 31 additions and 7 deletions
+25 -4
View File
@@ -149,7 +149,28 @@ func (self *SStorage) StartStorageUpdateTask(ctx context.Context, userCred mccli
return nil
}
func (self *SStorage) getFakeDeletedSnapshots() ([]SSnapshot, error) {
q := SnapshotManager.Query().Equals("storage_id", self.Id).IsTrue("fake_deleted")
snapshots := make([]SSnapshot, 0)
err := db.FetchModelObjects(SnapshotManager, q, &snapshots)
if err != nil {
return nil, errors.Wrap(err, "FetchModelObjects")
}
return snapshots, nil
}
func (self *SStorage) Delete(ctx context.Context, userCred mcclient.TokenCredential) error {
// delete all hidden snapshots
fakeDeletedSnapshots, err := self.getFakeDeletedSnapshots()
if err != nil {
return errors.Wrap(err, "getFakeDeletedSnapshots")
}
for i := range fakeDeletedSnapshots {
err := fakeDeletedSnapshots[i].Delete(ctx, userCred)
if err != nil {
return errors.Wrap(err, "fakeDeletedSnapshots.Delete")
}
}
DeleteResourceJointSchedtags(self, ctx, userCred)
return self.SEnabledStatusInfrasResourceBase.Delete(ctx, userCred)
}
@@ -240,9 +261,9 @@ func (self *SStorage) ValidateDeleteCondition(ctx context.Context, info jsonutil
if cnt > 0 {
return httperrors.NewNotEmptyError("storage has disks")
}
cnt, err = self.GetSnapshotCount()
cnt, err = self.GetVisibleSnapshotCount()
if err != nil {
return httperrors.NewInternalServerError("GetSnapshotCount fail %s", err)
return httperrors.NewInternalServerError("GetVisibleSnapshotCount fail %s", err)
}
if cnt > 0 {
return httperrors.NewNotEmptyError("storage has snapshots")
@@ -359,8 +380,8 @@ func (self *SStorage) GetDisks() []SDisk {
return disks
}
func (self *SStorage) GetSnapshotCount() (int, error) {
return SnapshotManager.Query().Equals("storage_id", self.Id).CountWithError()
func (self *SStorage) GetVisibleSnapshotCount() (int, error) {
return SnapshotManager.Query().Equals("storage_id", self.Id).IsFalse("fake_deleted").CountWithError()
}
func (self *SStorage) IsLocal() bool {
+6 -3
View File
@@ -58,9 +58,12 @@ func (self *SBaseStorageDriver) ValidateSnapshotDelete(ctx context.Context, snap
}
if !snapshot.OutOfChain && snapshot.FakeDeleted {
_, err := models.SnapshotManager.GetConvertSnapshot(snapshot)
if err != nil {
return httperrors.NewBadRequestError("disk need at least one of snapshot as backing file")
disk, _ := snapshot.GetDisk()
if disk != nil {
_, err := models.SnapshotManager.GetConvertSnapshot(snapshot)
if err != nil {
return httperrors.NewBadRequestError("disk need at least one of snapshot as backing file")
}
}
}
return nil