Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions server/src/main/java/com/cloud/resource/ResourceManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -2281,6 +2282,27 @@ private HostVO getNewHost(StartupCommand[] startupCommands) {
return null;
}

protected void validateExistingHostLocationImmutable(final HostVO host, final boolean newHost,
final long dcId, final Long podId, final Long clusterId, final StartupCommand startup) {
if (newHost || host == null || host.getType() != Host.Type.Routing) {
Comment thread
vishesh92 marked this conversation as resolved.
return;
}
final Long existingDcId = host.getDataCenterId();
final Long existingPodId = host.getPodId();
final Long existingClusterId = host.getClusterId();
if (existingDcId == null || existingPodId == null || existingClusterId == null) {
return;
}
if (existingDcId == dcId && Objects.equals(existingPodId, podId) && Objects.equals(existingClusterId, clusterId)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (existingDcId == dcId && Objects.equals(existingPodId, podId) && Objects.equals(existingClusterId, clusterId)) {
if (Objects.equals(existingDcId, dcId) && Objects.equals(existingPodId, podId) && Objects.equals(existingClusterId, clusterId)) {

return;
}
final String identity = host.getUuid() != null ? host.getUuid() : host.getGuid();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final String identity = host.getUuid() != null ? host.getUuid() : host.getGuid();
final String identity = ObjectUtils.defaultIfNull(host.getUuid(), host.getGuid());

final String ip = startup != null ? startup.getPrivateIpAddress() : "unknown";
throw new InvalidParameterValueException(String.format(
"Host %s (ip: %s) is already registered in [zone: %d, pod: %d, cluster: %d] and cannot be re-added or reconnected with [zone: %d, pod: %s, cluster: %s]. Zone, pod and cluster of an existing host are immutable.",
Comment thread
vishesh92 marked this conversation as resolved.
Outdated
identity, ip, existingDcId, existingPodId, existingClusterId, dcId, podId, clusterId));
}

protected HostVO createHostVO(final StartupCommand[] cmds, final ServerResource resource, final Map<String, String> details, List<String> hostTags,
final ResourceStateAdapter.Event stateEvent) {
boolean newHost = false;
Expand Down Expand Up @@ -2356,6 +2378,8 @@ protected HostVO createHostVO(final StartupCommand[] cmds, final ServerResource
}
}

validateExistingHostLocationImmutable(host, newHost, dcId, podId, clusterId, startup);

host.setDataCenterId(dc.getId());
host.setPodId(podId);
host.setClusterId(clusterId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.cloud.resource;

import com.cloud.agent.AgentManager;
import com.cloud.agent.api.StartupCommand;
import com.cloud.agent.api.GetVncPortAnswer;
import com.cloud.agent.api.GetVncPortCommand;
import com.cloud.capacity.dao.CapacityDao;
Expand Down Expand Up @@ -335,6 +336,74 @@ public void testGetHostCredentialsMissingParameter() {
resourceManager.getHostCredentials(host);
}

private HostVO mockExistingRoutingHost(long dcId, Long podId, Long clusterId) {
HostVO existing = Mockito.mock(HostVO.class);
when(existing.getType()).thenReturn(Host.Type.Routing);
when(existing.getDataCenterId()).thenReturn(dcId);
when(existing.getPodId()).thenReturn(podId);
when(existing.getClusterId()).thenReturn(clusterId);
when(existing.getUuid()).thenReturn("host-uuid");
return existing;
}

@Test(expected = InvalidParameterValueException.class)
public void testValidateExistingHostLocationImmutableRejectsZoneChange() {
HostVO existing = mockExistingRoutingHost(1L, 10L, 100L);
StartupCommand startup = Mockito.mock(StartupCommand.class);
when(startup.getPrivateIpAddress()).thenReturn("10.10.10.10");
resourceManager.validateExistingHostLocationImmutable(existing, false, 2L, 10L, 100L, startup);
}

@Test(expected = InvalidParameterValueException.class)
public void testValidateExistingHostLocationImmutableRejectsPodChange() {
HostVO existing = mockExistingRoutingHost(1L, 10L, 100L);
StartupCommand startup = Mockito.mock(StartupCommand.class);
when(startup.getPrivateIpAddress()).thenReturn("10.10.10.10");
resourceManager.validateExistingHostLocationImmutable(existing, false, 1L, 11L, 100L, startup);
}

@Test(expected = InvalidParameterValueException.class)
public void testValidateExistingHostLocationImmutableRejectsClusterChange() {
HostVO existing = mockExistingRoutingHost(1L, 10L, 100L);
StartupCommand startup = Mockito.mock(StartupCommand.class);
when(startup.getPrivateIpAddress()).thenReturn("10.10.10.10");
resourceManager.validateExistingHostLocationImmutable(existing, false, 1L, 10L, 101L, startup);
}

@Test
public void testValidateExistingHostLocationImmutableAllowsSameTupleReconnect() {
HostVO existing = mockExistingRoutingHost(1L, 10L, 100L);
resourceManager.validateExistingHostLocationImmutable(existing, false, 1L, 10L, 100L, null);
}

@Test
public void testValidateExistingHostLocationImmutableAllowsNewHost() {
HostVO existing = mockExistingRoutingHost(2L, 20L, 200L);
resourceManager.validateExistingHostLocationImmutable(existing, true, 1L, 10L, 100L, null);
}

@Test
public void testValidateExistingHostLocationImmutableSkipsNonRoutingHost() {
HostVO existing = Mockito.mock(HostVO.class);
when(existing.getType()).thenReturn(Host.Type.SecondaryStorageVM);
resourceManager.validateExistingHostLocationImmutable(existing, false, 1L, 10L, 100L, null);
}

@Test
public void testValidateExistingHostLocationImmutableSkipsPartialLocationRow() {
HostVO existing = Mockito.mock(HostVO.class);
when(existing.getType()).thenReturn(Host.Type.Routing);
when(existing.getDataCenterId()).thenReturn(1L);
when(existing.getPodId()).thenReturn(null);
when(existing.getClusterId()).thenReturn(null);
resourceManager.validateExistingHostLocationImmutable(existing, false, 2L, 10L, 100L, null);
}

@Test
public void testValidateExistingHostLocationImmutableSkipsNullExistingHost() {
resourceManager.validateExistingHostLocationImmutable(null, false, 2L, 10L, 100L, null);
}

@Test
public void testGetHostCredentials() {
Ternary<String, String, String> credentials = resourceManager.getHostCredentials(host);
Expand Down
Loading