|
52 | 52 | import javax.xml.parsers.DocumentBuilder; |
53 | 53 | import javax.xml.parsers.ParserConfigurationException; |
54 | 54 |
|
55 | | -import com.cloud.network.vpc.Vpc; |
56 | 55 | import org.apache.cloudstack.acl.ControlledEntity; |
57 | 56 | import org.apache.cloudstack.acl.ControlledEntity.ACLType; |
58 | 57 | import org.apache.cloudstack.acl.SecurityChecker.AccessType; |
@@ -4430,106 +4429,85 @@ protected void verifyIfHypervisorSupportsRootdiskSizeOverride(HypervisorType hyp |
4430 | 4429 | } |
4431 | 4430 | } |
4432 | 4431 |
|
4433 | | - private Map<String, Set<Long>> getNetworkIdPerNetworkDomain(List<? extends Network> networkList){ |
4434 | | - Map<String, Set<Long>> ntwkDomains = new HashMap<>(); |
4435 | | - Set<Long> vpcIds = new HashSet<>(); |
| 4432 | + private List<NetworkVO> getNetworksWithSameNetworkDomainInDomains(List<NetworkVO> networkList, boolean checkSubDomains) { |
| 4433 | + List<String> uniqueNtwkDomains = networkList.stream().map(NetworkVO::getNetworkDomain).collect(Collectors.toList()); |
| 4434 | + List<Long> domainIdList = new ArrayList<>(); |
4436 | 4435 | for (Network network : networkList) { |
4437 | | - String ntwkDomain = network.getNetworkDomain(); |
4438 | | - if (network.getVpcId() != null) { |
4439 | | - vpcIds.add(network.getVpcId()); |
4440 | | - } |
4441 | | - Set<Long> ntwkIds; |
4442 | | - if (!ntwkDomains.containsKey(ntwkDomain)) { |
4443 | | - ntwkIds = new HashSet<>(); |
4444 | | - } else { |
4445 | | - ntwkIds = ntwkDomains.get(ntwkDomain); |
4446 | | - } |
4447 | | - ntwkIds.add(network.getId()); |
4448 | | - ntwkDomains.put(ntwkDomain, ntwkIds); |
| 4436 | + domainIdList.add(network.getDomainId()); |
4449 | 4437 | } |
4450 | | - |
4451 | | - for (Long vpcId : vpcIds) { |
4452 | | - Vpc vpc = _vpcMgr.getActiveVpc(vpcId); |
4453 | | - List<NetworkVO> networks = _networkDao.listByVpc(vpcId); |
4454 | | - String ntwkDomain = vpc.getNetworkDomain(); |
4455 | | - |
4456 | | - Set<Long> ntwkIds; |
4457 | | - if (!ntwkDomains.containsKey(ntwkDomain)) { |
4458 | | - ntwkIds = new HashSet<>(); |
4459 | | - for (NetworkVO network : networks) { |
4460 | | - ntwkIds.add(network.getId()); |
4461 | | - } |
4462 | | - } else { |
4463 | | - ntwkIds = ntwkDomains.get(ntwkDomain); |
4464 | | - for (NetworkVO network : networks) { |
4465 | | - ntwkIds.add(network.getId()); |
4466 | | - } |
| 4438 | + Set<Long> finalDomainIdList = new HashSet<>(domainIdList); |
| 4439 | + if (checkSubDomains) { |
| 4440 | + for (Long domainId : domainIdList) { |
| 4441 | + DomainVO domain = _domainDao.findById(domainId); |
| 4442 | + List<Long> childDomainIds = _domainDao.getDomainChildrenIds(domain.getPath()); |
| 4443 | + finalDomainIdList.addAll(childDomainIds); |
4467 | 4444 | } |
4468 | | - ntwkDomains.put(ntwkDomain, ntwkIds); |
4469 | 4445 | } |
4470 | | - return ntwkDomains; |
| 4446 | + return _networkDao.listByNetworkDomainsAndDomainIds(uniqueNtwkDomains, finalDomainIdList.stream().collect(Collectors.toList())); |
4471 | 4447 | } |
4472 | 4448 |
|
4473 | | - private void checkIfHostNameUniqueInNtwkDomain(String hostName, List<? extends Network> networkList) { |
4474 | | - // Check that hostName is unique in the network domain |
4475 | | - Set<Long> domainIdList = new HashSet<>(); |
| 4449 | + private List<NetworkVO> getNetworksForCheckUniqueHostName(List<NetworkVO> networkList) { |
| 4450 | + List<NetworkVO> finalNetworkList; |
| 4451 | + List<String> uniqueNtwkDomains; |
4476 | 4452 | switch (VmDistinctHostNameScope.value()) { |
4477 | 4453 | case "global": |
4478 | | - // Check that hostName is unique in the zone |
4479 | | - VMInstanceVO vm = _vmInstanceDao.findVMByHostName(hostName); |
4480 | | - if (vm != null) { |
4481 | | - throw new InvalidParameterValueException("The vm with hostName " + hostName + " already exists in the zone"); |
4482 | | - } |
| 4454 | + uniqueNtwkDomains = networkList.stream().map(NetworkVO::getNetworkDomain).collect(Collectors.toList()); |
| 4455 | + finalNetworkList = _networkDao.listByNetworkDomains(uniqueNtwkDomains); |
| 4456 | + break; |
4483 | 4457 | case "domain": |
4484 | | - // Check that hostName is unique in the domain |
4485 | | - for (Network network : networkList) { |
4486 | | - domainIdList.add(network.getDomainId()); |
4487 | | - } |
4488 | | - if (_vmInstanceDao.hostNameExistsInDomainIds(hostName, domainIdList)) { |
4489 | | - throw new InvalidParameterValueException("The vm with hostName " + hostName + " already exists in the domain"); |
4490 | | - } |
| 4458 | + finalNetworkList = getNetworksWithSameNetworkDomainInDomains(networkList, false); |
4491 | 4459 | break; |
4492 | 4460 | case "subdomain": |
4493 | | - for (Network network : networkList) { |
4494 | | - domainIdList.add(network.getDomainId()); |
4495 | | - } |
4496 | | - Set<Long> finalDomainIdList = new HashSet<>(); |
4497 | | - for (Long domainId : domainIdList) { |
4498 | | - finalDomainIdList.add(domainId); |
4499 | | - DomainVO domain = _domainDao.findById(domainId); |
4500 | | - List<Long> childDomainIds = _domainDao.getDomainChildrenIds(domain.getPath()); |
4501 | | - finalDomainIdList.addAll(childDomainIds); |
4502 | | - } |
4503 | | - |
4504 | | - if (_vmInstanceDao.hostNameExistsInDomainIds(hostName, finalDomainIdList)) { |
4505 | | - throw new InvalidParameterValueException("The vm with hostName " + hostName + " already exists in the domain or subdomain"); |
4506 | | - } |
| 4461 | + finalNetworkList = getNetworksWithSameNetworkDomainInDomains(networkList, true); |
4507 | 4462 | break; |
4508 | 4463 | case "account": |
4509 | | - // Check that hostName is unique in the account |
4510 | | - Set<Long> accountIdList = new HashSet<>(); |
4511 | | - for (Network network : networkList) { |
4512 | | - accountIdList.add(network.getAccountId()); |
4513 | | - } |
4514 | | - if (_vmInstanceDao.hostNameExistsInDomainIdsAccountIds(hostName, accountIdList)) { |
4515 | | - throw new InvalidParameterValueException("The vm with hostName " + hostName + " already exists in the account"); |
4516 | | - } |
| 4464 | + uniqueNtwkDomains = networkList.stream().map(NetworkVO::getNetworkDomain).collect(Collectors.toList()); |
| 4465 | + List<Long> accountIds = networkList.stream().map(Network::getAccountId).collect(Collectors.toList()); |
| 4466 | + finalNetworkList = _networkDao.listByNetworkDomainsAndAccountIds(uniqueNtwkDomains, accountIds); |
4517 | 4467 | break; |
4518 | 4468 | default: |
4519 | | - Map<String, Set<Long>> ntwkDomains = getNetworkIdPerNetworkDomain(networkList); |
4520 | | - for (Entry<String, Set<Long>> ntwkDomain : ntwkDomains.entrySet()) { |
4521 | | - for (Long ntwkId : ntwkDomain.getValue()) { |
4522 | | - // * get all vms hostNames in the network |
4523 | | - List<String> hostNames = _vmInstanceDao.listDistinctHostNames(ntwkId); |
4524 | | - // * verify that there are no duplicates |
4525 | | - if (hostNames.contains(hostName)) { |
4526 | | - throw new InvalidParameterValueException("The vm with hostName " + hostName + " already exists in the network domain: " + ntwkDomain.getKey() + "; network=" |
4527 | | - + ((_networkModel.getNetwork(ntwkId) != null) ? _networkModel.getNetwork(ntwkId).getName() : "<unknown>")); |
4528 | | - } |
4529 | | - } |
| 4469 | + Set<Long> vpcIds = networkList.stream().map(Network::getVpcId).filter(Objects::nonNull).collect(Collectors.toSet()); |
| 4470 | + finalNetworkList = new ArrayList<>(networkList); |
| 4471 | + for (Long vpcId : vpcIds) { |
| 4472 | + finalNetworkList.addAll(_networkDao.listByVpc(vpcId)); |
4530 | 4473 | } |
| 4474 | + break; |
4531 | 4475 | } |
| 4476 | + return finalNetworkList; |
| 4477 | + } |
| 4478 | + |
| 4479 | + private Map<String, Set<Long>> getNetworkIdPerNetworkDomain(List<NetworkVO> networkList) { |
| 4480 | + Map<String, Set<Long>> ntwkDomains = new HashMap<>(); |
4532 | 4481 |
|
| 4482 | + List<NetworkVO> updatedNetworkList = getNetworksForCheckUniqueHostName(networkList); |
| 4483 | + for (Network network : updatedNetworkList) { |
| 4484 | + String ntwkDomain = network.getNetworkDomain(); |
| 4485 | + Set<Long> ntwkIds; |
| 4486 | + if (!ntwkDomains.containsKey(ntwkDomain)) { |
| 4487 | + ntwkIds = new HashSet<>(); |
| 4488 | + } else { |
| 4489 | + ntwkIds = ntwkDomains.get(ntwkDomain); |
| 4490 | + } |
| 4491 | + ntwkIds.add(network.getId()); |
| 4492 | + ntwkDomains.put(ntwkDomain, ntwkIds); |
| 4493 | + } |
| 4494 | + return ntwkDomains; |
| 4495 | + } |
| 4496 | + |
| 4497 | + private void checkIfHostNameUniqueInNtwkDomain(String hostName, List<NetworkVO> networkList) { |
| 4498 | + // Check that hostName is unique |
| 4499 | + Map<String, Set<Long>> ntwkDomains = getNetworkIdPerNetworkDomain(networkList); |
| 4500 | + for (Entry<String, Set<Long>> ntwkDomain : ntwkDomains.entrySet()) { |
| 4501 | + for (Long ntwkId : ntwkDomain.getValue()) { |
| 4502 | + // * get all vms hostNames in the network |
| 4503 | + List<String> hostNames = _vmInstanceDao.listDistinctHostNames(ntwkId); |
| 4504 | + // * verify that there are no duplicates |
| 4505 | + if (hostNames.contains(hostName)) { |
| 4506 | + throw new InvalidParameterValueException("The vm with hostName " + hostName + " already exists in the network domain: " + ntwkDomain.getKey() + "; network=" |
| 4507 | + + ((_networkModel.getNetwork(ntwkId) != null) ? _networkModel.getNetwork(ntwkId).getName() : "<unknown>")); |
| 4508 | + } |
| 4509 | + } |
| 4510 | + } |
4533 | 4511 | } |
4534 | 4512 |
|
4535 | 4513 | private String generateHostName(String uuidName) { |
|
0 commit comments