Skip to content

Commit

Permalink
Fixing PrivateDnsZone removal (#824)
Browse files Browse the repository at this point in the history
* privdns order

* module name out

* removal prints

* removal prints all

* change privatedns vnet link default name

* update api

* update vnetlink default value

* testing without order

* cleanup

* remove function prefix

Co-authored-by: Erika Gressi <erikag.microsoft.com>
  • Loading branch information
eriqua committed Dec 20, 2021
1 parent e40d15f commit bcb9f9f
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"contentVersion": "1.0.0.0",
"parameters": {
"name": {
"value": "sxx.az-privdns-x-001"
"value": "sxx-az-privdns-x-001.com"
},
"roleAssignments": {
"value": [
Expand Down
4 changes: 2 additions & 2 deletions arm/Microsoft.Network/privateDnsZones/deploy.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module pid_cuaId '.bicep/nested_cuaId.bicep' = if (!empty(cuaId)) {
params: {}
}

resource privateDnsZone 'Microsoft.Network/privateDnsZones@2018-09-01' = {
resource privateDnsZone 'Microsoft.Network/privateDnsZones@2020-06-01' = {
name: name
location: location
tags: tags
Expand All @@ -39,7 +39,7 @@ module privateDnsZone_virtualNetworkLinks 'virtualNetworkLinks/deploy.bicep' = [
name: '${uniqueString(deployment().name, location)}-PrivateDnsZone-VirtualNetworkLink-${index}'
params: {
privateDnsZoneName: privateDnsZone.name
name: contains(virtualNetworkLinks, 'name') ? virtualNetworkLinks.name : last(split(virtualNetworkLinks.virtualNetworkResourceId, '/'))
name: contains(virtualNetworkLinks, 'name') ? virtualNetworkLinks.name : '${last(split(virtualNetworkLinks.virtualNetworkResourceId, '/'))}-vnetlink'
virtualNetworkResourceId: virtualNetworkLinks.virtualNetworkResourceId
location: contains(virtualNetworkLinks, 'location') ? virtualNetworkLinks.location : 'global'
registrationEnabled: contains(virtualNetworkLinks, 'registrationEnabled') ? virtualNetworkLinks.registrationEnabled : false
Expand Down
8 changes: 4 additions & 4 deletions arm/Microsoft.Network/privateDnsZones/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ This template deploys a private DNS zone.
| :-- | :-- |
| `Microsoft.Authorization/locks` | 2016-09-01 |
| `Microsoft.Authorization/roleAssignments` | 2020-04-01-preview |
| `Microsoft.Network/privateDnsZones` | 2018-09-01 |
| `Microsoft.Network/privateDnsZones/virtualNetworkLinks` | 2018-09-01 |
| `Microsoft.Network/privateDnsZones` | 2020-06-01 |
| `Microsoft.Network/privateDnsZones/virtualNetworkLinks` | 2020-06-01 |

## Parameters

Expand Down Expand Up @@ -73,6 +73,6 @@ Tag names and tag values can be provided as needed. A tag can be left without a
## Template references

- [Locks](https://docs.microsoft.com/en-us/azure/templates/Microsoft.Authorization/2016-09-01/locks)
- [Privatednszones](https://docs.microsoft.com/en-us/azure/templates/Microsoft.Network/2020-06-01/privateDnsZones)
- [Privatednszones/Virtualnetworklinks](https://docs.microsoft.com/en-us/azure/templates/Microsoft.Network/2020-06-01/privateDnsZones/virtualNetworkLinks)
- [Roleassignments](https://docs.microsoft.com/en-us/azure/templates/Microsoft.Authorization/2020-04-01-preview/roleAssignments)
- [Privatednszones](https://docs.microsoft.com/en-us/azure/templates/Microsoft.Network/2018-09-01/privateDnsZones)
- [Privatednszones/Virtualnetworklinks](https://docs.microsoft.com/en-us/azure/templates/Microsoft.Network/2018-09-01/privateDnsZones/virtualNetworkLinks)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
param privateDnsZoneName string

@description('Optional. The name of the virtual network link.')
param name string = last(split(virtualNetworkResourceId, '/'))
param name string = '${last(split(virtualNetworkResourceId, '/'))}-vnetlink'

@description('Optional. The location of the PrivateDNSZone. Should be global.')
param location string = 'global'
Expand All @@ -24,11 +24,11 @@ module pid_cuaId '.bicep/nested_cuaId.bicep' = if (!empty(cuaId)) {
params: {}
}

resource privateDnsZone 'Microsoft.Network/privateDnsZones@2018-09-01' existing = {
resource privateDnsZone 'Microsoft.Network/privateDnsZones@2020-06-01' existing = {
name: privateDnsZoneName
}

resource virtualNetworkLink 'Microsoft.Network/privateDnsZones/virtualNetworkLinks@2018-09-01' = {
resource virtualNetworkLink 'Microsoft.Network/privateDnsZones/virtualNetworkLinks@2020-06-01' = {
name: name
parent: privateDnsZone
location: location
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ This module deploys private dns zone virtual network links.

| Resource Type | API Version |
| :-- | :-- |
| `Microsoft.Network/privateDnsZones/virtualNetworkLinks` | 2018-09-01 |
| `Microsoft.Network/privateDnsZones/virtualNetworkLinks` | 2020-06-01 |

## Parameters

| Parameter Name | Type | Default Value | Possible Values | Description |
| :-- | :-- | :-- | :-- | :-- |
| `cuaId` | string | | | Optional. Customer Usage Attribution ID (GUID). This GUID must be previously registered |
| `location` | string | `global` | | Optional. The location of the PrivateDNSZone. Should be global. |
| `name` | string | `[last(split(parameters('virtualNetworkResourceId'), '/'))]` | | Optional. The name of the virtual network link. |
| `name` | string | `[format('{0}-vnetlink', last(split(parameters('virtualNetworkResourceId'), '/')))]` | | Optional. The name of the virtual network link. |
| `privateDnsZoneName` | string | | | Required. Private DNS zone name. |
| `registrationEnabled` | bool | | | Optional. Is auto-registration of virtual machine records in the virtual network in the Private DNS zone enabled? |
| `tags` | object | `{object}` | | Optional. Tags of the resource. |
Expand Down Expand Up @@ -47,4 +47,4 @@ Tag names and tag values can be provided as needed. A tag can be left without a

## Template references

- [Privatednszones/Virtualnetworklinks](https://docs.microsoft.com/en-us/azure/templates/Microsoft.Network/2018-09-01/privateDnsZones/virtualNetworkLinks)
- [Privatednszones/Virtualnetworklinks](https://docs.microsoft.com/en-us/azure/templates/Microsoft.Network/2020-06-01/privateDnsZones/virtualNetworkLinks)
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ function Initialize-DeploymentRemoval {
'Microsoft.Resources/resourceGroups',
'Microsoft.Compute/virtualMachines'
)
Write-Verbose ('Template file path: [{0}]' -f $templateFilePath) -Verbose
Write-Verbose ('Module name: [{0}]' -f $moduleName) -Verbose

foreach ($deploymentName in $deploymentNames) {
Write-Verbose ('Handling resource removal with deployment name [{0}]' -f $deploymentName) -Verbose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function Get-ResourceIdsOfDeploymentInner {
if (Get-AzResourceGroup -Name $resourceGroupName -ErrorAction 'SilentlyContinue') {
[array]$deploymentTargets = (Get-AzResourceGroupDeploymentOperation -DeploymentName $name -ResourceGroupName $resourceGroupName).TargetResource | Where-Object { $_ -ne $null }
foreach ($deployment in ($deploymentTargets | Where-Object { $_ -notmatch '/deployments/' } )) {
Write-Verbose ('Found deployment [{0}]' -f $deployment) -Verbose
[array]$resultSet += $deployment
}
foreach ($deployment in ($deploymentTargets | Where-Object { $_ -match '/deployments/' } )) {
Expand All @@ -68,6 +69,7 @@ function Get-ResourceIdsOfDeploymentInner {
'subscription' {
[array]$deploymentTargets = (Get-AzDeploymentOperation -DeploymentName $name).TargetResource | Where-Object { $_ -ne $null }
foreach ($deployment in ($deploymentTargets | Where-Object { $_ -notmatch '/deployments/' } )) {
Write-Verbose ('Found deployment [{0}]' -f $deployment) -Verbose
[array]$resultSet += $deployment
}
foreach ($deployment in ($deploymentTargets | Where-Object { $_ -match '/deployments/' } )) {
Expand All @@ -86,6 +88,7 @@ function Get-ResourceIdsOfDeploymentInner {
'managementgroup' {
[array]$deploymentTargets = (Get-AzManagementGroupDeploymentOperation -DeploymentName $name).TargetResource | Where-Object { $_ -ne $null }
foreach ($deployment in ($deploymentTargets | Where-Object { $_ -notmatch '/deployments/' } )) {
Write-Verbose ('Found deployment [{0}]' -f $deployment) -Verbose
[array]$resultSet += $deployment
}
foreach ($deployment in ($deploymentTargets | Where-Object { $_ -match '/deployments/' } )) {
Expand All @@ -102,6 +105,7 @@ function Get-ResourceIdsOfDeploymentInner {
'tenant' {
[array]$deploymentTargets = (Get-AzTenantDeploymentOperation -DeploymentName $name).TargetResource | Where-Object { $_ -ne $null }
foreach ($deployment in ($deploymentTargets | Where-Object { $_ -notmatch '/deployments/' } )) {
Write-Verbose ('Found deployment [{0}]' -f $deployment) -Verbose
[array]$resultSet += $deployment
}
foreach ($deployment in ($deploymentTargets | Where-Object { $_ -match '/deployments/' } )) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,28 @@ function Remove-Deployment {
ResourceGroupName = $resourceGroupName
}
$deploymentResourceIds = Get-ResourceIdsOfDeployment @deploymentsInputObject -Verbose
Write-Verbose ('Total number of deployments after fetching deployments [{0}]' -f $deploymentResourceIds.Count) -Verbose

# Pre-Filter & order items
# ========================
$rawResourceIdsToRemove = $deploymentResourceIds | Sort-Object -Property { $_.Split('/').Count } -Descending | Select-Object -Unique
Write-Verbose ('Total number of deployments after pre-filtering & ordering items [{0}]' -f $rawResourceIdsToRemove.Count) -Verbose

# Format items
# ============
$resourcesToRemove = Get-ResourceIdsAsFormattedObjectList -ResourceIds $rawResourceIdsToRemove
Write-Verbose ('Total number of deployments after formatting items [{0}]' -f $resourcesToRemove.Count) -Verbose

# Filter all dependency resources
# ===============================
$dependencyResourceNames = Get-DependencyResourceNameList
$resourcesToRemove = $resourcesToRemove | Where-Object { (Split-Path $_.resourceId -Leaf) -notin $dependencyResourceNames }
Write-Verbose ('Total number of deployments after filtering all dependency resources [{0}]' -f $resourcesToRemove.Count) -Verbose

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

# Remove resources
# ================
Expand Down

0 comments on commit bcb9f9f

Please sign in to comment.