Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions server-2025-learning-lab/bicep/main.bicep
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Windows Server 2025 Learning Lab
// Main deployment file

targetScope = 'subscription'

// Parameters
Expand Down
30 changes: 29 additions & 1 deletion server-2025-learning-lab/deploy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Write-Host "Enjoy your Windows Server 2025 Learning Lab!" -ForegroundColor Green
38 changes: 38 additions & 0 deletions server-2025-learning-lab/tests/bicep_template_tests.ps1
Original file line number Diff line number Diff line change
@@ -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: $_"
}