From 10ff87d451988000fa2437f0ed8ce1c4f10e13eb Mon Sep 17 00:00:00 2001 From: Eduardo Valdes <1084551+emvaldes@users.noreply.github.com> Date: Mon, 27 Jan 2025 16:40:49 -0700 Subject: [PATCH] devsecops/emvaldes/export-azure-resources (#17184) * Shell/Bash, PowerShell and Python scripts to export Azure Resources into CSV format --- .../terraform/scripts/export-resources.ps1 | 17 ++++++++ .../app/terraform/scripts/export-resources.py | 39 +++++++++++++++++++ .../terraform/scripts/export-resources.shell | 13 +++++++ 3 files changed, 69 insertions(+) create mode 100755 operations/app/terraform/scripts/export-resources.ps1 create mode 100755 operations/app/terraform/scripts/export-resources.py create mode 100755 operations/app/terraform/scripts/export-resources.shell diff --git a/operations/app/terraform/scripts/export-resources.ps1 b/operations/app/terraform/scripts/export-resources.ps1 new file mode 100755 index 00000000000..878ab3d7cba --- /dev/null +++ b/operations/app/terraform/scripts/export-resources.ps1 @@ -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 ; diff --git a/operations/app/terraform/scripts/export-resources.py b/operations/app/terraform/scripts/export-resources.py new file mode 100755 index 00000000000..84644db2727 --- /dev/null +++ b/operations/app/terraform/scripts/export-resources.py @@ -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() ) ; diff --git a/operations/app/terraform/scripts/export-resources.shell b/operations/app/terraform/scripts/export-resources.shell new file mode 100755 index 00000000000..a96e549e35a --- /dev/null +++ b/operations/app/terraform/scripts/export-resources.shell @@ -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} ;