Skip to content

Commit b420194

Browse files
updated the script as per suggestion
1 parent 9017e3a commit b420194

2 files changed

Lines changed: 134 additions & 149 deletions

File tree

scripts/validate_model_quota.ps1

Lines changed: 92 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,42 @@ $RecommendedRegions = @()
1414
$NotRecommendedRegions = @()
1515
$EligibleFallbacks = @()
1616

17-
# ------------------ Validate Inputs ------------------
18-
$MissingParams = @()
19-
if (-not $Location) { $MissingParams += "location" }
20-
if (-not $Model) { $MissingParams += "model" }
21-
if (-not $Capacity -or $Capacity -le 0) { $MissingParams += "capacity" }
22-
23-
if ($MissingParams.Count -gt 0) {
24-
Write-Error "❌ ERROR: Missing or invalid parameters: $($MissingParams -join ', ')"
25-
Write-Host "Usage: .\validate_model_quota.ps1 -Location <LOCATION> -Model <MODEL> -Capacity <CAPACITY> [-DeploymentType <DEPLOYMENT_TYPE>]"
26-
exit 1
27-
}
17+
function Validate-Inputs {
18+
$MissingParams = @()
19+
if (-not $Location) { $MissingParams += "location" }
20+
if (-not $Model) { $MissingParams += "model" }
21+
if (-not $Capacity -or $Capacity -le 0) { $MissingParams += "capacity" }
22+
23+
if ($MissingParams.Count -gt 0) {
24+
Write-Error "❌ ERROR: Missing or invalid parameters: $($MissingParams -join ', ')"
25+
Write-Host "Usage: .\validate_model_quota.ps1 -Location <LOCATION> -Model <MODEL> -Capacity <CAPACITY> [-DeploymentType <DEPLOYMENT_TYPE>]"
26+
exit 1
27+
}
2828

29-
if ($DeploymentType -ne "Standard" -and $DeploymentType -ne "GlobalStandard") {
30-
Write-Error "❌ ERROR: Invalid deployment type: $DeploymentType. Allowed: 'Standard', 'GlobalStandard'"
31-
exit 1
29+
if ($DeploymentType -ne "Standard" -and $DeploymentType -ne "GlobalStandard") {
30+
Write-Error "❌ ERROR: Invalid deployment type: $DeploymentType. Allowed: 'Standard', 'GlobalStandard'"
31+
exit 1
32+
}
3233
}
3334

34-
$ModelType = "OpenAI.$DeploymentType.$Model"
35+
function Confirm-Action ($message) {
36+
do {
37+
$response = Read-Host "$message (y/n)"
38+
if ($response -notmatch "^[YyNn]$") {
39+
Write-Host "❌ Invalid input. Please enter 'y' or 'n'."
40+
}
41+
} while ($response -notmatch "^[YyNn]$")
42+
return $response -match "^[Yy]$"
43+
}
3544

3645
function Check-Quota {
3746
param ([string]$Region)
3847

3948
try {
49+
$ModelType = "OpenAI.$DeploymentType.$Model"
4050
$ModelInfoRaw = az cognitiveservices usage list --location $Region --query "[?name.value=='$ModelType']" --output json 2>$null
4151
$ModelInfo = $ModelInfoRaw | ConvertFrom-Json
42-
if (-not $ModelInfo -or $ModelInfo.Count -eq 0) {
43-
return $null
44-
}
52+
if (-not $ModelInfo -or $ModelInfo.Count -eq 0) { return $null }
4553

4654
$Current = [int]$ModelInfo[0].currentValue
4755
$Limit = [int]$ModelInfo[0].limit
@@ -99,7 +107,53 @@ function Set-DeploymentValues($Region, $Capacity) {
99107
}
100108
}
101109

102-
# ------------------ Check Primary Region ------------------
110+
function Manual-Prompt {
111+
while ($true) {
112+
$ManualRegion = Read-Host "`nPlease enter a region you want to try manually"
113+
if (-not $ManualRegion) {
114+
Write-Host "❌ ERROR: No region entered. Exiting."
115+
exit 1
116+
}
117+
118+
$ManualCapacityStr = Read-Host "Enter the capacity you want to use (numeric value)"
119+
if (-not ($ManualCapacityStr -as [int]) -or [int]$ManualCapacityStr -le 0) {
120+
Write-Host "❌ Invalid capacity value. Try again."
121+
continue
122+
}
123+
124+
$ManualCapacity = [int]$ManualCapacityStr
125+
126+
if ($ManualCapacity -lt $RECOMMENDED_TOKENS) {
127+
Write-Host "`n⚠️ You have entered a capacity of $ManualCapacity, which is less than the recommended minimum ($RECOMMENDED_TOKENS)."
128+
Write-Host "🚨 This may cause performance issues or unexpected behavior."
129+
Write-Host "ℹ️ Recommended regions (≥ $RECOMMENDED_TOKENS tokens available): $($RecommendedRegions -join ', ')"
130+
if (-not (Confirm-Action "❓ Proceed anyway?")) { continue }
131+
}
132+
133+
Write-Host "`n🔍 Checking quota in region '$ManualRegion' for requested capacity: $ManualCapacity..."
134+
$ManualResult = Check-Quota -Region $ManualRegion
135+
136+
if (-not $ManualResult) {
137+
Write-Host "⚠️ Could not retrieve quota info for region '$ManualRegion'. Try again."
138+
continue
139+
}
140+
141+
if ($ManualResult.Available -ge $ManualCapacity) {
142+
if ($ManualResult.Available -lt $RECOMMENDED_TOKENS) {
143+
if (-not (Confirm-Action "❓ Proceed anyway?")) { continue }
144+
}
145+
Set-DeploymentValues $ManualRegion $ManualCapacity
146+
Write-Host "✅ Deployment values set. Exiting."
147+
exit 0
148+
} else {
149+
Write-Host "❌ Quota in region '$ManualRegion' is insufficient. Available: $($ManualResult.Available), Required: $ManualCapacity"
150+
}
151+
}
152+
}
153+
154+
# Start validation and execution
155+
Validate-Inputs
156+
103157
Write-Host "`n🔍 Checking quota in the requested region '$Location'..."
104158
$PrimaryResult = Check-Quota -Region $Location
105159

@@ -110,40 +164,42 @@ if ($PrimaryResult) {
110164
Write-Host "`n⚠️ Selected region '$Location' has sufficient quota but is not among the recommended regions (≥ $RECOMMENDED_TOKENS tokens)."
111165
Write-Host "🚨 Your application may not work as expected due to limited quota."
112166
Write-Host "`nℹ️ Recommended regions: $($RecommendedRegions -join ', ')"
113-
Write-Host "👉 It's advisable to deploy in one of these regions for optimal app performance."
114-
do {
115-
$choice = Read-Host "❓ Do you want to choose a recommended region instead? (y/n)"
116-
if ($choice -notmatch "^[YyNn]$") {
117-
Write-Host "❌ Invalid input. Please enter 'y' or 'n'."
118-
}
119-
} while ($choice -notmatch "^[YyNn]$")
120-
121-
if ($choice -match "^[Yy]$") {
167+
if (Confirm-Action "❓ Do you want to choose a recommended region instead?") {
122168
Show-Table
123-
break
169+
exit 0
124170
} else {
125-
if ($Capacity -gt 200) {
126-
Write-Host "`n⚠️ Reducing capacity to 200 in '$BicepParamsFile' for safer deployment..."
127-
(Get-Content $BicepParamsFile) -replace "capacity\s*:\s*\d+", "capacity: 200" | Set-Content $BicepParamsFile
128-
Write-Host "✅ Updated '$BicepParamsFile' with capacity 200."
171+
if ($Capacity -gt $RECOMMENDED_TOKENS) {
172+
Write-Host "`n⚠️ Reducing capacity to $RECOMMENDED_TOKENS in '$BicepParamsFile' for safer deployment..."
173+
(Get-Content $BicepParamsFile) -replace "capacity\s*:\s*\d+", "capacity: $RECOMMENDED_TOKENS" | Set-Content $BicepParamsFile
174+
Write-Host "✅ Updated '$BicepParamsFile' with capacity $RECOMMENDED_TOKENS."
129175
}
130176
Set-DeploymentValues $Location $Capacity
131177
Write-Host "✅ Proceeding with '$Location' as selected."
132178
exit 0
133179
}
134180
} else {
135181
Write-Host "`n✅ Sufficient quota found in original region '$Location'."
182+
if ($PrimaryResult.Available -lt $RECOMMENDED_TOKENS) {
183+
Write-Host "`n⚠️ You have provided a capacity of $Capacity, which is less than the recommended minimum ($RECOMMENDED_TOKENS)."
184+
Write-Host "🚨 This may cause performance issues or unexpected behavior."
185+
if ($RecommendedRegions.Count -gt 0) {
186+
Write-Host "ℹ️ Recommended regions (≥ $RECOMMENDED_TOKENS tokens available): $($RecommendedRegions -join ', ')"
187+
}
188+
if (-not (Confirm-Action "❓ Proceed anyway?")) {
189+
Write-Host "❌ Deployment aborted by user. Please select another region or capacity."
190+
exit 1
191+
}
192+
}
136193
Set-DeploymentValues $Location $Capacity
137194
exit 0
138195
}
139196
} else {
140-
Write-Host "`n⚠️ Insufficient quota in '$LOCATION' (Available: $($PrimaryResult.Available), Required: $Capacity). Checking fallback regions..."
197+
Write-Host "`n⚠️ Insufficient quota in '$Location' (Available: $($PrimaryResult.Available), Required: $Capacity). Checking fallback regions..."
141198
}
142199
} else {
143200
Write-Host "`n⚠️ Could not retrieve quota info for region '$Location'."
144201
}
145202

146-
# ------------------ Check Fallback Regions ------------------
147203
foreach ($region in $PreferredRegions) {
148204
if ($region -eq $Location) { continue }
149205
$result = Check-Quota -Region $region
@@ -165,66 +221,10 @@ if ($EligibleFallbacks.Count -gt 0) {
165221
Write-Host " - $region"
166222
}
167223
}
168-
169224
Write-Host "`n❗ The originally selected region '$Location' does not have enough quota."
170225
Write-Host "👉 You can manually choose one of the recommended fallback regions for deployment."
171226
} else {
172227
Write-Host "`n❌ ERROR: No region has sufficient quota."
173228
}
174229

175-
# ------------------ Manual Prompt ------------------
176-
while ($true) {
177-
$ManualRegion = Read-Host "`nPlease enter a region you want to try manually"
178-
if (-not $ManualRegion) {
179-
Write-Host "❌ ERROR: No region entered. Exiting."
180-
exit 1
181-
}
182-
183-
$ManualCapacityStr = Read-Host "Enter the capacity you want to use (numeric value)"
184-
if (-not ($ManualCapacityStr -as [int]) -or [int]$ManualCapacityStr -le 0) {
185-
Write-Host "❌ Invalid capacity value. Try again."
186-
continue
187-
}
188-
189-
$ManualCapacity = [int]$ManualCapacityStr
190-
191-
if ($ManualCapacity -lt 200) {
192-
Write-Host "`n⚠️ You have entered a capacity of $ManualCapacity, which is less than the recommended minimum (200)."
193-
Write-Host "🚨 This may cause performance issues or unexpected behavior."
194-
Write-Host "ℹ️ Recommended regions (≥ $RECOMMENDED_TOKENS tokens available): $($RecommendedRegions -join ', ')"
195-
do {
196-
$proceed = Read-Host "❓ Proceed anyway? (y/n)"
197-
if ($proceed -notmatch "^[YyNn]$") {
198-
Write-Host "❌ Invalid input. Please enter 'y' or 'n'."
199-
}
200-
} while ($proceed -notmatch "^[YyNn]$")
201-
202-
if ($proceed -notmatch "^[Yy]$") {
203-
continue
204-
}
205-
}
206-
207-
Write-Host "`n🔍 Checking quota in region '$ManualRegion' for requested capacity: $ManualCapacity..."
208-
$ManualResult = Check-Quota -Region $ManualRegion
209-
210-
if (-not $ManualResult) {
211-
Write-Host "⚠️ Could not retrieve quota info for region '$ManualRegion'. Try again."
212-
continue
213-
}
214-
215-
if ($ManualResult.Available -ge $ManualCapacity) {
216-
if ($ManualResult.Available -lt $RECOMMENDED_TOKENS) {
217-
Write-Host "`n⚠️ Region '$ManualRegion' has less than recommended ($RECOMMENDED_TOKENS) tokens."
218-
$proceed = Read-Host "❓ Proceed anyway? (y/n)"
219-
if ($proceed -notmatch "^[Yy]$") {
220-
continue
221-
}
222-
}
223-
224-
Set-DeploymentValues $ManualRegion $ManualCapacity
225-
Write-Host "✅ Deployment values set. Exiting."
226-
exit 0
227-
} else {
228-
Write-Host "❌ Quota in region '$ManualRegion' is insufficient. Available: $($ManualResult.Available), Required: $ManualCapacity"
229-
}
230-
}
230+
Manual-Prompt

0 commit comments

Comments
 (0)