diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 3e5a60f5585c..08705c7248e7 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -400,17 +400,13 @@ try { exit } - $UserName = GetUserName - - if (!$BaseName) { - if ($CI) { - $BaseName = 't' + (New-Guid).ToString('n').Substring(0, 16) - Log "Generated base name '$BaseName' for CI build" - } else { - $BaseName = GetBaseName $UserName (GetServiceLeafDirectoryName $ServiceDirectory) - Log "BaseName was not set. Using default base name '$BaseName'" - } - } + $serviceName = GetServiceLeafDirectoryName $ServiceDirectory + $BaseName, $ResourceGroupName = GetBaseAndResourceGroupNames ` + -baseNameDefault $BaseName ` + -resourceGroupNameDefault $ResourceGroupName ` + -user (GetUserName) ` + -serviceDirectoryName $serviceName ` + -CI $CI # Make sure pre- and post-scripts are passed formerly required arguments. $PSBoundParameters['BaseName'] = $BaseName @@ -546,19 +542,8 @@ try { $ProvisionerApplicationOid = $sp.Id } - $serviceName = GetServiceLeafDirectoryName $ServiceDirectory - - $ResourceGroupName = if ($ResourceGroupName) { - $ResourceGroupName - } elseif ($CI) { - # Format the resource group name based on resource group naming recommendations and limitations. - "rg-{0}-$BaseName" -f ($serviceName -replace '[\.\\\/:]', '-').ToLowerInvariant().Substring(0, [Math]::Min($serviceName.Length, 90 - $BaseName.Length - 4)).Trim('-') - } else { - "rg-$BaseName" - } - $tags = @{ - Owners = $UserName + Owners = (GetUserName) ServiceDirectory = $ServiceDirectory } diff --git a/eng/common/TestResources/Remove-TestResources.ps1 b/eng/common/TestResources/Remove-TestResources.ps1 index 069f2bf8a634..4e2b52c63a6d 100644 --- a/eng/common/TestResources/Remove-TestResources.ps1 +++ b/eng/common/TestResources/Remove-TestResources.ps1 @@ -147,14 +147,13 @@ if (!$ResourceGroupName) { exit 0 } } else { - if (!$BaseName) { - $UserName = GetUserName - $BaseName = GetBaseName $UserName $ServiceDirectory - Log "BaseName was not set. Using default base name '$BaseName'" - } - - # Format the resource group name like in New-TestResources.ps1. - $ResourceGroupName = "rg-$BaseName" + $serviceName = GetServiceLeafDirectoryName $ServiceDirectory + $BaseName, $ResourceGroupName = GetBaseAndResourceGroupNames ` + -baseNameDefault $BaseName ` + -resourceGroupNameDefault $ResourceGroupName ` + -user (GetUserName) ` + -serviceDirectoryName $serviceName ` + -CI $CI } } diff --git a/eng/common/TestResources/SubConfig-Helpers.ps1 b/eng/common/TestResources/SubConfig-Helpers.ps1 index cc93def6aa89..6a9d8ea79200 100644 --- a/eng/common/TestResources/SubConfig-Helpers.ps1 +++ b/eng/common/TestResources/SubConfig-Helpers.ps1 @@ -16,10 +16,40 @@ function GetUserName() { return $UserName } -function GetBaseName([string]$user, [string]$serviceDirectoryName) { +function GetBaseAndResourceGroupNames( + [string]$baseNameDefault, + [string]$resourceGroupNameDefault, + [string]$user, + [string]$serviceDirectoryName, + [bool]$CI +) { + if ($CI) { + $base = 't' + (New-Guid).ToString('n').Substring(0, 16) + # Format the resource group name based on resource group naming recommendations and limitations. + $generatedGroup = "rg-{0}-$BaseName" -f ($serviceName -replace '[\.\\\/:]', '-').ToLowerInvariant().Substring(0, [Math]::Min($serviceName.Length, 90 - $BaseName.Length - 4)).Trim('-') + $group = $resourceGroupNameDefault ? $resourceGroupNameDefault : $generatedGroup + + Log "Generated resource base name '$base' and resource group name '$group' for CI build" + + return $base, $group + } + + if ($baseNameDefault) { + $base = $baseNameDefault.ToLowerInvariant() + $group = $resourceGroupNameDefault ? $resourceGroupNameDefault : ("rg-$baseNameDefault".ToLowerInvariant()) + return $base, $group + } + # Handle service directories in nested directories, e.g. `data/aztables` $serviceDirectorySafeName = $serviceDirectoryName -replace '[\./\\]', '' - return "$user$serviceDirectorySafeName".ToLowerInvariant() + # Hash to keep resource names short enough to not break naming restrictions (e.g. keyvault name length) + $baseNameStream = [IO.MemoryStream]::new([Text.Encoding]::UTF8.GetBytes("${user}${serviceDirectorySafeName}")) + $base = 't' + (Get-FileHash -InputStream $baseNameStream -Algorithm SHA1).Hash.Substring(0, 16).ToLowerInvariant() + $group = $resourceGroupNameDefault ? $resourceGroupNameDefault : "rg-${user}${serviceDirectorySafeName}".ToLowerInvariant(); + + Log "BaseName was not set. Generating resource group name '$group' and resource base name '$base'" + + return $base, $group } function ShouldMarkValueAsSecret([string]$serviceName, [string]$key, [string]$value, [array]$allowedValues = @()) diff --git a/eng/common/TestResources/Update-TestResources.ps1 b/eng/common/TestResources/Update-TestResources.ps1 index 7715ec4fcfbc..b57ee485fcf7 100644 --- a/eng/common/TestResources/Update-TestResources.ps1 +++ b/eng/common/TestResources/Update-TestResources.ps1 @@ -69,17 +69,13 @@ $exitActions = @({ } }) -# Make sure $ResourceGroupName is set. -if (!$ResourceGroupName) { - # Make sure $BaseName is set. - if (!$BaseName) { - $UserName = GetUserName - $BaseName = GetBaseName $UserName $ServiceDirectory - Log "BaseName was not set. Using default base name '$BaseName'" - } - - $ResourceGroupName = "rg-$BaseName" -} +$serviceName = GetServiceLeafDirectoryName $ServiceDirectory +$BaseName, $ResourceGroupName = GetBaseAndResourceGroupNames ` + -baseNameDefault $BaseName ` + -resourceGroupNameDefault $ResourceGroupName ` + -user (GetUserName) ` + -serviceDirectoryName $serviceName ` + -CI $CI # This script is intended for interactive users. Make sure they are logged in or fail. $context = Get-AzContext diff --git a/eng/common/TestResources/build-test-resource-config.yml b/eng/common/TestResources/build-test-resource-config.yml index 2335f94f48db..215901c418b2 100644 --- a/eng/common/TestResources/build-test-resource-config.yml +++ b/eng/common/TestResources/build-test-resource-config.yml @@ -36,3 +36,4 @@ steps: UpdateSubscriptionConfiguration $configBase $config displayName: Merge Test Resource Configurations +) \ No newline at end of file