Skip to content

Commit 47615a2

Browse files
author
Alena Prokharchyk
committed
VPC: CS-15583 - hostName for the vm should be unique inside the network domain
1 parent a3d4ee3 commit 47615a2

4 files changed

Lines changed: 73 additions & 3 deletions

File tree

‎server/src/com/cloud/network/dao/NetworkDaoImpl.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ public class NetworkDaoImpl extends GenericDaoBase<NetworkVO, Long> implements N
7171
private final GenericSearchBuilder<NetworkVO, Integer> NetworksCount;
7272
final SearchBuilder<NetworkVO> SourceNATSearch;
7373
final GenericSearchBuilder<NetworkVO, Long> CountByZoneAndURI;
74+
75+
7476
ResourceTagsDaoImpl _tagsDao = ComponentLocator.inject(ResourceTagsDaoImpl.class);
75-
7677
NetworkAccountDaoImpl _accountsDao = ComponentLocator.inject(NetworkAccountDaoImpl.class);
7778
NetworkDomainDaoImpl _domainsDao = ComponentLocator.inject(NetworkDomainDaoImpl.class);
7879
NetworkOpDaoImpl _opDao = ComponentLocator.inject(NetworkOpDaoImpl.class);

‎server/src/com/cloud/vm/UserVmManagerImpl.java‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2403,14 +2403,43 @@ protected UserVm createVirtualMachine(DataCenter zone, ServiceOffering serviceOf
24032403
String instanceName = VirtualMachineName.getVmName(id, owner.getId(), _instance);
24042404

24052405
String uuidName = UUID.randomUUID().toString();
2406+
2407+
//verify hostname information
24062408
if (hostName == null) {
24072409
hostName = uuidName;
24082410
} else {
2409-
// verify hostName (hostname doesn't have to be unique)
2411+
//1) check is hostName is RFC complient
24102412
if (!NetUtils.verifyDomainNameLabel(hostName, true)) {
24112413
throw new InvalidParameterValueException("Invalid name. Vm name can contain ASCII letters 'a' through 'z', the digits '0' through '9', "
24122414
+ "and the hyphen ('-'), must be between 1 and 63 characters long, and can't start or end with \"-\" and can't start with digit");
24132415
}
2416+
//2) hostName has to be unique in the network domain
2417+
Map<String, List<Long>> ntwkDomains = new HashMap<String, List<Long>>();
2418+
for (NetworkVO network : networkList) {
2419+
String ntwkDomain = network.getNetworkDomain();
2420+
if (!ntwkDomains.containsKey(ntwkDomain)) {
2421+
List<Long> ntwkIds = new ArrayList<Long>();
2422+
ntwkIds.add(network.getId());
2423+
ntwkDomains.put(ntwkDomain, ntwkIds);
2424+
} else {
2425+
List<Long> ntwkIds = ntwkDomains.get(ntwkDomain);
2426+
ntwkIds.add(network.getId());
2427+
ntwkDomains.put(ntwkDomain, ntwkIds);
2428+
}
2429+
}
2430+
2431+
for (String ntwkDomain : ntwkDomains.keySet()) {
2432+
for (Long ntwkId : ntwkDomains.get(ntwkDomain)) {
2433+
//* get all vms hostNames in the network
2434+
List<String> hostNames = _vmInstanceDao.listDistinctHostNames(ntwkId);
2435+
//* verify that there are no duplicates
2436+
if (hostNames.contains(hostName)) {
2437+
throw new InvalidParameterValueException("The vm with hostName " + hostName
2438+
+ " already exists in the network domain: " + ntwkDomain + "; network="
2439+
+ _networkMgr.getNetwork(ntwkId));
2440+
}
2441+
}
2442+
}
24142443
}
24152444

24162445
HypervisorType hypervisorType = null;

‎server/src/com/cloud/vm/dao/VMInstanceDao.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.cloud.vm.VMInstanceVO;
2727
import com.cloud.vm.VirtualMachine;
2828
import com.cloud.vm.VirtualMachine.State;
29+
import com.cloud.vm.VirtualMachine.Type;
2930

3031

3132
/*
@@ -100,4 +101,11 @@ public interface VMInstanceDao extends GenericDao<VMInstanceVO, Long>, StateDao<
100101

101102
List<VMInstanceVO> listNonRemovedVmsByTypeAndNetwork(long networkId, VirtualMachine.Type... types);
102103

104+
/**
105+
* @param networkId
106+
* @param types
107+
* @return
108+
*/
109+
List<String> listDistinctHostNames(long networkId, VirtualMachine.Type... types);
110+
103111
}

