|
174 | 174 | import org.apache.log4j.Logger; |
175 | 175 |
|
176 | 176 | import javax.inject.Inject; |
| 177 | +import java.net.InetAddress; |
| 178 | +import java.net.UnknownHostException; |
177 | 179 | import java.util.ArrayList; |
178 | 180 | import java.util.Arrays; |
179 | 181 | import java.util.HashMap; |
@@ -695,16 +697,30 @@ private void checkUnmanagedNicIpAndNetworkForImport(String instanceName, Unmanag |
695 | 697 | } |
696 | 698 | // If network is non L2, IP v4 is assigned and not set to auto-assign, check it is available for network |
697 | 699 | if (!network.getGuestType().equals(Network.GuestType.L2) && ipAddresses != null && StringUtils.isNotEmpty(ipAddresses.getIp4Address()) && !ipAddresses.getIp4Address().equals("auto")) { |
698 | | - List<String> usedIps = networkModel.getUsedIpsInNetwork(network); |
699 | | - if (LOGGER.isDebugEnabled() && usedIps != null) { |
700 | | - String s = usedIps.stream().collect(Collectors.joining(", ")); |
701 | | - LOGGER.debug(String.format("available IPs for network(ID: %s) are [%s]",network.getUuid(), s)); |
702 | | - } |
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 | | - } |
| 700 | + canIpBeUsedForNicInNetwork(nic, network, ipAddresses); |
| 701 | + } |
| 702 | + } |
| 703 | + |
| 704 | + private void canIpBeUsedForNicInNetwork(UnmanagedInstanceTO.Nic nic, Network network, Network.IpAddresses ipAddresses) { |
| 705 | + List<String> usedIps = networkModel.getUsedIpsInNetwork(network); |
| 706 | + if (LOGGER.isDebugEnabled() && usedIps != null) { |
| 707 | + String s = usedIps.stream().collect(Collectors.joining(", ")); |
| 708 | + LOGGER.debug(String.format("used IPs for network(ID: %s) to check against are [%s]", network.getUuid(), s)); |
| 709 | + } |
| 710 | + for (String usedIp : usedIps) { |
| 711 | + // only half witted support for ipv6 here :( |
| 712 | + if (usedIp.equals(ipAddresses.getIp4Address()) || usedIp.equals(ipAddresses.getIp6Address())) { |
| 713 | + 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())); |
| 714 | + } |
| 715 | + } |
| 716 | + if (network.getCidr() != null) { |
| 717 | + String[] cidrs = Arrays.asList(network.getCidr()).toArray(new String[0]); |
| 718 | + try { |
| 719 | + NetUtils.isIpInCidrList(InetAddress.getByName(ipAddresses.getIp4Address()), cidrs); |
| 720 | + } catch (UnknownHostException e) { |
| 721 | + String msg = String.format("request an invalid IP address during import %s, ignoring", ipAddresses.getIp4Address()); |
| 722 | + LOGGER.warn(msg); |
| 723 | + throw new ServerApiException(ApiErrorCode.MALFORMED_PARAMETER_ERROR, msg, e); |
708 | 724 | } |
709 | 725 | } |
710 | 726 | } |
|
0 commit comments