Skip to content

Commit d24ffc3

Browse files
committed
change
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 313e4d4 commit d24ffc3

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;
@@ -1962,7 +1965,9 @@ public void updateVmDetails(Map<String, String> deployParams, Map<String, String
19621965

19631966
protected String getTrimmedHostNameForWindows(String name) {
19641967
String valid = name.replaceAll("[^a-zA-Z0-9-]", "");
1965-
String hostName = valid.length() > 15 ? valid.substring(valid.length() - 15) : valid;
1968+
String hostName = valid.length() > MAX_WINDOWS_VM_HOSTNAME_LENGTH ?
1969+
valid.substring(valid.length() - MAX_WINDOWS_VM_HOSTNAME_LENGTH) :
1970+
valid;
19661971
if (!hostName.isEmpty() && !Character.isLetter(hostName.charAt(0))) {
19671972
for (int i = 0; i < hostName.length(); i++) {
19681973
if (Character.isLetter(hostName.charAt(i))) {
@@ -1971,7 +1976,7 @@ protected String getTrimmedHostNameForWindows(String name) {
19711976
}
19721977
}
19731978
if (!Character.isLetter(hostName.charAt(0))) {
1974-
hostName = hostName.length() < 15 ? "a" + hostName : "a" + hostName.substring(1);
1979+
hostName = "a" + hostName.substring(hostName.length() < MAX_WINDOWS_VM_HOSTNAME_LENGTH ? 0 : 1);
19751980
}
19761981
}
19771982
return hostName;
@@ -1996,7 +2001,7 @@ protected Pair<String, String> getNextVmHostAndDisplayName(AutoScaleVmGroupVO as
19962001
// Truncate vm group name because max length of vm name is 63
19972002
int subStringLength = Math.min(asGroup.getName().length(), 63 - VM_HOSTNAME_PREFIX.length() - vmHostNameSuffix.length());
19982003
String name = VM_HOSTNAME_PREFIX + asGroup.getName().substring(0, subStringLength) + vmHostNameSuffix;
1999-
if (!isWindows || name.length() <= 15) {
2004+
if (!isWindows || name.length() <= MAX_WINDOWS_VM_HOSTNAME_LENGTH) {
20002005
return new Pair<>(name, name);
20012006
}
20022007
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)