Sync eng/common directory with azure-sdk-tools for PR 10532 (#6571)

* Allow for empty serviceDirectory in deploy-test-resources

* Revert some unintentional changes

* Restore missing comment

---------

Co-authored-by: Patrick Hallisey <pahallis@microsoft.com>
This commit is contained in:
Azure SDK Bot 2025-05-14 12:45:38 -07:00 committed by GitHub
parent 91d579b8cf
commit c61528188c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 37 additions and 18 deletions

View File

@ -18,7 +18,7 @@ param (
[ValidatePattern('^[-\w\._\(\)]+$')]
[string] $ResourceGroupName,
[Parameter(Mandatory = $true, Position = 0)]
[Parameter(Position = 0)]
[string] $ServiceDirectory,
[Parameter()]
@ -159,10 +159,13 @@ if ($initialContext) {
# try..finally will also trap Ctrl+C.
try {
# Enumerate test resources to deploy. Fail if none found.
$repositoryRoot = "$PSScriptRoot/../../.." | Resolve-Path
$root = [System.IO.Path]::Combine($repositoryRoot, "sdk", $ServiceDirectory) | Resolve-Path
$root = $repositoryRoot = "$PSScriptRoot/../../.." | Resolve-Path
if($ServiceDirectory) {
$root = "$repositoryRoot/sdk/$ServiceDirectory" | Resolve-Path
}
if ($TestResourcesDirectory) {
$root = $TestResourcesDirectory | Resolve-Path
# Add an explicit check below in case ErrorActionPreference is overridden and Resolve-Path doesn't stop execution
@ -171,6 +174,7 @@ try {
}
Write-Verbose "Overriding test resources search directory to '$root'"
}
$templateFiles = @()
"$ResourceType-resources.json", "$ResourceType-resources.bicep" | ForEach-Object {
@ -192,7 +196,12 @@ try {
exit
}
# returns empty string if $ServiceDirectory is not set
$serviceName = GetServiceLeafDirectoryName $ServiceDirectory
# in ci, random names are used
# in non-ci, without BaseName, ResourceGroupName or ServiceDirectory, all invocations will
# generate the same resource group name and base name for a given user
$BaseName, $ResourceGroupName = GetBaseAndResourceGroupNames `
-baseNameDefault $BaseName `
-resourceGroupNameDefault $ResourceGroupName `
@ -296,7 +305,7 @@ try {
}
}
# This needs to happen after we set the TenantId but before we use the ResourceGroupName
# This needs to happen after we set the TenantId but before we use the ResourceGroupName
if ($wellKnownTMETenants.Contains($TenantId)) {
# Add a prefix to the resource group name to avoid flagging the usages of local auth
# See details at https://eng.ms/docs/products/onecert-certificates-key-vault-and-dsms/key-vault-dsms/certandsecretmngmt/credfreefaqs#how-can-i-disable-s360-reporting-when-testing-customer-facing-3p-features-that-depend-on-use-of-unsafe-local-auth
@ -364,9 +373,10 @@ try {
$ProvisionerApplicationOid = $sp.Id
}
$tags = @{
Owners = (GetUserName)
ServiceDirectory = $ServiceDirectory
$tags = @{ Owners = (GetUserName) }
if ($ServiceDirectory) {
$tags['ServiceDirectory'] = $ServiceDirectory
}
# Tag the resource group to be deleted after a certain number of hours.

View File

@ -157,10 +157,6 @@ $context = Get-AzContext
if (!$ResourceGroupName) {
if ($CI) {
if (!$ServiceDirectory) {
Write-Warning "ServiceDirectory parameter is empty, nothing to remove"
exit 0
}
$envVarName = (BuildServiceDirectoryPrefix (GetServiceLeafDirectoryName $ServiceDirectory)) + "RESOURCE_GROUP"
$ResourceGroupName = [Environment]::GetEnvironmentVariable($envVarName)
if (!$ResourceGroupName) {
@ -221,7 +217,12 @@ if ($wellKnownSubscriptions.ContainsKey($subscriptionName)) {
Log "Selected subscription '$subscriptionName'"
if ($ServiceDirectory) {
$root = [System.IO.Path]::Combine("$PSScriptRoot/../../../sdk", $ServiceDirectory) | Resolve-Path
$root = "$PSScriptRoot/../../.."
if($ServiceDirectory) {
$root = "$root/sdk/$ServiceDirectory"
}
$root = $root | Resolve-Path
$preRemovalScript = Join-Path -Path $root -ChildPath "remove-$ResourceType-resources-pre.ps1"
if (Test-Path $preRemovalScript) {
Log "Invoking pre resource removal script '$preRemovalScript'"

View File

@ -1,4 +1,7 @@
function BuildServiceDirectoryPrefix([string]$serviceName) {
if(!$serviceName) {
return ""
}
$serviceName = $serviceName -replace '[\./\\]', '_'
return $serviceName.ToUpperInvariant() + "_"
}
@ -32,10 +35,15 @@ function GetBaseAndResourceGroupNames(
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}-$base" -f ($serviceName -replace '[\.\\\/:]', '-').
Substring(0, [Math]::Min($serviceDirectoryName.Length, 90 - $base.Length - 4)).
Trim('-').
ToLowerInvariant()
if ($serviceDirectoryName) {
$generatedGroup = "rg-{0}-$base" -f ($serviceName -replace '[\.\\\/:]', '-').
Substring(0, [Math]::Min($serviceDirectoryName.Length, 90 - $base.Length - 4)).
Trim('-').
ToLowerInvariant()
} else {
$generatedGroup = "rg-$base"
}
$group = $resourceGroupNameDefault ? $resourceGroupNameDefault : $generatedGroup
Log "Generated resource base name '$base' and resource group name '$group' for CI build"

View File

@ -1,5 +1,5 @@
parameters:
ServiceDirectory: not-set
ServiceDirectory: ''
ArmTemplateParameters: '@{}'
DeleteAfterHours: 8
Location: ''