Ori Liel has uploaded a new change for review.

Change subject: engine: support non-unique vm names across DCs
......................................................................

engine: support non-unique vm names across DCs

Change-Id: I5f3244ec1885d54e58b475d0e74f59e26fa492a0
Signed-off-by: Ori Liel <[email protected]>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmTemplateCommand.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetVmTemplateQuery.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmTemplateCommand.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/IsVmTemlateWithSameNameExistQuery.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateVmTemplateCommand.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/VmTemplateCommand.java
M 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/AddVmTemplateCommandTest.java
M 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/ImportVmTemplateCommandTest.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/GetVmTemplateParameters.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/NameQueryParameters.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/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmPoolsResource.java
M 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmsResource.java
M 
backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmPoolsResourceTest.java
M 
backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmsResourceTest.java
M 
frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/dataprovider/AsyncDataProvider.java
M 
frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/configure/instancetypes/InstanceTypeListModel.java
M 
frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/templates/TemplateListModel.java
M 
frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/userportal/UserPortalListModel.java
M 
frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/VmListModel.java
M packaging/dbscripts/vm_templates_sp.sql
22 files changed, 264 insertions(+), 118 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/63/41263/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmTemplateCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmTemplateCommand.java
index 09120a5..5c833b4 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmTemplateCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmTemplateCommand.java
@@ -439,9 +439,19 @@
         if (isVmInDb && getVm().getStatus() != VMStatus.Down) {
             return 
failCanDoAction(VdcBllMessages.VMT_CANNOT_CREATE_TEMPLATE_FROM_DOWN_VM);
         }
-
-        if (!isTemplateVersion() && 
isVmTemlateWithSameNameExist(getVmTemplateName())) {
-            return 
failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_NAME_ALREADY_USED);
+        // validate uniqueness of template name. If template is a regular 
template, uniqueness
+        // is considered in context of the datacenter. If template is an 
'Instance' name must
+        // be unique also across datacenters.
+        if (!isTemplateVersion()) {
+            if (isInstanceType) {
+                if (isInstanceWithSameNameExists(getVmTemplateName())) {
+                    return 
failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_NAME_ALREADY_USED);
+                }
+            } else {
+                if (isVmTemlateWithSameNameExist(getVmTemplateName(), 
getVdsGroup().getStoragePoolId())) {
+                    return 
failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_NAME_ALREADY_USED);
+                }
+            }
         }
 
         if (isTemplateVersion()) {
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetVmTemplateQuery.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetVmTemplateQuery.java
index 50082bf..079f1d9 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetVmTemplateQuery.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetVmTemplateQuery.java
@@ -2,6 +2,7 @@
 
 import org.ovirt.engine.core.common.businessentities.VmTemplate;
 import org.ovirt.engine.core.common.queries.GetVmTemplateParameters;
+import org.ovirt.engine.core.compat.Guid;
 import org.ovirt.engine.core.dal.dbbroker.DbFacade;
 
 public class GetVmTemplateQuery<P extends GetVmTemplateParameters> extends 
QueriesCommandBase<P> {
@@ -12,12 +13,15 @@
     @Override
     protected void executeQueryCommand() {
         VmTemplate vmt;
-        if (getParameters().getName() != null) {
+        GetVmTemplateParameters params = getParameters();
+        if (params.getName() != null) {
+            Guid storagePoolId = getStoragePoolId(params);
             vmt = DbFacade.getInstance().getVmTemplateDao()
-                .getByName(getParameters().getName(), getUserID(), 
getParameters().isFiltered());
-        } else {
+                    .getByName(params.getName(), storagePoolId, getUserID(), 
params.isFiltered());
+        }
+        else {
             vmt = DbFacade.getInstance().getVmTemplateDao()
-                .get(getParameters().getId(), getUserID(), 
getParameters().isFiltered());
+                    .get(getParameters().getId(), getUserID(), 
getParameters().isFiltered());
         }
         if (vmt != null) {
             VmTemplateHandler.updateDisksFromDb(vmt);
@@ -25,4 +29,10 @@
         }
         getQueryReturnValue().setReturnValue(vmt);
     }
+
+    // Get the DC ID. If no DC info available, the query will return the first 
VM
+    // with the given name found.
+    private Guid getStoragePoolId(GetVmTemplateParameters params) {
+        return params.getStoragePoolId() != null ? params.getStoragePoolId() : 
params.getVdsGroupId() != null ?  
DbFacade.getInstance().getVdsGroupDao().get(params.getVdsGroupId()).getStoragePoolId()
 : null;
+    }
 }
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmTemplateCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmTemplateCommand.java
index 9ff9c7a..d829e22 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmTemplateCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmTemplateCommand.java
@@ -237,7 +237,9 @@
     }
 
     protected boolean isVmTemplateWithSameNameExist() {
-        return 
VmTemplateCommand.isVmTemlateWithSameNameExist(getParameters().getVmTemplate().getName());
+        return DbFacade.getInstance()
+                .getVmTemplateDao()
+                .getByName(getParameters().getVmTemplate().getName(), 
getParameters().getStoragePoolId(), null, false) != null;
     }
 
     private void initImportClonedTemplate() {
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/IsVmTemlateWithSameNameExistQuery.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/IsVmTemlateWithSameNameExistQuery.java
index b50286b..ecf0779 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/IsVmTemlateWithSameNameExistQuery.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/IsVmTemlateWithSameNameExistQuery.java
@@ -1,6 +1,8 @@
 package org.ovirt.engine.core.bll;
 
 import org.ovirt.engine.core.common.queries.NameQueryParameters;
+import org.ovirt.engine.core.compat.Guid;
+import org.ovirt.engine.core.dal.dbbroker.DbFacade;
 
 public class IsVmTemlateWithSameNameExistQuery<P extends NameQueryParameters>
         extends QueriesCommandBase<P> {
@@ -11,6 +13,11 @@
     @Override
     protected void executeQueryCommand() {
         getQueryReturnValue().setReturnValue(
-                
VmTemplateCommand.isVmTemlateWithSameNameExist(getParameters().getName()));
+                isVmTemlateWithSameNameExist(getParameters().getName(),
+                        getParameters().getDatacenterId()));
+    }
+
+    public boolean isVmTemlateWithSameNameExist(String name, Guid 
datacenterId) {
+        return DbFacade.getInstance().getVmTemplateDao().getByName(name, 
datacenterId, null, false) != null;
     }
 }
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateVmTemplateCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateVmTemplateCommand.java
index 72b5adc..c9a4082 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateVmTemplateCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/UpdateVmTemplateCommand.java
@@ -96,8 +96,19 @@
             if (!getVmTemplate().isBaseTemplate()) {
                 // template version should always have the name of the base 
template
                 return 
failCanDoAction(VdcBllMessages.VMT_CANNOT_UPDATE_VERSION_NAME);
-            } else if (isVmTemlateWithSameNameExist(getVmTemplateName())) {
-                return 
failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_NAME_ALREADY_USED);
+            } else {
+                // validate uniqueness of template name. If template is a 
regular template, uniqueness
+                // is considered in context of the datacenter. If template is 
an 'Instance' name must
+                // be unique also across datacenters.
+                if (isInstanceType) {
+                    if (isInstanceWithSameNameExists(getVmTemplateName())) {
+                        return 
failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_NAME_ALREADY_USED);
+                    }
+                } else {
+                    if (isVmTemlateWithSameNameExist(getVmTemplateName(), 
getVdsGroup().getStoragePoolId())) {
+                        return 
failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_NAME_ALREADY_USED);
+                    }
+                }
             }
         }
 
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/VmTemplateCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/VmTemplateCommand.java
index 921e918..e2f0d08 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/VmTemplateCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/VmTemplateCommand.java
@@ -62,8 +62,12 @@
         throw new NotImplementedException();
     }
 
