nvazquez commented on code in PR #10212:
URL: https://github.com/apache/cloudstack/pull/10212#discussion_r2393140645


##########
server/src/main/java/com/cloud/vm/UserVmManagerImpl.java:
##########
@@ -4691,23 +4691,75 @@ protected void 
verifyIfHypervisorSupportsRootdiskSizeOverride(HypervisorType hyp
         }
     }
 
-    private void checkIfHostNameUniqueInNtwkDomain(String hostName, List<? 
extends Network> networkList) {
-        // Check that hostName is unique in the network domain
-        Map<String, List<Long>> ntwkDomains = new HashMap<String, 
List<Long>>();
+    private List<NetworkVO> 
getNetworksWithSameNetworkDomainInDomains(List<NetworkVO> networkList, boolean 
checkSubDomains) {
+        List<String> uniqueNtwkDomains = 
networkList.stream().map(NetworkVO::getNetworkDomain).collect(Collectors.toList());
+        List<Long> domainIdList = new ArrayList<>();
         for (Network network : networkList) {
+            domainIdList.add(network.getDomainId());
+        }
+        Set<Long> finalDomainIdList = new HashSet<>(domainIdList);
+        if (checkSubDomains) {
+            for (Long domainId : domainIdList) {
+                DomainVO domain = _domainDao.findById(domainId);
+                List<Long> childDomainIds = 
_domainDao.getDomainChildrenIds(domain.getPath());
+                finalDomainIdList.addAll(childDomainIds);
+            }
+        }
+        return _networkDao.listByNetworkDomainsAndDomainIds(uniqueNtwkDomains, 
finalDomainIdList.stream().collect(Collectors.toList()));
+    }
+
+    private List<NetworkVO> getNetworksForCheckUniqueHostName(List<NetworkVO> 
networkList) {
+        List<NetworkVO> finalNetworkList;
+        List<String> uniqueNtwkDomains;
+        switch (VmDistinctHostNameScope.value()) {
+            case "global":
+                uniqueNtwkDomains = 
networkList.stream().map(NetworkVO::getNetworkDomain).collect(Collectors.toList());

Review Comment:
   I think `uniqueNtwDomains` should be a Set instead of a List, otherwise 
there could be duplicates



##########
server/src/main/java/com/cloud/vm/UserVmManager.java:
##########
@@ -83,6 +83,12 @@ public interface UserVmManager extends UserVmService {
             "If set to true, tags specified in `resource.limit.host.tags` are 
also included in vm.strict.host.tags.",
             true);
 
+    ConfigKey<String> VmDistinctHostNameScope = new ConfigKey<>(String.class, 
"vm.distinct.hostname.scope", ConfigKey.CATEGORY_ADVANCED,
+            "network",
+            "Scope of resources to check while checking if the hostname is 
unique. Possible values are global, domain, subdomain, account, network.",

Review Comment:
   Maybe the first part of the description could be improved a bit?



##########
server/src/main/java/com/cloud/vm/UserVmManagerImpl.java:
##########
@@ -4691,23 +4691,75 @@ protected void 
verifyIfHypervisorSupportsRootdiskSizeOverride(HypervisorType hyp
         }
     }
 
-    private void checkIfHostNameUniqueInNtwkDomain(String hostName, List<? 
extends Network> networkList) {
-        // Check that hostName is unique in the network domain
-        Map<String, List<Long>> ntwkDomains = new HashMap<String, 
List<Long>>();
+    private List<NetworkVO> 
getNetworksWithSameNetworkDomainInDomains(List<NetworkVO> networkList, boolean 
checkSubDomains) {
+        List<String> uniqueNtwkDomains = 
networkList.stream().map(NetworkVO::getNetworkDomain).collect(Collectors.toList());
+        List<Long> domainIdList = new ArrayList<>();
         for (Network network : networkList) {
+            domainIdList.add(network.getDomainId());
+        }
+        Set<Long> finalDomainIdList = new HashSet<>(domainIdList);
+        if (checkSubDomains) {
+            for (Long domainId : domainIdList) {
+                DomainVO domain = _domainDao.findById(domainId);
+                List<Long> childDomainIds = 
_domainDao.getDomainChildrenIds(domain.getPath());
+                finalDomainIdList.addAll(childDomainIds);
+            }
+        }
+        return _networkDao.listByNetworkDomainsAndDomainIds(uniqueNtwkDomains, 
finalDomainIdList.stream().collect(Collectors.toList()));
+    }
+
+    private List<NetworkVO> getNetworksForCheckUniqueHostName(List<NetworkVO> 
networkList) {
+        List<NetworkVO> finalNetworkList;
+        List<String> uniqueNtwkDomains;
+        switch (VmDistinctHostNameScope.value()) {
+            case "global":
+                uniqueNtwkDomains = 
networkList.stream().map(NetworkVO::getNetworkDomain).collect(Collectors.toList());
+                finalNetworkList = 
_networkDao.listByNetworkDomains(uniqueNtwkDomains);
+                break;
+            case "domain":
+                finalNetworkList = 
getNetworksWithSameNetworkDomainInDomains(networkList, false);
+                break;
+            case "subdomain":
+                finalNetworkList = 
getNetworksWithSameNetworkDomainInDomains(networkList, true);
+                break;
+            case "account":
+                uniqueNtwkDomains = 
networkList.stream().map(NetworkVO::getNetworkDomain).collect(Collectors.toList());
+                List<Long> accountIds = 
networkList.stream().map(Network::getAccountId).collect(Collectors.toList());

Review Comment:
   Same here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to