Skip to content

Commit 9922690

Browse files
committed
updating script to wait for delete before purging apim instance
1 parent 655e4c9 commit 9922690

2 files changed

Lines changed: 77 additions & 15 deletions

File tree

tests/integration/all-resource-types/phases/run-phase7-teardown.ps1

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ param(
3131
[Parameter(Mandatory)]
3232
[string]$TargetResourceGroup,
3333

34+
[string]$SourceSubscriptionId,
35+
36+
[string]$TargetSubscriptionId,
37+
3438
[string]$Location = 'eastus2',
3539

3640
[switch]$SkipTeardown
@@ -49,12 +53,42 @@ Write-Host "🧹 PHASE 7 — Teardown"
4953

5054
$sourceApimName = $null
5155
$targetApimName = $null
52-
$sourceApimName = az apim list --resource-group $SourceResourceGroup --query "[0].name" -o tsv 2>$null
53-
$targetApimName = az apim list --resource-group $TargetResourceGroup --query "[0].name" -o tsv 2>$null
56+
57+
function Get-SubscriptionArgs {
58+
param([string]$SubscriptionId)
59+
60+
if ([string]::IsNullOrWhiteSpace($SubscriptionId)) {
61+
return @()
62+
}
63+
64+
return @('--subscription', $SubscriptionId)
65+
}
66+
67+
function Get-GroupExists {
68+
param(
69+
[Parameter(Mandatory)]
70+
[string]$ResourceGroup,
71+
[string]$SubscriptionId
72+
)
73+
74+
$subArgs = Get-SubscriptionArgs -SubscriptionId $SubscriptionId
75+
$exists = az group exists --name $ResourceGroup @subArgs -o tsv 2>$null
76+
if ($LASTEXITCODE -ne 0) {
77+
throw "Failed to query existence for resource group '$(Protect-ResourceGroupName -Value $ResourceGroup)'."
78+
}
79+
80+
return $exists -eq 'true'
81+
}
82+
83+
$sourceListArgs = @('apim', 'list', '--resource-group', $SourceResourceGroup, '--query', '[0].name', '-o', 'tsv') + (Get-SubscriptionArgs -SubscriptionId $SourceSubscriptionId)
84+
$targetListArgs = @('apim', 'list', '--resource-group', $TargetResourceGroup, '--query', '[0].name', '-o', 'tsv') + (Get-SubscriptionArgs -SubscriptionId $TargetSubscriptionId)
85+
86+
$sourceApimName = az @sourceListArgs 2>$null
87+
$targetApimName = az @targetListArgs 2>$null
5488

5589
function Wait-ForResourceGroupsDeletion {
5690
param(
57-
[string[]]$ResourceGroups,
91+
[hashtable[]]$Groups,
5892
[int]$TimeoutMinutes = 60,
5993
[int]$IntervalSeconds = 30
6094
)
@@ -65,9 +99,9 @@ function Wait-ForResourceGroupsDeletion {
6599
while ($waitedSeconds -lt $timeoutSeconds) {
66100
$existingGroups = @()
67101

68-
foreach ($resourceGroup in $ResourceGroups) {
69-
if ((az group exists --name $resourceGroup -o tsv 2>$null) -eq 'true') {
70-
$existingGroups += $resourceGroup
102+
foreach ($group in $Groups) {
103+
if (Get-GroupExists -ResourceGroup $group.ResourceGroup -SubscriptionId $group.SubscriptionId) {
104+
$existingGroups += $group.ResourceGroup
71105
}
72106
}
73107

@@ -82,7 +116,7 @@ function Wait-ForResourceGroupsDeletion {
82116
$waitedSeconds += $IntervalSeconds
83117
}
84118

85-
$maskedGroups = $ResourceGroups | ForEach-Object { Protect-ResourceGroupName -Value $_ }
119+
$maskedGroups = $Groups | ForEach-Object { Protect-ResourceGroupName -Value $_.ResourceGroup }
86120
throw "Timed out waiting for resource group deletion for: $($maskedGroups -join ', ')."
87121
}
88122

@@ -114,12 +148,35 @@ function Wait-ForDeletedApimService {
114148
}
115149

116150
Write-Host " Deleting $(Protect-ResourceGroupName -Value $SourceResourceGroup)..."
117-
az group delete --name $SourceResourceGroup --yes --no-wait 2>$null
151+
if (Get-GroupExists -ResourceGroup $SourceResourceGroup -SubscriptionId $SourceSubscriptionId) {
152+
$sourceDeleteArgs = @('group', 'delete', '--name', $SourceResourceGroup, '--yes', '--no-wait') + (Get-SubscriptionArgs -SubscriptionId $SourceSubscriptionId)
153+
az @sourceDeleteArgs 2>$null
154+
if ($LASTEXITCODE -ne 0) {
155+
throw "Failed to start deletion for source resource group '$(Protect-ResourceGroupName -Value $SourceResourceGroup)'."
156+
}
157+
}
158+
else {
159+
Write-Host " Source resource group already absent"
160+
}
161+
118162
Write-Host " Deleting $(Protect-ResourceGroupName -Value $TargetResourceGroup)..."
119-
az group delete --name $TargetResourceGroup --yes --no-wait 2>$null
163+
if (Get-GroupExists -ResourceGroup $TargetResourceGroup -SubscriptionId $TargetSubscriptionId) {
164+
$targetDeleteArgs = @('group', 'delete', '--name', $TargetResourceGroup, '--yes', '--no-wait') + (Get-SubscriptionArgs -SubscriptionId $TargetSubscriptionId)
165+
az @targetDeleteArgs 2>$null
166+
if ($LASTEXITCODE -ne 0) {
167+
throw "Failed to start deletion for target resource group '$(Protect-ResourceGroupName -Value $TargetResourceGroup)'."
168+
}
169+
}
170+
else {
171+
Write-Host " Target resource group already absent"
172+
}
120173

121174
Write-Host " ⏳ Waiting for resource group deletions to complete for hard-delete..."
122-
Wait-ForResourceGroupsDeletion -ResourceGroups @($SourceResourceGroup, $TargetResourceGroup)
175+
$groups = @(
176+
@{ ResourceGroup = $SourceResourceGroup; SubscriptionId = $SourceSubscriptionId },
177+
@{ ResourceGroup = $TargetResourceGroup; SubscriptionId = $TargetSubscriptionId }
178+
)
179+
Wait-ForResourceGroupsDeletion -Groups $groups
123180

124181
if ($sourceApimName) {
125182
Wait-ForDeletedApimService -ServiceName $sourceApimName -ServiceLocation $Location

tests/integration/all-resource-types/run-roundtrip-test.ps1

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,16 @@ catch {
257257
}
258258
finally {
259259
# Phase 7: Teardown apim instances and supporting resources
260-
& $phase7TeardownScript `
261-
-SourceResourceGroup $SourceResourceGroup `
262-
-TargetResourceGroup $TargetResourceGroup `
263-
-Location $Location `
264-
-SkipTeardown:$SkipTeardown
260+
$phase7Args = @{
261+
SourceResourceGroup = $SourceResourceGroup
262+
TargetResourceGroup = $TargetResourceGroup
263+
Location = $Location
264+
SkipTeardown = $SkipTeardown
265+
}
266+
Add-ArgumentIfSet -Hashtable $phase7Args -Key 'SourceSubscriptionId' -Value $SourceSubscriptionId
267+
Add-ArgumentIfSet -Hashtable $phase7Args -Key 'TargetSubscriptionId' -Value $TargetSubscriptionId
268+
269+
& $phase7TeardownScript @phase7Args
265270
}
266271

267272
exit $exitCode

0 commit comments

Comments
 (0)