Skip to content

Commit 6aee0d9

Browse files
committed
Resolve IP from the hostname to avoid conflicts
1 parent 42369dc commit 6aee0d9

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

server/src/main/java/com/cloud/resource/ResourceManagerImpl.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import static com.cloud.configuration.ConfigurationManagerImpl.MIGRATE_VM_ACROSS_CLUSTERS;
2020
import static com.cloud.configuration.ConfigurationManagerImpl.SET_HOST_DOWN_TO_MAINTENANCE;
2121

22+
import java.net.InetAddress;
2223
import java.net.URI;
2324
import java.net.URISyntaxException;
2425
import java.net.URLDecoder;
26+
import java.net.UnknownHostException;
2527
import java.util.ArrayList;
2628
import java.util.Arrays;
2729
import java.util.Collections;
@@ -2613,19 +2615,22 @@ private Host createHostAndAgent(final ServerResource resource, final Map<String,
26132615

26142616
void checkForDuplicateHost(final String url) {
26152617
String hostIpOrName = null;
2618+
String ipAddress = null;
26162619
try {
26172620
hostIpOrName = new URI(UriUtils.encodeURIComponent(url)).getHost();
2618-
} catch (final URISyntaxException ignore) {
2619-
// unparseable URL - discoverer will reject it shortly anyway
2621+
InetAddress ip = InetAddress.getByName(hostIpOrName);
2622+
ipAddress = ip.getHostAddress();
2623+
} catch (final URISyntaxException | UnknownHostException ignore) {
2624+
// unparseable URL or unknown host - discoverer will reject it shortly anyway
26202625
}
26212626
if (StringUtils.isBlank(hostIpOrName)) {
26222627
return;
26232628
}
2624-
final HostVO existingByIp = _hostDao.findByIp(hostIpOrName);
2629+
final HostVO existingByIp = _hostDao.findByIp(ipAddress);
26252630
if (existingByIp != null) {
26262631
throw new InvalidParameterValueException(String.format(
2627-
"A host with IP address / hostname '%s' already exists (id: %s). Remove it before adding again.",
2628-
hostIpOrName, existingByIp.getUuid()));
2632+
"A host with IP address / hostname '%s' (%s) already exists (id: %s). Remove it before adding again.",
2633+
hostIpOrName, ipAddress, existingByIp.getUuid()));
26292634
}
26302635
}
26312636

0 commit comments

Comments
 (0)