Skip to content

Commit 380ce1d

Browse files
committed
check chosen ip against used ones instead of available
1 parent efec9a9 commit 380ce1d

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -695,16 +695,16 @@ private void checkUnmanagedNicIpAndNetworkForImport(String instanceName, Unmanag
695695
}
696696
// If network is non L2, IP v4 is assigned and not set to auto-assign, check it is available for network
697697
if (!network.getGuestType().equals(Network.GuestType.L2) && ipAddresses != null && StringUtils.isNotEmpty(ipAddresses.getIp4Address()) && !ipAddresses.getIp4Address().equals("auto")) {
698-
Set<Long> ips = networkModel.getAvailableIps(network, ipAddresses.getIp4Address());
699-
if (LOGGER.isDebugEnabled() && ips != null) {
700-
String s = ips.stream().map(NetUtils::long2Ip).collect(Collectors.joining(", "));
698+
List<String> usedIps = networkModel.getUsedIpsInNetwork(network);
699+
if (LOGGER.isDebugEnabled() && usedIps != null) {
700+
String s = usedIps.stream().collect(Collectors.joining(", "));
701701
LOGGER.debug(String.format("available IPs for network(ID: %s) are [%s]",network.getUuid(), s));
702702
}
703-
if (CollectionUtils.isEmpty(ips)) {
704-
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Cannot assign IP address %s to NIC(ID: %s) as no available IP addresses are found for network(ID: %s)", ipAddresses.getIp4Address(), nic.getNicId(), network.getUuid()));
705-
}
706-
if (!ips.contains(NetUtils.ip2Long(ipAddresses.getIp4Address()))) {
707-
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("IP address %s for NIC(ID: %s) is not available in network(ID: %s)", ipAddresses.getIp4Address(), nic.getNicId(), network.getUuid()));
703+
for (String usedIp : usedIps) {
704+
// only half witted support for ipv6 here :(
705+
if (usedIp.equals(ipAddresses.getIp4Address()) || usedIp.equals(ipAddresses.getIp6Address())) {
706+
throw new ServerApiException(ApiErrorCode.RESOURCE_IN_USE_ERROR, String.format("Cannot assign IP address %s to NIC(ID: %s) as it is in use in network(ID: %s)", ipAddresses.getIp4Address(), nic.getNicId(), network.getUuid()));
707+
}
708708
}
709709
}
710710
}

0 commit comments

Comments
 (0)