From 92f16c6031c00ef3275b729371860cc8ec948990 Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Thu, 26 Feb 2026 16:44:42 -0500 Subject: [PATCH] Remove storage sync services in resource cleanup --- .../TestResources/Remove-TestResources.ps1 | 14 ++++-- .../TestResources/Update-TestResources.ps1 | 11 +++-- .../scripts/Helpers/Resource-Helpers.ps1 | 48 +++++++++++++++++++ 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/eng/common/TestResources/Remove-TestResources.ps1 b/eng/common/TestResources/Remove-TestResources.ps1 index 232c86196c15..5ca8fc1582a2 100755 --- a/eng/common/TestResources/Remove-TestResources.ps1 +++ b/eng/common/TestResources/Remove-TestResources.ps1 @@ -7,6 +7,8 @@ #Requires -PSEdition Core #Requires -Modules @{ModuleName='Az.Accounts'; ModuleVersion='1.6.4'} #Requires -Modules @{ModuleName='Az.Resources'; ModuleVersion='1.8.0'} +#Requires -Modules @{ModuleName='Az.Storage'; ModuleVersion='7.0.0'} +#Requires -Modules @{ModuleName='Az.StorageSync'; ModuleVersion='2.1.1'} [CmdletBinding(DefaultParameterSetName = 'Default', SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] param ( @@ -174,25 +176,25 @@ if (!$ResourceGroupName) { } } -# If no subscription was specified, try to select the Azure SDK Developer Playground subscription. +# If no subscription was specified, try to select the 'Azure SDK Test Resources - TME' subscription. # Ignore errors to leave the automatically selected subscription. if ($SubscriptionId) { $currentSubcriptionId = $context.Subscription.Id if ($currentSubcriptionId -ne $SubscriptionId) { Log "Selecting subscription '$SubscriptionId'" - $null = Select-AzSubscription -Subscription $SubscriptionId + $null = Select-AzSubscription -Subscription $SubscriptionId -WarningAction Ignore $exitActions += { Log "Selecting previous subscription '$currentSubcriptionId'" - $null = Select-AzSubscription -Subscription $currentSubcriptionId + $null = Select-AzSubscription -Subscription $currentSubcriptionId -WarningAction Ignore } # Update the context. $context = Get-AzContext } } else { - Log "Attempting to select subscription 'Azure SDK Developer Playground (faa080af-c1d8-40ad-9cce-e1a450ca5b57)'" - $null = Select-AzSubscription -Subscription 'faa080af-c1d8-40ad-9cce-e1a450ca5b57' -ErrorAction Ignore + Log "Attempting to select subscription 'Azure SDK Test Resources - TME (4d042dc6-fe17-4698-a23f-ec6a8d1e98f4)'" + $null = Select-AzSubscription -Subscription '4d042dc6-fe17-4698-a23f-ec6a8d1e98f4' -ErrorAction Ignore -WarningAction Ignore # Update the context. $context = Get-AzContext @@ -206,6 +208,7 @@ $wellKnownSubscriptions = @{ 'faa080af-c1d8-40ad-9cce-e1a450ca5b57' = 'Azure SDK Developer Playground' 'a18897a6-7e44-457d-9260-f2854c0aca42' = 'Azure SDK Engineering System' '2cd617ea-1866-46b1-90e3-fffb087ebf9b' = 'Azure SDK Test Resources' + '4d042dc6-fe17-4698-a23f-ec6a8d1e98f4' = 'Azure SDK Test Resources - TME' } # Print which subscription is currently selected. @@ -261,6 +264,7 @@ $purgeableResources = Get-PurgeableGroupResources $ResourceGroupName SetResourceNetworkAccessRules -ResourceGroupName $ResourceGroupName -AllowIpRanges $AllowIpRanges -SetFirewall -CI:$CI Remove-WormStorageAccounts -GroupPrefix $ResourceGroupName -CI:$CI +Remove-StorageSyncServices -GroupPrefix $ResourceGroupName -CI:$CI Log "Deleting resource group '$ResourceGroupName'" if ($Force -and !$purgeableResources) { diff --git a/eng/common/TestResources/Update-TestResources.ps1 b/eng/common/TestResources/Update-TestResources.ps1 index 9592e8a8caa0..35e40fbd55ba 100755 --- a/eng/common/TestResources/Update-TestResources.ps1 +++ b/eng/common/TestResources/Update-TestResources.ps1 @@ -83,25 +83,25 @@ if (!$context) { throw "You must be already logged in to use this script. Run 'Connect-AzAccount' and try again." } -# If no subscription was specified, try to select the Azure SDK Developer Playground subscription. +# If no subscription was specified, try to select the 'Azure SDK Test Resources - TME' subscription. # Ignore errors to leave the automatically selected subscription. if ($SubscriptionId) { $currentSubcriptionId = $context.Subscription.Id if ($currentSubcriptionId -ne $SubscriptionId) { Log "Selecting subscription '$SubscriptionId'" - $null = Select-AzSubscription -Subscription $SubscriptionId + $null = Select-AzSubscription -Subscription $SubscriptionId -WarningAction Ignore $exitActions += { Log "Selecting previous subscription '$currentSubcriptionId'" - $null = Select-AzSubscription -Subscription $currentSubcriptionId + $null = Select-AzSubscription -Subscription $SubscriptionId -WarningAction Ignore } # Update the context. $context = Get-AzContext } } else { - Log "Attempting to select subscription 'Azure SDK Developer Playground (faa080af-c1d8-40ad-9cce-e1a450ca5b57)'" - $null = Select-AzSubscription -Subscription 'faa080af-c1d8-40ad-9cce-e1a450ca5b57' -ErrorAction Ignore + Log "Attempting to select subscription 'Azure SDK Test Resources - TME (4d042dc6-fe17-4698-a23f-ec6a8d1e98f4)'" + $null = Select-AzSubscription -Subscription '4d042dc6-fe17-4698-a23f-ec6a8d1e98f4' -ErrorAction Ignore -WarningAction Ignore # Update the context. $context = Get-AzContext @@ -115,6 +115,7 @@ $wellKnownSubscriptions = @{ 'faa080af-c1d8-40ad-9cce-e1a450ca5b57' = 'Azure SDK Developer Playground' 'a18897a6-7e44-457d-9260-f2854c0aca42' = 'Azure SDK Engineering System' '2cd617ea-1866-46b1-90e3-fffb087ebf9b' = 'Azure SDK Test Resources' + '4d042dc6-fe17-4698-a23f-ec6a8d1e98f4' = 'Azure SDK Test Resources - TME' } # Print which subscription is currently selected. diff --git a/eng/common/scripts/Helpers/Resource-Helpers.ps1 b/eng/common/scripts/Helpers/Resource-Helpers.ps1 index f124ff217d40..5fca15845aeb 100644 --- a/eng/common/scripts/Helpers/Resource-Helpers.ps1 +++ b/eng/common/scripts/Helpers/Resource-Helpers.ps1 @@ -309,6 +309,54 @@ function Remove-WormStorageAccounts() { } } +# Helper function for removing Storage Sync Services that block resource group deletion. +# Sync groups and their endpoints must be removed before the sync service can be deleted. +function Remove-StorageSyncServices() { + [CmdletBinding(SupportsShouldProcess = $True)] + param( + [string]$GroupPrefix, + [switch]$CI + ) + + $ErrorActionPreference = 'Stop' + + if (!$groupPrefix -or ($CI -and (!$GroupPrefix.StartsWith('rg-') -and !$GroupPrefix.StartsWith('SSS3PT_rg-')))) { + throw "The -GroupPrefix parameter must not be empty, or must start with 'rg-' or 'SSS3PT_rg-' in CI contexts" + } + + $groups = Get-AzResourceGroup | Where-Object { $_.ResourceGroupName.StartsWith($GroupPrefix) } | Where-Object { $_.ProvisioningState -ne 'Deleting' } + + foreach ($group in $groups) { + $syncServices = Get-AzResource -ResourceGroupName $group.ResourceGroupName -ResourceType 'Microsoft.StorageSync/storageSyncServices' -ErrorAction SilentlyContinue + if (!$syncServices) { continue } + + foreach ($syncService in $syncServices) { + Write-Host "Removing Storage Sync Service '$($syncService.Name)' from resource group '$($group.ResourceGroupName)'" + + $syncGroups = Get-AzStorageSyncGroup -ResourceGroupName $group.ResourceGroupName -StorageSyncServiceName $syncService.Name -ErrorAction SilentlyContinue + foreach ($syncGroup in $syncGroups) { + Write-Host " Removing sync group '$($syncGroup.SyncGroupName)'" + + $cloudEndpoints = Get-AzStorageSyncCloudEndpoint -ResourceGroupName $group.ResourceGroupName -StorageSyncServiceName $syncService.Name -SyncGroupName $syncGroup.SyncGroupName -ErrorAction SilentlyContinue + foreach ($ce in $cloudEndpoints) { + Write-Host " Removing cloud endpoint '$($ce.CloudEndpointName)'" + Remove-AzStorageSyncCloudEndpoint -ResourceGroupName $group.ResourceGroupName -StorageSyncServiceName $syncService.Name -SyncGroupName $syncGroup.SyncGroupName -Name $ce.CloudEndpointName -Force + } + + $serverEndpoints = Get-AzStorageSyncServerEndpoint -ResourceGroupName $group.ResourceGroupName -StorageSyncServiceName $syncService.Name -SyncGroupName $syncGroup.SyncGroupName -ErrorAction SilentlyContinue + foreach ($se in $serverEndpoints) { + Write-Host " Removing server endpoint '$($se.ServerEndpointName)'" + Remove-AzStorageSyncServerEndpoint -ResourceGroupName $group.ResourceGroupName -StorageSyncServiceName $syncService.Name -SyncGroupName $syncGroup.SyncGroupName -Name $se.ServerEndpointName -Force + } + + Remove-AzStorageSyncGroup -ResourceGroupName $group.ResourceGroupName -StorageSyncServiceName $syncService.Name -Name $syncGroup.SyncGroupName -Force + } + + Remove-AzStorageSyncService -ResourceGroupName $group.ResourceGroupName -Name $syncService.Name -Force + } + } +} + function SetResourceNetworkAccessRules([string]$ResourceGroupName, [array]$AllowIpRanges, [switch]$CI, [switch]$SetFirewall) { SetStorageNetworkAccessRules -ResourceGroupName $ResourceGroupName -AllowIpRanges $AllowIpRanges -CI:$CI -SetFirewall:$SetFirewall }