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.

(cherry picked from commit 5f9e4fddf303f312a0b17abc0d837f28042caeda)
Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
This commit is contained in:
Likitha Shetty 2014-11-12 17:27:21 +05:30 committed by Rohit Yadav
parent 0c1172ffe9
commit f00140243c
3 changed files with 22 additions and 0 deletions

View File

@ -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);
}

View File

@ -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);
@ -414,6 +420,14 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
return findOneBy(sc);
}
@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();

View File

@ -2919,6 +2919,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