diff --git a/server-2025-learning-lab/bicep/main.bicep b/server-2025-learning-lab/bicep/main.bicep index 52145ae..d0f714d 100644 --- a/server-2025-learning-lab/bicep/main.bicep +++ b/server-2025-learning-lab/bicep/main.bicep @@ -1,6 +1,3 @@ -// Windows Server 2025 Learning Lab -// Main deployment file - targetScope = 'subscription' // Parameters diff --git a/server-2025-learning-lab/deploy.ps1 b/server-2025-learning-lab/deploy.ps1 index a8f5442..1b4a387 100644 --- a/server-2025-learning-lab/deploy.ps1 +++ b/server-2025-learning-lab/deploy.ps1 @@ -120,6 +120,25 @@ function Save-DeploymentState { } } +# Function to validate virtual network configuration +function Validate-VnetConfiguration { + param ( + [string]$VnetAddressSpace, + [string]$SubnetAddressPrefix + ) + + # Convert address spaces to IPNetwork objects + $vnetNetwork = [System.Net.IPNetwork]::Parse($VnetAddressSpace) + $subnetNetwork = [System.Net.IPNetwork]::Parse($SubnetAddressPrefix) + + # Check if subnet is within the VNet address space + if (-not [System.Net.IPNetwork]::Contains($vnetNetwork, $subnetNetwork)) { + return $false + } + + return $true +} + # Check if user is logged in to Azure $context = Get-AzContext -ErrorAction SilentlyContinue if (-not $context) { @@ -263,6 +282,15 @@ try { Save-DeploymentState -Stage "Prerequisites" -Completed $true +# Validate virtual network configuration +$vnetAddressSpace = "10.0.0.0/16" # Example VNet address space +$subnetAddressPrefix = "10.0.1.0/24" # Example subnet address prefix + +if (-not (Validate-VnetConfiguration -VnetAddressSpace $vnetAddressSpace -SubnetAddressPrefix $subnetAddressPrefix)) { + Write-Host "Error: Subnet address prefix $subnetAddressPrefix is not within the VNet address space $vnetAddressSpace" -ForegroundColor Red + exit 1 +} + # Deploy bicep template Write-Host "Starting deployment..." -ForegroundColor Cyan Save-DeploymentState -Stage "Infrastructure" -Completed $false -Message "Starting infrastructure deployment" @@ -394,4 +422,4 @@ Write-Host "- ADCS deployment logs are at C:\Logs\ADCS-Setup.log" -ForegroundCol Write-Host "" Write-Host "Deployment state saved at: $DeploymentStatePath" -ForegroundColor White Write-Host "" -Write-Host "Enjoy your Windows Server 2025 Learning Lab!" -ForegroundColor Green \ No newline at end of file +Write-Host "Enjoy your Windows Server 2025 Learning Lab!" -ForegroundColor Green diff --git a/server-2025-learning-lab/tests/bicep_template_tests.ps1 b/server-2025-learning-lab/tests/bicep_template_tests.ps1 new file mode 100644 index 0000000..398eee1 --- /dev/null +++ b/server-2025-learning-lab/tests/bicep_template_tests.ps1 @@ -0,0 +1,38 @@ +# Bicep Template Unit Tests + +# Load the required modules +Import-Module Az.Resources + +# Define the path to the Bicep template +$templatePath = "server-2025-learning-lab/bicep/main.bicep" + +# Function to validate the Bicep template +function Test-BicepTemplate { + param ( + [string]$TemplatePath + ) + + # Compile the Bicep template + $compiledTemplate = az bicep build --file $TemplatePath --outFile "$TemplatePath.json" + + # Load the compiled template + $template = Get-Content -Path "$TemplatePath.json" -Raw | ConvertFrom-Json + + # Validate the subnet address prefix + $vnetAddressSpace = $template.resources | Where-Object { $_.type -eq 'Microsoft.Network/virtualNetworks' } | Select-Object -ExpandProperty properties | Select-Object -ExpandProperty addressSpace | Select-Object -ExpandProperty addressPrefixes + $subnetAddressPrefix = $template.resources | Where-Object { $_.type -eq 'Microsoft.Network/virtualNetworks/subnets' } | Select-Object -ExpandProperty properties | Select-Object -ExpandProperty addressPrefix + + if ($subnetAddressPrefix -notin $vnetAddressSpace) { + throw "Subnet address prefix $subnetAddressPrefix is not within the VNet address space $vnetAddressSpace" + } + + Write-Output "Bicep template validation passed." +} + +# Run the test +try { + Test-BicepTemplate -TemplatePath $templatePath + Write-Output "Bicep template unit test passed." +} catch { + Write-Error "Bicep template unit test failed: $_" +}