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

Fixing PrivateDnsZone removal #824

Merged
merged 10 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
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 @@ -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