|
| 1 | +param ( |
| 2 | + [string]$Location, |
| 3 | + [string]$Model, |
| 4 | + [string]$DeploymentType = "Standard", |
| 5 | + [int]$Capacity |
| 6 | +) |
| 7 | + |
| 8 | +# Verify required parameters |
| 9 | +$MissingParams = @() |
| 10 | +if (-not $Location) { $MissingParams += "location" } |
| 11 | +if (-not $Model) { $MissingParams += "model" } |
| 12 | +if (-not $Capacity) { $MissingParams += "capacity" } |
| 13 | +if (-not $DeploymentType) { $MissingParams += "deployment-type" } |
| 14 | + |
| 15 | +if ($MissingParams.Count -gt 0) { |
| 16 | + Write-Error "❌ ERROR: Missing required parameters: $($MissingParams -join ', ')" |
| 17 | + Write-Host "Usage: .\validate_model_quota.ps1 -Location <LOCATION> -Model <MODEL> -Capacity <CAPACITY> [-DeploymentType <DEPLOYMENT_TYPE>]" |
| 18 | + exit 1 |
| 19 | +} |
| 20 | + |
| 21 | +if ($DeploymentType -ne "Standard" -and $DeploymentType -ne "GlobalStandard") { |
| 22 | + Write-Error "❌ ERROR: Invalid deployment type: $DeploymentType. Allowed values are 'Standard' or 'GlobalStandard'." |
| 23 | + exit 1 |
| 24 | +} |
| 25 | + |
| 26 | +$ModelType = "OpenAI.$DeploymentType.$Model" |
| 27 | + |
| 28 | +$PreferredRegions = @('australiaeast', 'eastus2', 'francecentral', 'japaneast', 'norwayeast', 'swedencentral', 'uksouth', 'westus') |
| 29 | +$AllResults = @() |
| 30 | + |
| 31 | +function Check-Quota { |
| 32 | + param ( |
| 33 | + [string]$Region |
| 34 | + ) |
| 35 | + |
| 36 | + $ModelInfoRaw = az cognitiveservices usage list --location $Region --query "[?name.value=='$ModelType']" --output json |
| 37 | + $ModelInfo = $null |
| 38 | + |
| 39 | + try { |
| 40 | + $ModelInfo = $ModelInfoRaw | ConvertFrom-Json |
| 41 | + } catch { |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + if (-not $ModelInfo) { |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + $CurrentValue = ($ModelInfo | Where-Object { $_.name.value -eq $ModelType }).currentValue |
| 50 | + $Limit = ($ModelInfo | Where-Object { $_.name.value -eq $ModelType }).limit |
| 51 | + |
| 52 | + $CurrentValue = [int]($CurrentValue -replace '\.0+$', '') |
| 53 | + $Limit = [int]($Limit -replace '\.0+$', '') |
| 54 | + $Available = $Limit - $CurrentValue |
| 55 | + |
| 56 | + $script:AllResults += [PSCustomObject]@{ |
| 57 | + Region = $Region |
| 58 | + Model = $ModelType |
| 59 | + Limit = $Limit |
| 60 | + Used = $CurrentValue |
| 61 | + Available = $Available |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +foreach ($region in $PreferredRegions) { |
| 66 | + Check-Quota -Region $region |
| 67 | +} |
| 68 | + |
| 69 | +# Display Results Table |
| 70 | +Write-Host "\n-------------------------------------------------------------------------------------------------------------" |
| 71 | +Write-Host "| No. | Region | Model Name | Limit | Used | Available |" |
| 72 | +Write-Host "-------------------------------------------------------------------------------------------------------------" |
| 73 | + |
| 74 | +$count = 1 |
| 75 | +foreach ($entry in $AllResults) { |
| 76 | + $index = $PreferredRegions.IndexOf($entry.Region) + 1 |
| 77 | + $modelShort = $entry.Model.Substring($entry.Model.LastIndexOf(".") + 1) |
| 78 | + Write-Host ("| {0,-4} | {1,-16} | {2,-35} | {3,-7} | {4,-7} | {5,-9} |" -f $index, $entry.Region, $entry.Model, $entry.Limit, $entry.Used, $entry.Available) |
| 79 | + $count++ |
| 80 | +} |
| 81 | +Write-Host "-------------------------------------------------------------------------------------------------------------" |
| 82 | + |
| 83 | +$EligibleRegion = $AllResults | Where-Object { $_.Region -eq $Location -and $_.Available -ge $Capacity } |
| 84 | +if ($EligibleRegion) { |
| 85 | + Write-Host "\n✅ Sufficient quota found in original region '$Location'." |
| 86 | + exit 0 |
| 87 | +} |
| 88 | + |
| 89 | +$FallbackRegions = $AllResults | Where-Object { $_.Region -ne $Location -and $_.Available -ge $Capacity } |
| 90 | + |
| 91 | +if ($FallbackRegions.Count -gt 0) { |
| 92 | + Write-Host "`n❌ Deployment cannot proceed because the original region '$Location' lacks sufficient quota." |
| 93 | + Write-Host "➡️ You can retry using one of the following regions with sufficient quota:`n" |
| 94 | + |
| 95 | + foreach ($region in $FallbackRegions) { |
| 96 | + Write-Host " • $($region.Region) (Available: $($region.Available))" |
| 97 | + } |
| 98 | + |
| 99 | + Write-Host "`n🔧 To proceed, run:" |
| 100 | + Write-Host " azd env set AZURE_ENV_OPENAI_LOCATION '<region>'" |
| 101 | + Write-Host "📌 To confirm it's set correctly, run:" |
| 102 | + Write-Host " azd env get-value AZURE_ENV_OPENAI_LOCATION" |
| 103 | + Write-Host "▶️ Once confirmed, re-run azd up to deploy the model in the new region." |
| 104 | + exit 2 |
| 105 | +} |
| 106 | + |
| 107 | +Write-Error "❌ ERROR: No available quota found in any region." |
| 108 | +exit 1 |
0 commit comments