Skip to content

Commit 4d797ef

Browse files
DEVOPS-282 added list of resources got decommsioned in the outputs
1 parent bc5d684 commit 4d797ef

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

terraforminator.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os
2-
from datetime import datetime
2+
from datetime import datetime, date
33
import argparse
44
import asyncio
5+
from tabulate import tabulate
56
from dotenv import load_dotenv
67
from azure.identity import DefaultAzureCredential
78
from azure.mgmt.resource.resources.v2022_09_01 import ResourceManagementClient
@@ -26,9 +27,10 @@ async def list_resource_groups_with_temporary_tag(subscription_id: str):
2627
}
2728
rgs_to_deleted.append(rg_dict) # final dictionary of rgs to be deleted with Temporary tag value as TRUE
2829

29-
print(rgs_to_deleted)
30-
30+
# print(rgs_to_deleted)
3131
return rgs_to_deleted
32+
33+
3234
async def delete_resource_groups(subscription_id: str, rgs_to_be_deleted: list[dict]):
3335
"""
3436
Delete the resource groups with Temporary tag value as TRUE
@@ -39,7 +41,7 @@ async def delete_resource_groups(subscription_id: str, rgs_to_be_deleted: list[d
3941

4042
for rg in rgs_to_be_deleted:
4143
try:
42-
print(f"Deleting {rg['name']} from {subscription_id}")
44+
print(f"Deleting {rg['name']} from {subscription_id} subscription")
4345
resource_management_client.resource_groups.begin_delete(resource_group_name=rg['name']).result()
4446
print(f"Successfully deleted {rg['name']}")
4547

@@ -54,6 +56,35 @@ async def delete_resource_groups(subscription_id: str, rgs_to_be_deleted: list[d
5456
await asyncio.sleep(1)
5557

5658

59+
def list_resources_in_rg(subscription_id:str, rgs_to_be_deleted: list[dict]):
60+
"""
61+
get the list of resources inside an RG
62+
:param rgs_to_be_deleted:
63+
:return:
64+
"""
65+
credential = DefaultAzureCredential()
66+
resource_management_client = ResourceManagementClient(subscription_id=subscription_id, credential=credential)
67+
68+
details_to_display = []
69+
for rg in rgs_to_be_deleted:
70+
try:
71+
resource_list = resource_management_client.resources.list_by_resource_group(resource_group_name=rg['name'])
72+
for resources in resource_list:
73+
resource = {
74+
'name' : resources.name,
75+
'resource_id' : resources.id,
76+
'resource_type' : resources.type,
77+
'resource_group' : rg['name']
78+
}
79+
details_to_display.append(resource)
80+
81+
except Exception as e:
82+
83+
print(f"Failed to reteive resources from resource group '{rg['name']}': {e}")
84+
85+
return details_to_display
86+
87+
5788
async def main():
5889
"""To test the code"""
5990
start_time = datetime.utcnow() # Get start time in UTC
@@ -67,7 +98,14 @@ async def main():
6798
subscription_name = args.subscription_name
6899
subscription_id = run_azure_rg_query(subscription_name=subscription_name)
69100
rgs_to_deleted = await list_resource_groups_with_temporary_tag(subscription_id=subscription_id)
101+
details_to_dispaly = list_resources_in_rg(subscription_id=subscription_id, rgs_to_be_deleted=rgs_to_deleted)
70102
await delete_resource_groups(subscription_id=subscription_id, rgs_to_be_deleted=rgs_to_deleted)
103+
print(f"The below resources are decommisioned on {date.today()}")
104+
# Extracting headers and rows
105+
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]
107+
# Printing in tabular format
108+
print(tabulate(rows, headers=headers, tablefmt="grid"))
71109
end_time = datetime.utcnow() # Get end time in UTC
72110
print(f"Process completed at (UTC): {end_time}")
73111
# Calculate and print elapsed time

0 commit comments

Comments
 (0)