Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improved removal resiliency #3038

Merged
merged 9 commits into from
Sep 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ Optional. The ID of the management group to fetch deployments from. Relevant for
.PARAMETER Scope
Mandatory. The scope to search in

.PARAMETER DoThrow
Optional. Throw an exception if a deployment cannot be found. If not set, a warning is returned instead.

.EXAMPLE
Get-DeploymentTargetResourceListInner -Name 'keyvault-12356' -Scope 'resourcegroup'

Expand Down Expand Up @@ -159,7 +162,10 @@ function Get-DeploymentTargetResourceListInner {
'managementgroup',
'tenant'
)]
[string] $Scope
[string] $Scope,

[Parameter(Mandatory = $false)]
[switch] $DoThrow
)

$resultSet = [System.Collections.ArrayList]@()
Expand All @@ -178,7 +184,13 @@ function Get-DeploymentTargetResourceListInner {
if ($op = Get-DeploymentOperationAtScope @baseInputObject -ResourceGroupName $resourceGroupName -SubscriptionId $currentContext.Subscription.Id) {
[array]$deploymentTargets = $op.TargetResource.id | Where-Object { $_ -ne $null } | Select-Object -Unique
} else {
throw 'NoDeploymentFound'
$message = "Not found deployment [$Name] in scope [$Scope] of Resource Group [$ResourceGroupName]."
if ($DoThrow) {
throw $message
} else {
Write-Warning "$message Ignoring, as nested deployment."
return
}
}
} else {
# In case the resource group itself was already deleted, there is no need to try and fetch deployments from it
Expand All @@ -191,23 +203,41 @@ function Get-DeploymentTargetResourceListInner {
if ($op = Get-DeploymentOperationAtScope @baseInputObject -SubscriptionId $currentContext.Subscription.Id) {
[array]$deploymentTargets = $op.TargetResource.id | Where-Object { $_ -ne $null } | Select-Object -Unique
} else {
throw 'NoDeploymentFound'
$message = "Not found deployment [$Name] in scope [$Scope]."
if ($DoThrow) {
throw $message
} else {
Write-Warning "$message Ignoring, as nested deployment."
return
}
}
break
}
'managementgroup' {
if ($op = Get-DeploymentOperationAtScope @baseInputObject -ManagementGroupId $ManagementGroupId) {
[array]$deploymentTargets = $op.TargetResource.id | Where-Object { $_ -ne $null } | Select-Object -Unique
} else {
throw 'NoDeploymentFound'
$message = "Not found deployment [$Name] in scope [$Scope]."
if ($DoThrow) {
throw $message
} else {
Write-Warning "$message Ignoring, as nested deployment."
return
}
}
break
}
'tenant' {
if ($op = Get-DeploymentOperationAtScope @baseInputObject) {
[array]$deploymentTargets = $op.TargetResource.id | Where-Object { $_ -ne $null } | Select-Object -Unique
} else {
throw 'NoDeploymentFound'
$message = "Not found deployment [$Name] in scope [$Scope]."
if ($DoThrow) {
throw $message
} else {
Write-Warning "$message Ignoring, as nested deployment."
return
}
}
break
}
Expand All @@ -216,15 +246,15 @@ function Get-DeploymentTargetResourceListInner {
###########################
# Manage nested resources #
###########################
foreach ($deployment in ($deploymentTargets | Where-Object { $_ -notmatch '\/deployments\/' } )) {
foreach ($deployment in ($deploymentTargets | Where-Object { $_ -notmatch '\/Microsoft\.Resources\/deployments\/' } )) {
Write-Verbose ('Found deployed resource [{0}]' -f $deployment)
[array]$resultSet += $deployment
}

#############################
# Manage nested deployments #
#############################
foreach ($deployment in ($deploymentTargets | Where-Object { $_ -match '\/deployments\/' } )) {
foreach ($deployment in ($deploymentTargets | Where-Object { $_ -match '\/Microsoft\.Resources\/deployments\/' } )) {
$name = Split-Path $deployment -Leaf
if ($deployment -match '/resourceGroups/') {
# Resource Group Level Child Deployments #
Expand Down Expand Up @@ -361,7 +391,7 @@ function Get-DeploymentTargetResourceList {
$innerInputObject['ManagementGroupId'] = $ManagementGroupId
}
try {
$targetResources = Get-DeploymentTargetResourceListInner @innerInputObject
$targetResources = Get-DeploymentTargetResourceListInner @innerInputObject -DoThrow # Specifying [-DoThrow] for top-level deployments that we definitely want to resolve
Write-Verbose ('Found & resolved deployment [{0}]. [{1}] resources found to remove.' -f $deploymentNameObject.Name, $targetResources.Count) -Verbose
$deploymentNameObject.Resolved = $true
$resourcesToRemove += $targetResources
Expand Down