|
| 1 | +param( |
| 2 | + [string]$billingScope, |
| 3 | + [string]$subscriptionNamePrefix = "accelerator-bootstrap-modules", |
| 4 | + [string[]]$subscriptionTypes = @("connectivity", "management", "identity", "security"), |
| 5 | + [int]$maxRetries = 5, |
| 6 | + [int]$throttleLimit = 2, |
| 7 | + [switch]$planOnly |
| 8 | +) |
| 9 | + |
| 10 | +# Get current Azure account information |
| 11 | +$accountInfo = az account show --output json | ConvertFrom-Json |
| 12 | + |
| 13 | +# Look up tenant name from Graph API domains |
| 14 | +$domains = az rest --method get --url "https://graph.microsoft.com/v1.0/domains" --output json | ConvertFrom-Json |
| 15 | +$defaultDomain = $domains.value | Where-Object { $_.isDefault -eq $true } |
| 16 | +$tenantName = if ($defaultDomain.id) { $defaultDomain.id } else { "(unknown)" } |
| 17 | + |
| 18 | +Write-Host "" |
| 19 | +Write-Host "=== Azure Connection Information ===" -ForegroundColor Cyan |
| 20 | +Write-Host "Tenant ID: $($accountInfo.tenantId)" -ForegroundColor Yellow |
| 21 | +Write-Host "Tenant Name: $tenantName" -ForegroundColor Yellow |
| 22 | +Write-Host "Account: $($accountInfo.user.name)" -ForegroundColor Yellow |
| 23 | +Write-Host "Subscription: $($accountInfo.name)" -ForegroundColor Yellow |
| 24 | +Write-Host "====================================" -ForegroundColor Cyan |
| 25 | +Write-Host "" |
| 26 | + |
| 27 | +$confirmation = Read-Host "Do you want to continue with this account? (y/n)" |
| 28 | +if ($confirmation -ne 'y' -and $confirmation -ne 'Y') { |
| 29 | + Write-Host "Operation cancelled by user." -ForegroundColor Red |
| 30 | + exit 0 |
| 31 | +} |
| 32 | + |
| 33 | +Write-Host "" |
| 34 | + |
| 35 | +$tests = ./.github/tests/scripts/generate-matrix.ps1 |
| 36 | + |
| 37 | +# Get all existing aliases once using REST API with paging (more efficient than checking each one individually) |
| 38 | +Write-Host "Fetching existing subscription aliases..." -ForegroundColor Cyan |
| 39 | +$existingAliasNames = @() |
| 40 | +$aliasUrl = "https://management.azure.com/providers/Microsoft.Subscription/aliases?api-version=2021-10-01" |
| 41 | + |
| 42 | +do { |
| 43 | + $response = az rest --method get --url "`"$aliasUrl`"" | ConvertFrom-Json |
| 44 | + if ($response.value) { |
| 45 | + $existingAliasNames += $response.value | ForEach-Object { $_.name } |
| 46 | + } |
| 47 | + $aliasUrl = $response.nextLink |
| 48 | +} while ($aliasUrl) |
| 49 | + |
| 50 | +Write-Host "Fetched $($existingAliasNames.Count) existing aliases." -ForegroundColor Green |
| 51 | + |
| 52 | +# Build list of subscriptions to create |
| 53 | +$subscriptionsToCreate = @() |
| 54 | +$existingSubscriptions = @() |
| 55 | +$skippedTests = @() |
| 56 | + |
| 57 | +foreach ($test in $tests) { |
| 58 | + # Only create subscriptions for tests that deploy Azure resources |
| 59 | + if ($test.deployAzureResources -ne "true") { |
| 60 | + $skippedTests += $test.Name |
| 61 | + continue |
| 62 | + } |
| 63 | + |
| 64 | + foreach ($subscriptionType in $subscriptionTypes) { |
| 65 | + $subscriptionName = "$subscriptionNamePrefix-$($test.ShortNamePrefix)-$subscriptionType" |
| 66 | + |
| 67 | + if ($existingAliasNames -notcontains $subscriptionName) { |
| 68 | + $subscriptionsToCreate += $subscriptionName |
| 69 | + } else { |
| 70 | + $existingSubscriptions += $subscriptionName |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +# Display skipped tests |
| 76 | +if ($skippedTests.Count -gt 0) { |
| 77 | + Write-Host "" |
| 78 | + Write-Host "=== Tests Skipped (deployAzureResources=false) ===" -ForegroundColor Cyan |
| 79 | + foreach ($test in $skippedTests) { |
| 80 | + Write-Host " - $test" -ForegroundColor Gray |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +# Display existing subscriptions |
| 85 | +if ($existingSubscriptions.Count -gt 0) { |
| 86 | + Write-Host "" |
| 87 | + Write-Host "=== Existing Subscription Aliases (will be skipped) ===" -ForegroundColor Cyan |
| 88 | + foreach ($sub in $existingSubscriptions) { |
| 89 | + Write-Host " - $sub" -ForegroundColor Gray |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +# Display subscriptions to create |
| 94 | +Write-Host "" |
| 95 | +if ($subscriptionsToCreate.Count -eq 0) { |
| 96 | + Write-Host "No new subscriptions to create. All aliases already exist." -ForegroundColor Green |
| 97 | + return |
| 98 | +} |
| 99 | + |
| 100 | +Write-Host "=== Subscriptions to Create ===" -ForegroundColor Cyan |
| 101 | +foreach ($sub in $subscriptionsToCreate) { |
| 102 | + Write-Host " - $sub" -ForegroundColor Yellow |
| 103 | +} |
| 104 | +Write-Host "" |
| 105 | +Write-Host "Total: $($subscriptionsToCreate.Count) subscription(s) to create" -ForegroundColor Cyan |
| 106 | +Write-Host "" |
| 107 | + |
| 108 | +if ($planOnly) { |
| 109 | + Write-Host "Plan only mode - no subscriptions will be created." -ForegroundColor Magenta |
| 110 | + return |
| 111 | +} |
| 112 | + |
| 113 | +# Prompt for confirmation before creating |
| 114 | +$createConfirmation = Read-Host "Do you want to create these $($subscriptionsToCreate.Count) subscription(s)? (y/n)" |
| 115 | +if ($createConfirmation -ne 'y' -and $createConfirmation -ne 'Y') { |
| 116 | + Write-Host "Operation cancelled by user." -ForegroundColor Red |
| 117 | + return |
| 118 | +} |
| 119 | + |
| 120 | +Write-Host "" |
| 121 | + |
| 122 | +# Create a thread-safe hashtable to track rate limiting across parallel tasks |
| 123 | +$rateLimitState = [hashtable]::Synchronized(@{ |
| 124 | + WaitUntil = [DateTime]::MinValue |
| 125 | +}) |
| 126 | + |
| 127 | +# Create the subscriptions in parallel with retry logic |
| 128 | +Write-Host "Creating subscriptions (throttle: $throttleLimit)..." -ForegroundColor Cyan |
| 129 | + |
| 130 | +$results = $subscriptionsToCreate | ForEach-Object -Parallel { |
| 131 | + $subscriptionName = $_ |
| 132 | + $scope = $using:billingScope |
| 133 | + $retries = $using:maxRetries |
| 134 | + $state = $using:rateLimitState |
| 135 | + $retryCount = 0 |
| 136 | + $success = $false |
| 137 | + |
| 138 | + while (-not $success -and $retryCount -lt $retries) { |
| 139 | + # Check if we're in a rate limit wait period |
| 140 | + $waitUntil = $state.WaitUntil |
| 141 | + if ($waitUntil -gt [DateTime]::Now) { |
| 142 | + $waitSeconds = [math]::Ceiling(($waitUntil - [DateTime]::Now).TotalSeconds) |
| 143 | + Write-Host "Rate limit active. $subscriptionName waiting $waitSeconds seconds..." -ForegroundColor Yellow |
| 144 | + Start-Sleep -Seconds $waitSeconds |
| 145 | + } |
| 146 | + |
| 147 | + Write-Host "Creating subscription: $subscriptionName (Attempt $($retryCount + 1) of $retries)" -ForegroundColor Yellow |
| 148 | + $result = az account alias create --name "$subscriptionName" --billing-scope "$scope" --display-name "$subscriptionName" --workload "Production" 2>&1 |
| 149 | + |
| 150 | + if ($LASTEXITCODE -eq 0) { |
| 151 | + $success = $true |
| 152 | + Write-Host "Successfully created: $subscriptionName" -ForegroundColor Green |
| 153 | + } else { |
| 154 | + $errorMessage = $result | Out-String |
| 155 | + if ($errorMessage -match "TooManyRequests.*Retry in (\d{2}):(\d{2}):(\d{2})") { |
| 156 | + $hours = [int]$Matches[1] |
| 157 | + $minutes = [int]$Matches[2] |
| 158 | + $seconds = [int]$Matches[3] |
| 159 | + $waitSeconds = ($hours * 3600) + ($minutes * 60) + $seconds + 10 # Add 10 seconds buffer |
| 160 | + |
| 161 | + # Set the shared rate limit wait time |
| 162 | + $newWaitUntil = [DateTime]::Now.AddSeconds($waitSeconds) |
| 163 | + if ($newWaitUntil -gt $state.WaitUntil) { |
| 164 | + $state.WaitUntil = $newWaitUntil |
| 165 | + Write-Host "Rate limit hit! All tasks will wait until $($newWaitUntil.ToString('HH:mm:ss'))" -ForegroundColor Red |
| 166 | + } |
| 167 | + |
| 168 | + Write-Host "Rate limited for $subscriptionName. Waiting $waitSeconds seconds before retry..." -ForegroundColor Yellow |
| 169 | + Start-Sleep -Seconds $waitSeconds |
| 170 | + $retryCount++ |
| 171 | + } else { |
| 172 | + Write-Host "Failed to create $subscriptionName : $errorMessage" -ForegroundColor Red |
| 173 | + break |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + [PSCustomObject]@{ |
| 179 | + Name = $subscriptionName |
| 180 | + Success = $success |
| 181 | + } |
| 182 | +} -ThrottleLimit $throttleLimit |
| 183 | + |
| 184 | +$successCount = ($results | Where-Object { $_.Success }).Count |
| 185 | +$failCount = ($results | Where-Object { -not $_.Success }).Count |
| 186 | + |
| 187 | +Write-Host "" |
| 188 | +Write-Host "Subscription creation complete." -ForegroundColor Green |
| 189 | +Write-Host " Successful: $successCount" -ForegroundColor Green |
| 190 | +if ($failCount -gt 0) { |
| 191 | + Write-Host " Failed: $failCount" -ForegroundColor Red |
| 192 | +} |
0 commit comments