Skip to content

Commit f7bc0ba

Browse files
committed
Add maxRetry parameter to ARM API functions for improved reliability
1 parent 9933137 commit f7bc0ba

2 files changed

Lines changed: 32 additions & 22 deletions

File tree

‎scripts/pipelines/helper/helper-functions.ps1‎

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ function getResourceViaARMAPI {
218218
Param(
219219
[Parameter(Mandatory = $true)][string]$resourceId,
220220
[Parameter(Mandatory = $true)][string]$apiVersion,
221-
[Parameter(Mandatory = $false)][string]$token
221+
[Parameter(Mandatory = $false)][string]$token,
222+
[parameter(Mandatory = $false)][int]$maxRetry = 3
222223
)
223224
$uri = "https://management.azure.com{0}?api-version={1}" -f $resourceId, $apiVersion
224225
Write-Verbose "[$(getCurrentUTCString)]: Trying getting resource via the Resource provider API endpoint '$uri'" -Verbose
@@ -228,14 +229,18 @@ function getResourceViaARMAPI {
228229
$headers = @{
229230
'Authorization' = "Bearer $token"
230231
}
231-
try {
232-
$request = Invoke-WebRequest -Uri $uri -Method "GET" -Headers $headers
233-
if ($request.StatusCode -ge 200 -and $request.StatusCode -lt 300) {
234-
$resourceExists = $true
232+
$retryCount = 0
233+
do {
234+
try {
235+
$request = Invoke-WebRequest -Uri $uri -Method "GET" -Headers $headers
236+
if ($request.StatusCode -ge 200 -and $request.StatusCode -lt 300) {
237+
$resourceExists = $true
238+
}
239+
} catch {
240+
$resourceExists = $false
235241
}
236-
} catch {
237-
$resourceExists = $false
238-
}
242+
$retryCount++
243+
} while (-not $resourceExists -and $retryCount -lt $maxRetry)
239244
if ($resourceExists) {
240245
$resource = ($request.Content | ConvertFrom-Json -Depth 99)
241246
}
@@ -250,7 +255,8 @@ function newResourceGroupViaARMAPI {
250255
[Parameter(Mandatory = $true)][string]$location,
251256
[Parameter(Mandatory = $false)][hashtable]$tags,
252257
[Parameter(Mandatory = $false)][string]$token,
253-
[Parameter(Mandatory = $false)][string]$apiVersion = '2021-04-01'
258+
[Parameter(Mandatory = $false)][string]$apiVersion = '2021-04-01',
259+
[parameter(Mandatory = $false)][int]$maxRetry = 3
254260
)
255261
$uri = "https://management.azure.com/subscriptions/{0}/resourceGroups/{1}?api-version={2}" -f $subscriptionId, $resourceGroupName, $apiVersion
256262
Write-Verbose "[$(getCurrentUTCString)]: Trying creating resource group via the Resource provider API endpoint '$uri'" -Verbose
@@ -268,15 +274,19 @@ function newResourceGroupViaARMAPI {
268274
$body.tags = $tags
269275
}
270276
$body = $body | ConvertTo-Json -Depth 10
271-
try {
272-
$request = Invoke-WebRequest -Uri $uri -Method "PUT" -Headers $headers -Body $body
273-
if ($request.StatusCode -ge 200 -and $request.StatusCode -lt 300) {
274-
$resourceGroupCreated = $true
277+
$retryCount = 0
278+
do {
279+
try {
280+
$request = Invoke-WebRequest -Uri $uri -Method "PUT" -Headers $headers -Body $body
281+
if ($request.StatusCode -ge 200 -and $request.StatusCode -lt 300) {
282+
$resourceGroupCreated = $true
283+
}
284+
} catch {
285+
Write-Error $_.Exception.Message
286+
$resourceGroupCreated = $false
275287
}
276-
} catch {
277-
Write-Error $_.Exception.Message
278-
$resourceGroupCreated = $false
279-
}
288+
$retryCount++
289+
} while (-not $resourceGroupCreated -and $retryCount -lt $maxRetry)
280290
if ($resourceGroupCreated) {
281291
Write-Verbose "Resource group '$resourceGroupName' created successfully." -Verbose
282292
$resourceGroupId = ($request.Content | ConvertFrom-Json -Depth 99).id

‎scripts/pipelines/policy-integration-tests/pipeline-deploy-policy-test-bicep-template.ps1‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ If ($deploymentResultFilePath -ne '') {
8888
#create the test resource group if it's specified in the local config file and doesn't exist
8989
if ($testResourceGroupName) {
9090
$testResourceGroupResourceId = '/subscriptions/{0}/resourceGroups/{1}' -f $testSubId, $testResourceGroupName
91-
$existingTestResourceGroup = getResourceViaARMAPI -ResourceId $testResourceGroupResourceId -apiVersion $resourceGroupApiVersion
91+
$existingTestResourceGroup = getResourceViaARMAPI -ResourceId $testResourceGroupResourceId -apiVersion $resourceGroupApiVersion -maxRetry $maxRetry
9292
if (!($existingTestResourceGroup)) {
9393
if ($tagsForResourceGroup) {
9494
Write-Output "[$(getCurrentUTCString)]: Resource group '$testResourceGroupName' doesn't exist. Creating the resource group '$testResourceGroupName' with predefined tags..."
95-
$testResourceGroup = newResourceGroupViaARMAPI -subscriptionId $testSubId -resourceGroupName $testResourceGroupName -location $testLocation -apiVersion $resourceGroupApiVersion-Tag $tags
95+
$testResourceGroup = newResourceGroupViaARMAPI -subscriptionId $testSubId -resourceGroupName $testResourceGroupName -location $testLocation -apiVersion $resourceGroupApiVersion-Tag $tags -maxRetry $maxRetry
9696
} else {
9797
Write-Output "[$(getCurrentUTCString)]: Resource group '$testResourceGroupName' doesn't exist. Creating the resource group '$testResourceGroupName' without any tags..."
98-
$testResourceGroup = newResourceGroupViaARMAPI -subscriptionId $testSubId -resourceGroupName $testResourceGroupName -location $testLocation -apiVersion $resourceGroupApiVersion
98+
$testResourceGroup = newResourceGroupViaARMAPI -subscriptionId $testSubId -resourceGroupName $testResourceGroupName -location $testLocation -apiVersion $resourceGroupApiVersion -maxRetry $maxRetry
9999
}
100100

101101
} else {
@@ -145,14 +145,14 @@ if (Test-path $BicepFilePath -PathType Leaf) {
145145
$subscriptionId = $globalTestConfig.subscriptions.$subscriptionName.id
146146
$rgResourceId = '/subscriptions/{0}/resourceGroups/{1}' -f $subscriptionId, $resourceGroupName
147147
Write-Verbose "Checking if the resource group '$rgResourceId' exists..." -verbose
148-
$existingRg = getResourceViaARMAPI -ResourceId $rgResourceId -apiVersion $resourceGroupApiVersion
148+
$existingRg = getResourceViaARMAPI -ResourceId $rgResourceId -apiVersion $resourceGroupApiVersion -maxRetry $maxRetry
149149
if ($existingRg) {
150150
Write-Verbose "[$(getCurrentUTCString)]: Resource group '$rgResourceId' already exists. Skipping creation." -verbose
151151
$additionalResourceGroups += $existingRg.id
152152
} else {
153153
Write-Output "[$(getCurrentUTCString)]: Resource group '$rgResourceId' doesn't exist. Creating in location '$testLocation'..."
154154
#create the resource group using ARM REST API directly so we don't have to change the subscription in the Az context
155-
$additionalResourceGroups += newResourceGroupViaARMAPI -subscriptionId $subscriptionId -resourceGroupName $resourceGroupName -location $testLocation -apiVersion $resourceGroupApiVersion
155+
$additionalResourceGroups += newResourceGroupViaARMAPI -subscriptionId $subscriptionId -resourceGroupName $resourceGroupName -location $testLocation -apiVersion $resourceGroupApiVersion -maxRetry $maxRetry
156156
}
157157
}
158158
Write-Verbose "[$(getCurrentUTCString)]: Additional resource groups:" -verbose

0 commit comments

Comments
 (0)