-    public static boolean isVmTemlateWithSameNameExist(String name) {
-        return DbFacade.getInstance().getVmTemplateDao().getByName(name, null, 
false) != null;
+    public boolean isVmTemlateWithSameNameExist(String name, Guid 
datacenterId) {
+        return DbFacade.getInstance().getVmTemplateDao().getByName(name, 
datacenterId, null, false) != null;
+    }
+
+    public boolean isInstanceWithSameNameExists(String name) {
+        return 
DbFacade.getInstance().getVmTemplateDao().getInstanceByName(name) != null;
     }
 
     public static boolean isVmTemplateImagesReady(VmTemplate vmTemplate,
diff --git 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/AddVmTemplateCommandTest.java
 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/AddVmTemplateCommandTest.java
index 963ffa1..c990819 100644
--- 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/AddVmTemplateCommandTest.java
+++ 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/AddVmTemplateCommandTest.java
@@ -31,6 +31,7 @@
 import org.ovirt.engine.core.common.businessentities.VDSGroup;
 import org.ovirt.engine.core.common.businessentities.VM;
 import org.ovirt.engine.core.common.businessentities.VMStatus;
+import org.ovirt.engine.core.common.businessentities.VmEntityType;
 import org.ovirt.engine.core.common.businessentities.VmStatic;
 import org.ovirt.engine.core.common.businessentities.storage.DiskImage;
 import org.ovirt.engine.core.common.config.ConfigValues;
@@ -139,6 +140,21 @@
     }
 
     @Test
+    // When Template by the same name already exists in the datacenter - fail.
+    public void testCanDoActionDuplicateTemplateName() {
+        doReturn(true).when(cmd).isVmTemlateWithSameNameExist("templateName", 
spId);
+        CanDoActionTestUtils.runAndAssertCanDoActionFailure(cmd, 
VdcBllMessages.ACTION_TYPE_FAILED_NAME_ALREADY_USED);
+    }
+
+    @Test
+    // When Instance by same name exists - fail (regardless of datacenter).
+    public void testCanDoActionInstanceNameDuplicate() {
+        cmd.getParameters().setTemplateType(VmEntityType.INSTANCE_TYPE);
+        doReturn(true).when(cmd).isInstanceWithSameNameExists("templateName");
+        CanDoActionTestUtils.runAndAssertCanDoActionFailure(cmd, 
VdcBllMessages.ACTION_TYPE_FAILED_NAME_ALREADY_USED);
+    }
+
+    @Test
     public void sufficientStorageSpace() {
         setupForStorageTests();
         assertTrue(cmd.imagesRelatedChecks());
diff --git 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/ImportVmTemplateCommandTest.java
 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/ImportVmTemplateCommandTest.java
index 1c7de9e..264ae94 100644
--- 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/ImportVmTemplateCommandTest.java
+++ 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/ImportVmTemplateCommandTest.java
@@ -99,6 +99,15 @@
         assertInvalidVolumeInfoCombination(VolumeFormat.Unassigned, 
VolumeType.Preallocated, StorageType.LOCALFS);
     }
 
+    public void testValidateUniqueTemplateNameInDC() {
+        ImportVmTemplateCommand command =
+                setupVolumeFormatAndTypeTest(VolumeFormat.RAW, 
VolumeType.Preallocated, StorageType.NFS);
+        doReturn(true).when(command).isVmTemplateWithSameNameExist();
+        CanDoActionTestUtils.runAndAssertCanDoActionFailure(command,
+                VdcBllMessages.VM_CANNOT_IMPORT_TEMPLATE_NAME_EXISTS);
+
+    }
+
     private void assertValidVolumeInfoCombination(VolumeFormat volumeFormat,
             VolumeType volumeType,
             StorageType storageType) {
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/GetVmTemplateParameters.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/GetVmTemplateParameters.java
index f669183..a4afb57 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/GetVmTemplateParameters.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/GetVmTemplateParameters.java
@@ -7,6 +7,8 @@
 
     private Guid _id;
     private String _name;
+    private Guid vdsGroupId;
+    private Guid storagePoolId;
 
     public GetVmTemplateParameters(Guid id) {
         _id = id;
@@ -27,4 +29,21 @@
 
     public GetVmTemplateParameters() {
     }
+
+    public Guid getStoragePoolId() {
+        return storagePoolId;
+    }
+
+    public void setStoragePoolId(Guid storagePoolId) {
+        this.storagePoolId = storagePoolId;
+    }
+
+    public Guid getVdsGroupId() {
+        return vdsGroupId;
+    }
+
+    public void setVdsGroupId(Guid vdsGroupId) {
+        this.vdsGroupId = vdsGroupId;
+    }
+
 }
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/NameQueryParameters.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/NameQueryParameters.java
index 9277749..364ad64 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/NameQueryParameters.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/NameQueryParameters.java
@@ -1,5 +1,7 @@
 package org.ovirt.engine.core.common.queries;
 
+import org.ovirt.engine.core.compat.Guid;
+
 
 /**
  * Parameter class for the "GetByName" queries
@@ -8,6 +10,15 @@
 
     private static final long serialVersionUID = 4281700157334399396L;
     private String name;
+    private Guid datacenterId;
+
+    public Guid getDatacenterId() {
+        return datacenterId;
+    }
+
+    public void setDatacenterId(Guid datacenterId) {
+        this.datacenterId = datacenterId;
+    }
 
     public NameQueryParameters() {
     }
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 e7a7062..3e769de 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
@@ -30,17 +30,28 @@
     public VmTemplate get(Guid id, Guid userID, boolean isFiltered);
 
     /**
-    * Retrieves the template with the given id with optional filtering.
-    *
-    * @param name
-    *            The name to look by (can't be <code>null</code>).
-    * @param userID
-    *            the ID of the user requesting the information
-    * @param isFiltered
-    *            Whether the results should be filtered according to the 
user's permissions
-    * @return The entity instance, or <code>null</code> if not found.
-    */
-    public VmTemplate getByName(String name, Guid userID, boolean isFiltered);
+     * Retrieves the template with the given id with optional filtering.
+     *
+     * @param name
+     *            The name to look by (can't be <code>null</code>).
+     * @param storagePoolId
+     *            The ID of the datacenter
+     * @param userID
+     *            the ID of the user requesting the information
+     * @param isFiltered
+     *            Whether the results should be filtered according to the 
user's permissions
+     * @return The entity instance, or <code>null</code> if not found.
+     */
+    public VmTemplate getByName(String name, Guid storagePoolId, Guid userID, 
boolean isFiltered);
+
+    /**
+     * Get the Instance with the given name.
+     *
+     * @param name
+     *            The Instance name
+     * @return The Instance which has this name of null if no such exists.
+     */
+    public VmTemplate getInstanceByName(String name);
 
     /**
      * Retrieves all templates with optional filtering.
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 9599bd1..0299da9 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
@@ -66,11 +66,19 @@
     }
 
     @Override
-    public VmTemplate getByName(String name, Guid userID, boolean isFiltered) {
+    public VmTemplate getByName(String name, Guid datacenterId, Guid userID, 
boolean isFiltered) {
         return getCallsHandler().executeRead("GetVmTemplateByVmtName",
                 VMTemplateRowMapper.instance,
                 getCustomMapSqlParameterSource()
-                        .addValue("vmt_name", name).addValue("user_id", 
userID).addValue("is_filtered", isFiltered));
+                        .addValue("vmt_name", name)
+                        .addValue("storage_pool_id", datacenterId)
+                        .addValue("user_id", userID)
+                        .addValue("is_filtered", isFiltered));
+    }
+
+    @Override
+    public VmTemplate getInstanceByName(String name) {
+        return getByName(name, null, null, false);
     }
 
     @Override
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmPoolsResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmPoolsResource.java
index 593aa4a..679df47 100644
--- 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmPoolsResource.java
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmPoolsResource.java
@@ -54,13 +54,14 @@
     public Response add(VmPool pool) {
         validateParameters(pool, "name", "template.id|name", 
"cluster.id|name");
 
+        if (namedCluster(pool)) {
+            VDSGroup cluster = getCluster(pool);
+            pool.getCluster().setId(cluster.getId().toString());
+        }
+
         VmTemplate template = getVmTemplate(pool);
         if (namedTemplate(pool)) {
             pool.getTemplate().setId(template.getId().toString());
-        }
-
-        if (namedCluster(pool)) {
-            pool.getCluster().setId(getClusterId(pool));
         }
 
         org.ovirt.engine.core.common.businessentities.VmPool entity = 
map(pool);
@@ -154,11 +155,11 @@
         return pool.getCluster().isSetName() && !pool.getCluster().isSetId();
     }
 
-    protected String getClusterId(VmPool pool) {
+    protected VDSGroup getCluster(VmPool pool) {
         return getEntity(VDSGroup.class,
                 VdcQueryType.GetVdsGroupByName,
                 new NameQueryParameters(pool.getCluster().getName()),
-                "Cluster: name=" + 
pool.getCluster().getName()).getId().toString();
+                "Cluster: name=" + pool.getCluster().getName());
     }
 
     protected boolean namedTemplate(VmPool pool) {
@@ -172,9 +173,11 @@
                              new 
GetVmTemplateParameters(asGuid(pool.getTemplate().getId())),
                              pool.getTemplate().getId());
         } else {
+            GetVmTemplateParameters params = new 
GetVmTemplateParameters(pool.getTemplate().getName());
+            params.setVdsGroupId(asGuid(pool.getCluster().getId()));
             return getEntity(VmTemplate.class,
                     VdcQueryType.GetVmTemplate,
-                    new GetVmTemplateParameters(pool.getTemplate().getName()),
+                    params,
                     "Template: name=" + pool.getTemplate().getName());
         }
     }
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmsResource.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmsResource.java
index 9025558..ca55804 100644
--- 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmsResource.java
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/BackendVmsResource.java
@@ -110,27 +110,24 @@
                 response = createVmFromSnapshot(vm);
             } else {
                 validateParameters(vm, "template.id|name");
-                Guid templateId = getTemplateId(vm.getTemplate());
-
-                VmTemplate templateEntity = lookupTemplate(templateId);
-
                 VDSGroup cluster = getCluster(vm);
+                VmTemplate template = lookupTemplate(vm.getTemplate(), 
cluster.getStoragePoolId());
 
-                VmStatic builtFromTemplate = VmMapper.map(templateEntity, 
null, cluster.getCompatibilityVersion());
+                VmStatic builtFromTemplate = VmMapper.map(template, null, 
cluster.getCompatibilityVersion());
                 // if VM is based on a template, and going to be on another 
cluster then template, clear the cpu_profile
                 // since the template cpu_profile doesn't match cluster.
                 if (!vm.isSetCpuProfile() && vm.isSetCluster()
-                        && !ObjectUtils.equals(templateEntity.getVdsGroupId(), 
vm.getCluster().getId())) {
+                        && !ObjectUtils.equals(template.getVdsGroupId(), 
vm.getCluster().getId())) {
                     builtFromTemplate.setCpuProfileId(null);
                 }
 
                 VmStatic builtFromInstanceType = null;
-                org.ovirt.engine.core.common.businessentities.InstanceType 
instanceTypeEntity = null;
+                InstanceType instanceTypeEntity = null;
                 if (vm.isSetInstanceType() && (vm.getInstanceType().isSetId() 
|| vm.getInstanceType().isSetName())) {
-                    
Mapper<org.ovirt.engine.core.common.businessentities.InstanceType, VmStatic> 
instanceTypeMapper = 
getMapper(org.ovirt.engine.core.common.businessentities.InstanceType.class, 
VmStatic.class);
-                    instanceTypeEntity = 
lookupInstanceType(getTemplateId(vm.getInstanceType()));
+                    Mapper<InstanceType, VmStatic> instanceTypeMapper = 
getMapper(InstanceType.class, VmStatic.class);
+                    instanceTypeEntity = lookupInstance(vm.getInstanceType());
                     builtFromInstanceType = 
instanceTypeMapper.map(instanceTypeEntity, builtFromTemplate);
-                    
builtFromInstanceType.setInstanceTypeId(getTemplateId(vm.getInstanceType()));
+                    
builtFromInstanceType.setInstanceTypeId(instanceTypeEntity.getId());
                 }
 
                 VmStatic staticVm = getMapper(VM.class, 
VmStatic.class).map(vm, builtFromInstanceType != null ? builtFromInstanceType : 
builtFromTemplate);
@@ -138,7 +135,7 @@
                     staticVm.setVdsGroupId(cluster.getId());
                 }
 
-                if (Guid.Empty.equals(templateId) && !vm.isSetOs()) {
+                if (Guid.Empty.equals(template.getId()) && !vm.isSetOs()) {
                     staticVm.setOsId(OsRepository.AUTO_SELECT_OS);
                 }
 
@@ -160,7 +157,7 @@
                 // If the user omits the placement policy in the incoming XML 
and the selected template
                 // is the blank one, the AddVmCommand must auto-select a 
proper default value for the
                 // migration support (disabling it in architectures that do 
not support this feature)
-                if (!vm.isSetPlacementPolicy() && 
templateId.equals(Guid.Empty)) {
+                if (!vm.isSetPlacementPolicy() && 
template.getId().equals(Guid.Empty)) {
                     staticVm.setMigrationSupport(null);
                 }
 
@@ -170,11 +167,11 @@
                                 : Guid.Empty;
 
                 if (vm.isSetDisks() && vm.getDisks().isSetClone() && 
vm.getDisks().isClone()) {
-                    response = cloneVmFromTemplate(staticVm, vm, 
templateEntity, instanceTypeEntity, cluster);
-                } else if (Guid.Empty.equals(templateId)) {
+                    response = cloneVmFromTemplate(staticVm, vm, template, 
instanceTypeEntity, cluster);
+                } else if (Guid.Empty.equals(template.getId())) {
                     response = addVmFromScratch(staticVm, vm, 
instanceTypeEntity, cluster);
                 } else {
-                    response = addVm(staticVm, vm, storageDomainId, 
templateEntity, instanceTypeEntity, cluster);
+                    response = addVm(staticVm, vm, storageDomainId, template, 
instanceTypeEntity, cluster);
                 }
             }
         }
@@ -622,24 +619,26 @@
         return vm.isSetTemplate() && (vm.getTemplate().isSetId() || 
vm.getTemplate().isSetName());
     }
 
-    protected Guid getTemplateId(Template template) {
-        return template.isSetId() ? asGuid(template.getId()) : 
getTemplateByName(template).getId();
+    protected VmTemplate lookupTemplate(Template template, Guid datacenterId) {
+        if (template.isSetId()) {
+            return getEntity(VmTemplate.class,
+                    VdcQueryType.GetVmTemplate,
+                    new GetVmTemplateParameters(asGuid(template.getId())),
+                    "GetVmTemplate");
+        } else if (template.isSetName()) {
+            GetVmTemplateParameters params = new 
GetVmTemplateParameters(template.getName());
+            params.setStoragePoolId(datacenterId);
+            return getEntity(VmTemplate.class, VdcQueryType.GetVmTemplate, 
params, "GetVmTemplate");
+        }
+        return null; // should never happen.
     }
 
-    private VmTemplate getTemplateByName(Template template) {
-        return lookupTemplateByName(template.getName());
-    }
-
-    public VmTemplate lookupTemplateByName(String name) {
-        return getEntity(VmTemplate.class, VdcQueryType.GetVmTemplate, new 
GetVmTemplateParameters(name), "GetVmTemplate");
+    protected InstanceType lookupInstance(Template template) {
+        return (InstanceType) lookupTemplate(template, null);
     }
 
     public VmTemplate lookupTemplate(Guid id) {
         return getEntity(VmTemplate.class, VdcQueryType.GetVmTemplate, new 
GetVmTemplateParameters(id), "GetVmTemplate");
-    }
-
-    public org.ovirt.engine.core.common.businessentities.InstanceType 
lookupInstanceType(Guid id) {
-        return 
getEntity(org.ovirt.engine.core.common.businessentities.InstanceType.class, 
VdcQueryType.GetVmTemplate, new GetVmTemplateParameters(id), "GetVmTemplate");
     }
 
     private VDSGroup lookupCluster(Guid id) {
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmPoolsResourceTest.java
 
b/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmPoolsResourceTest.java
index 7247df4..6f13d76 100644
--- 
a/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmPoolsResourceTest.java
+++ 
b/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmPoolsResourceTest.java
@@ -129,8 +129,8 @@
 
         setUpEntityQueryExpectations(VdcQueryType.GetVmTemplate,
                 GetVmTemplateParameters.class,
-                new String[] { "Name" },
-                new Object[] { NAMES[1] },
+                new String[] { "Name", "VdsGroupId" },
+                new Object[] { NAMES[1], GUIDS[2] },
                 getTemplateEntity());
 
         setUpGetConsoleExpectations(new int[] { 1 });
diff --git 
a/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmsResourceTest.java
 
b/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmsResourceTest.java
index b6f83c5..6f75491 100644
--- 
a/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmsResourceTest.java
+++ 
b/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/BackendVmsResourceTest.java
@@ -280,8 +280,8 @@
         setUpGetGraphicsExpectations(1);
         setUpGetConsoleExpectations(new int[]{0});
         setUpGetVirtioScsiExpectations(new int[]{0});
-        setUpGetSoundcardExpectations(new int[]{0});
-        setUpGetRngDeviceExpectations(new int[]{0});
+        setUpGetSoundcardExpectations(new int[] { 0 });
+        setUpGetRngDeviceExpectations(new int[] { 0 });
         setUpGetVmOvfExpectations(new int[]{0});
         setUpGetCertuficateExpectations(1, 0);
         setUpEntityQueryExpectations(VdcQueryType.GetVdsGroupByVdsGroupId,
@@ -291,10 +291,10 @@
                 getVdsGroupEntity());
 
         setUpEntityQueryExpectations(VdcQueryType.GetVmTemplate,
-                                     GetVmTemplateParameters.class,
-                                     new String[] { "Id" },
-                                     new Object[] { GUIDS[0] },
-                                     getTemplateEntity(0));
+                GetVmTemplateParameters.class,
+                new String[] { "Id" },
+                new Object[] { GUIDS[0] },
+                getTemplateEntity(0));
         setUpCreationExpectations(VdcActionType.AddVmFromScratch,
                 AddVmParameters.class,
                 new String[]{"StorageDomainId"},
@@ -408,7 +408,7 @@
                                      GetVmTemplateParameters.class,
                                      new String[] { "Id" },
                                      new Object[] { GUIDS[0] },
-                                     getTemplateEntity(0));
+                getTemplateEntity(0));
 
         setUpCreationExpectations(VdcActionType.AddVmFromScratch,
                                   AddVmParameters.class,
@@ -451,8 +451,8 @@
         setUpEntityQueryExpectations(VdcQueryType.GetVmTemplate,
                                      GetVmTemplateParameters.class,
                                      new String[] { "Id" },
-                                     new Object[] { GUIDS[0] },
-                                     getTemplateEntity(0));
+                new Object[] { GUIDS[0] },
+                getTemplateEntity(0));
 
 
         expect(osRepository.isBalloonEnabled(anyInt(), 
anyObject(Version.class))).andReturn(false).anyTimes();
@@ -485,7 +485,7 @@
     @Test
     public void testCloneWithDisk() throws Exception {
         setUriInfo(setUpBasicUriExpectations());
-        setUpTemplateDisksExpectations(GUIDS[0]);
+        setUpTemplateDisksExpectations(GUIDS[1]);
         setUriInfo(setUpBasicUriExpectations());
         setUpGetPayloadExpectations(1, 2);
         setUpGetBallooningExpectations(1, 2);
@@ -493,14 +493,14 @@
         setUpGetConsoleExpectations(new int[]{2});
         setUpGetVmOvfExpectations(new int[]{2});
         setUpGetVirtioScsiExpectations(new int[]{2});
-        setUpGetSoundcardExpectations(new int[]{0, 2});
-        setUpGetRngDeviceExpectations(new int[]{0, 2});
+        setUpGetSoundcardExpectations(new int[] { 1, 2 });
+        setUpGetRngDeviceExpectations(new int[] { 1, 2 });
         setUpGetCertuficateExpectations(1, 2);
         setUpEntityQueryExpectations(VdcQueryType.GetVmTemplate,
                                      GetVmTemplateParameters.class,
                                      new String[] { "Id" },
                                      new Object[] { GUIDS[1] },
-                                     getTemplateEntity(0));
+                getTemplateEntity(1));
         setUpEntityQueryExpectations(VdcQueryType.GetVdsGroupByVdsGroupId,
                 IdQueryParameters.class,
                 new String[] { "Id" },
@@ -578,14 +578,14 @@
         setUpGetConsoleExpectations(2);
         setUpGetVmOvfExpectations(2);
         setUpGetVirtioScsiExpectations(2);
-        setUpGetSoundcardExpectations(0, 2);
-        setUpGetRngDeviceExpectations(0, 2);
+        setUpGetSoundcardExpectations(1, 2);
+        setUpGetRngDeviceExpectations(1, 2);
         setUpGetCertuficateExpectations(1, 2);
         setUpEntityQueryExpectations(VdcQueryType.GetVmTemplate,
                 GetVmTemplateParameters.class,
                 new String[]{"Id"},
                 new Object[]{GUIDS[1]},
-                getTemplateEntity(0));
+                getTemplateEntity(1));
         setUpEntityQueryExpectations(VdcQueryType.GetVdsGroupByVdsGroupId,
                 IdQueryParameters.class,
                 new String[]{"Id"},
@@ -620,13 +620,13 @@
         setUpGetConsoleExpectations(new int[]{2});
         setUpGetVmOvfExpectations(new int[]{2});
         setUpGetVirtioScsiExpectations(new int[]{2});
-        setUpGetSoundcardExpectations(new int[]{0, 2});
-        setUpGetRngDeviceExpectations(new int[]{2, 0});
+        setUpGetSoundcardExpectations(new int[] { 1, 2 });
+        setUpGetRngDeviceExpectations(new int[] { 2, 1 });
         setUpEntityQueryExpectations(VdcQueryType.GetVmTemplate,
                 GetVmTemplateParameters.class,
                 new String[]{"Id"},
                 new Object[]{GUIDS[1]},
-                getTemplateEntity(0));
+                getTemplateEntity(1));
         setUpEntityQueryExpectations(VdcQueryType.GetVdsGroupByVdsGroupId,
                 IdQueryParameters.class,
                 new String[]{"Id"},
@@ -662,6 +662,36 @@
 
     @Test
     public void testAdd() throws Exception {
+        setUpEntityQueryExpectations(VdcQueryType.GetVmTemplate,
+                GetVmTemplateParameters.class,
+                new String[] { "Id" },
+                new Object[] { GUIDS[1] },
+                getTemplateEntity(1));
+        setupAddExpectations();
+        Response response = collection.add(createModel(null));
+        assertEquals(201, response.getStatus());
+        assertTrue(response.getEntity() instanceof VM);
+        verifyModel((VM) response.getEntity(), 2);
+    }
+
+    @Test
+    public void testAddPassTemplateByName() throws Exception {
+        setUpEntityQueryExpectations(VdcQueryType.GetVmTemplate,
+                GetVmTemplateParameters.class,
+                new String[] { "Name", "StoragePoolId" },
+                new Object[] { NAMES[1], GUIDS[3] },
+                getTemplateEntity(1));
+        setupAddExpectations();
+        VM model = getModel(2);
+        model.setTemplate(new Template());
+        model.getTemplate().setName(NAMES[1].toString());
+        Response response = collection.add(model);
+        assertEquals(201, response.getStatus());
+        assertTrue(response.getEntity() instanceof VM);
+        verifyModel((VM) response.getEntity(), 2);
+    }
+
+    private void setupAddExpectations() throws Exception {
         setUriInfo(setUpBasicUriExpectations());
         setUpGetPayloadExpectations(1, 2);
         setUpGetBallooningExpectations(1, 2);
@@ -669,14 +699,9 @@
         setUpGetCertuficateExpectations(1, 2);
         setUpGetConsoleExpectations(new int[] { 2 });
         setUpGetVirtioScsiExpectations(new int[]{2});
-        setUpGetSoundcardExpectations(new int[]{0, 2});
-        setUpGetRngDeviceExpectations(new int[]{0, 2});
+        setUpGetSoundcardExpectations(new int[] { 1, 2 });
+        setUpGetRngDeviceExpectations(new int[] { 1, 2 });
         setUpGetVmOvfExpectations(new int[]{2});
-        setUpEntityQueryExpectations(VdcQueryType.GetVmTemplate,
-                GetVmTemplateParameters.class,
-                new String[]{"Id"},
-                new Object[]{GUIDS[1]},
-                getTemplateEntity(0));
         setUpEntityQueryExpectations(VdcQueryType.GetVdsGroupByVdsGroupId,
                 IdQueryParameters.class,
                 new String[]{"Id"},
@@ -694,11 +719,8 @@
                 new String[] { "Id" },
                 new Object[] { GUIDS[2] },
                 getEntity(2));
-        Response response = collection.add(createModel(null));
-        assertEquals(201, response.getStatus());
-        assertTrue(response.getEntity() instanceof VM);
-        verifyModel((VM) response.getEntity(), 2);
     }
+
 
     @Test
     public void testAddFromConfigurationWithRegenerateTrue() throws Exception {
@@ -897,8 +919,8 @@
         setUpGetConsoleExpectations(new int[]{2});
         setUpGetVmOvfExpectations(new int[]{2});
         setUpGetVirtioScsiExpectations(new int[]{2});
-        setUpGetSoundcardExpectations(new int[]{0, 2});
-        setUpGetRngDeviceExpectations(new int[]{0, 2});
+        setUpGetSoundcardExpectations(new int[] { 1, 2 });
+        setUpGetRngDeviceExpectations(new int[] { 1, 2 });
         setUpGetCertuficateExpectations(1, 2);
         setUpEntityQueryExpectations(VdcQueryType.GetVdsStaticByName,
                 NameQueryParameters.class,
@@ -909,7 +931,7 @@
                 GetVmTemplateParameters.class,
                 new String[]{"Id"},
                 new Object[]{GUIDS[1]},
-                getTemplateEntity(0));
+                getTemplateEntity(1));
         setUpEntityQueryExpectations(VdcQueryType.GetVdsGroupByVdsGroupId,
                 IdQueryParameters.class,
                 new String[] { "Id" },
@@ -954,14 +976,14 @@
         setUpGetConsoleExpectations(new int[]{2});
         setUpGetVmOvfExpectations(new int[]{2});
         setUpGetVirtioScsiExpectations(new int[]{2});
-        setUpGetSoundcardExpectations(new int[]{0, 2});
-        setUpGetRngDeviceExpectations(new int[]{0, 2});
+        setUpGetSoundcardExpectations(new int[] { 1, 2 });
+        setUpGetRngDeviceExpectations(new int[] { 1, 2 });
         setUpGetCertuficateExpectations(1, 2);
         setUpEntityQueryExpectations(VdcQueryType.GetVmTemplate,
                                      GetVmTemplateParameters.class,
                                      new String[] { "Id" },
                                      new Object[] { GUIDS[1] },
-                                     getTemplateEntity(0));
+                getTemplateEntity(1));
         setUpEntityQueryExpectations(VdcQueryType.GetVdsGroupByVdsGroupId,
                 IdQueryParameters.class,
                 new String[] { "Id" },
@@ -998,14 +1020,14 @@
         setUpGetConsoleExpectations(new int[]{2});
         setUpGetVmOvfExpectations(new int[]{2});
         setUpGetVirtioScsiExpectations(new int[]{2});
-        setUpGetSoundcardExpectations(new int[]{0, 2});
-        setUpGetRngDeviceExpectations(new int[]{0, 2});
+        setUpGetSoundcardExpectations(new int[] { 1, 2 });
+        setUpGetRngDeviceExpectations(new int[] { 1, 2 });
         setUpGetCertuficateExpectations(1, 2);
         setUpEntityQueryExpectations(VdcQueryType.GetVmTemplate,
                                      GetVmTemplateParameters.class,
                                      new String[] { "Id" },
                                      new Object[] { GUIDS[1] },
-                                     getTemplateEntity(0));
+                getTemplateEntity(1));
         setUpEntityQueryExpectations(VdcQueryType.GetVdsGroupByName,
                 NameQueryParameters.class,
                 new String[] { "Name" },
@@ -1060,13 +1082,13 @@
         setUpGetConsoleExpectations(new int[]{2});
         setUpGetVmOvfExpectations(new int[]{2});
         setUpGetVirtioScsiExpectations(new int[]{2});
-        setUpGetSoundcardExpectations(new int[]{0, 2});
-        setUpGetRngDeviceExpectations(new int[]{0, 2});
+        setUpGetSoundcardExpectations(new int[] { 1, 2 });
+        setUpGetRngDeviceExpectations(new int[] { 1, 2 });
         setUpEntityQueryExpectations(VdcQueryType.GetVmTemplate,
                                      GetVmTemplateParameters.class,
                                      new String[] { "Id" },
                                      new Object[] { GUIDS[1] },
-                                     getTemplateEntity(0));
+                getTemplateEntity(1));
         setUpEntityQueryExpectations(VdcQueryType.GetVdsGroupByVdsGroupId,
                                      IdQueryParameters.class,
                                      new String[] { "Id" },
@@ -1105,7 +1127,7 @@
 
     private void doTestCloneFromTemplateWithClonePermissions(VM model, boolean 
copy) throws Exception {
         setUriInfo(setUpBasicUriExpectations());
-        setUpTemplateDisksExpectations(GUIDS[0]);
+        setUpTemplateDisksExpectations(GUIDS[1]);
         setUriInfo(setUpBasicUriExpectations());
         setUpGetPayloadExpectations(1, 2);
         setUpGetBallooningExpectations(1, 2);
@@ -1115,13 +1137,13 @@
         setUpGetConsoleExpectations(new int[]{2});
         setUpGetVmOvfExpectations(new int[]{2});
         setUpGetVirtioScsiExpectations(new int[]{2});
-        setUpGetSoundcardExpectations(new int[]{0, 2});
-        setUpGetRngDeviceExpectations(new int[]{0, 2});
+        setUpGetSoundcardExpectations(new int[] { 1, 2 });
+        setUpGetRngDeviceExpectations(new int[] { 1, 2 });
         setUpEntityQueryExpectations(VdcQueryType.GetVmTemplate,
                                      GetVmTemplateParameters.class,
                                      new String[]{"Id"},
                                      new Object[]{GUIDS[1]},
-                                     getTemplateEntity(0));
+                getTemplateEntity(1));
                                      
setUpEntityQueryExpectations(VdcQueryType.GetVdsGroupByVdsGroupId,
                                      IdQueryParameters.class,
                                      new String[]{"Id"},
@@ -1264,9 +1286,9 @@
                                      GetVmTemplateParameters.class,
                                      new String[] { "Id" },
                                      new Object[] { GUIDS[1] },
-                                     getTemplateEntity(0));
-        setUpGetSoundcardExpectations(new int[]{0});
-        setUpGetRngDeviceExpectations(new int[]{0});
+                getTemplateEntity(1));
+        setUpGetSoundcardExpectations(new int[] { 1 });
+        setUpGetRngDeviceExpectations(new int[] { 1 });
 
         setUpEntityQueryExpectations(VdcQueryType.GetVdsGroupByVdsGroupId,
                 IdQueryParameters.class,
@@ -1477,6 +1499,7 @@
 
     protected org.ovirt.engine.core.common.businessentities.VDSGroup 
getVdsGroupEntity() {
         VDSGroup cluster = new VDSGroup();
+        cluster.setStoragePoolId(GUIDS[3]);
         cluster.setArchitecture(ArchitectureType.x86_64);
         cluster.setCompatibilityVersion(Version.getLast());
         return cluster;
diff --git 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/dataprovider/AsyncDataProvider.java
 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/dataprovider/AsyncDataProvider.java
index 76c3f27..18ed9a4 100644
--- 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/dataprovider/AsyncDataProvider.java
+++ 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/dataprovider/AsyncDataProvider.java
@@ -1905,7 +1905,7 @@
                 aQuery);
     }
 
-    public void isTemplateNameUnique(AsyncQuery aQuery, String name) {
+    public void isTemplateNameUnique(AsyncQuery aQuery, String templateName, 
Guid datacenterId) {
         aQuery.converterCallback = new IAsyncConverter() {
             @Override
             public Object Convert(Object source, AsyncQuery _asyncQuery)
@@ -1913,8 +1913,10 @@
                 return source != null ? !((Boolean) source).booleanValue() : 
false;
             }
         };
+        NameQueryParameters params = new NameQueryParameters(templateName);
+        params.setDatacenterId(datacenterId);
         
Frontend.getInstance().runQuery(VdcQueryType.IsVmTemlateWithSameNameExist,
-                new NameQueryParameters(name),
+                params,
                 aQuery);
     }
 
diff --git 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/configure/instancetypes/InstanceTypeListModel.java
 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/configure/instancetypes/InstanceTypeListModel.java
index 1a39f16..be77c8e 100644
--- 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/configure/instancetypes/InstanceTypeListModel.java
+++ 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/configure/instancetypes/InstanceTypeListModel.java
@@ -200,7 +200,7 @@
                             VmModel.setIsValid(false);
                         }
                     }
-                }), ((UnitVmModel) getWindow()).getName().getEntity());
+                }), ((UnitVmModel) getWindow()).getName().getEntity(), null);
     }
 
     private void buildVmStatic(VmBase vmBase) {
diff --git 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/templates/TemplateListModel.java
 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/templates/TemplateListModel.java
index 7c76bc9..a349f1a 100644
--- 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/templates/TemplateListModel.java
+++ 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/templates/TemplateListModel.java
@@ -619,7 +619,7 @@
                             
templateListModel.postNameUniqueCheck(isNameUnique);
 
                         }
-                    }), name);
+                    }), name, 
model.getDataCenterWithClustersList().getSelectedItem().getDataCenter().getId());
         } else {
             postNameUniqueCheck(true);
         }
diff --git 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/userportal/UserPortalListModel.java
 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/userportal/UserPortalListModel.java
index 2a4de28..aebf0a5 100644
--- 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/userportal/UserPortalListModel.java
+++ 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/userportal/UserPortalListModel.java
@@ -654,7 +654,7 @@
 
                         }
                     }),
-                    name);
+                    name, 
model.getDataCenterWithClustersList().getSelectedItem().getDataCenter().getId());
         }
     }
 
diff --git 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/VmListModel.java
 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/VmListModel.java
index 2fdd318..438c148 100644
--- 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/VmListModel.java
+++ 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/VmListModel.java
@@ -1346,7 +1346,7 @@
 
                         }
                     }),
-                    name);
+                    name, 
model.getDataCenterWithClustersList().getSelectedItem().getDataCenter().getId());
         }
     }
 
diff --git a/packaging/dbscripts/vm_templates_sp.sql 
b/packaging/dbscripts/vm_templates_sp.sql
index a469c75..95a29c5 100644
--- a/packaging/dbscripts/vm_templates_sp.sql
+++ b/packaging/dbscripts/vm_templates_sp.sql
@@ -458,12 +458,13 @@
 LANGUAGE plpgsql;
 
 
-Create or replace FUNCTION GetVmTemplateByVmtName(v_vmt_name VARCHAR(255), 
v_user_id UUID, v_is_filtered boolean) RETURNS SETOF vm_templates_view STABLE
+Create or replace FUNCTION GetVmTemplateByVmtName(v_storage_pool_id UUID, 
v_vmt_name VARCHAR(255), v_user_id UUID, v_is_filtered boolean) RETURNS SETOF 
vm_templates_view STABLE
    AS $procedure$
 BEGIN
       RETURN QUERY SELECT vm_templates.*
       FROM vm_templates_view vm_templates
       WHERE name = v_vmt_name
+      AND   (v_storage_pool_id is null OR storage_pool_id = v_storage_pool_id) 
       AND (NOT v_is_filtered OR EXISTS (SELECT 1
                                         FROM   
user_vm_template_permissions_view
                                         WHERE  user_id = v_user_id AND 
entity_id = vmt_guid));


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

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

Reply via email to