Omer Frenkel has uploaded a new change for review.

Change subject: core: use template version on add vm/pool
......................................................................

core: use template version on add vm/pool

When adding a stateless vm, or vm-pool,
if using the latest version of a template, we need to get latest version 
available and use
it.
http://www.ovirt.org/index.php?title=Features/Template_Versions

Bug-Url: https://bugzilla.redhat.com/show_bug.cgi?id=1037478

Change-Id: I2d1b3ee9cd64d73866762fbf5fe02a468cdd578e
Signed-off-by: Omer Frenkel <ofren...@redhat.com>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmCommand.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmFromTemplateCommand.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommonVmPoolWithVmsCommand.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/errors/VdcBllMessages.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VmTemplateDAO.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VmTemplateDAODbFacadeImpl.java
M backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties
M 
backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/VmTemplateDAOTest.java
M 
frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/AppErrors.java
M 
frontend/webadmin/modules/userportal-gwtp/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
M 
frontend/webadmin/modules/webadmin/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
11 files changed, 79 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/09/23609/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmCommand.java
index db7d0a0..75cfb95 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmCommand.java
@@ -111,7 +111,25 @@
         parameters.setVmId(getVmId());
         setStorageDomainId(getParameters().getStorageDomainId());
         if (parameters.getVmStaticData() != null) {
-            setVmTemplateId(parameters.getVmStaticData().getVmtGuid());
+            Guid templateIdToUse = 
getParameters().getVmStaticData().getVmtGuid();
+
+            if (parameters.getVmStaticData().isUseLatestVersion()) {
+                VmTemplate latest = 
getVmTemplateDAO().getTemplateWithLatestVersionInChain(templateIdToUse);
+
+                if (latest != null) {
+                    // if not using original template, need to override 
storage mappings
+                    // as it may have different set of disks
+                    if (!templateIdToUse.equals(latest.getId())) {
+                        getParameters().setDiskInfoDestinationMap(new 
HashMap<Guid, DiskImage>());
+                    }
+
+                    setVmTemplate(latest);
+                    templateIdToUse = latest.getId();
+                    
getParameters().getVmStaticData().setVmtGuid(templateIdToUse);
+                }
+            }
+
+            setVmTemplateId(templateIdToUse);
 
             // API backward compatibility
             if (parameters.isSoundDeviceEnabled() == null) {
@@ -776,6 +794,7 @@
 
     protected void addVmStatic() {
         VmStatic vmStatic = getParameters().getVmStaticData();
+
         if (vmStatic.getOrigin() == null) {
             vmStatic.setOrigin(OriginType.valueOf(Config.<String> 
getValue(ConfigValues.OriginType)));
         }
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmFromTemplateCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmFromTemplateCommand.java
index de315fc..6ba8b1b 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmFromTemplateCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmFromTemplateCommand.java
@@ -12,6 +12,7 @@
 import org.ovirt.engine.core.common.businessentities.VMStatus;
 import org.ovirt.engine.core.common.errors.VdcBLLException;
 import org.ovirt.engine.core.common.errors.VdcBllErrors;
+import org.ovirt.engine.core.common.errors.VdcBllMessages;
 import org.ovirt.engine.core.compat.Guid;
 import org.ovirt.engine.core.dal.dbbroker.DbFacade;
 import org.ovirt.engine.core.common.AuditLogType;
@@ -126,6 +127,10 @@
                     break;
                 }
             }
+
+            if (getParameters().getVm().isUseLatestVersion()) {
+                return 
failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_CANNOT_USE_LATEST_WITH_CLONE);
+            }
         }
         return retValue;
     }
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommonVmPoolWithVmsCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommonVmPoolWithVmsCommand.java
index 6dd960c..4e6490c 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommonVmPoolWithVmsCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommonVmPoolWithVmsCommand.java
@@ -26,6 +26,7 @@
 import org.ovirt.engine.core.common.businessentities.StorageDomainType;
 import org.ovirt.engine.core.common.businessentities.VmPool;
 import org.ovirt.engine.core.common.businessentities.VmStatic;
+import org.ovirt.engine.core.common.businessentities.VmTemplate;
 import org.ovirt.engine.core.common.businessentities.VmType;
 import org.ovirt.engine.core.common.config.Config;
 import org.ovirt.engine.core.common.config.ConfigValues;
