-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add availability zones Bicep parameter (#108)
# 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
Showing
3 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/ALZ/Private/Config-Helpers/Add-AvailabilityZonesBicepParameters.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/Tests/Unit/Private/Add-AvailabilityZonesBicepParameters.Tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 @() | ||
} | ||
} | ||
} | ||
|
||
} |