Skip to content

Commit

Permalink
[test resources] Consolidate naming logic and generate short hash nam…
Browse files Browse the repository at this point in the history
…es for local use
  • Loading branch information
benbp committed Jan 26, 2023
1 parent 9ced934 commit 2796617
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 44 deletions.
31 changes: 8 additions & 23 deletions eng/common/TestResources/New-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
15 changes: 7 additions & 8 deletions eng/common/TestResources/Remove-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
34 changes: 32 additions & 2 deletions eng/common/TestResources/SubConfig-Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = @())
Expand Down
18 changes: 7 additions & 11 deletions eng/common/TestResources/Update-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions eng/common/TestResources/build-test-resource-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ steps:
UpdateSubscriptionConfiguration $configBase $config
displayName: Merge Test Resource Configurations
)

0 comments on commit 2796617

Please sign in to comment.