Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions eng/common/TestResources/Remove-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 6 additions & 5 deletions eng/common/TestResources/Update-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
48 changes: 48 additions & 0 deletions eng/common/scripts/Helpers/Resource-Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading