Skip to content

Commit 42369dc

Browse files
shwstpprvishesh92
authored andcommitted
server: add duplicate host check
Fixes #13080 Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 6bf5881 commit 42369dc

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,8 @@ private List<HostVO> discoverHostsFull(final Long dcId, final Long podId, Long c
799799
throw new InvalidParameterValueException(url + " is not a valid uri");
800800
}
801801

802+
checkForDuplicateHost(url);
803+
802804
final List<HostVO> hosts = new ArrayList<>();
803805
logger.info("Trying to add a new host at {} in data center {}", url, zone);
804806
boolean isHypervisorTypeSupported = false;
@@ -2609,6 +2611,24 @@ private Host createHostAndAgent(final ServerResource resource, final Map<String,
26092611
return host;
26102612
}
26112613

2614+
void checkForDuplicateHost(final String url) {
2615+
String hostIpOrName = null;
2616+
try {
2617+
hostIpOrName = new URI(UriUtils.encodeURIComponent(url)).getHost();
2618+
} catch (final URISyntaxException ignore) {
2619+
// unparseable URL - discoverer will reject it shortly anyway
2620+
}
2621+
if (StringUtils.isBlank(hostIpOrName)) {
2622+
return;
2623+
}
2624+
final HostVO existingByIp = _hostDao.findByIp(hostIpOrName);
2625+
if (existingByIp != null) {
2626+
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()));
2629+
}
2630+
}
2631+
26122632
private Host createHostAndAgentDeferred(final ServerResource resource, final Map<String, String> details, final boolean old, final List<String> hostTags, final boolean forRebalance) {
26132633
HostVO host = null;
26142634
StartupCommand[] cmds = null;

server/src/test/java/com/cloud/resource/ResourceManagerImplTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,19 @@ public void tearDown() throws Exception {
219219
closeable.close();
220220
}
221221

222+
@Test(expected = InvalidParameterValueException.class)
223+
public void testCheckForDuplicateHostThrowsWhenIpAlreadyExists() {
224+
when(hostDao.findByIp("10.0.0.10")).thenReturn(host);
225+
resourceManager.checkForDuplicateHost("http://10.0.0.10");
226+
}
227+
228+
@Test
229+
public void testCheckForDuplicateHostAllowsUniqueHost() {
230+
when(hostDao.findByIp("10.0.0.30")).thenReturn(null);
231+
resourceManager.checkForDuplicateHost("http://10.0.0.30");
232+
verify(hostDao, times(1)).findByIp("10.0.0.30");
233+
}
234+
222235
@Test
223236
public void testCheckAndMaintainEnterMaintenanceModeNoVms() throws NoTransitionException {
224237
// Test entering into maintenance with no VMs running on host.

0 commit comments

Comments
 (0)