@@ -66,7 +67,26 @@
 
     public CommonVmPoolWithVmsCommand(T parameters) {
         super(parameters);
-        setVmTemplateId(getParameters().getVmStaticData().getVmtGuid());
+
+        Guid templateIdToUse = getParameters().getVmStaticData().getVmtGuid();
+        // if set to use latest version, get it from db and use it as template
+        if (parameters.getVmStaticData().isUseLatestVersion()) {
+            VmTemplate latest = 
getVmTemplateDAO().getTemplateWithLatestVersionInChain(templateIdToUse);
+
+            if (latest != null) {
+                // if not using original template, need to override storage 
mappings
+                // as it may have different set of disks
+                if (!templateIdToUse.equals(latest.getId())) {
+                    getParameters().setDiskInfoDestinationMap(null);
+                }
+
+                setVmTemplate(latest);
+                templateIdToUse = latest.getId();
+                getParameters().getVmStaticData().setVmtGuid(templateIdToUse);
+            }
+        }
+
+        setVmTemplateId(templateIdToUse);
         initTemplate();
         diskInfoDestinationMap = getParameters().getDiskInfoDestinationMap();
         if (diskInfoDestinationMap == null) {
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/errors/VdcBllMessages.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/errors/VdcBllMessages.java
index 29a19bd..d867358 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/errors/VdcBllMessages.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/errors/VdcBllMessages.java
@@ -158,6 +158,7 @@
     ACTION_TYPE_FAILED_VM_IS_SAVING_RESTORING(ErrorType.CONFLICT),
     ACTION_TYPE_FAILED_VM_IS_DURING_SNAPSHOT(ErrorType.CONFLICT),
     ACTION_TYPE_FAILED_VM_NOT_FOUND(ErrorType.BAD_PARAMETERS),
+    ACTION_TYPE_FAILED_CANNOT_USE_LATEST_WITH_CLONE(ErrorType.BAD_PARAMETERS),
     ACTION_TYPE_FAILED_VM_NOT_EXIST(ErrorType.BAD_PARAMETERS),
     ACTION_TYPE_FAILED_VM_ALREADY_EXIST(ErrorType.CONFLICT),
     ACTION_TYPE_FAILED_VM_GUID_ALREADY_EXIST(ErrorType.CONFLICT),
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VmTemplateDAO.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VmTemplateDAO.java
index 9df4e3a..84d41b0 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VmTemplateDAO.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VmTemplateDAO.java
@@ -164,4 +164,14 @@
      *             list of template versions for this base template
      */
     List<VmTemplate> getTemplateVersionsForBaseTemplate(Guid id);
+
+    /**
+     * Retrieve the id of the latest template for the given template (base or 
version)
+     *
+     * @param id
+     *             the template id to get latest version for
+     * @return
+     *             id of the latest template version in the chain
+     */
+    VmTemplate getTemplateWithLatestVersionInChain(Guid id);
 }
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VmTemplateDAODbFacadeImpl.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VmTemplateDAODbFacadeImpl.java
index f4dcf66..d6f547d 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VmTemplateDAODbFacadeImpl.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/VmTemplateDAODbFacadeImpl.java
@@ -255,6 +255,14 @@
                         .addValue("base_template_id", id));
     }
 
+    @Override
+    public VmTemplate getTemplateWithLatestVersionInChain(Guid id) {
+        return 
getCallsHandler().executeRead("GetTemplateWithLatestVersionInChain",
+                VMTemplateRowMapper.instance,
+                getCustomMapSqlParameterSource()
+                        .addValue("template_id", id));
+    }
+
     private final static class VMTemplateRowMapper extends 
AbstractVmRowMapper<VmTemplate> {
         public static final VMTemplateRowMapper instance = new 
VMTemplateRowMapper();
 
diff --git 
a/backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties 
b/backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties
index 8f61a9b..3c97e10 100644
--- 
a/backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties
+++ 
b/backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties
@@ -172,6 +172,7 @@
 ACTION_TYPE_FAILED_VM_HAS_STATELESS_SNAPSHOT_LEFTOVER=Cannot ${action} 
${type}. The VM was running as Stateless and didn't clean up successfully. 
Please try to run the VM which should clean up the VM, and then try again when 
VM is not running.
 ACTION_TYPE_FAILED_VM_IN_USE_BY_OTHER_USER=Cannot ${action} ${type}. The VM is 
in use by other user.
 ACTION_TYPE_FAILED_VM_NOT_FOUND=Cannot ${action} ${type}. VM is not found.
+ACTION_TYPE_FAILED_CANNOT_USE_LATEST_WITH_CLONE=Cannot ${action} ${type}. 
Cannot use 'Latest Version' when using clone from Template.
 ACTION_TYPE_FAILED_VM_IS_NON_MIGRTABLE=Cannot ${action} ${type}. VM is non 
migratable.
 
ACTION_TYPE_FAILED_VM_IS_NON_MIGRTABLE_AND_IS_NOT_FORCED_BY_USER_TO_MIGRATE=Cannot
 ${action} ${type}. VM is non migratable and user did not specify the 
force-migration flag
 ACTION_TYPE_FAILED_VM_IS_PINNED_TO_HOST=Cannot ${action} ${type}. VM is pinned 
to Host.
diff --git 
a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/VmTemplateDAOTest.java
 
b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/VmTemplateDAOTest.java
index 48bb1a6..05e24ce 100644
--- 
a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/VmTemplateDAOTest.java
+++ 
b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/VmTemplateDAOTest.java
@@ -366,4 +366,12 @@
         assertEquals(1, tVersions.size());
         assertEquals(FixturesTool.VM_TEMPLATE_RHEL5_V2, 
tVersions.get(0).getId());
     }
