Skip to content

Commit 9fe7062

Browse files
shwstpprweizhouapache
authored andcommitted
address comment
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 001f320 commit 9fe7062

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,23 @@ public void updateVmDetails(Map<String, String> deployParams, Map<String, String
19711971
}
19721972
}
19731973

1974+
protected String getTrimmedHostNameForWindows(String name) {
1975+
String valid = name.replaceAll("[^a-zA-Z0-9-]", "");
1976+
String hostName = valid.length() > 15 ? valid.substring(valid.length() - 15) : valid;
1977+
if (!hostName.isEmpty() && !Character.isLetter(hostName.charAt(0))) {
1978+
for (int i = 0; i < hostName.length(); i++) {
1979+
if (Character.isLetter(hostName.charAt(i))) {
1980+
hostName = hostName.charAt(i) + hostName.substring(i + 1);
1981+
break;
1982+
}
1983+
}
1984+
if (!Character.isLetter(hostName.charAt(0))) {
1985+
hostName = hostName.length() < 15 ? "a" + hostName : "a" + hostName.substring(1);
1986+
}
1987+
}
1988+
return hostName;
1989+
}
1990+
19741991
protected Pair<String, String> getNextVmHostAndDisplayName(AutoScaleVmGroupVO asGroup, VirtualMachineTemplate template) {
19751992
template.getGuestOSId();
19761993
GuestOSVO guestOSVO = guestOSDao.findById(template.getGuestOSId());
@@ -1983,15 +2000,7 @@ protected Pair<String, String> getNextVmHostAndDisplayName(AutoScaleVmGroupVO as
19832000
if (!isWindows || name.length() <= 15) {
19842001
return new Pair<>(name, name);
19852002
}
1986-
String hostName = name.substring(Math.max(0, name.length() - 15));
1987-
if (Character.isLetterOrDigit(hostName.charAt(0))) {
1988-
return new Pair<>(hostName, name);
1989-
}
1990-
String temp = name.substring(0, Math.max(0, name.length() - 15)).replaceAll("[^a-zA-Z0-9]", "");
1991-
if (!temp.isEmpty()) {
1992-
return new Pair<>(temp.charAt(temp.length() - 1) + hostName.substring(1), name);
1993-
}
1994-
return new Pair<>('a' + hostName.substring(1), name);
2003+
return new Pair<>(getTrimmedHostNameForWindows(name), name);
19952004
}
19962005

19972006
@Override

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,4 +2552,53 @@ public void getNextVmHostAndDisplayNameHandlesNullGuestOS() {
25522552
Assert.assertTrue(result.first().matches(vmHostNamePattern));
25532553
Assert.assertEquals(result.first(), result.second());
25542554
}
2555+
2556+
@Test
2557+
public void trimsValidWindowsHostName() {
2558+
AutoScaleManagerImpl manager = new AutoScaleManagerImpl();
2559+
String result = manager.getTrimmedHostNameForWindows("ValidVMName1234");
2560+
Assert.assertEquals("ValidVMName1234", result);
2561+
}
2562+
2563+
@Test
2564+
public void trimsInvalidCharactersFromHostName() {
2565+
AutoScaleManagerImpl manager = new AutoScaleManagerImpl();
2566+
String result = manager.getTrimmedHostNameForWindows("Invalid@Host#Name!");
2567+
Assert.assertEquals("InvalidHostName", result);
2568+
}
2569+
2570+
@Test
2571+
public void ensuresHostNameStartsWithLetter() {
2572+
AutoScaleManagerImpl manager = new AutoScaleManagerImpl();
2573+
String result = manager.getTrimmedHostNameForWindows("123456789012345");
2574+
Assert.assertTrue(Character.isLetter(result.charAt(0)));
2575+
}
2576+
2577+
@Test
2578+
public void prefixesWithLetterIfNoLetterExists() {
2579+
AutoScaleManagerImpl manager = new AutoScaleManagerImpl();
2580+
String result = manager.getTrimmedHostNameForWindows("1234567890-12345");
2581+
Assert.assertEquals("a34567890-12345", result);
2582+
}
2583+
2584+
@Test
2585+
public void trimsHostNameToLast15Characters() {
2586+
AutoScaleManagerImpl manager = new AutoScaleManagerImpl();
2587+
String result = manager.getTrimmedHostNameForWindows("ThisIsAReallyLongHost-Name_1234");
2588+
Assert.assertEquals("ngHost-Name1234", result);
2589+
}
2590+
2591+
@Test
2592+
public void handlesEmptyHostName() {
2593+
AutoScaleManagerImpl manager = new AutoScaleManagerImpl();
2594+
String result = manager.getTrimmedHostNameForWindows("");
2595+
Assert.assertEquals("", result);
2596+
}
2597+
2598+
@Test
2599+
public void handlesHostNameWithOnlyMostlyInvalidCharacters() {
2600+
AutoScaleManagerImpl manager = new AutoScaleManagerImpl();
2601+
String result = manager.getTrimmedHostNameForWindows("@#$%^&*() 1");
2602+
Assert.assertEquals("a1", result);
2603+
}
25552604
}

0 commit comments

Comments
 (0)