Skip to content

Commit

Permalink
devsecops/emvaldes/export-azure-resources (#17184)
Browse files Browse the repository at this point in the history
* Shell/Bash, PowerShell and Python scripts to export Azure Resources into CSV format
  • Loading branch information
emvaldes authored Jan 27, 2025
1 parent 2d2df65 commit 10ff87d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
17 changes: 17 additions & 0 deletions operations/app/terraform/scripts/export-resources.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env pwsh

# Define parameters
param (
[string]$OutputFile = "azure-resources--powershell.csv"
)

# Output header
"Location,Name,Resource Group" | Out-File -FilePath "azure-resources.csv" -Encoding utf8 ;

# Fetch Azure resources and append to CSV
az resource list --query '[].{"Location":location,"Name":name,"Resource Group":resourceGroup}' --output tsv |
ForEach-Object { $_ -replace "`t", "," } |
Out-File -FilePath $OutputFile -Append -Encoding utf8 ;

# Display the contents of the generated CSV
Get-Content -Path $OutputFile ;
39 changes: 39 additions & 0 deletions operations/app/terraform/scripts/export-resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python

import subprocess
import csv
import argparse

# Set up argument parser
parser = argparse.ArgumentParser( description="Fetch Azure resources and export them to a CSV file." ) ;
parser.add_argument(
"--output-file",
type=str,
default="azure-resources--python.csv",
help="Path to the output CSV file (default: azure-resources--python.csv)"
) ;

# Parse arguments
args = parser.parse_args() ;
output_file = args.output_file ;

# Command to fetch Azure resources
cmd = [
"az", "resource", "list",
"--query", "[].{\"Location\":location, \"Name\":name, \"Resource Group\":resourceGroup}",
"--output", "tsv"
] ;

# Run the Azure CLI command
result = subprocess.run( cmd, stdout=subprocess.PIPE, text=True ) ;

# Write header and data to CSV file
with open( output_file, "w", newline="" ) as csvfile:
writer = csv.writer( csvfile ) ;
writer.writerow( ["Location", "Name", "Resource Group"] ) ;
for line in result.stdout.splitlines():
writer.writerow( line.split( "\t" ) ) ;

# Print the contents of the generated CSV
with open( output_file, "r" ) as csvfile:
print( csvfile.read() ) ;
13 changes: 13 additions & 0 deletions operations/app/terraform/scripts/export-resources.shell
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

if [[ ${#1} -gt 0 ]]; then
OutputFile="${1}" ;
else OutputFile="azure-resources--shell.csv" ;
fi ;

echo "Location,Name,Resource Group" \
| cat - <( az resource list --query "[].{\"Location\":location, \"Name\":name, \"Resource Group\":resourceGroup}" --output tsv \
| sed 's/\t/,/g' ) \
> ${OutputFile} ;

cat ${OutputFile} ;

0 comments on commit 10ff87d

Please sign in to comment.