Juan Hernandez has uploaded a new change for review.

Change subject: restapi: Allow override of allocation policy of imported disks
......................................................................

restapi: Allow override of allocation policy of imported disks

Currently the RESTAPI doesn't support changing the allocation policy of
disks imported from an export domain. This patch adds support for this,
so that the caller can specify for each disks if its format and
allocation policy. For example, to force one of the disks to be
preallocated the caller will be able to use a request like this:

  POST /storagedomains/{storagedomain:id}/vms/{vm:id}/import
  <action>
    <storage_domain>
      <name>mydata</name>
    </storage_domain>
    <cluster>
      <name>mycluster</name>
    </cluster>
    <vm>
      <disks>
        <disk id="...">
          <format>raw</format>
          <sparse>false</sparse>
        </disk>
      </disks>
      <snapshots>
        <collapse_snapshots>true</collapse_snapshots>
      </snapshots>
    </vm>
  </action>

This support can be used in the Python SDK without changing it, for
example:

  # Find the export domain:
  sd = api.storagedomains.get(name="myexport")

  # Find the VM to import:
  vm = sd.vms.get(name="myvm")

  # For each disk in the VM to be imported override the disk
  # format, so they will be preallocated:
  disks = vm.disks.list()
  preallocated_disks = params.Disks()
  for disk in disks:
    preallocated_disk = params.Disk(
      id=disk.get_id(),
      format="raw",
      sparse=False
    )
    preallocated_disks.add_disk(preallocated_disk)

  # Import it (note that this has to be combined with collapsing
  # snapshots, otherwise the new disk format will be ignored):
  action = params.Action(
    storage_domain=params.StorageDomain(
      name="mydata"
    ),
    cluster=params.Cluster(
      name="mycluster"
    )
    vm=params.VM(
      snapshots=params.Snapshots(
        collapse_snapshots=True
      ),
      disks=preallocated_disks
    )
  )
  vm.import_vm(action)

Change-Id: Idc9945968b7cd2b8a82d953f659967d72064d583
Bug-Url: https://bugzilla.redhat.com/998607
Signed-off-by: Juan Hernandez <[email protected]>
---
M 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendStorageDomainVmResource.java
1 file changed, 34 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/31/36131/1

diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendStorageDomainVmResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendStorageDomainVmResource.java
index cbe2397..7ef6f94 100644
--- 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendStorageDomainVmResource.java
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendStorageDomainVmResource.java
@@ -3,17 +3,24 @@
 import javax.ws.rs.core.Response;
 
 import org.ovirt.engine.api.model.Action;
+import org.ovirt.engine.api.model.Disk;
+import org.ovirt.engine.api.model.DiskFormat;
 import org.ovirt.engine.api.model.VM;
 import org.ovirt.engine.api.model.VMs;
 import org.ovirt.engine.api.resource.ActionResource;
 import org.ovirt.engine.api.resource.StorageDomainContentDisksResource;
 import org.ovirt.engine.api.resource.StorageDomainContentResource;
+import org.ovirt.engine.api.restapi.types.DiskMapper;
 import org.ovirt.engine.core.common.action.ImportVmParameters;
 import org.ovirt.engine.core.common.action.VdcActionType;
-import org.ovirt.engine.core.common.businessentities.Disk;
+import org.ovirt.engine.core.common.businessentities.DiskImage;
+import org.ovirt.engine.core.common.businessentities.VolumeFormat;
+import org.ovirt.engine.core.common.businessentities.VolumeType;
 import org.ovirt.engine.core.common.queries.IdQueryParameters;
 import org.ovirt.engine.core.common.queries.VdcQueryType;
 import org.ovirt.engine.core.compat.Guid;
+
+import java.util.Map;
 
 public class BackendStorageDomainVmResource
     extends AbstractBackendStorageDomainContentResource<VMs, VM, 
org.ovirt.engine.core.common.businessentities.VM>
@@ -73,6 +80,31 @@
             
params.setCopyCollapse(action.getVm().getSnapshots().isCollapseSnapshots());
         }
 
+        // The caller may override the format and allocation policy for each 
disk of the virtual machine:
+        if (action.isSetVm()) {
+            VM modelVm = action.getVm();
+            if (modelVm.isSetDisks()) {
+                Map<Guid, org.ovirt.engine.core.common.businessentities.Disk> 
entityDisks = getDiskMap();
+                for (Disk modelDisk : modelVm.getDisks().getDisks()) {
+                    if (modelDisk.isSetId()) {
+                        Guid modelDiskId = 
Guid.createGuidFromString(modelDisk.getId());
+                        DiskImage entityDisk = (DiskImage) 
entityDisks.get(modelDiskId);
+                        if (entityDisk != null) {
+                            if (modelDisk.isSetFormat()) {
+                                DiskFormat modelDiskFormat = 
DiskFormat.fromValue(modelDisk.getFormat());
+                                VolumeFormat entityDiskFormat = 
DiskMapper.map(modelDiskFormat, null);
+                                entityDisk.setvolumeFormat(entityDiskFormat);
+                            }
+                            if (modelDisk.isSetSparse()) {
+                                VolumeType entityDiskType = 
modelDisk.isSparse()? VolumeType.Sparse: VolumeType.Preallocated;
+                                entityDisk.setVolumeType(entityDiskType);
+                            }
+                        }
+                    }
+                }
+            }
+        }
+
         if (action.isSetClone()) {
             params.setImportAsNewEntity(action.isClone());
             if (action.isSetVm() && action.getVm().isSetName()) {
@@ -113,7 +145,7 @@
     }
 
     @Override
-    public java.util.Map<Guid, Disk> getDiskMap() {
+    public Map<Guid, org.ovirt.engine.core.common.businessentities.Disk> 
getDiskMap() {
         return getEntity().getDiskMap();
     }
 


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idc9945968b7cd2b8a82d953f659967d72064d583
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Juan Hernandez <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to