-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Modules] Fix/Add purge during removal of machine learning workspace (#…
…3597) * Update custom publishing resources * Add script for purging machine learning workspace * reset settings * remove double space * fix attempt #1 * correct spelling of retry * align with Invoke-ResourceRemoval.ps1 * fix * Reset files * Update utilities/pipelines/resourceRemoval/helper/Remove-ResourceList.ps1 Co-authored-by: Alexander Sehr <ASehr@hotmail.de> * Update utilities/pipelines/resourceRemoval/helper/Invoke-ResourceRemoval.ps1 Co-authored-by: Alexander Sehr <ASehr@hotmail.de> * Restructure * added break again * align outputs/logs * Moved out of default to before switch * Apply suggestions from code review Co-authored-by: Alexander Sehr <ASehr@hotmail.de> * Update utilities/pipelines/resourceRemoval/helper/Invoke-ResourceRemoval.ps1 Co-authored-by: Alexander Sehr <ASehr@hotmail.de> * Update utilities/pipelines/resourceRemoval/helper/Invoke-ResourceRemoval.ps1 Co-authored-by: Alexander Sehr <ASehr@hotmail.de> * Update utilities/pipelines/resourceRemoval/helper/Invoke-ResourceRemoval.ps1 Co-authored-by: Alexander Sehr <ASehr@hotmail.de> * Update utilities/pipelines/resourceRemoval/helper/Invoke-ResourceRemoval.ps1 Co-authored-by: Alexander Sehr <ASehr@hotmail.de> * Remove DevTestLab as logic is covered with regular flow now * Change logging and removal loop slightly * Fixing logic * Update utilities/pipelines/resourceRemoval/helper/Invoke-ResourceRemoval.ps1 * Update utilities/pipelines/resourceRemoval/helper/Invoke-ResourceLockRemoval.ps1 Co-authored-by: Alexander Sehr <ASehr@hotmail.de> * Update utilities/pipelines/resourceRemoval/helper/Invoke-ResourceLockRemoval.ps1 Co-authored-by: Alexander Sehr <ASehr@hotmail.de> --------- Co-authored-by: Alexander Sehr <ASehr@hotmail.de>
- Loading branch information
1 parent
f27f09f
commit 2bd0601
Showing
8 changed files
with
214 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
utilities/pipelines/resourceRemoval/helper/Invoke-ResourceLockRemoval.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<# | ||
.SYNOPSIS | ||
Remove resource locks from a resource or a specific resource lock. | ||
.DESCRIPTION | ||
Remove resource locks from a resource or a specific resource lock. | ||
.PARAMETER ResourceId | ||
Mandatory. The resourceID of the resource to check, and remove a resource lock from. | ||
.PARAMETER Type | ||
Optional. The type of the resource. If the resource is a lock, the lock itself will be removed. If the resource is a resource, all locks on the resource will be removed. If not specified, the resource will be checked for locks, and if any are found, all locks will be removed. | ||
.PARAMETER RetryLimit | ||
Optional. The number of times to retry checking if the lock is removed. | ||
.PARAMETER RetryInterval | ||
Optional. The number of seconds to wait between each retry. | ||
.EXAMPLE | ||
Invoke-ResourceLockRemoval -ResourceId '/subscriptions/.../resourceGroups/validation-rg/.../resource-name' | ||
Check if the resource 'resource-name' is locked. If it is, remove the lock. | ||
#> | ||
function Invoke-ResourceLockRemoval { | ||
[CmdletBinding(SupportsShouldProcess)] | ||
param ( | ||
[Parameter(Mandatory = $true)] | ||
[string] $ResourceId, | ||
|
||
[Parameter(Mandatory = $false)] | ||
[string] $Type, | ||
|
||
[Parameter(Mandatory = $false)] | ||
[int] $RetryLimit = 10, | ||
|
||
[Parameter(Mandatory = $false)] | ||
[int] $RetryInterval = 10 | ||
) | ||
# Load functions | ||
. (Join-Path $PSScriptRoot 'Invoke-ResourceLockRetrieval.ps1') | ||
|
||
$resourceLock = Invoke-ResourceLockRetrieval -ResourceId $ResourceId -Type $Type | ||
|
||
$isLocked = $resourceLock.count -gt 0 | ||
if (-not $isLocked) { | ||
return | ||
} | ||
|
||
$resourceLock | ForEach-Object { | ||
Write-Warning (' [-] Removing lock [{0}] on [{1}] of type [{2}].' -f $_.Name, $_.ResourceName, $_.ResourceType) | ||
if ($PSCmdlet.ShouldProcess(('Lock [{0}] on resource [{1}] of type [{2}].' -f $_.Name, $_.ResourceName, $_.ResourceType ), 'Remove')) { | ||
$null = $_ | Remove-AzResourceLock -Force | ||
} | ||
} | ||
|
||
$retryCount = 0 | ||
do { | ||
$retryCount++ | ||
if ($retryCount -ge $RetryLimit) { | ||
Write-Warning (' [!] Lock was not removed after {1} seconds. Continuing with resource removal.' -f ($retryCount * $RetryInterval)) | ||
break | ||
} | ||
Write-Verbose ' [⏱️] Waiting for lock to be removed.' -Verbose | ||
Start-Sleep -Seconds $RetryInterval | ||
|
||
# Rechecking the resource locks to see if they have been removed. | ||
$resourceLock = Invoke-ResourceLockRetrieval -ResourceId $ResourceId -Type $Type | ||
$isLocked = $resourceLock.count -gt 0 | ||
} while ($isLocked) | ||
|
||
Write-Verbose (' [-] [{0}] resource lock(s) removed.' -f $resourceLock.count) -Verbose | ||
} |
45 changes: 45 additions & 0 deletions
45
utilities/pipelines/resourceRemoval/helper/Invoke-ResourceLockRetrieval.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<# | ||
.SYNOPSIS | ||
Gets resource locks on a resource or a specific resource lock. | ||
.DESCRIPTION | ||
Gets resource locks on a resource or a specific resource lock. | ||
.PARAMETER ResourceId | ||
Mandatory. The resourceID of the resource to check or the resource lock to check. | ||
.PARAMETER Type | ||
Optional. The type of the resource. | ||
If the resource is a lock, the lock itself will be returned. | ||
If the resource is not a lock, all locks on the resource will be returned. | ||
.EXAMPLE | ||
Invoke-ResourceLockRetrieval -ResourceId '/subscriptions/.../resourceGroups/validation-rg/.../resource-name' | ||
Check if the resource 'resource-name' is locked. If it is, return the lock. | ||
.EXAMPLE | ||
Invoke-ResourceLockRetrieval -ResourceId '/subscriptions/.../resourceGroups/validation-rg/.../resource-name/providers/Microsoft.Authorization/locks/lock-name' -Type 'Microsoft.Authorization/locks' | ||
Return the lock 'lock-name' on the resource 'resource-name'. | ||
.NOTES | ||
Needed as the AzPwsh cmdlet Get-AzResourceLock does not support getting a specific lock by LockId. | ||
#> | ||
function Invoke-ResourceLockRetrieval { | ||
[OutputType([System.Management.Automation.PSCustomObject])] | ||
param ( | ||
[Parameter(Mandatory = $true)] | ||
[string] $ResourceId, | ||
|
||
[Parameter(Mandatory = $false)] | ||
[string] $Type = '' | ||
) | ||
if ($Type -eq 'Microsoft.Authorization/locks') { | ||
$lockName = ($ResourceId -split '/')[-1] | ||
$lockScope = ($ResourceId -split '/providers/Microsoft.Authorization/locks')[0] | ||
return Get-AzResourceLock -LockName $lockName -Scope $lockScope -ErrorAction SilentlyContinue | ||
} else { | ||
return Get-AzResourceLock -Scope $ResourceId -ErrorAction SilentlyContinue | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.