Repository: cloudstack Updated Branches: refs/heads/master 33179cce5 -> 5f9e4fddf
CLOUDSTACK-8112. CS allows creation of VM's with the same Display name when vm.instancename.flag is set to true. During VM creation, if vm.instancename.flag is set to true and hypervisor type is VMware, check if VM with the same hostname already exists in the zone. Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/5f9e4fdd Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/5f9e4fdd Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/5f9e4fdd Branch: refs/heads/master Commit: 5f9e4fddf303f312a0b17abc0d837f28042caeda Parents: 33179cc Author: Likitha Shetty <likitha.she...@citrix.com> Authored: Wed Nov 12 17:27:21 2014 +0530 Committer: Likitha Shetty <likitha.she...@citrix.com> Committed: Tue Dec 23 14:00:32 2014 +0530 ---------------------------------------------------------------------- engine/schema/src/com/cloud/vm/dao/VMInstanceDao.java | 2 ++ .../src/com/cloud/vm/dao/VMInstanceDaoImpl.java | 14 ++++++++++++++ server/src/com/cloud/vm/UserVmManagerImpl.java | 6 ++++++ 3 files changed, 22 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/5f9e4fdd/engine/schema/src/com/cloud/vm/dao/VMInstanceDao.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/vm/dao/VMInstanceDao.java b/engine/schema/src/com/cloud/vm/dao/VMInstanceDao.java index 6ba7c36..1e4c8b6 100644 --- a/engine/schema/src/com/cloud/vm/dao/VMInstanceDao.java +++ b/engine/schema/src/com/cloud/vm/dao/VMInstanceDao.java @@ -136,4 +136,6 @@ public interface VMInstanceDao extends GenericDao<VMInstanceVO, Long>, StateDao< void resetHostPowerStateTracking(long hostId); HashMap<String, Long> countVgpuVMs(Long dcId, Long podId, Long clusterId); + + VMInstanceVO findVMByHostNameInZone(String hostName, long zoneId); } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/5f9e4fdd/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java index df023bf..3eabbdb 100644 --- a/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java +++ b/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java @@ -85,6 +85,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem protected SearchBuilder<VMInstanceVO> HostUpSearch; protected SearchBuilder<VMInstanceVO> InstanceNameSearch; protected SearchBuilder<VMInstanceVO> HostNameSearch; + protected SearchBuilder<VMInstanceVO> HostNameAndZoneSearch; protected GenericSearchBuilder<VMInstanceVO, Long> FindIdsOfVirtualRoutersByAccount; protected GenericSearchBuilder<VMInstanceVO, Long> CountRunningByHost; protected GenericSearchBuilder<VMInstanceVO, Long> CountRunningByAccount; @@ -218,6 +219,11 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem HostNameSearch.and("hostName", HostNameSearch.entity().getHostName(), Op.EQ); HostNameSearch.done(); + HostNameAndZoneSearch = createSearchBuilder(); + HostNameAndZoneSearch.and("hostName", HostNameAndZoneSearch.entity().getHostName(), Op.EQ); + HostNameAndZoneSearch.and("zone", HostNameAndZoneSearch.entity().getDataCenterId(), Op.EQ); + HostNameAndZoneSearch.done(); + FindIdsOfVirtualRoutersByAccount = createSearchBuilder(Long.class); FindIdsOfVirtualRoutersByAccount.selectFields(FindIdsOfVirtualRoutersByAccount.entity().getId()); FindIdsOfVirtualRoutersByAccount.and("account", FindIdsOfVirtualRoutersByAccount.entity().getAccountId(), SearchCriteria.Op.EQ); @@ -415,6 +421,14 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem } @Override + public VMInstanceVO findVMByHostNameInZone(String hostName, long zoneId) { + SearchCriteria<VMInstanceVO> sc = HostNameAndZoneSearch.create(); + sc.setParameters("hostName", hostName); + sc.setParameters("zone", zoneId); + return findOneBy(sc); + } + + @Override public void updateProxyId(long id, Long proxyId, Date time) { VMInstanceVO vo = createForUpdate(); vo.setProxyId(proxyId); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/5f9e4fdd/server/src/com/cloud/vm/UserVmManagerImpl.java ---------------------------------------------------------------------- diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 03c9a23..605306e 100644 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -2942,6 +2942,12 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir hostName = generateHostName(uuidName); } } + // If global config vm.instancename.flag is set to true, then CS will set guest VM's name as it appears on the hypervisor, to its hostname. + // In case of VMware since VM name must be unique within a DC, check if VM with the same hostname already exists in the zone. + VMInstanceVO vmByHostName = _vmInstanceDao.findVMByHostNameInZone(hostName, zone.getId()); + if (vmByHostName != null && vmByHostName.getState() != VirtualMachine.State.Expunging) { + throw new InvalidParameterValueException("There already exists a VM by the name: " + hostName + "."); + } } else { if (hostName == null) { //Generate name using uuid and instance.name global config