From f17590efd1cf3a2ecc04f55ca53dc994f46fad65 Mon Sep 17 00:00:00 2001 From: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com> Date: Thu, 29 Oct 2020 16:25:33 -0700 Subject: [PATCH] Sync eng/common directory with azure-sdk-tools for PR 1146 (#16968) * Remove resource group asynchronously and do not wait for completion * Verify resource group has reached Deleting state before exiting script * Use $_ instead of $Error[0] for resource group removal handling Co-authored-by: Ben Broderick Phillips --- .../TestResources/Remove-TestResources.ps1 | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/eng/common/TestResources/Remove-TestResources.ps1 b/eng/common/TestResources/Remove-TestResources.ps1 index 37f3f5851c603..83b1a58c347f9 100644 --- a/eng/common/TestResources/Remove-TestResources.ps1 +++ b/eng/common/TestResources/Remove-TestResources.ps1 @@ -137,9 +137,31 @@ if (![string]::IsNullOrWhiteSpace($ServiceDirectory)) { } } +$verifyDeleteScript = { + try { + $group = Get-AzResourceGroup -name $ResourceGroupName + } catch { + if ($_.ToString().Contains("Provided resource group does not exist")) { + Write-Verbose "Resource group '$ResourceGroupName' not found. Continuing..." + return + } + throw $_ + } + + if ($group.ProvisioningState -ne "Deleting") + { + throw "Resource group is in '$($group.ProvisioningState)' state, expected 'Deleting'" + } +} + Log "Deleting resource group '$ResourceGroupName'" -if (Retry { Remove-AzResourceGroup -Name "$ResourceGroupName" -Force:$Force }) { - Write-Verbose "Successfully deleted resource group '$ResourceGroupName'" +if ($Force) { + Remove-AzResourceGroup -Name "$ResourceGroupName" -Force:$Force -AsJob + Retry $verifyDeleteScript 3 + Write-Verbose "Requested async deletion of resource group '$ResourceGroupName'" +} else { + # Don't swallow interactive confirmation when Force is false + Remove-AzResourceGroup -Name "$ResourceGroupName" -Force:$Force } $exitActions.Invoke() @@ -194,4 +216,4 @@ resource group whose name is stored in the environment variable AZURE_RESOURCEGROUP_NAME. .LINK New-TestResources.ps1 -#> \ No newline at end of file +#>