Skip to content

Commit 467d02e

Browse files
committed
removing auto-generated subs with max 1per user.
1 parent 6922bec commit 467d02e

1 file changed

Lines changed: 62 additions & 1 deletion

File tree

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

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,63 @@ function Get-ApiopsLogLevel([string]$ScriptLogLevel) {
135135
}
136136
}
137137

138+
function Remove-KnownAutoSubscriptions {
139+
param(
140+
[Parameter(Mandatory)][string]$SubscriptionId,
141+
[Parameter(Mandatory)][string]$ResourceGroup,
142+
[Parameter(Mandatory)][string]$ServiceName,
143+
[Parameter(Mandatory)][string]$ScopeLabel
144+
)
145+
146+
Write-Host " Cleaning auto-created subscriptions in ${ScopeLabel} APIM..."
147+
$listUri = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroup/providers/Microsoft.ApiManagement/service/$ServiceName/subscriptions?api-version=2025-09-01-preview"
148+
$listRaw = Invoke-MaskedAzCommand -Replacements @{
149+
$SubscriptionId = Protect-SubscriptionId -Value $SubscriptionId
150+
$ResourceGroup = Protect-ResourceGroupName -Value $ResourceGroup
151+
$ServiceName = Protect-ApimName -Value $ServiceName
152+
} -Arguments @('rest', '--method', 'get', '--url', $listUri)
153+
154+
if ($LASTEXITCODE -ne 0 -or -not $listRaw) {
155+
Write-Host " ⚠️ Could not list subscriptions for cleanup in ${ScopeLabel}"
156+
return
157+
}
158+
159+
try {
160+
$payload = $listRaw | ConvertFrom-Json
161+
} catch {
162+
Write-Host " ⚠️ Could not parse subscription list for cleanup in ${ScopeLabel}"
163+
return
164+
}
165+
166+
$candidates = @($payload.value | Where-Object {
167+
$name = [string]$_.name
168+
$displayName = [string]$_.properties.displayName
169+
$scope = [string]$_.properties.scope
170+
($name -match '^[a-f0-9]{24}$') -and [string]::IsNullOrEmpty($displayName) -and ($scope -match '/products/.+$')
171+
})
172+
173+
if ($candidates.Count -eq 0) {
174+
Write-Host " No auto-created subscriptions found in ${ScopeLabel}"
175+
return
176+
}
177+
178+
foreach ($candidate in $candidates) {
179+
$subName = [string]$candidate.name
180+
$uri = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroup/providers/Microsoft.ApiManagement/service/$ServiceName/subscriptions/$subName?api-version=2025-09-01-preview"
181+
$null = Invoke-MaskedAzCommand -Replacements @{
182+
$SubscriptionId = Protect-SubscriptionId -Value $SubscriptionId
183+
$ResourceGroup = Protect-ResourceGroupName -Value $ResourceGroup
184+
$ServiceName = Protect-ApimName -Value $ServiceName
185+
} -Arguments @('rest', '--method', 'delete', '--url', $uri)
186+
187+
if ($LASTEXITCODE -eq 0) {
188+
Write-Host " Deleted subscription: $subName"
189+
} else {
190+
Write-Host " ⚠️ Failed to delete subscription: $subName"
191+
}
192+
}
193+
}
194+
138195
function Invoke-Teardown {
139196
if ($SkipTeardown) {
140197
Write-Host "⏭️ Teardown skipped (-SkipTeardown)"
@@ -487,7 +544,9 @@ try {
487544
Write-Host " Source: $(Protect-ApimName -Value $sourceName) (sub: $(Protect-SubscriptionId -Value $sourceSubId), rg: $(Protect-ResourceGroupName -Value $sourceRg))"
488545
Write-Host " Output: $ExtractOutputDir"
489546

490-
$apiopsLogLevel = Get-ApiopsLogLevel -ScriptLogLevel $LogLevel
547+
Remove-KnownAutoSubscriptions -SubscriptionId $sourceSubId -ResourceGroup $sourceRg -ServiceName $sourceName -ScopeLabel 'source'
548+
549+
$apiopsLogLevel = Get-ApiopsLogLevel -ScriptLogLevel $LogLevel
491550

492551
# For SKUs that support workspaces (Premium / PremiumV2), pass a filter file
493552
# with the workspace name so the extractor includes workspace-scoped resources.
@@ -630,6 +689,8 @@ loggers:
630689
Write-Host " Target: $(Protect-ApimName -Value $targetName) (sub: $(Protect-SubscriptionId -Value $targetSubId), rg: $(Protect-ResourceGroupName -Value $targetRg))"
631690
Write-Host " Input: $ExtractOutputDir"
632691

692+
Remove-KnownAutoSubscriptions -SubscriptionId $targetSubId -ResourceGroup $targetRg -ServiceName $targetName -ScopeLabel 'target'
693+
633694
$publishExitCode = Invoke-MaskedApiopsCommand -Replacements @{
634695
$targetSubId = Protect-SubscriptionId -Value $targetSubId
635696
$targetRg = Protect-ResourceGroupName -Value $targetRg

0 commit comments

Comments
 (0)