Skip to content

Commit 627a2ad

Browse files
authored
Sync eng/common directory with azure-sdk-tools repository (#43056)
1 parent 1492c5e commit 627a2ad

File tree

1 file changed

+73
-4
lines changed

1 file changed

+73
-4
lines changed

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

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,32 @@ function Get-PurgeableGroupResources {
3838
$purgeableResources += $deletedKeyVaults
3939
}
4040

41+
Write-Verbose "Retrieving AI resources from resource group $ResourceGroupName"
42+
43+
# Get AI resources that will go into soft-deleted state when the resource group is deleted
44+
$subscriptionId = (Get-AzContext).Subscription.Id
45+
$aiResources = @()
46+
47+
# Get active Cognitive Services accounts from the resource group
48+
$response = Invoke-AzRestMethod -Method GET -Path "/subscriptions/$subscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.CognitiveServices/accounts?api-version=2024-10-01" -ErrorAction Ignore
49+
if ($response.StatusCode -ge 200 -and $response.StatusCode -lt 300 -and $response.Content) {
50+
$content = $response.Content | ConvertFrom-Json
51+
52+
foreach ($r in $content.value) {
53+
$aiResources += [pscustomobject] @{
54+
AzsdkResourceType = "Cognitive Services ($($r.kind))"
55+
AzsdkName = $r.name
56+
Name = $r.name
57+
Id = $r.id
58+
}
59+
}
60+
}
61+
62+
if ($aiResources) {
63+
Write-Verbose "Found $($aiResources.Count) AI resources to potentially purge after resource group deletion."
64+
$purgeableResources += $aiResources
65+
}
66+
4167
return $purgeableResources
4268
}
4369

@@ -94,6 +120,29 @@ function Get-PurgeableResources {
94120
}
95121
catch { }
96122

123+
Write-Verbose "Retrieving deleted Cognitive Services accounts from subscription $subscriptionId"
124+
125+
# Get deleted Cognitive Services accounts for the current subscription.
126+
$response = Invoke-AzRestMethod -Method GET -Path "/subscriptions/$subscriptionId/providers/Microsoft.CognitiveServices/deletedAccounts?api-version=2024-10-01" -ErrorAction Ignore
127+
if ($response.StatusCode -ge 200 -and $response.StatusCode -lt 300 -and $response.Content) {
128+
$content = $response.Content | ConvertFrom-Json
129+
130+
$deletedCognitiveServices = @()
131+
foreach ($r in $content.value) {
132+
$deletedCognitiveServices += [pscustomobject] @{
133+
AzsdkResourceType = "Cognitive Services ($($r.kind))"
134+
AzsdkName = $r.name
135+
Name = $r.name
136+
Id = $r.id
137+
}
138+
}
139+
140+
if ($deletedCognitiveServices) {
141+
Write-Verbose "Found $($deletedCognitiveServices.Count) deleted Cognitive Services accounts to potentially purge."
142+
$purgeableResources += $deletedCognitiveServices
143+
}
144+
}
145+
97146
return $purgeableResources
98147
}
99148

@@ -117,33 +166,37 @@ filter Remove-PurgeableResources {
117166
}
118167

119168
$subscriptionId = (Get-AzContext).Subscription.Id
169+
$verboseFlag = $VerbosePreference -eq 'Continue'
120170

121171
foreach ($r in $Resource) {
122-
Log "Attempting to purge $($r.AzsdkResourceType) '$($r.AzsdkName)'"
123172
switch ($r.AzsdkResourceType) {
124173
'Key Vault' {
125174
if ($r.EnablePurgeProtection) {
126-
Write-Warning "Key Vault '$($r.VaultName)' has purge protection enabled and may not be purged until $($r.ScheduledPurgeDate)"
175+
Write-Verbose "Key Vault '$($r.VaultName)' has purge protection enabled and may not be purged until $($r.ScheduledPurgeDate)" -Verbose:$verboseFlag
127176
continue
128177
}
129178

179+
Log "Attempting to purge $($r.AzsdkResourceType) '$($r.AzsdkName)'"
180+
130181
# Use `-AsJob` to start a lightweight, cancellable job and pass to `Wait-PurgeableResoruceJob` for consistent behavior.
131182
Remove-AzKeyVault -VaultName $r.VaultName -Location $r.Location -InRemovedState -Force -ErrorAction Continue -AsJob `
132183
| Wait-PurgeableResourceJob -Resource $r -Timeout $Timeout -PassThru:$PassThru
133184
}
134185

135186
'Managed HSM' {
136187
if ($r.EnablePurgeProtection) {
137-
Write-Warning "Managed HSM '$($r.Name)' has purge protection enabled and may not be purged until $($r.ScheduledPurgeDate)"
188+
Write-Verbose "Managed HSM '$($r.Name)' has purge protection enabled and may not be purged until $($r.ScheduledPurgeDate)" -Verbose:$verboseFlag
138189
continue
139190
}
140191

192+
Log "Attempting to purge $($r.AzsdkResourceType) '$($r.AzsdkName)'"
193+
141194
# Use `GetNewClosure()` on the `-Action` ScriptBlock to make sure variables are captured.
142195
Invoke-AzRestMethod -Method POST -Path "/subscriptions/$subscriptionId/providers/Microsoft.KeyVault/locations/$($r.Location)/deletedManagedHSMs/$($r.Name)/purge?api-version=2023-02-01" -ErrorAction Ignore -AsJob `
143196
| Wait-PurgeableResourceJob -Resource $r -Timeout $Timeout -PassThru:$PassThru -Action {
144197
param ( $response )
145198
if ($response.StatusCode -ge 200 -and $response.StatusCode -lt 300) {
146-
Write-Warning "Successfully requested that Managed HSM '$($r.Name)' be purged, but may take a few minutes before it is actually purged."
199+
Write-Verbose "Successfully requested that Managed HSM '$($r.Name)' be purged, but may take a few minutes before it is actually purged." -Verbose:$verboseFlag
147200
}
148201
elseif ($response.Content) {
149202
$content = $response.Content | ConvertFrom-Json
@@ -155,6 +208,22 @@ filter Remove-PurgeableResources {
155208
}.GetNewClosure()
156209
}
157210

211+
{ $_.StartsWith('Cognitive Services') }
212+
{
213+
Log "Attempting to purge $($r.AzsdkResourceType) '$($r.AzsdkName)'"
214+
# 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 `
216+
| Wait-PurgeableResourceJob -Resource $r -Timeout $Timeout -PassThru:$PassThru -Action {
217+
param ( $response )
218+
219+
if ($response.StatusCode -eq 200 -or $response.StatusCode -eq 202 -or $response.StatusCode -eq 204) {
220+
Write-Verbose "Successfully purged $($r.AzsdkResourceType) '$($r.Name)'." -Verbose:$verboseFlag
221+
} else {
222+
Write-Warning "Failed purging $($r.AzsdkResourceType) '$($r.Name)' with status code $($response.StatusCode)."
223+
}
224+
}.GetNewClosure()
225+
}
226+
158227
default {
159228
Write-Warning "Cannot purge $($r.AzsdkResourceType) '$($r.AzsdkName)'. Add support to https://github.com/Azure/azure-sdk-tools/blob/main/eng/common/scripts/Helpers/Resource-Helpers.ps1."
160229
}

0 commit comments

Comments
 (0)