Skip to content

Commit d2544d4

Browse files
committed
as: simplify windows vm name
1 parent 6d28bd2 commit d2544d4

File tree

2 files changed

+15
-84
lines changed

2 files changed

+15
-84
lines changed

server/src/main/java/com/cloud/network/as/AutoScaleManagerImpl.java

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,6 @@
195195
import com.google.gson.reflect.TypeToken;
196196

197197
public class AutoScaleManagerImpl extends ManagerBase implements AutoScaleManager, AutoScaleService, Configurable {
198-
// Windows OS has a limit of 15 characters for hostname
199-
// https://learn.microsoft.com/en-us/troubleshoot/windows-server/active-directory/naming-conventions-for-computer-domain-site-ou
200-
protected static final int MAX_WINDOWS_VM_HOSTNAME_LENGTH = 15;
201198

202199
@Inject
203200
protected DispatchChainFactory dispatchChainFactory = null;
@@ -303,6 +300,10 @@ public class AutoScaleManagerImpl extends ManagerBase implements AutoScaleManage
303300
protected static final String VM_HOSTNAME_PREFIX = "autoScaleVm-";
304301
protected static final int VM_HOSTNAME_RANDOM_SUFFIX_LENGTH = 6;
305302

303+
// Windows OS has a limit of 15 characters for hostname
304+
// https://learn.microsoft.com/en-us/troubleshoot/windows-server/active-directory/naming-conventions-for-computer-domain-site-ou
305+
protected static final String WINDOWS_VM_HOSTNAME_PREFIX = "as-WinVm-";
306+
306307
private static final Long DEFAULT_HOST_ID = -1L;
307308

308309
ExecutorService groupExecutor;
@@ -1974,25 +1975,6 @@ public void updateVmDetails(Map<String, String> deployParams, Map<String, String
19741975
}
19751976
}
19761977

