Maor Lipchuk has uploaded a new change for review.

Change subject: core: Introduce TryBackToCinderSnapshotCommand.
......................................................................

core: Introduce TryBackToCinderSnapshotCommand.

Introduce preview command for single Cinder disk.

Change-Id: I532fed7fe11a033c8b86251e5ad8b73230801c2d
Bug-Url: https://bugzilla.redhat.com/1185826
Signed-off-by: Maor Lipchuk <mlipc...@redhat.com>
---
A 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/storage/TryBackToCinderSnapshotCommand.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java
2 files changed, 136 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/62/42062/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/storage/TryBackToCinderSnapshotCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/storage/TryBackToCinderSnapshotCommand.java
new file mode 100644
index 0000000..0f07271
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/storage/TryBackToCinderSnapshotCommand.java
@@ -0,0 +1,135 @@
+package org.ovirt.engine.core.bll.storage;
+
+import org.ovirt.engine.core.bll.ImagesHandler;
+import org.ovirt.engine.core.bll.InternalCommandAttribute;
+import org.ovirt.engine.core.bll.NonTransactiveCommandAttribute;
+import org.ovirt.engine.core.bll.TryBackToSnapshotCommand;
+import org.ovirt.engine.core.bll.context.CommandContext;
+import org.ovirt.engine.core.bll.tasks.interfaces.CommandCallback;
+import org.ovirt.engine.core.common.action.ImagesContainterParametersBase;
+import org.ovirt.engine.core.common.businessentities.Snapshot;
+import org.ovirt.engine.core.common.businessentities.storage.CinderDisk;
+import org.ovirt.engine.core.common.businessentities.storage.DiskImage;
+import org.ovirt.engine.core.common.businessentities.storage.ImageStatus;
+import 
org.ovirt.engine.core.common.businessentities.storage.VolumeClassification;
+import org.ovirt.engine.core.compat.Guid;
+import org.ovirt.engine.core.utils.transaction.TransactionMethod;
+import org.ovirt.engine.core.utils.transaction.TransactionSupport;
+
+@InternalCommandAttribute
+@NonTransactiveCommandAttribute
+public class TryBackToCinderSnapshotCommand<T extends 
ImagesContainterParametersBase> extends TryBackToSnapshotCommand<T> {
+
+    private CinderDisk oldActiveDisk;
+    private CinderDisk previewedSnapshot;
+
+    public TryBackToCinderSnapshotCommand(T parameters) {
+        this(parameters, null);
+    }
+
+    public TryBackToCinderSnapshotCommand(T parameters, CommandContext 
commandContext) {
+        super(parameters, commandContext);
+    }
+
+    @Override
+    protected void executeCommand() {
+        final CinderDisk newCinderVolume = createVolumeFromSnapshotInCinder();
+        TransactionSupport.executeInNewTransaction(new 
TransactionMethod<Void>() {
+            @Override
+            public Void runInTransaction() {
+                processOldImageFromDb();
+                addDiskImageToDb(newCinderVolume, getCompensationContext(), 
Boolean.TRUE);
+                setSucceeded(true);
+                return null;
+            }
+        });
+
+        getReturnValue().setActionReturnValue(newCinderVolume.getImageId());
+        persistCommand(getParameters().getParentCommand(), true);
+        setSucceeded(true);
+        return;
+    }
+
+    /**
+     * Update the old image that represents the disk of the command's image to 
be in the given active state.
+     *
+     * @param snapshotType
+     *            The type of snapshot to look for the same image in.
+     * @param active
+     *            The active state.
+     */
+    protected void updateOldImageActive(Snapshot.SnapshotType snapshotType, 
boolean active) {
+        Guid oldImageId = findImageForSameDrive(snapshotType);
+        if (oldImageId == null) {
+            log.error("Can't find image to update to active '{}', snapshot 
type '{}', original image id '{}'",
+                    active,
+                    snapshotType,
+                    getImageId());
+            return;
+        }
+
+        DiskImage oldImage = getDiskImageDao().getSnapshotById(oldImageId);
+        oldImage.setActive(active);
+        getImageDao().update(oldImage.getImage());
+    }
+
+    private CinderDisk createVolumeFromSnapshotInCinder() {
+        Guid newVolumeId = cinderCloneDiskFromSnapshot();
+        CinderDisk clonedDiskFromSnapshot = 
initializeNewCinderVolumeDisk(newVolumeId);
+
+        // Setting this for the callback from coco.
+        getParameters().setDestinationImageId(newVolumeId);
+        return clonedDiskFromSnapshot;
+    }
+
+    private CinderDisk initializeNewCinderVolumeDisk(Guid newVolumeId) {
+        CinderDisk clonedDiskFromSnapshot = (CinderDisk) 
getDiskDao().get(getParameters().getImageId());
+        
clonedDiskFromSnapshot.setParentId(clonedDiskFromSnapshot.getImageId());
+        clonedDiskFromSnapshot.setImageId(newVolumeId);
+        clonedDiskFromSnapshot.setActive(true);
+        
clonedDiskFromSnapshot.setVolumeClassification(VolumeClassification.Volume);
+        return clonedDiskFromSnapshot;
+    }
+
+    private Guid cinderCloneDiskFromSnapshot() {
+        CinderDisk activeCinderVolume = getOldActiveDisk();
+        String newVolumeId = 
getCinderBroker().cloneVolumeFromSnapshot(activeCinderVolume, getImageId());
+        return Guid.createGuidFromString(newVolumeId);
+    }
+
+    protected CinderDisk getOldActiveDisk() {
+        if (oldActiveDisk == null) {
+            oldActiveDisk = (CinderDisk) 
getDiskDao().get(getParameters().getContainerId());
+        }
+        return oldActiveDisk;
+    }
+
+    @Override
+    public Guid getStorageDomainId() {
+        return getOldActiveDisk().getStorageIds().get(0);
+    }
+
+    @Override
+    protected void lockImage() {
+        ImagesHandler.updateImageStatus(getParameters().getImageId(), 
ImageStatus.LOCKED);
+    }
+
+    @Override
+    public CommandCallback getCallback() {
+        return new CloneCinderDisksCommandCallback<>();
+    }
+
+    @Override
+    protected void endSuccessfully() {
+        if (!getParameters().isLeaveLocked()) {
+            getDestinationDiskImage().setImageStatus(ImageStatus.OK);
+            getImageDao().update(getDestinationDiskImage().getImage());
+        }
+        if (!getParameters().isParentHasTasks()) {
+            getBackend().endAction(getParameters().getParentCommand(),
+                    getParameters().getParentParameters(),
+                    
getContext().clone().withoutCompensationContext().withoutExecutionContext().withoutLock());
+        }
+        setSucceeded(true);
+    }
+}
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java
index c143803..28738d7 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java
@@ -457,6 +457,7 @@
     CreateCinderSnapshot(3207, ActionGroup.CONFIGURE_VM_STORAGE, false, 
QuotaDependency.STORAGE),
     RemoveCinderSnapshotDisk(3208, ActionGroup.CONFIGURE_VM_STORAGE, false, 
QuotaDependency.STORAGE),
     TryBackToAllCinderSnapshots(3209, ActionGroup.CONFIGURE_VM_STORAGE, false, 
QuotaDependency.STORAGE),
+    TryBackToCinderSnapshot(3210, ActionGroup.CONFIGURE_VM_STORAGE, false, 
QuotaDependency.STORAGE),
 
     // Host Devices
     RefreshHostDevices(4000, ActionGroup.MANIPULATE_HOST, false, 
QuotaDependency.NONE),


-- 
To view, visit https://gerrit.ovirt.org/42062
To unsubscribe, visit https://gerrit.ovirt.org/settings

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

Reply via email to