Skip to content

Commit 6d28bd2

Browse files
shwstpprweizhouapache
authored andcommitted
change
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 0e9fd67 commit 6d28bd2

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@
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;
198201

199202
@Inject
200203
protected DispatchChainFactory dispatchChainFactory = null;
@@ -1973,7 +1976,9 @@ public void updateVmDetails(Map<String, String> deployParams, Map<String, String
19731976

19741977
protected String getTrimmedHostNameForWindows(String name) {
19751978
String valid = name.replaceAll("[^a-zA-Z0-9-]", "");
1976-
String hostName = valid.length() > 15 ? valid.substring(valid.length() - 15) : valid;
1979+
String hostName = valid.length() > MAX_WINDOWS_VM_HOSTNAME_LENGTH ?
1980+
valid.substring(valid.length() - MAX_WINDOWS_VM_HOSTNAME_LENGTH) :
1981+
valid;
19771982
if (!hostName.isEmpty() && !Character.isLetter(hostName.charAt(0))) {
19781983
for (int i = 0; i < hostName.length(); i++) {
19791984
if (Character.isLetter(hostName.charAt(i))) {
@@ -1982,7 +1987,7 @@ protected String getTrimmedHostNameForWindows(String name) {
19821987
}
19831988
}
19841989
if (!Character.isLetter(hostName.charAt(0))) {
1985-
hostName = hostName.length() < 15 ? "a" + hostName : "a" + hostName.substring(1);
1990+
hostName = "a" + hostName.substring(hostName.length() < MAX_WINDOWS_VM_HOSTNAME_LENGTH ? 0 : 1);
19861991
}
19871992
}
19881993
return hostName;
@@ -2007,7 +2012,7 @@ protected Pair<String, String> getNextVmHostAndDisplayName(AutoScaleVmGroupVO as
20072012
// Truncate vm group name because max length of vm name is 63
20082013
int subStringLength = Math.min(asGroup.getName().length(), 63 - VM_HOSTNAME_PREFIX.length() - vmHostNameSuffix.length());
20092014
String name = VM_HOSTNAME_PREFIX + asGroup.getName().substring(0, subStringLength) + vmHostNameSuffix;
2010-
if (!isWindows || name.length() <= 15) {
2015+
if (!isWindows || name.length() <= MAX_WINDOWS_VM_HOSTNAME_LENGTH) {
20112016
return new Pair<>(name, name);
20122017
}
20132018
return new Pair<>(getTrimmedHostNameForWindows(name), name);

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,10 +2595,14 @@ public void prefixesWithLetterIfNoLetterExists() {
25952595
}
25962596

25972597
@Test
2598-
public void trimsHostNameToLast15Characters() {
2598+
public void trimsHostNameToLastMaxCharacters() {
2599+
String name = "ThisIsAReallyLongHost-Name_1234";
25992600
AutoScaleManagerImpl manager = new AutoScaleManagerImpl();
2600-
String result = manager.getTrimmedHostNameForWindows("ThisIsAReallyLongHost-Name_1234");
2601-
Assert.assertEquals("ngHost-Name1234", result);
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);
26022606
}
26032607

26042608
@Test

0 commit comments

Comments
 (0)