Skip to content

Commit

Permalink
feat: Added the option to declare a specific resource type to be remo…
Browse files Browse the repository at this point in the history
…ved last (#1887)

## Description

- Added the option to declare a specific resource type to be removed
last
- This is required if a resource like a subscription should be removed
after everything else is removed. This is done in preparation for the
LZ-Accelerator

## Pipeline Reference

<!-- Insert your Pipeline Status Badge below -->

| Pipeline |
| -------- |
|
[![avm.res.key-vault.vault](https://github.com/Azure/bicep-registry-modules/actions/workflows/avm.res.key-vault.vault.yml/badge.svg?branch=users%2Falsehr%2FremoveFinalSequence&event=workflow_dispatch)](https://github.com/Azure/bicep-registry-modules/actions/workflows/avm.res.key-vault.vault.yml)
|

## Type of Change

<!-- Use the check-boxes [x] on the options that are relevant. -->

- [x] Update to CI Environment or utlities (Non-module effecting
changes)
- [ ] Azure Verified Module updates:
- [ ] Bugfix containing backwards compatible bug fixes, and I have NOT
bumped the MAJOR or MINOR version in `version.json`:
- [ ] Someone has opened a bug report issue, and I have included "Closes
#{bug_report_issue_number}" in the PR description.
- [ ] The bug was found by the module author, and no one has opened an
issue to report it yet.
- [ ] Feature update backwards compatible feature updates, and I have
bumped the MINOR version in `version.json`.
- [ ] Breaking changes and I have bumped the MAJOR version in
`version.json`.
  - [ ] Update to documentation
  • Loading branch information
AlexanderSehr authored May 10, 2024
1 parent cf31866 commit dcd987f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function Initialize-DeploymentRemoval {
}

# The initial sequence is a general order-recommendation
$removalSequence = @(
$RemoveFirstSequence = @(
'Microsoft.Authorization/locks',
'Microsoft.Authorization/roleAssignments',
'Microsoft.Insights/diagnosticSettings',
Expand All @@ -95,6 +95,10 @@ function Initialize-DeploymentRemoval {
'Microsoft.Resources/resourceGroups'
)

$removeLastSequence = @(
'Microsoft.Subscription/aliases'
)

if ($DeploymentNames.Count -gt 0) {
Write-Verbose 'Handling resource removal with deployment names' -Verbose
foreach ($DeploymentName in $DeploymentNames) {
Expand All @@ -113,7 +117,12 @@ function Initialize-DeploymentRemoval {
# $moduleName = Split-Path (Split-Path (Split-Path $templateFilePath -Parent) -Parent) -LeafBase
# switch ($moduleName) {
# '<moduleName01>' { # For example: 'virtualWans', 'automationAccounts'
# $removalSequence += @(
# $RemoveFirstSequence += @(
# '<resourceType01>', # For example: 'Microsoft.Network/vpnSites', 'Microsoft.OperationalInsights/workspaces/linkedServices'
# '<resourceType02>',
# '<resourceType03>'
# )
# $RemoveLastSequence += @(
# '<resourceType01>', # For example: 'Microsoft.Network/vpnSites', 'Microsoft.OperationalInsights/workspaces/linkedServices'
# '<resourceType02>',
# '<resourceType03>'
Expand All @@ -134,10 +143,11 @@ function Initialize-DeploymentRemoval {

# Invoke removal
$inputObject = @{
DeploymentNames = $DeploymentNames
ResourceIds = $ResourceIds
TemplateFilePath = $TemplateFilePath
RemovalSequence = $removalSequence
DeploymentNames = $DeploymentNames
ResourceIds = $ResourceIds
TemplateFilePath = $TemplateFilePath
RemoveFirstSequence = $removeFirstSequence
RemoveLastSequence = $removeLastSequence
}
if (-not [String]::IsNullOrEmpty($TemplateFilePath)) {
$inputObject['TemplateFilePath'] = $TemplateFilePath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ Each item should be in format:
type = '...'
}
.PARAMETER Order
Optional. The order of resource types to apply for deletion. If order is provided, the list is returned as is
.PARAMETER RemoveFirstSequence
Optional. The order of resource types to remove before all others. If no sequence is provided, the list is returned as is
.PARAMETER RemoveLastSequence
Optional. The order of resource types to remove after all others. If no sequence is provided, the list is returned as is
.EXAMPLE
Get-OrderedResourcesList -ResourcesToOrder @(@{ name = 'myAccount'; resourceId '(..)/Microsoft.Automation/automationAccounts/myAccount'; type = 'Microsoft.Automation/automationAccounts'}) -Order @('Microsoft.Insights/diagnosticSettings','Microsoft.Automation/automationAccounts')
Get-OrderedResourcesList -ResourcesToOrder @(@{ name = 'myAccount'; resourceId '(..)/Microsoft.Automation/automationAccounts/myAccount'; type = 'Microsoft.Automation/automationAccounts'}) -RemoveFirstSequence @('Microsoft.Insights/diagnosticSettings','Microsoft.Automation/automationAccounts')
Order the given list of resources which would put the diagnostic settings to the front of the list, then the automation account, then the rest. As only one item exists, the list is returned as is.
#>
Expand All @@ -31,16 +34,27 @@ function Get-OrderedResourcesList {
[hashtable[]] $ResourcesToOrder,

[Parameter(Mandatory = $false)]
[string[]] $Order = @()
[string[]] $RemoveFirstSequence = @(),

[Parameter(Mandatory = $false)]
[string[]] $RemoveLastSequence = @()
)

# Going from back to front of the list to stack in the correct order
for ($orderIndex = ($order.Count - 1); $orderIndex -ge 0; $orderIndex--) {
$searchItem = $order[$orderIndex]
# Order resources to remove first. Going from back to front of the list to stack in the correct order
for ($orderIndex = ($RemoveFirstSequence.Count - 1); $orderIndex -ge 0; $orderIndex--) {
$searchItem = $RemoveFirstSequence[$orderIndex]
if ($elementsContained = $resourcesToOrder | Where-Object { $_.type -eq $searchItem }) {
$resourcesToOrder = @() + $elementsContained + ($resourcesToOrder | Where-Object { $_.type -ne $searchItem })
}
}

# Order resources to remove last. Going from front to back of the list to stack in the correct order
for ($orderIndex = 0; $orderIndex -lt $RemoveLastSequence.Count; $orderIndex++) {
$searchItem = $RemoveLastSequence[$orderIndex]
if ($elementsContained = $resourcesToOrder | Where-Object { $_.type -eq $searchItem }) {
$resourcesToOrder = @() + ($resourcesToOrder | Where-Object { $_.type -ne $searchItem }) + $elementsContained
}
}

return $resourcesToOrder
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ Optional. The resource Id(s) of the resources to remove. Combined with resources
.PARAMETER TemplateFilePath
Optional. The path to the template used for the deployment(s). Used to determine the level/scope (e.g. subscription). Required if deploymentName(s) are provided.
.PARAMETER RemovalSequence
Optional. The order of resource types to apply for deletion
.PARAMETER RemoveFirstSequence
Optional. The order of resource types to remove before all others
.PARAMETER RemoveLastSequence
Optional. The order of resource types to remove after all others
.EXAMPLE
Remove-Deployment -DeploymentNames @('KeyVault-t1','KeyVault-t2') -TemplateFilePath 'C:/main.json'
Expand All @@ -52,7 +55,10 @@ function Remove-Deployment {
[string] $TemplateFilePath,

[Parameter(Mandatory = $false)]
[string[]] $RemovalSequence = @()
[string[]] $RemoveFirstSequence = @(),

[Parameter(Mandatory = $false)]
[string[]] $RemoveLastSequence = @()
)

begin {
Expand Down Expand Up @@ -152,7 +158,12 @@ function Remove-Deployment {

# Order resources
# ===============
[array] $resourcesToRemove = Get-OrderedResourcesList -ResourcesToOrder $resourcesToRemove -Order $RemovalSequence
$orderListInputObject = @{
ResourcesToOrder = $resourcesToRemove
RemoveFirstSequence = $RemoveFirstSequence
RemoveLastSequence = $RemoveLastSequence
}
[array] $resourcesToRemove = Get-OrderedResourcesList @orderListInputObject
Write-Verbose ('Total number of deployments after final ordering of resources [{0}]' -f $resourcesToRemove.Count) -Verbose

# Remove resources
Expand Down

0 comments on commit dcd987f

Please sign in to comment.