diff --git a/src/ALZ/Private/Config-Helpers/Add-AvailabilityZonesBicepParameters.ps1 b/src/ALZ/Private/Config-Helpers/Add-AvailabilityZonesBicepParameters.ps1 new file mode 100644 index 0000000..38a4c60 --- /dev/null +++ b/src/ALZ/Private/Config-Helpers/Add-AvailabilityZonesBicepParameters.ps1 @@ -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 + } +} \ No newline at end of file diff --git a/src/ALZ/Private/Legacy-Bicep/New-ALZEnvironmentBicep.ps1 b/src/ALZ/Private/Legacy-Bicep/New-ALZEnvironmentBicep.ps1 index a258cb0..1cf3a06 100644 --- a/src/ALZ/Private/Legacy-Bicep/New-ALZEnvironmentBicep.ps1 +++ b/src/ALZ/Private/Legacy-Bicep/New-ALZEnvironmentBicep.ps1 @@ -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 diff --git a/src/Tests/Unit/Private/Add-AvailabilityZonesBicepParameters.Tests.ps1 b/src/Tests/Unit/Private/Add-AvailabilityZonesBicepParameters.Tests.ps1 new file mode 100644 index 0000000..ae75d99 --- /dev/null +++ b/src/Tests/Unit/Private/Add-AvailabilityZonesBicepParameters.Tests.ps1 @@ -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 @() + } + } + } + +} \ No newline at end of file