Skip to content

Commit 6e8d103

Browse files
authored
Purge delete cog service accounts (#1442)
1 parent 8f26cfc commit 6e8d103

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

eng/common/scripts/Helpers/Resource-Helpers.ps1

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ function Get-PurgeableGroupResources {
5555
AzsdkName = $r.name
5656
Name = $r.name
5757
Id = $r.id
58+
Location = $r.location
59+
ResourceGroup = $ResourceGroupName
5860
}
5961
}
6062
}
@@ -129,11 +131,19 @@ function Get-PurgeableResources {
129131

130132
$deletedCognitiveServices = @()
131133
foreach ($r in $content.value) {
134+
# Extract resource group from the deletedAccounts id path
135+
# Format: /subscriptions/{sub}/providers/Microsoft.CognitiveServices/locations/{loc}/resourceGroups/{rg}/deletedAccounts/{name}
136+
$resourceGroup = $null
137+
if ($r.id -match '/resourceGroups/([^/]+)/') {
138+
$resourceGroup = $Matches[1]
139+
}
132140
$deletedCognitiveServices += [pscustomobject] @{
133141
AzsdkResourceType = "Cognitive Services ($($r.kind))"
134142
AzsdkName = $r.name
135143
Name = $r.name
136144
Id = $r.id
145+
Location = $r.properties.location
146+
ResourceGroup = $resourceGroup
137147
}
138148
}
139149

@@ -211,8 +221,22 @@ filter Remove-PurgeableResources {
211221
{ $_.StartsWith('Cognitive Services') }
212222
{
213223
Log "Attempting to purge $($r.AzsdkResourceType) '$($r.AzsdkName)'"
224+
225+
# Construct the correct purge path for deleted Cognitive Services accounts
226+
# The purge API requires: /subscriptions/{sub}/providers/Microsoft.CognitiveServices/locations/{loc}/resourceGroups/{rg}/deletedAccounts/{name}
227+
$purgePath = if ($r.Id -match '/deletedAccounts/') {
228+
# Already a deletedAccounts path from Get-PurgeableResources
229+
$r.Id
230+
} elseif ($r.Location -and $r.ResourceGroup) {
231+
# Convert active account path to deletedAccounts purge path
232+
"/subscriptions/$subscriptionId/providers/Microsoft.CognitiveServices/locations/$($r.Location)/resourceGroups/$($r.ResourceGroup)/deletedAccounts/$($r.Name)"
233+
} else {
234+
# Fallback to original ID (may fail but logged for debugging)
235+
$r.Id
236+
}
237+
214238
# Use `GetNewClosure()` on the `-Action` ScriptBlock to make sure variables are captured.
215-
Invoke-AzRestMethod -Method DELETE -Path "$($r.id)?api-version=2024-10-01" -ErrorAction Ignore -AsJob `
239+
Invoke-AzRestMethod -Method DELETE -Path "$($purgePath)?api-version=2024-10-01" -ErrorAction Ignore -AsJob `
216240
| Wait-PurgeableResourceJob -Resource $r -Timeout $Timeout -PassThru:$PassThru -Action {
217241
param ( $response )
218242

0 commit comments

Comments
 (0)