1977-
protected String getTrimmedHostNameForWindows(String name) {
1978-
String valid = name.replaceAll("[^a-zA-Z0-9-]", "");
1979-
String hostName = valid.length() > MAX_WINDOWS_VM_HOSTNAME_LENGTH ?
1980-
valid.substring(valid.length() - MAX_WINDOWS_VM_HOSTNAME_LENGTH) :
1981-
valid;
1982-
if (!hostName.isEmpty() && !Character.isLetter(hostName.charAt(0))) {
1983-
for (int i = 0; i < hostName.length(); i++) {
1984-
if (Character.isLetter(hostName.charAt(i))) {
1985-
hostName = hostName.charAt(i) + hostName.substring(i + 1);
1986-
break;
1987-
}
1988-
}
1989-
if (!Character.isLetter(hostName.charAt(0))) {
1990-
hostName = "a" + hostName.substring(hostName.length() < MAX_WINDOWS_VM_HOSTNAME_LENGTH ? 0 : 1);
1991-
}
1992-
}
1993-
return hostName;
1994-
}
1995-
19961978
protected boolean isWindowsOs(VirtualMachineTemplate template) {
19971979
GuestOSVO guestOSVO = guestOSDao.findById(template.getGuestOSId());
19981980
if (guestOSVO == null) {
@@ -2007,15 +1989,15 @@ protected boolean isWindowsOs(VirtualMachineTemplate template) {
20071989

20081990
protected Pair<String, String> getNextVmHostAndDisplayName(AutoScaleVmGroupVO asGroup, VirtualMachineTemplate template) {
20091991
boolean isWindows = isWindowsOs(template);
2010-
String vmHostNameSuffix = "-" + asGroup.getNextVmSeq() + "-" +
2011-
RandomStringUtils.random(VM_HOSTNAME_RANDOM_SUFFIX_LENGTH, 0, 0, true, false, (char[])null, new SecureRandom()).toLowerCase();
1992+
String winVmHostNameSuffix = RandomStringUtils.random(VM_HOSTNAME_RANDOM_SUFFIX_LENGTH, 0, 0, true, false, (char[])null, new SecureRandom()).toLowerCase();
1993+
String vmHostNameSuffix = "-" + asGroup.getNextVmSeq() + "-" + winVmHostNameSuffix;
20121994
// Truncate vm group name because max length of vm name is 63
20131995
int subStringLength = Math.min(asGroup.getName().length(), 63 - VM_HOSTNAME_PREFIX.length() - vmHostNameSuffix.length());
20141996
String name = VM_HOSTNAME_PREFIX + asGroup.getName().substring(0, subStringLength) + vmHostNameSuffix;
2015-
if (!isWindows || name.length() <= MAX_WINDOWS_VM_HOSTNAME_LENGTH) {
1997+
if (!isWindows) {
20161998
return new Pair<>(name, name);
20171999
}
2018-
return new Pair<>(getTrimmedHostNameForWindows(name), name);
2000+
return new Pair<>(WINDOWS_VM_HOSTNAME_PREFIX + winVmHostNameSuffix, name);
20192001
}
20202002

20212003
@Override

server/src/test/java/com/cloud/network/as/AutoScaleManagerImplTest.java

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,12 +2520,14 @@ private void runGetNextVmHostAndDisplayNameGeneratesCorrectHostAndDisplayNameFor
25202520
when(asVmGroupMock.getNextVmSeq()).thenReturn(1L);
25212521
when(templateMock.getGuestOSId()).thenReturn(1L);
25222522
Pair<String, String> result = autoScaleManagerImplSpy.getNextVmHostAndDisplayName(asVmGroupMock, templateMock);
2523-
String vmHostNamePattern = AutoScaleManagerImpl.VM_HOSTNAME_PREFIX + vmGroupName +
2524-
"-" + asVmGroupMock.getNextVmSeq() + "-[a-z]{6}";
2525-
Assert.assertTrue(result.second().matches(vmHostNamePattern));
2523+
String vmHostNamePattern = AutoScaleManagerImpl.WINDOWS_VM_HOSTNAME_PREFIX + "[a-z]{6}";
2524+
Assert.assertTrue(result.first().matches(vmHostNamePattern));
25262525
Assert.assertEquals(15, result.first().length());
2527-
Assert.assertTrue(result.second().endsWith(result.first()));
2528-
2526+
String vmDisplayHostNamePattern = AutoScaleManagerImpl.VM_HOSTNAME_PREFIX + vmGroupName +
2527+
"-" + asVmGroupMock.getNextVmSeq() + "-[a-z]{6}";
2528+
Assert.assertTrue(result.second().matches(vmDisplayHostNamePattern));
2529+
Assert.assertTrue(result.second().length() <= 63);
2530+
Assert.assertTrue(result.second().endsWith(result.first().split("-")[2]));
25292531
}
25302532

25312533
@Test
@@ -2565,57 +2567,4 @@ public void getNextVmHostAndDisplayNameHandlesNullGuestOS() {
25652567
Assert.assertTrue(result.first().matches(vmHostNamePattern));
25662568
Assert.assertEquals(result.first(), result.second());
25672569
}
2568-
2569-
@Test
2570-
public void trimsValidWindowsHostName() {
2571-
AutoScaleManagerImpl manager = new AutoScaleManagerImpl();
2572-
String result = manager.getTrimmedHostNameForWindows("ValidVMName1234");
2573-
Assert.assertEquals("ValidVMName1234", result);
2574-
}
2575-
2576-
@Test
2577-
public void trimsInvalidCharactersFromHostName() {
2578-
AutoScaleManagerImpl manager = new AutoScaleManagerImpl();
2579-
String result = manager.getTrimmedHostNameForWindows("Invalid@Host#Name!");
2580-
Assert.assertEquals("InvalidHostName", result);
2581-
}
2582-
2583-
@Test
2584-
public void ensuresHostNameStartsWithLetter() {
2585-
AutoScaleManagerImpl manager = new AutoScaleManagerImpl();
2586-
String result = manager.getTrimmedHostNameForWindows("123456789012345");
2587-
Assert.assertTrue(Character.isLetter(result.charAt(0)));
2588-
}
2589-
2590-
@Test
2591-
public void prefixesWithLetterIfNoLetterExists() {
2592-
AutoScaleManagerImpl manager = new AutoScaleManagerImpl();
2593-
String result = manager.getTrimmedHostNameForWindows("1234567890-12345");
2594-
Assert.assertEquals("a34567890-12345", result);
2595-
}
2596-
2597-
@Test
2598-
public void trimsHostNameToLastMaxCharacters() {
2599-
String name = "ThisIsAReallyLongHost-Name_1234";
2600-
AutoScaleManagerImpl manager = new AutoScaleManagerImpl();
2601-
String result = manager.getTrimmedHostNameForWindows(name);
2602-
String normalized = name.replace("_", "");
2603-
Assert.assertEquals(normalized
2604-
.substring(normalized.length() - AutoScaleManagerImpl.MAX_WINDOWS_VM_HOSTNAME_LENGTH),
2605-
result);
2606-
}
2607-
2608-
@Test
2609-
public void handlesEmptyHostName() {
2610-
AutoScaleManagerImpl manager = new AutoScaleManagerImpl();
2611-
String result = manager.getTrimmedHostNameForWindows("");
2612-
Assert.assertEquals("", result);
2613-
}
2614-
2615-
@Test
2616-
public void handlesHostNameWithOnlyMostlyInvalidCharacters() {
2617-
AutoScaleManagerImpl manager = new AutoScaleManagerImpl();
2618-
String result = manager.getTrimmedHostNameForWindows("@#$%^&*() 1");
2619-
Assert.assertEquals("a1", result);
2620-
}
26212570
}

0 commit comments

Comments
 (0)