Skip to content

Commit

Permalink
Fix remaining conditions for vWan deployment (#172)
Browse files Browse the repository at this point in the history
* Update pipeline

* Condition update

* Include remaining conditions

* Push new script to clean up after deploy

* Remove wipe-ESLZAzTenant script

* Remove unused variable
  • Loading branch information
jfaurskov authored Mar 4, 2022
1 parent 3ad115f commit 62b0023
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 224 deletions.
102 changes: 102 additions & 0 deletions .github/scripts/Wipe-AlzTenant.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
[CmdletBinding()]
param (
#Added this back into parameters as error occurs if multiple tenants are found when using Get-AzTenant
[Parameter(Mandatory = $true, Position = 1, HelpMessage = "Please the Insert Tenant ID (GUID) of your Azure AD tenant e.g.'f73a2b89-6c0e-4382-899f-ea227cd6b68f'")]
[string]
$tenantRootGroupID = "<Insert the Tenant ID (GUID) of your Azure AD tenant>",

[Parameter(Mandatory = $true, Position = 2, HelpMessage = "Insert the name of your intermediate root Management Group e.g. 'Contoso'")]
[string]
$intermediateRootGroupID = "<Insert the name of your intermediate root Management Group e.g. Contoso>",

[Parameter(Mandatory = $true, Position = 4, HelpMessage = "Insert the subscription name of the subscription to wipe e.g. sub-unit-test-pr-108")]
[string]
$subscriptionName = "<Insert the subscription Id of the subscription to wipe e.g. sub-unit-test-pr-108>"
)


# Start timer
$StopWatch = New-Object -TypeName System.Diagnostics.Stopwatch
$StopWatch.Start()

$subToMove = Get-AzSubscription -SubscriptionName $subscriptionName

if ($subToMove.State -ne "Disabled") {
Write-Information "Moving Subscription: '$($subscriptionName)' under Tenant Root Management Group: '$tenantRootGroupID'"
New-AzManagementGroupSubscription -GroupId $tenantRootGroupID -SubscriptionId $subToMove.Id
}


# For each Subscription in the Intermediate Root Management Group's hierarchy tree, remove all Resources, Resource Groups and Deployments
Write-Information "Removing all Azure Resources, Resource Groups and Deployments from subscription $($subscriptionName)"
Write-Information "Set context to SubscriptionId: '$($subToMove.Id)'"
Set-AzContext -Subscription $subToMove.Id #| Out-Null

# Get all Resource Groups in Subscription
$resources = Get-AzResourceGroup

$resources | ForEach-Object -Parallel {
Write-Information "Deleting $_.ResourceGroupName ..."
Remove-AzResourceGroup -Name $_.ResourceGroupName -Force | Out-Null
}

# Get Deployments for Subscription
$subDeployments = Get-AzSubscriptionDeployment

Write-Information "Removing All Subscription Deployments for: $($subscriptionName)"

# For each Subscription level deployment, remove it
$subDeployments | ForEach-Object -Parallel {
Write-Information "Removing $($_.DeploymentName) ..."
Remove-AzSubscriptionDeployment -Id $_.Id
}


# Get all AAD Tenant level deployments
$tenantDeployments = Get-AzTenantDeployment

Write-Information "Removing all Tenant level deployments"

# For each AAD Tenant level deployment, remove it
$tenantDeployments | ForEach-Object -Parallel {
Write-Information "Removing $($_.DeploymentName) ..."
Remove-AzTenantDeployment -Id $_.Id
}

# This function only deletes Management Groups in the Intermediate Root Management Group's hierarchy tree and will NOT delete other Intermediate Root level Management Groups and their children e.g. in the case of "canary"
function Remove-Recursively {
[CmdletBinding(SupportsShouldProcess)]
param($name)
# Enters the parent Level
Write-Information "Entering the scope with $name"
$parent = Get-AzManagementGroup -GroupId $name -Expand -Recurse

# Checks if there is any parent level
if ($null -ne $parent.Children) {
Write-Information "Found the following Children :"
Write-Information ($parent.Children | Select-Object Name).Name

foreach ($children in $parent.Children) {
# Tries to recur to each child item
if ($PSCmdlet.ShouldProcess($children.Name)) {
Remove-Recursively($children.Name)
}
}
}

# If no children are found at each scope
Write-Information "No children found in scope $name"
Write-Information "Removing the scope $name"

Remove-AzManagementGroup -InputObject $parent -ErrorAction SilentlyContinue
}

# Remove all the Management Groups in Intermediate Root Management Group's hierarchy tree, including itself
Remove-Recursively($intermediateRootGroupID)

# Stop timer
$StopWatch.Stop()

# Display timer output as table
Write-Information "Time taken to complete task:"
$StopWatch.Elapsed | Format-Table
203 changes: 0 additions & 203 deletions .github/scripts/Wipe-ESLZAzTenant.ps1

This file was deleted.

27 changes: 6 additions & 21 deletions tests/pipelines/bicep-build-to-validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ jobs:
- task: Bash@3
displayName: Az CLI Refresh subscription list
name: refresh_subscription
condition: and(or(ne(variables['gitManagementOUTPUT'], ''), ne(variables['gitLoggingOUTPUT'], ''), ne(variables['gitSpokeOUTPUT'], ''), ne(variables['gitHubOUTPUT'], '')), ne(variables['subscriptionId'], ''))
condition: and(or(ne(variables['gitManagementOUTPUT'], ''), ne(variables['gitLoggingOUTPUT'], ''), ne(variables['gitSpokeOUTPUT'], ''), ne(variables['gitHubOUTPUT'], ''), ne(variables['gitVwanOUTPUT'], '')), ne(variables['subscriptionId'], ''))
inputs:
targetType: 'inline'
script: |
Expand All @@ -105,7 +105,7 @@ jobs:
- task: Bash@3
displayName: Az CLI Create Resource Group for PR
name: create_rsg
condition: and(or(ne(variables['gitManagementOUTPUT'], ''), ne(variables['gitLoggingOUTPUT'], ''), ne(variables['gitSpokeOUTPUT'], ''), ne(variables['gitHubOUTPUT'], '')), ne(variables['subscriptionId'], ''))
condition: and(or(ne(variables['gitManagementOUTPUT'], ''), ne(variables['gitLoggingOUTPUT'], ''), ne(variables['gitSpokeOUTPUT'], ''), ne(variables['gitHubOUTPUT'], ''), ne(variables['gitVwanOUTPUT'], '')), ne(variables['subscriptionId'], ''))
inputs:
targetType: 'inline'
script: |
Expand All @@ -118,7 +118,7 @@ jobs:
- task: Bash@3
displayName: Az CLI Deploy Management Groups for PR
name: create_mgs
condition: and(or(ne(variables['gitManagementOUTPUT'], ''), ne(variables['gitLoggingOUTPUT'], ''), ne(variables['gitSpokeOUTPUT'], ''), ne(variables['gitHubOUTPUT'], '')), ne(variables['subscriptionId'], ''))
condition: and(or(ne(variables['gitManagementOUTPUT'], ''), ne(variables['gitLoggingOUTPUT'], ''), ne(variables['gitSpokeOUTPUT'], ''), ne(variables['gitHubOUTPUT'], ''), ne(variables['gitVwanOUTPUT'], '')), ne(variables['subscriptionId'], ''))
inputs:
targetType: 'inline'
script: |
Expand Down Expand Up @@ -163,7 +163,7 @@ jobs:
- task: Bash@3
displayName: Az CLI Subscription Placement for PR
name: move_sub
condition: and(or(ne(variables['gitManagementOUTPUT'], ''), ne(variables['gitLoggingOUTPUT'], ''), ne(variables['gitSpokeOUTPUT'], ''), ne(variables['gitHubOUTPUT'], '')), ne(variables['subscriptionId'], ''))
condition: and(or(ne(variables['gitManagementOUTPUT'], ''), ne(variables['gitLoggingOUTPUT'], ''), ne(variables['gitSpokeOUTPUT'], ''), ne(variables['gitHubOUTPUT'], ''), ne(variables['gitVwanOUTPUT'], '')), ne(variables['subscriptionId'], ''))
inputs:
targetType: 'inline'
script: |
Expand Down Expand Up @@ -204,29 +204,14 @@ jobs:
pool:
vmImage: ubuntu-latest
steps:
- task: Bash@3
displayName: Login to Azure
name: git_azlogin
inputs:
targetType: 'inline'
script: |
az login --service-principal --username $(azclilogin) --password $(azclipwd) --tenant $(azclitenant)
- task: PowerShell@2
displayName: Az CLI Remove/Cleanup Deployment
condition: ne(variables['isDeployed'], '')
inputs:
targetType: 'inline'
script: |
install-module -Name "Az.ResourceGraph" -MinimumVersion "0.7.7"-Force
- task: AzurePowerShell@5
displayName: Az CLI Remove/Cleanup Deployment
condition: ne(variables['isDeployed'], '')
inputs:
azureSubscription: 'azserviceconnection'
ScriptType: 'FilePath'
ScriptPath: '.github/scripts/Wipe-ESLZAzTenant.ps1'
ScriptPath: '.github/scripts/Wipe-AlzTenant.ps1'
ScriptArguments: '-tenantRootGroupID $(azclitenant) -intermediateRootGroupID "$(ManagementGroupPrefix)" -subscriptionName "$(SubscriptionName)"'
azurePowerShellVersion: 'LatestVersion'
pwsh: true

0 comments on commit 62b0023

Please sign in to comment.