-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release/8.0] Generate and store random RG name when provisioning (#3508
) * Generate and store random RG name when provisioning * Update src/Aspire.Hosting.Azure/Provisioning/Provisioners/AzureProvisioner.cs Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com> * Noarmlize generated RG names * PR feedback * Move helpers to Aspite.Hosting.Azure * Remove unused code --------- Co-authored-by: Sebastien Ros <sebastienros@gmail.com> Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
- Loading branch information
1 parent
fa6d363
commit 8cac9f3
Showing
4 changed files
with
126 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/Aspire.Hosting.Azure/Utils/ResourceGroupNameHelpers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Globalization; | ||
using System.Text; | ||
|
||
namespace Aspire.Hosting.Azure.Utils; | ||
|
||
internal static class ResourceGroupNameHelpers | ||
{ | ||
public static int MaxResourceGroupNameLength = 90; | ||
|
||
/// <summary> | ||
/// Converts or excludes any characters which are not valid resource group name components. | ||
/// </summary> | ||
/// <param name="resourceGroupName">The text to normalize.</param> | ||
/// <returns>The normalized resource group name or an empty string if no characters were valid.</returns> | ||
public static string NormalizeResourceGroupName(string resourceGroupName) | ||
{ | ||
resourceGroupName = RemoveDiacritics(resourceGroupName); | ||
|
||
var stringBuilder = new StringBuilder(capacity: resourceGroupName.Length); | ||
|
||
for (var i = 0; i < resourceGroupName.Length; i++) | ||
{ | ||
var c = resourceGroupName[i]; | ||
|
||
if (!char.IsAsciiLetterOrDigit(c) && c != '-' && c != '_') | ||
{ | ||
continue; | ||
} | ||
|
||
stringBuilder.Append(c); | ||
} | ||
|
||
return stringBuilder.ToString(); | ||
} | ||
|
||
private static string RemoveDiacritics(string text) | ||
{ | ||
var normalizedString = text.Normalize(NormalizationForm.FormD); | ||
var stringBuilder = new StringBuilder(capacity: normalizedString.Length); | ||
|
||
for (var i = 0; i < normalizedString.Length; i++) | ||
{ | ||
var c = normalizedString[i]; | ||
var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c); | ||
if (unicodeCategory != UnicodeCategory.NonSpacingMark) | ||
{ | ||
stringBuilder.Append(c); | ||
} | ||
} | ||
|
||
return stringBuilder | ||
.ToString() | ||
.Normalize(NormalizationForm.FormC); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/Aspire.Hosting.Tests/Azure/ResourceGroupNameHelpersTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Aspire.Hosting.Azure.Utils; | ||
using Xunit; | ||
|
||
namespace Aspire.Hosting.Tests.Azure; | ||
|
||
public class ResourceGroupNameHelpersTests | ||
{ | ||
[Theory] | ||
[InlineData("äæǽåàçéïôùÀÇÉÏÔÙ", "aaaceiouACEIOU")] | ||
[InlineData("🔥🤔😅🤘", "")] | ||
[InlineData("こんにちは", "")] | ||
[InlineData("", "")] | ||
[InlineData(" ", "")] | ||
[InlineData("-.()_", "-_")] | ||
[InlineData("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_")] | ||
public void ShouldCreateAzdCompatibleResourceGroupNames(string input, string expected) | ||
{ | ||
var result = ResourceGroupNameHelpers.NormalizeResourceGroupName(input); | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
} |