Skip to content

Commit

Permalink
Automatically Add Tags to Azure Resources (#186)
Browse files Browse the repository at this point in the history
* Add tags to all Azure resource creations calls

---------

Co-authored-by: Clint Baxley <[email protected]>
  • Loading branch information
adhilto and cbaxley authored Feb 12, 2024
1 parent a367188 commit 27aae85
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
26 changes: 19 additions & 7 deletions testing/SetupTestbed.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ $Ports = 22, 3389, 443, 9200, 5044
$Priorities = 1001, 1002, 1003, 1004, 1005
$Protocols = "Tcp", "Tcp", "Tcp", "Tcp", "Tcp"

# Variables used for Azure tags
$CurrentUser = $(az account show | ConvertFrom-Json).user.name
$Today = $(Get-Date).ToString("yyyy-MM-dd")
$Project = "LME"

function Get-RandomPassword {
param (
Expand Down Expand Up @@ -173,7 +177,8 @@ function Set-NetworkRules {
--source-address-prefixes $AllowedSourcesList `
--destination-address-prefixes '*' `
--destination-port-ranges $port `
--description "Allow inbound from $sources on $port via $protocol connections."
--description "Allow inbound from $sources on $port via $protocol connections." `
--tags project=$Project created=$Today createdBy=$CurrentUser
Write-Output $networkRuleResponse
}
}
Expand Down Expand Up @@ -232,7 +237,9 @@ if (-Not $NoPrompt) {
# Setup resource group #
########################
Write-Output "`nCreating resource group..."
$createResourceGroupResponse = az group create --name $ResourceGroup --location $Location
$createResourceGroupResponse = az group create --name $ResourceGroup `
--location $Location `
--tags project=$Project created=$Today createdBy=$CurrentUser
Write-Output $createResourceGroupResponse

#################
Expand All @@ -244,13 +251,15 @@ $createVirtualNetworkResponse = az network vnet create --resource-group $Resourc
--name VNet1 `
--address-prefix $VNetPrefix `
--subnet-name SNet1 `
--subnet-prefix $SubnetPrefix
--subnet-prefix $SubnetPrefix `
--tags project=$Project created=$Today createdBy=$CurrentUser
Write-Output $createVirtualNetworkResponse

Write-Output "`nCreating nsg..."
$createNsgResponse = az network nsg create --name NSG1 `
--resource-group $ResourceGroup `
--location $Location
--location $Location `
--tags project=$Project created=$Today createdBy=$CurrentUser
Write-Output $createNsgResponse

Set-NetworkRules -AllowedSourcesList $AllowedSourcesList
Expand All @@ -276,7 +285,8 @@ $createLs1Response = az vm create `
--public-ip-sku Standard `
--size Standard_E2d_v4 `
--os-disk-size-gb 128 `
--private-ip-address $LsIP
--private-ip-address $LsIP `
--tags project=$Project created=$Today createdBy=$CurrentUser
Write-Output $createLs1Response

if (-Not $LinuxOnly){
Expand All @@ -291,7 +301,8 @@ if (-Not $LinuxOnly){
--vnet-name VNet1 `
--subnet SNet1 `
--public-ip-sku Standard `
--private-ip-address $DcIP
--private-ip-address $DcIP `
--tags project=$Project created=$Today createdBy=$CurrentUser
Write-Output $createDc1Response
for ($i = 1; $i -le $NumClients; $i++) {
Write-Output "`nCreating C$i..."
Expand All @@ -304,7 +315,8 @@ if (-Not $LinuxOnly){
--admin-password $VMPassword `
--vnet-name VNet1 `
--subnet SNet1 `
--public-ip-sku Standard
--public-ip-sku Standard `
--tags project=$Project created=$Today createdBy=$CurrentUser
Write-Output $createClientResponse
}
}
Expand Down
8 changes: 7 additions & 1 deletion testing/configure/azure_scripts/create_blob_container.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,18 @@ $StorageAccountName = New-AzureName -Prefix "st"
# Generate a container name
$ContainerName = New-AzureName -Prefix "container"

# Variables used for Azure tags
$CurrentUser = $(az account show | ConvertFrom-Json).user.name
$Today = $(Get-Date).ToString("yyyy-MM-dd")
$Project = "LME"

# Create a new storage account
az storage account create `
--name $StorageAccountName `
--resource-group $ResourceGroup `
--location $Location `
--sku Standard_LRS
--sku Standard_LRS `
--tags project=$Project created=$Today createdBy=$CurrentUser

# Wait for a moment to ensure the storage account is available
Start-Sleep -Seconds 10
Expand Down

0 comments on commit 27aae85

Please sign in to comment.