Arik Hadas has uploaded a new change for review.

Change subject: core: fix removal of attached disks to stateless vm
......................................................................

core: fix removal of attached disks to stateless vm

Change-Id: I3a1d6eb8d2f4606622c7ed5c73370792406bb9b3
Bug-Url: https://bugzilla.redhat.com/1161204
Signed-off-by: Arik Hadas <aha...@redhat.com>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreStatelessVmCommand.java
1 file changed, 38 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/35/36035/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreStatelessVmCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreStatelessVmCommand.java
index 5061c71..4560f69 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreStatelessVmCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreStatelessVmCommand.java
@@ -1,8 +1,12 @@
 package org.ovirt.engine.core.bll;
 
+import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import org.ovirt.engine.core.bll.context.CommandContext;
+import org.ovirt.engine.core.common.action.AttachDetachVmDiskParameters;
 import org.ovirt.engine.core.common.action.RestoreAllSnapshotsParameters;
 import org.ovirt.engine.core.common.action.UpdateVmVersionParameters;
 import org.ovirt.engine.core.common.action.VdcActionType;
@@ -55,19 +59,48 @@
     }
 
     private boolean restoreInitialState() {
-        Guid snapshotId = getSnapshotDao().getId(getVmId(), 
SnapshotType.STATELESS);
-        if (snapshotId == null) {
+        Guid statelessSnapshotId = getSnapshotDao().getId(getVmId(), 
SnapshotType.STATELESS);
+        if (statelessSnapshotId == null) {
             return true;
         }
 
-        List<DiskImage> imagesList = 
getDiskImageDao().getAllSnapshotsForVmSnapshot(snapshotId);
-        if (imagesList == null || imagesList.isEmpty()) {
+        List<DiskImage> statelessSnapshotImages = 
getDiskImageDao().getAllSnapshotsForVmSnapshot(statelessSnapshotId);
+        if (statelessSnapshotImages == null || 
statelessSnapshotImages.isEmpty()) {
             return true;
+        }
+
+        List<DiskImage> imagesToRestore;
+
+        Guid activeSnapshotId = getSnapshotDao().getId(getVmId(), 
SnapshotType.ACTIVE);
+        List<DiskImage> activeSnapshotImages = 
getDiskImageDao().getAllSnapshotsForVmSnapshot(activeSnapshotId);
+        if (activeSnapshotImages == null || activeSnapshotImages.isEmpty()) {
+            imagesToRestore = statelessSnapshotImages;
+        } else {
+            Set<Guid> activeSnapshotDiskIds = new HashSet<>();
+            for (DiskImage diskImage : activeSnapshotImages) {
+                activeSnapshotDiskIds.add(diskImage.getId());
+            }
+
+            imagesToRestore = new ArrayList<>();
+            for (DiskImage diskImage : statelessSnapshotImages) {
+                if (activeSnapshotDiskIds.contains(diskImage.getId())) {
+                    imagesToRestore.add(diskImage);
+                } else {
+                    VdcReturnValueBase returnValue = 
runInternalActionWithTasksContext(
+                            VdcActionType.DetachDiskFromVm,
+                            new AttachDetachVmDiskParameters(
+                                    getVmId(), diskImage.getId(),false, 
false));
+
+                    if (!returnValue.getSucceeded()) {
+                        return false;
+                    }
+                }
+            }
         }
 
         // restore all snapshots
         return 
runInternalActionWithTasksContext(VdcActionType.RestoreAllSnapshots,
-                        buildRestoreAllSnapshotsParameters(imagesList),
+                        buildRestoreAllSnapshotsParameters(imagesToRestore),
                 getLock()).getSucceeded();
     }
 


-- 
To view, visit http://gerrit.ovirt.org/36035
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3a1d6eb8d2f4606622c7ed5c73370792406bb9b3
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Arik Hadas <aha...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to