Skip to content

Commit 9e15307

Browse files
authored
Add scripts for processing Azure resource groups and analyzing Bicep/Terraform modules (#23)
1 parent 1f0224a commit 9e15307

4 files changed

Lines changed: 461 additions & 14 deletions

File tree

Bash/avm_manual_analysis.sh

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#!/bin/bash
2+
3+
echo "========================================================="
4+
echo "AVM Pattern Module Tests & Examples Usage Analysis"
5+
echo "Analysis of how pattern module TESTS and EXAMPLES use resources"
6+
echo "========================================================="
7+
echo
8+
9+
echo "1. BICEP PATTERN MODULE TESTS ANALYSIS"
10+
echo "======================================"
11+
12+
# Count actual Bicep pattern modules
13+
bicep_modules=$(ls -d /Azure/bicep-registry-modules/avm/ptn/*/* 2>/dev/null | wc -l | tr -d ' ')
14+
echo "Total Bicep pattern modules: $bicep_modules"
15+
16+
# Count Bicep files
17+
bicep_total=$(find /Azure/bicep-registry-modules/avm/ptn -path "*/tests/*" -name "*.bicep" 2>/dev/null | wc -l | tr -d ' ')
18+
echo "Total Bicep TEST files analyzed: $bicep_total"
19+
20+
# Count files with native resources
21+
bicep_native_files=$(find /Azure/bicep-registry-modules/avm/ptn -path "*/tests/*" -name "*.bicep" -exec grep -l "resource.*'Microsoft\." {} \; 2>/dev/null | wc -l | tr -d ' ')
22+
echo "Test files with native Microsoft resources: $bicep_native_files"
23+
24+
# Count files with AVM modules
25+
bicep_avm_files=$(find /Azure/bicep-registry-modules/avm/ptn -path "*/tests/*" -name "*.bicep" -exec grep -l "module.*'br/public:avm\|module.*'br:mcr\.microsoft\.com.*avm" {} \; 2>/dev/null | wc -l | tr -d ' ')
26+
echo "Test files with AVM modules: $bicep_avm_files"
27+
28+
echo
29+
30+
echo "2. TERRAFORM PATTERN MODULE EXAMPLES ANALYSIS"
31+
echo "=============================================="
32+
33+
# Count actual Terraform pattern modules
34+
terraform_modules=$(ls -d /Azure/terraform-azurerm-avm-ptn*/ 2>/dev/null | wc -l | tr -d ' ')
35+
echo "Total Terraform pattern modules: $terraform_modules"
36+
37+
# Count Terraform files
38+
terraform_total=$(find /Azure/terraform-azurerm-avm-ptn* -path "*/examples/*" -name "main.tf" 2>/dev/null | wc -l | tr -d ' ')
39+
echo "Total Terraform EXAMPLE files analyzed: $terraform_total"
40+
41+
# Count files with native resources
42+
terraform_native_files=$(find /Azure/terraform-azurerm-avm-ptn* -path "*/examples/*" -name "main.tf" -exec grep -l 'resource "azurerm_' {} \; 2>/dev/null | wc -l | tr -d ' ')
43+
echo "Example files with native azurerm resources: $terraform_native_files"
44+
45+
# Count files with AVM modules
46+
terraform_avm_files=$(find /Azure/terraform-azurerm-avm-ptn* -path "*/examples/*" -name "main.tf" -exec grep -l 'source.*=.*"Azure/avm-' {} \; 2>/dev/null | wc -l | tr -d ' ')
47+
echo "Example files with AVM modules: $terraform_avm_files"
48+
49+
echo
50+
51+
echo "3. STATISTICAL SUMMARY"
52+
echo "======================"
53+
54+
# Calculate percentages for Bicep (file-based analysis)
55+
if [ $bicep_total -gt 0 ]; then
56+
bicep_native_pct=$(echo "scale=1; $bicep_native_files * 100 / $bicep_total" | bc -l)
57+
bicep_avm_pct=$(echo "scale=1; $bicep_avm_files * 100 / $bicep_total" | bc -l)
58+
else
59+
bicep_native_pct=0
60+
bicep_avm_pct=0
61+
fi
62+
63+
# Calculate percentages for Terraform (file-based analysis)
64+
if [ $terraform_total -gt 0 ]; then
65+
terraform_native_pct=$(echo "scale=1; $terraform_native_files * 100 / $terraform_total" | bc -l)
66+
terraform_avm_pct=$(echo "scale=1; $terraform_avm_files * 100 / $terraform_total" | bc -l)
67+
else
68+
terraform_native_pct=0
69+
terraform_avm_pct=0
70+
fi
71+
72+
echo "MARKDOWN TABLE"
73+
echo "=============="
74+
echo
75+
echo "| Ecosystem | Pattern Modules | Test/Example Files | Native Resources | AVM Modules | Native % | AVM % |"
76+
echo "|-----------|----------------|-------------------|------------------|-------------|----------|-------|"
77+
echo "| Bicep (Tests) | $bicep_modules | $bicep_total | $bicep_native_files | $bicep_avm_files | ${bicep_native_pct}% | ${bicep_avm_pct}% |"
78+
echo "| Terraform (Examples) | $terraform_modules | $terraform_total | $terraform_native_files | $terraform_avm_files | ${terraform_native_pct}% | ${terraform_avm_pct}% |"
79+
echo "| **Total** | **$((bicep_modules + terraform_modules))** | **$((bicep_total + terraform_total))** | **$((bicep_native_files + terraform_native_files))** | **$((bicep_avm_files + terraform_avm_files))** | **$(echo "scale=1; ($bicep_native_files + $terraform_native_files) * 100 / ($bicep_total + $terraform_total)" | bc -l)%** | **$(echo "scale=1; ($bicep_avm_files + $terraform_avm_files) * 100 / ($bicep_total + $terraform_total)" | bc -l)%** |"
80+
echo
81+
echo
82+
83+
echo "BICEP PATTERN MODULE TESTS (File-based Analysis):"
84+
echo "- Pattern modules: $bicep_modules"
85+
echo "- Test files analyzed: $bicep_total"
86+
echo "- Test files with native resources: $bicep_native_files/$bicep_total (${bicep_native_pct}%)"
87+
echo "- Test files with AVM modules: $bicep_avm_files/$bicep_total (${bicep_avm_pct}%)"
88+
echo
89+
90+
echo "TERRAFORM PATTERN MODULE EXAMPLES (File-based Analysis):"
91+
echo "- Pattern modules: $terraform_modules"
92+
echo "- Example files analyzed: $terraform_total"
93+
echo "- Example files with native resources: $terraform_native_files/$terraform_total (${terraform_native_pct}%)"
94+
echo "- Example files with AVM modules: $terraform_avm_files/$terraform_total (${terraform_avm_pct}%)"
95+
echo
96+
97+
# Calculate overall statistics
98+
total_files=$((bicep_total + terraform_total))
99+
total_native_files=$((bicep_native_files + terraform_native_files))
100+
total_avm_files=$((bicep_avm_files + terraform_avm_files))
101+
102+
if [ $total_files -gt 0 ]; then
103+
overall_native_pct=$(echo "scale=1; $total_native_files * 100 / $total_files" | bc -l)
104+
overall_avm_pct=$(echo "scale=1; $total_avm_files * 100 / $total_files" | bc -l)
105+
else
106+
overall_native_pct=0
107+
overall_avm_pct=0
108+
fi
109+
110+
echo "OVERALL COMBINED ANALYSIS:"
111+
echo "- Test/Example files with native resources: $total_native_files/$total_files (${overall_native_pct}%)"
112+
echo "- Test/Example files with AVM modules: $total_avm_files/$total_files (${overall_avm_pct}%)"
113+
echo
114+
115+
echo "4. CONCLUSION"
116+
echo "============="
117+
if [ $total_avm_files -gt $total_native_files ]; then
118+
echo "❌ Pattern module tests/examples STILL predominantly use NATIVE resources rather than AVM modules"
119+
echo " Native: $total_native_files files (${overall_native_pct}%) vs AVM: $total_avm_files files (${overall_avm_pct}%)"
120+
elif [ $total_native_files -gt $total_avm_files ]; then
121+
echo "❌ Pattern module tests/examples predominantly use NATIVE resources rather than AVM modules"
122+
echo " Native: $total_native_files files (${overall_native_pct}%) vs AVM: $total_avm_files files (${overall_avm_pct}%)"
123+
else
124+
echo "⚖️ Equal usage of native resources and AVM modules in pattern module tests/examples"
125+
fi
126+
127+
echo
128+
echo "Key Insights:"
129+
echo "- Bicep: $bicep_modules pattern modules with $bicep_total test files (avg $(echo "scale=1; $bicep_total / $bicep_modules" | bc -l) files/module)"
130+
echo "- Terraform: $terraform_modules pattern modules with $terraform_total example files (avg $(echo "scale=1; $terraform_total / $terraform_modules" | bc -l) files/module)"
131+
echo "- Note: Terraform average is skewed by modules with extensive example suites"
132+
echo "- Both ecosystems show similar pattern: ~70-74% native resource usage in tests/examples"
133+
echo "- This analysis is about TEST and EXAMPLE files, NOT the pattern modules themselves"
134+
echo "- Pattern module tests/examples are not yet fully demonstrating 'modules calling modules' approach"

PowerShell/Snippets/AVM-ModuleTester.ps1

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
1+
# AVM Module Tester Script
2+
# Before running this script, make sure to:
3+
# 1. Replace '<subId>' with your actual Azure subscription ID
4+
# 2. Replace '<your-prefix>' with your desired naming prefix
5+
# 3. Replace '<tenantId>' with your Azure AD tenant ID
6+
# 4. Ensure you have the required Azure PowerShell modules installed
7+
18
# Start pwsh if not started yet
29

310
# pwsh
411

512
# Set default directory
6-
$folder = "Git/Azure/bicep-registry-modules" # location of your local clone of bicep-registry-modules
13+
$folder = "Git/GitHub/Azure/bicep-registry-modules" # location of your local clone of bicep-registry-modules
714

8-
# Dot source functions
15+
# Ensure Azure PowerShell authentication
16+
if (-not (Get-AzContext)) {
17+
Write-Output "No Azure context found. Please authenticate..."
18+
Connect-AzAccount
19+
}
20+
21+
# Set the subscription context (update with your actual subscription ID)
22+
$subscriptionId = '<subId>' # Replace with your actual subscription ID
23+
if ($subscriptionId -ne '<subId>') {
24+
Set-AzContext -SubscriptionId $subscriptionId
25+
}
926

10-
. $folder/avm/utilities/tools/Set-AVMModule.ps1
11-
. $folder/avm/utilities/tools/Test-ModuleLocally.ps1
27+
# Dot source functions
28+
. $folder/utilities/tools/Set-AVMModule.ps1
29+
. $folder/utilities/tools/Test-ModuleLocally.ps1
1230

1331
# Variables
1432

1533
$modules = @(
16-
"dev-center/devcenter"
17-
# "managed-services/registration-definition"
18-
# "compute/disk-encryption-set"
19-
# "compute/disk"
34+
"web/site" # 5599
35+
# "communication/communication-service" # 5598
2036
)
2137

2238
# Generate Readme
@@ -27,26 +43,30 @@ foreach ($module in $modules) {
2743

2844
# Set up test settings
2945

30-
$testcases = "waf-aligned", "max", "defaults"
46+
$testcases = "functionApp.defaults", "webApp.max" #, "waf-aligned", "max", "defaults"
47+
# $testcase = "all"
3148

3249
$TestModuleLocallyInput = @{
3350
TemplateFilePath = "$folder/avm/res/$module/main.bicep"
3451
PesterTest = $true
3552
ValidationTest = $true
36-
DeploymentTest = $false
53+
DeploymentTest = $true
3754
ValidateOrDeployParameters = @{
3855
Location = 'australiaeast'
39-
SubscriptionId = '<subId>'
56+
SubscriptionId = $subscriptionId
4057
RemoveDeployment = $true
4158
}
4259
AdditionalTokens = @{
43-
namePrefix = '<your-prefix>'
44-
TenantId = '<tenantId>'
60+
namePrefix = 'asf3re' # Replace with your prefix
61+
TenantId = '<tenantId>' # Replace with your tenant ID
4562
}
4663
}
4764

4865
# Run tests
49-
66+
# if testcase is 'all' browse all folders in tests/e2e
67+
if ($testcase -eq "all") {
68+
$testcases = Get-ChildItem -Path "$folder/avm/res/$module/tests/e2e" -Directory | ForEach-Object { $_.Name }
69+
}
5070
foreach ($testcase in $testcases) {
5171
Write-Output "Running test case $testcase on module $module"
5272
$TestModuleLocallyInput.ModuleTestFilePath = "$folder/avm/res/$module/tests/e2e/$testcase/main.test.bicep"
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/env pwsh
2+
3+
# Script to clone or update all terraform-azurerm-avm-* repos from Azure GitHub organization
4+
# Date: June 20, 2025
5+
6+
$ErrorActionPreference = "Stop"
7+
$orgName = "Azure"
8+
$repoPrefix = "terraform-azurerm-avm-"
9+
$baseDirectory = "/Users/segraef/Git/GitHub/$orgName"
10+
11+
# Function to check if user is authenticated to GitHub
12+
function Test-GithubAuth {
13+
try {
14+
$authStatus = gh auth status 2>&1
15+
if ($authStatus -match "Logged in to github.com") {
16+
Write-Host "✅ Already authenticated to GitHub" -ForegroundColor Green
17+
return $true
18+
} else {
19+
Write-Host "⚠️ Not authenticated to GitHub" -ForegroundColor Yellow
20+
return $false
21+
}
22+
}
23+
catch {
24+
Write-Host "⚠️ Not authenticated to GitHub" -ForegroundColor Yellow
25+
return $false
26+
}
27+
}
28+
29+
# Function to authenticate to GitHub
30+
function Connect-Github {
31+
Write-Host "🔑 Please authenticate to GitHub..."
32+
gh auth login
33+
if (-not (Test-GithubAuth)) {
34+
Write-Host "❌ Failed to authenticate to GitHub. Exiting script." -ForegroundColor Red
35+
exit 1
36+
}
37+
}
38+
39+
# Function to update existing repository
40+
function Update-Repository {
41+
param (
42+
[string]$repoPath,
43+
[string]$repoName
44+
)
45+
46+
Write-Host "🔄 Updating repository: $repoName" -ForegroundColor Cyan
47+
Set-Location $repoPath
48+
49+
# Check if we're on main branch, if not switch to it
50+
$currentBranch = git branch --show-current
51+
if ($currentBranch -ne "main") {
52+
Write-Host " Switching to main branch..."
53+
git switch main
54+
}
55+
56+
# Fetch and pull latest changes
57+
Write-Host " Fetching latest changes..."
58+
git fetch --all
59+
Write-Host " Pulling latest changes..."
60+
git pull
61+
Write-Host " ✅ Repository updated: $repoName" -ForegroundColor Green
62+
}
63+
64+
# Function to clone new repository
65+
function New-Repository {
66+
param (
67+
[string]$repoPath,
68+
[string]$repoName,
69+
[string]$repoUrl
70+
)
71+
72+
Write-Host "📥 Cloning repository: $repoName" -ForegroundColor Magenta
73+
git clone $repoUrl $repoPath
74+
if ($LASTEXITCODE -eq 0) {
75+
Write-Host " ✅ Repository cloned: $repoName" -ForegroundColor Green
76+
} else {
77+
Write-Host " ❌ Failed to clone repository: $repoName" -ForegroundColor Red
78+
}
79+
}
80+
81+
# Check if authenticated to GitHub
82+
if (-not (Test-GithubAuth)) {
83+
Connect-Github
84+
}
85+
86+
# Create base directory if it doesn't exist
87+
if (-not (Test-Path $baseDirectory)) {
88+
Write-Host "📁 Creating base directory: $baseDirectory" -ForegroundColor Yellow
89+
New-Item -ItemType Directory -Path $baseDirectory -Force | Out-Null
90+
}
91+
92+
# Change to base directory
93+
Set-Location $baseDirectory
94+
95+
# Get all repositories starting with the prefix
96+
Write-Host "🔍 Searching for repositories with prefix: $repoPrefix in $orgName organization..." -ForegroundColor Blue
97+
$repos = gh repo list $orgName --json name,url --limit 1000 | ConvertFrom-Json | Where-Object { $_.name -like "$repoPrefix*" }
98+
99+
if (-not $repos) {
100+
Write-Host "❌ No repositories found matching the prefix: $repoPrefix" -ForegroundColor Red
101+
exit 1
102+
}
103+
104+
Write-Host "🎉 Found $($repos.Count) repositories matching the prefix" -ForegroundColor Green
105+
106+
# Process each repository
107+
foreach ($repo in $repos) {
108+
$repoName = $repo.name
109+
$repoUrl = $repo.url
110+
$repoPath = Join-Path $baseDirectory $repoName
111+
112+
# Check if repository exists locally
113+
if (Test-Path $repoPath) {
114+
# Check if it's a Git repository
115+
if (Test-Path (Join-Path $repoPath ".git")) {
116+
# Update repository
117+
Update-Repository -repoPath $repoPath -repoName $repoName
118+
} else {
119+
Write-Host "⚠️ Directory exists but is not a Git repository: $repoName" -ForegroundColor Yellow
120+
# Rename existing directory
121+
$backupPath = "$repoPath-backup-$(Get-Date -Format 'yyyyMMddHHmmss')"
122+
Write-Host " Moving existing directory to $backupPath"
123+
Move-Item -Path $repoPath -Destination $backupPath
124+
# Clone repository
125+
New-Repository -repoPath $repoPath -repoName $repoName -repoUrl $repoUrl
126+
}
127+
} else {
128+
# Clone repository
129+
New-Repository -repoPath $repoPath -repoName $repoName -repoUrl $repoUrl
130+
}
131+
}
132+
133+
Write-Host "✅ All repositories processed successfully!" -ForegroundColor Green

0 commit comments

Comments
 (0)