‎server/src/com/cloud/vm/dao/VMInstanceDaoImpl.java‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
7878
protected GenericSearchBuilder<VMInstanceVO, Long> CountRunningByHost;
7979
protected GenericSearchBuilder<VMInstanceVO, Long> CountRunningByAccount;
8080
protected SearchBuilder<VMInstanceVO> NetworkTypeSearch;
81+
protected GenericSearchBuilder<VMInstanceVO, String> DistinctHostNameSearch;
82+
8183
ResourceTagsDaoImpl _tagsDao = ComponentLocator.inject(ResourceTagsDaoImpl.class);
84+
NicDao _nicDao = ComponentLocator.inject(NicDaoImpl.class);
8285

8386
protected final Attribute _updateTimeAttr;
8487

@@ -98,6 +101,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
98101
protected final HostDaoImpl _hostDao = ComponentLocator.inject(HostDaoImpl.class);
99102

100103
protected VMInstanceDaoImpl() {
104+
101105
IdStatesSearch = createSearchBuilder();
102106
IdStatesSearch.and("id", IdStatesSearch.entity().getId(), Op.EQ);
103107
IdStatesSearch.and("states", IdStatesSearch.entity().getState(), Op.IN);
@@ -533,7 +537,7 @@ public Long countRunningByAccount(long accountId){
533537
@Override
534538
public List<VMInstanceVO> listNonRemovedVmsByTypeAndNetwork(long networkId, VirtualMachine.Type... types) {
535539
if (NetworkTypeSearch == null) {
536-
NicDao _nicDao = ComponentLocator.getLocator("management-server").getDao(NicDao.class);
540+
537541
SearchBuilder<NicVO> nicSearch = _nicDao.createSearchBuilder();
538542
nicSearch.and("networkId", nicSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
539543

@@ -554,6 +558,34 @@ public List<VMInstanceVO> listNonRemovedVmsByTypeAndNetwork(long networkId, Virt
554558
return listBy(sc);
555559
}
556560

561+
562+
563+
@Override
564+
public List<String> listDistinctHostNames(long networkId, VirtualMachine.Type... types) {
565+
if (DistinctHostNameSearch == null) {
566+
567+
SearchBuilder<NicVO> nicSearch = _nicDao.createSearchBuilder();
568+
nicSearch.and("networkId", nicSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
569+
570+
DistinctHostNameSearch = createSearchBuilder(String.class);
571+
DistinctHostNameSearch.selectField(DistinctHostNameSearch.entity().getHostName());
572+
573+
DistinctHostNameSearch.and("types", DistinctHostNameSearch.entity().getType(), SearchCriteria.Op.IN);
574+
DistinctHostNameSearch.and("removed", DistinctHostNameSearch.entity().getRemoved(), SearchCriteria.Op.NULL);
575+
DistinctHostNameSearch.join("nicSearch", nicSearch, DistinctHostNameSearch.entity().getId(),
576+
nicSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER);
577+
DistinctHostNameSearch.done();
578+
}
579+
580+
SearchCriteria<String> sc = DistinctHostNameSearch.create();
581+
if (types != null && types.length != 0) {
582+
sc.setParameters("types", (Object[]) types);
583+
}
584+
sc.setJoinParameters("nicSearch", "networkId", networkId);
585+
586+
return customSearch(sc, null);
587+
}
588+
557589
@Override
558590
@DB
559591
public boolean remove(Long id) {

0 commit comments

Comments
 (0)