Skip to content

Commit 02c0288

Browse files
committed
Address CRs
1 parent 343836e commit 02c0288

File tree

2 files changed

+27
-14
lines changed
  • src/ServiceManagement

2 files changed

+27
-14
lines changed

src/ServiceManagement/Common/Commands.ScenarioTest/ServiceManagement/UnitTests.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public void TestExtensionRoleNames()
4040
foreach (var roleName in roleNames)
4141
{
4242
ExtensionRole er = new ExtensionRole(roleName);
43+
Assert.Equal(er.RoleName, roleName.Trim());
4344
Assert.Equal(er.PrefixName, expectedPrefixName);
4445
Assert.Equal(er.GetExtensionId("test", "test", 0), expectedExtensionId);
4546
}
@@ -48,26 +49,41 @@ public void TestExtensionRoleNames()
4849
{
4950
"A123456789B123456789C123456789D123456789E123456789F123456789G123456789H123456789",
5051
" A123456789B123456789C123456789D123456789E123456789F123456789G123456789H123456789 ~~~"
51-
};
52-
53-
// Extenion ID's Max Length 60 = 43 + 1 + 4 + 1 + 4 + 1 + 5
52+
};
53+
54+
// PrefixName = A123456789B123456789C123456789D123456789E123456789F123456789G123456789H123456789
55+
// Extension Name = test
56+
// Slot = test
57+
// Index = 0
58+
// Extension ID Format = {prefix_name_part}-{extension_name_part}-{slot}-Ext-{index}
59+
// Extenion ID's Max Length: 60 = 43 + 1 + 4 + 1 + 4 + 1 + 5
5460
// i.e. 'A123...E123' + '-' + 'test' + '-' + 'test' + '-' + 'Ext-0'
55-
// L=43 L=1 L=4 L=1 L=4 L=1 L=5
61+
// L=43 L=1 L=4 L=1 L=4 L=1 L=5
5662
expectedPrefixName = longRoleNames[0];
5763
expectedExtensionId = "A123456789B123456789C123456789D123456789E123-test-test-Ext-0";
5864
foreach (var roleName in longRoleNames)
5965
{
6066
ExtensionRole er = new ExtensionRole(roleName);
67+
Assert.Equal(er.RoleName, roleName.Trim());
6168
Assert.Equal(er.PrefixName, expectedPrefixName);
6269
Assert.Equal(er.GetExtensionId("test", "test", 0), expectedExtensionId);
6370
}
6471

6572

6673
var longExtensionNames = longRoleNames;
74+
// PrefixName = Default
75+
// Extension Name = A123456789B123456789C123456789D123456789E123456789F123456789G123456789H123456789
76+
// Slot = test
77+
// Index = 1
78+
// Extension ID Format = {prefix_name_part}-{extension_name_part}-{slot}-Ext-{index}
79+
// Extenion ID's Max Length: 60 = 1 + 1 + 47 + 1 + 4 + 1 + 5
80+
// i.e. 'D' + '-' + 'A123...456' + '-' + 'test' + '-' + 'Ext-0'
81+
// L=1 L=1 L=47 L=1 L=4 L=1 L=5
6782
expectedExtensionId = "D-A123456789B123456789C123456789D123456789E123456-test-Ext-1";
6883
foreach (var extensionName in longExtensionNames)
6984
{
7085
ExtensionRole er = new ExtensionRole();
86+
Assert.Equal(er.RoleName, string.Empty);
7187
Assert.Equal(er.PrefixName, "Default");
7288
Assert.Equal(er.GetExtensionId(extensionName, "test", 1), expectedExtensionId);
7389
}

src/ServiceManagement/Compute/Commands.ServiceManagement/Extensions/Common/ExtensionRole.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public class ExtensionRole
2323
protected const string DefaultExtensionIdPrefixStr = "Default";
2424
protected const string ExtensionIdSuffixTemplate = "-{0}-{1}-Ext-{2}";
2525
protected const int MaxExtensionIdLength = 60;
26-
protected const int MaxSuffixLength = MaxExtensionIdLength - 1;
26+
protected const int MinRoleNamePartLength = 1;
27+
protected const int MaxSuffixLength = MaxExtensionIdLength - MinRoleNamePartLength;
2728

2829
public string RoleName { get; private set; }
2930
public string PrefixName { get; private set; }
@@ -34,15 +35,7 @@ private static string RemoveDisallowedCharacters(string roleName)
3435
{
3536
// Remove characters that are not allowed in the extension id
3637
var disallowedCharactersRegex = new Regex(@"[^A-Za-z0-9\-]");
37-
var match = disallowedCharactersRegex.Match(roleName);
38-
39-
while (match.Success)
40-
{
41-
roleName = roleName.Remove(match.Index, match.Length);
42-
match = disallowedCharactersRegex.Match(roleName);
43-
}
44-
45-
return roleName;
38+
return disallowedCharactersRegex.Replace(roleName, string.Empty);
4639
}
4740

4841
public ExtensionRole()
@@ -81,14 +74,18 @@ public string GetExtensionId(string extensionName, string slot, int index)
8174
var normalizedExtName = RemoveDisallowedCharacters(extensionName);
8275

8376
var suffix = new StringBuilder();
77+
// Suffix format: -{extension_name_part}-{slot}-Ext-{index}
8478
suffix.AppendFormat(ExtensionIdSuffixTemplate, normalizedExtName, slot, index);
8579
if (suffix.Length > MaxSuffixLength)
8680
{
81+
// If the suffix is too long, truncate the {extension_name_part}
8782
int lenDiff = suffix.Length - MaxSuffixLength;
8883
int startIndex = 1; // Suffix starts with '-'
8984
suffix.Remove(startIndex + normalizedExtName.Length - lenDiff, lenDiff);
9085
}
9186

87+
// Calculate the prefix length by the difference between the suffix and the max ID length.
88+
// The difference should always be at least 1.
9289
int prefixSubStrLen = Math.Min(Math.Max(MaxExtensionIdLength - suffix.Length, 0), PrefixName.Length);
9390

9491
var result = new StringBuilder();

0 commit comments

Comments
 (0)