+
+    /**
+     * Assert that latest version (VM_TEMPLATE_RHEL5_V2) returns for base 
template (VM_TEMPLATE_RHEL5)
+     */
+    @Test
+    public void testGetTemplateWithLatestVersionInChain() {
+        assertEquals(FixturesTool.VM_TEMPLATE_RHEL5_V2, 
dao.getTemplateWithLatestVersionInChain(FixturesTool.VM_TEMPLATE_RHEL5).getId());
+    }
 }
diff --git 
a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/AppErrors.java
 
b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/AppErrors.java
index 1d1dd17..3825f02 100644
--- 
a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/AppErrors.java
+++ 
b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/AppErrors.java
@@ -442,6 +442,9 @@
     @DefaultStringValue("Cannot ${action} ${type}. VM is not found.")
     String ACTION_TYPE_FAILED_VM_NOT_FOUND();
 
+    @DefaultStringValue("Cannot ${action} ${type}. Cannot use 'Latest Version' 
when using clone from Template.")
+    String ACTION_TYPE_FAILED_CANNOT_USE_LATEST_WITH_CLONE();
+
     @DefaultStringValue("Cannot ${action} ${type}. VM is non migratable.")
     String ACTION_TYPE_FAILED_VM_IS_NON_MIGRTABLE();
 
diff --git 
a/frontend/webadmin/modules/userportal-gwtp/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
 
b/frontend/webadmin/modules/userportal-gwtp/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
index 569b5fb..5149a20 100644
--- 
a/frontend/webadmin/modules/userportal-gwtp/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
+++ 
b/frontend/webadmin/modules/userportal-gwtp/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
@@ -165,6 +165,7 @@
 ACTION_TYPE_FAILED_VM_HAS_STATELESS_SNAPSHOT_LEFTOVER=Cannot ${action} 
${type}. The VM was running as Stateless and didn't clean up successfully. 
Please try to run the VM which should clean up the VM, and then try again when 
VM is not running.
 ACTION_TYPE_FAILED_VM_IN_USE_BY_OTHER_USER=Cannot ${action} ${type}. The VM is 
in use by other user.
 ACTION_TYPE_FAILED_VM_NOT_FOUND=Cannot ${action} ${type}. VM is not found.
+ACTION_TYPE_FAILED_CANNOT_USE_LATEST_WITH_CLONE=Cannot ${action} ${type}. 
Cannot use 'Latest Version' when using clone from Template.
 ACTION_TYPE_FAILED_VM_IS_NON_MIGRTABLE=Cannot ${action} ${type}. VM is non 
migratable.
 
ACTION_TYPE_FAILED_VM_IS_NON_MIGRTABLE_AND_IS_NOT_FORCED_BY_USER_TO_MIGRATE=Cannot
 ${action} ${type}. VM is non migratable and user did not specify the 
force-migration flag
 ACTION_TYPE_FAILED_VM_IS_PINNED_TO_HOST=Cannot ${action} ${type}. VM is pinned 
to Host.
diff --git 
a/frontend/webadmin/modules/webadmin/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
 
b/frontend/webadmin/modules/webadmin/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
index 64605b2..8f3559c 100644
--- 
a/frontend/webadmin/modules/webadmin/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
+++ 
b/frontend/webadmin/modules/webadmin/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
@@ -169,6 +169,7 @@
 ACTION_TYPE_FAILED_VM_HAS_STATELESS_SNAPSHOT_LEFTOVER=Cannot ${action} 
${type}. The VM was running as Stateless and didn't clean up successfully. 
Please try to run the VM which should clean up the VM, and then try again when 
VM is not running.
 ACTION_TYPE_FAILED_VM_IN_USE_BY_OTHER_USER=Cannot ${action} ${type}. The VM is 
in use by other user.
 ACTION_TYPE_FAILED_VM_NOT_FOUND=Cannot ${action} ${type}. VM is not found.
+ACTION_TYPE_FAILED_CANNOT_USE_LATEST_WITH_CLONE=Cannot ${action} ${type}. 
Cannot use 'Latest Version' when using clone from Template.
 ACTION_TYPE_FAILED_VM_IS_NON_MIGRTABLE=Cannot ${action} ${type}. VM is non 
migratable.
 
ACTION_TYPE_FAILED_VM_IS_NON_MIGRTABLE_AND_IS_NOT_FORCED_BY_USER_TO_MIGRATE=Cannot
 ${action} ${type}. VM is non migratable and user did not specify the 
force-migration flag
 ACTION_TYPE_FAILED_VM_IS_PINNED_TO_HOST=Cannot ${action} ${type}. VM is pinned 
to Host.


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2d1b3ee9cd64d73866762fbf5fe02a468cdd578e
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: ovirt-engine-3.4
Gerrit-Owner: Omer Frenkel <ofren...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to