Skip to content

Commit

Permalink
Update to latest
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderSehr committed Aug 8, 2024
1 parent f22fa07 commit aca4a3f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ function Get-DeploymentTargetResourceListInner {
switch ($Scope) {
'resourcegroup' {
if (Get-AzResourceGroup -Name $resourceGroupName -ErrorAction 'SilentlyContinue') {
[array]$deploymentTargets = (Get-DeploymentOperationAtScope @baseInputObject -ResourceGroupName $resourceGroupName -SubscriptionId $currentContext.Subscription.Id).targetResource.id | Where-Object { $_ -ne $null } | Select-Object -Unique
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'
}
} else {
# In case the resource group itself was already deleted, there is no need to try and fetch deployments from it
# In case we already have any such resources in the list, we should remove them
Expand All @@ -183,15 +187,25 @@ function Get-DeploymentTargetResourceListInner {
break
}
'subscription' {
[array]$deploymentTargets = (Get-DeploymentOperationAtScope @baseInputObject -SubscriptionId $currentContext.Subscription.Id).targetResource.id | Where-Object { $_ -ne $null } | Select-Object -Unique
if ($op = Get-DeploymentOperationAtScope @baseInputObject -SubscriptionId $currentContext.Subscription.Id) {
[array]$deploymentTargets = $op.TargetResource.id | Where-Object { $_ -ne $null } | Select-Object -Unique
} else {
throw 'NoDeploymentFound'
}
break
}
'managementgroup' {
[array]$deploymentTargets = (Get-DeploymentOperationAtScope @baseInputObject -ManagementGroupId $ManagementGroupId).targetResource.id | Where-Object { $_ -ne $null } | Select-Object -Unique
break
if ($op = Get-DeploymentOperationAtScope @baseInputObject -ManagementGroupId $ManagementGroupId) {
[array]$deploymentTargets = $op.TargetResource.id | Where-Object { $_ -ne $null } | Select-Object -Unique
}
throw 'NoDeploymentFound'
}
'tenant' {
[array]$deploymentTargets = (Get-DeploymentOperationAtScope @baseInputObject).targetResource.id | Where-Object { $_ -ne $null } | Select-Object -Unique
if ($op = Get-DeploymentOperationAtScope @baseInputObject) {
[array]$deploymentTargets = $op.TargetResource.id | Where-Object { $_ -ne $null } | Select-Object -Unique
} else {
throw 'NoDeploymentFound'
}
break
}
}
Expand Down Expand Up @@ -343,30 +357,35 @@ function Get-DeploymentTargetResourceList {
if (-not [String]::IsNullOrEmpty($ManagementGroupId)) {
$innerInputObject['ManagementGroupId'] = $ManagementGroupId
}

[array]$targetResources = Get-DeploymentTargetResourceListInner @innerInputObject
if ($targetResources.Count -gt 0) {
Write-Verbose ('Found & resolved deployment [{0}]' -f $deploymentNameObject.Name) -Verbose
try {
$targetResources = Get-DeploymentTargetResourceListInner @innerInputObject
Write-Verbose ('Found & resolved deployment [{0}]. [{1}] resources found to remove.' -f $deploymentNameObject.Name, $targetResources.Count) -Verbose
$deploymentNameObject.Resolved = $true
$resourcesToRemove += $targetResources
} catch {
$remainingDeploymentNames = ($deploymentNameObjects | Where-Object { -not $_.Resolved }).Name
Write-Verbose ('No deployment found by name(s) [{0}] in scope [{1}]. Retrying in [{2}] seconds [{3}/{4}]' -f ($remainingDeploymentNames -join ', '), $scope, $searchRetryInterval, $searchRetryCount, $searchRetryLimit) -Verbose
Start-Sleep $searchRetryInterval
$searchRetryCount++
}
}

# Break check
if ($deploymentNameObjects.Resolved -notcontains $false) {
break
}
} while ($searchRetryCount -le $searchRetryLimit)

if ($searchRetryCount -gt $searchRetryLimit) {
$remainingDeploymentNames = ($deploymentNameObjects | Where-Object { -not $_.Resolved }).Name
Write-Verbose ('No deployment found by name(s) [{0}] in scope [{1}]. Retrying in [{2}] seconds [{3}/{4}]' -f ($remainingDeploymentNames -join ', '), $scope, $searchRetryInterval, $searchRetryCount, $searchRetryLimit) -Verbose
Start-Sleep $searchRetryInterval
$searchRetryCount++
} while ($searchRetryCount -le $searchRetryLimit)

if (-not $resourcesToRemove) {
Write-Warning ('No deployment target resources found for [{0}]' -f ($DeploymentNames -join ', '))
return @()
# We don't want to outright throw an exception as we want to remove as many resources as possible before failing the script in the calling functino
return @{
resolveError = ('No deployment for the deployment name(s) [{0}] found' -f ($remainingDeploymentNames -join ', '))
resourcesToRemove = $resourcesToRemove
}
}
return @{
resourcesToRemove = $resourcesToRemove
}

return $resourcesToRemove
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,21 @@ function Remove-Deployment {
if (-not [String]::IsNullOrEmpty($ManagementGroupId)) {
$deploymentsInputObject['ManagementGroupId'] = $ManagementGroupId
}
$deployedTargetResources += Get-DeploymentTargetResourceList @deploymentsInputObject

if ($deployedTargetResources.Count -eq 0) {
throw 'No deployment target resources found.'
}
# In case the function also returns an error, we'll throw a corresponding exception at the end of this script (see below)
$resolveResult = Get-DeploymentTargetResourceList @deploymentsInputObject
$deployedTargetResources += $resolveResult.resourcesToRemove
}

[array] $deployedTargetResources = $deployedTargetResources | Select-Object -Unique

Write-Verbose ('Total number of deployment target resources after fetching deployments [{0}]' -f $deployedTargetResources.Count) -Verbose

if (-not $deployedTargetResources) {
# Nothing to do
return
}

# Pre-Filter & order items
# ========================
$rawTargetResourceIdsToRemove = $deployedTargetResources | Sort-Object -Culture 'en-US' -Property { $_.Split('/').Count } -Descending | Select-Object -Unique
Expand All @@ -121,10 +125,6 @@ function Remove-Deployment {
[array] $resourcesToRemove = Get-ResourceIdsAsFormattedObjectList -ResourceIds $rawTargetResourceIdsToRemove
Write-Verbose ('Total number of deployment target resources after formatting items [{0}]' -f $resourcesToRemove.Count) -Verbose

if ($resourcesToRemove.Count -eq 0) {
return
}

# Filter resources
# ================

Expand Down Expand Up @@ -172,6 +172,11 @@ function Remove-Deployment {
} else {
Write-Verbose 'Found [0] resources to remove'
}

# In case any deployment was not resolved as planned we finally want to throw an exception to make this visible in the pipeline
if ($resolveResult.resolveError) {
throw ('The following error was thrown while resolving the original deployment names: [{0}]' -f $resolveResult.resolveError)
}
}

end {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,6 @@ function Remove-ResourceList {
if ($resourcesToRetry.Count -gt 0) {
throw ('The removal failed for resources [{0}]' -f ((Split-Path $resourcesToRetry.resourceId -Leaf) -join ', '))
} else {
Write-Verbose 'The removal completed successfully'
Write-Verbose ('The removal of the [{0}] completed successfully' -f $ResourcesToRemove.Count)
}
}

0 comments on commit aca4a3f

Please sign in to comment.