Skip to content

Commit

Permalink
Add availability zones Bicep parameter (#108)
Browse files Browse the repository at this point in the history
# Pull Request

## Issue

Issue #103 and #102

## Description

Adding a new private function to determine the availability zones for
the selected regions and edit the parameters files of the relevant
networking modules to include them by default.

## License

By submitting this pull request, I confirm that my contribution is made
under the terms of the projects associated license.
  • Loading branch information
sebassem authored Mar 21, 2024
1 parent a2e0390 commit 79d7c6d
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function Add-AvailabilityZonesBicepParameter {
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(Mandatory = $true)]
[Alias("Output")]
[Alias("OutputDirectory")]
[Alias("O")]
[string] $alzEnvironmentDestination,

[Parameter(Mandatory = $true)]
[PSCustomObject]$configFile
)

$parametersConfig = @(
[pscustomobject]@{
source = "hubNetworking.parameters.all.json";
parameters = "parAzErGatewayAvailabilityZones,parAzVpnGatewayAvailabilityZones,parAzFirewallAvailabilityZones"
}
[pscustomobject]@{
source = "vwanConnectivity.parameters.all.json";
parameters = "parAzFirewallAvailabilityZones"
}
)

foreach ($parametersFile in $parametersConfig) {
$parametersFilePath = Join-Path -Path $alzEnvironmentDestination "config\custom-parameters\$($parametersFile.source)"
$region = (Get-Content $parametersFilePath | ConvertFrom-Json).parameters.parLocation.Value
$zones = ($configFile.PsObject.Properties["zonesSupport"].Value | Where-Object { $_.region -eq $region }).zones
$parametersFileJsonContent = Get-Content -Path $parametersFilePath -Raw
$jsonObject = $parametersFileJsonContent | ConvertFrom-Json
$parametersFile.parameters.Split(",") | ForEach-Object {
$parameter = $_
try {
if ($null -eq $jsonObject.parameters.$parameter.value) {
$jsonObject.parameters.$parameter.value = @($zones)
}

else {
$jsonObject.parameters.$parameter.value = $zones
}

}

catch {
Write-Error -Message "The parameter $parameter does not exist in the file $parametersFilePath"
}
}
$parametersFileJsonContent = $jsonObject | ConvertTo-Json -Depth 10
Set-Content -Path $parametersFilePath -Value $parametersFileJsonContent
}
}
1 change: 1 addition & 0 deletions src/ALZ/Private/Legacy-Bicep/New-ALZEnvironmentBicep.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function New-ALZEnvironmentBicep {
Set-ComputedConfiguration -configuration $configuration | Out-String | Write-Verbose
Edit-ALZConfigurationFilesInPlace -alzEnvironmentDestination $targetDirectory -configuration $configuration | Out-String | Write-Verbose
Build-ALZDeploymentEnvFile -configuration $configuration -Destination $targetDirectory -version $upstreamReleaseVersion | Out-String | Write-Verbose
Add-AvailabilityZonesBicepParameter -alzEnvironmentDestination $alzEnvironmentDestination -configFile $bicepConfig| Out-String | Write-Verbose

if($local) {
$isGitRepo = Test-ALZGitRepository -alzEnvironmentDestination $targetDirectory -autoApprove:$autoApprove.IsPresent
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#-------------------------------------------------------------------------
Set-Location -Path $PSScriptRoot
#-------------------------------------------------------------------------
$ModuleName = 'ALZ'
$PathToManifest = [System.IO.Path]::Combine('..', '..', '..', $ModuleName, "$ModuleName.psd1")
#-------------------------------------------------------------------------
if (Get-Module -Name $ModuleName -ErrorAction 'SilentlyContinue') {
#if the module is already in memory, remove it
Remove-Module -Name $ModuleName -Force
}
Import-Module $PathToManifest -Force
#-------------------------------------------------------------------------

InModuleScope 'ALZ' {
Describe "Add-AvailabilityZonesBicepParameter" {
BeforeAll {
$alzEnvironmentDestination = "TestDrive:\"
$hubParametersPath = "https://raw.githubusercontent.com/Azure/ALZ-Bicep/main/infra-as-code/bicep/modules/hubNetworking/parameters/hubNetworking.parameters.all.json"
Invoke-WebRequest -Uri $hubParametersPath -OutFile "$alzEnvironmentDestination\hubNetworking.parameters.all.json"
}
Context "Hub networking parameters availability zones check" {
BeforeAll {
Mock -CommandName Join-Path -MockWith {
$alzEnvironmentDestination + "\hubNetworking.parameters.all.json"
}
Mock -CommandName Get-Content -ParameterFilter { $Path -contains 'parametersFilePath' } -MockWith {
Get-Content -Path "TestDrive:\hubNetworking.parameters.all.json"
}
}
It "Should add 3 availability zones for hub networking parameters" {
Add-AvailabilityZonesBicepParameter -alzEnvironmentDestination $alzEnvironmentDestination -configFile ([PSCustomObject]@{
zonesSupport = @(
[PSCustomObject]@{
region = "eastus"
zones = @("1", "2", "3")
}
)
})
$parametersFileJsonContent = Get-Content -Path "TestDrive:\hubNetworking.parameters.all.json" -Raw
$jsonObject = $parametersFileJsonContent | ConvertFrom-Json
$jsonObject.parameters.parAzErGatewayAvailabilityZones.value | Should -Be @("1", "2", "3")
$jsonObject.parameters.parAzVpnGatewayAvailabilityZones.value | Should -Be @("1", "2", "3")
$jsonObject.parameters.parAzFirewallAvailabilityZones.value | Should -Be @("1", "2", "3")
}
It "Should add 2 availability zones for hub networking parameters" {
Add-AvailabilityZonesBicepParameter -alzEnvironmentDestination $alzEnvironmentDestination -configFile ([PSCustomObject]@{
zonesSupport = @(
[PSCustomObject]@{
region = "eastus"
zones = @("1", "2")
}
)
})
$parametersFileJsonContent = Get-Content -Path "TestDrive:\hubNetworking.parameters.all.json" -Raw
$jsonObject = $parametersFileJsonContent | ConvertFrom-Json
$jsonObject.parameters.parAzErGatewayAvailabilityZones.value | Should -Be @("1", "2")
$jsonObject.parameters.parAzVpnGatewayAvailabilityZones.value | Should -Be @("1", "2")
$jsonObject.parameters.parAzFirewallAvailabilityZones.value | Should -Be @("1", "2")
}
It "Should add 0 availability zones for hub networking parameters" {
Add-AvailabilityZonesBicepParameter -alzEnvironmentDestination $alzEnvironmentDestination -configFile ([PSCustomObject]@{
zonesSupport = @(
[PSCustomObject]@{
region = "eastus"
zones = @()
}
)
})
$parametersFileJsonContent = Get-Content -Path "TestDrive:\hubNetworking.parameters.all.json" -Raw
$jsonObject = $parametersFileJsonContent | ConvertFrom-Json
$jsonObject.parameters.parAzErGatewayAvailabilityZones.value | Should -Be @()
$jsonObject.parameters.parAzVpnGatewayAvailabilityZones.value | Should -Be @()
$jsonObject.parameters.parAzFirewallAvailabilityZones.value | Should -Be @()
}
}
}

}

0 comments on commit 79d7c6d

Please sign in to comment.