Skip to content

Commit

Permalink
fix: Added missing context switching to deployment target resolution (#…
Browse files Browse the repository at this point in the history
…1871)

## Description

Added missing context switching to deployment target resolution

This is important if e.g. resources are deployed into a new subscription
which, by default, cannot be the context at the time the pipeline starts
runnning.

Important to 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%2FdeploymentNameResolutino&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 7, 2024
1 parent e3505a6 commit fa3bf6d
Showing 1 changed file with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,57 +58,70 @@ function Get-DeploymentTargetResourceListInner {
)

$resultSet = [System.Collections.ArrayList]@()
$currentContext = Get-AzContext

##############################################
# Get all deployment children based on scope #
##############################################
switch ($Scope) {
'resourcegroup' {
if (Get-AzResourceGroup -Name $resourceGroupName -ErrorAction 'SilentlyContinue') {
[array]$deploymentTargets = (Get-AzResourceGroupDeploymentOperation -DeploymentName $name -ResourceGroupName $resourceGroupName).TargetResource | Where-Object { $_ -ne $null }
[array]$deploymentTargets = (Get-AzResourceGroupDeploymentOperation -DeploymentName $name -ResourceGroupName $resourceGroupName).TargetResource | Where-Object { $_ -ne $null } | Select-Object -Unique
} 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
[array]$resultSet = $resultSet | Where-Object { $_ -notmatch "/resourceGroups/$resourceGroupName/" }
[array]$resultSet = $resultSet | Where-Object { $_ -notmatch "\/resourceGroups\/$resourceGroupName\/" }
}
break
}
'subscription' {
[array]$deploymentTargets = (Get-AzDeploymentOperation -DeploymentName $name).TargetResource | Where-Object { $_ -ne $null }
[array]$deploymentTargets = (Get-AzDeploymentOperation -DeploymentName $name).TargetResource | Where-Object { $_ -ne $null } | Select-Object -Unique
break
}
'managementgroup' {
[array]$deploymentTargets = (Get-AzManagementGroupDeploymentOperation -DeploymentName $name -ManagementGroupId $ManagementGroupId).TargetResource | Where-Object { $_ -ne $null }
[array]$deploymentTargets = (Get-AzManagementGroupDeploymentOperation -DeploymentName $name -ManagementGroupId $ManagementGroupId).TargetResource | Where-Object { $_ -ne $null } | Select-Object -Unique
break
}
'tenant' {
[array]$deploymentTargets = (Get-AzTenantDeploymentOperation -DeploymentName $name).TargetResource | Where-Object { $_ -ne $null }
[array]$deploymentTargets = (Get-AzTenantDeploymentOperation -DeploymentName $name).TargetResource | Where-Object { $_ -ne $null } | Select-Object -Unique
break
}
}

###########################
# Manage nested resources #
###########################
foreach ($deployment in ($deploymentTargets | Where-Object { $_ -notmatch '/deployments/' } )) {
foreach ($deployment in ($deploymentTargets | Where-Object { $_ -notmatch '\/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 '\/deployments\/' } )) {
$name = Split-Path $deployment -Leaf
if ($deployment -match '/resourceGroups/') {
# Resource Group Level Child Deployments #
##########################################
if ($deployment -match '^\/subscriptions\/([0-9a-zA-Z-]+?)\/') {
$subscriptionId = $Matches[1]
if ($currentContext.Subscription.Id -ne $subscriptionId) {
$null = Set-AzContext -Subscription $subscriptionId
}
}
Write-Verbose ('Found [resource group] deployment [{0}]' -f $deployment)
$resourceGroupName = $deployment.split('/resourceGroups/')[1].Split('/')[0]
[array]$resultSet += Get-DeploymentTargetResourceListInner -Name $name -Scope 'resourcegroup' -ResourceGroupName $ResourceGroupName
} elseif ($deployment -match '/subscriptions/') {
# Subscription Level Child Deployments #
########################################
if ($deployment -match '^\/subscriptions\/([0-9a-zA-Z-]+?)\/') {
$subscriptionId = $Matches[1]
if ($currentContext.Subscription.Id -ne $subscriptionId) {
$null = Set-AzContext -Subscription $subscriptionId
}
}
Write-Verbose ('Found [subscription] deployment [{0}]' -f $deployment)
[array]$resultSet += Get-DeploymentTargetResourceListInner -Name $name -Scope 'subscription'
} elseif ($deployment -match '/managementgroups/') {
Expand All @@ -124,7 +137,7 @@ function Get-DeploymentTargetResourceListInner {
}
}

return $resultSet
return $resultSet | Select-Object -Unique
}
#endregion

Expand Down

0 comments on commit fa3bf6d

Please sign in to comment.