Skip to content

Commit 170594f

Browse files
Merge pull request #2 from devwithkrishna/feature/caseinsensitive-temporarytag
updated script to pick up temporarty tag with case insensitive true value
2 parents 5538010 + a098dfd commit 170594f

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

terraforminator.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import os
2+
from tabulate import tabulate
23
from datetime import datetime, date
34
import argparse
45
import asyncio
5-
from tabulate import tabulate
66
from dotenv import load_dotenv
77
from azure.identity import DefaultAzureCredential
88
from azure.mgmt.resource.resources.v2022_09_01 import ResourceManagementClient
@@ -17,20 +17,24 @@ async def list_resource_groups_with_temporary_tag(subscription_id: str):
1717
# load_dotenv()
1818
credential = DefaultAzureCredential()
1919
resource_management_client = ResourceManagementClient(subscription_id=subscription_id, credential=credential)
20-
tag_filter= f"tagName eq 'Temporary' and tagValue eq 'TRUE'"
20+
tag_filter= f"tagName eq 'Temporary'"
2121
all_rgs_filtered = resource_management_client.resource_groups.list(filter=tag_filter)
2222
rgs_to_deleted = []
23-
for rg in all_rgs_filtered:
23+
# Programmatically filter resource groups for tagValue in a case-insensitive manner
24+
case_insensitive_rgs = [
25+
rg for rg in all_rgs_filtered
26+
if rg.tags and 'Temporary' in rg.tags and rg.tags['Temporary'].lower() == 'true'
27+
]
28+
for rg in case_insensitive_rgs:
2429
rg_dict = {
2530
'name': rg.name,
2631
'location': rg.location
2732
}
2833
rgs_to_deleted.append(rg_dict) # final dictionary of rgs to be deleted with Temporary tag value as TRUE
2934

30-
# print(rgs_to_deleted)
31-
return rgs_to_deleted
32-
35+
print(rgs_to_deleted)
3336

37+
return rgs_to_deleted
3438
async def delete_resource_groups(subscription_id: str, rgs_to_be_deleted: list[dict]):
3539
"""
3640
Delete the resource groups with Temporary tag value as TRUE
@@ -41,7 +45,7 @@ async def delete_resource_groups(subscription_id: str, rgs_to_be_deleted: list[d
4145

4246
for rg in rgs_to_be_deleted:
4347
try:
44-
print(f"Deleting {rg['name']} from {subscription_id} subscription")
48+
print(f"Deleting {rg['name']} from {subscription_id}")
4549
resource_management_client.resource_groups.begin_delete(resource_group_name=rg['name']).result()
4650
print(f"Successfully deleted {rg['name']}")
4751

@@ -55,7 +59,6 @@ async def delete_resource_groups(subscription_id: str, rgs_to_be_deleted: list[d
5559
# Optional: Add a short delay between deletions to prevent overwhelming the service
5660
await asyncio.sleep(1)
5761

58-
5962
def list_resources_in_rg(subscription_id:str, rgs_to_be_deleted: list[dict]):
6063
"""
6164
get the list of resources inside an RG
@@ -84,7 +87,6 @@ def list_resources_in_rg(subscription_id:str, rgs_to_be_deleted: list[dict]):
8487

8588
return details_to_display
8689

87-
8890
async def main():
8991
"""To test the code"""
9092
start_time = datetime.utcnow() # Get start time in UTC
@@ -103,7 +105,8 @@ async def main():
103105
print(f"The below resources are decommisioned on {date.today()}")
104106
# Extracting headers and rows
105107
headers = ["Name", "Type", "ID", "Resource Group Name"]
106-
rows = [[item["name"], item["resource_type"], item["resource_id"], item["resource_group"]] for item in details_to_dispaly]
108+
rows = [[item["name"], item["resource_type"], item["resource_id"], item["resource_group"]] for item in
109+
details_to_dispaly]
107110
# Printing in tabular format
108111
print(tabulate(rows, headers=headers, tablefmt="grid"))
109112
end_time = datetime.utcnow() # Get end time in UTC

0 commit comments

Comments
 (0)