1
1
import os
2
- from datetime import datetime
2
+ from datetime import datetime , date
3
3
import argparse
4
4
import asyncio
5
+ from tabulate import tabulate
5
6
from dotenv import load_dotenv
6
7
from azure .identity import DefaultAzureCredential
7
8
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):
26
27
}
27
28
rgs_to_deleted .append (rg_dict ) # final dictionary of rgs to be deleted with Temporary tag value as TRUE
28
29
29
- print (rgs_to_deleted )
30
-
30
+ # print(rgs_to_deleted)
31
31
return rgs_to_deleted
32
+
33
+
32
34
async def delete_resource_groups (subscription_id : str , rgs_to_be_deleted : list [dict ]):
33
35
"""
34
36
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
39
41
40
42
for rg in rgs_to_be_deleted :
41
43
try :
42
- print (f"Deleting { rg ['name' ]} from { subscription_id } " )
44
+ print (f"Deleting { rg ['name' ]} from { subscription_id } subscription " )
43
45
resource_management_client .resource_groups .begin_delete (resource_group_name = rg ['name' ]).result ()
44
46
print (f"Successfully deleted { rg ['name' ]} " )
45
47
@@ -54,6 +56,35 @@ async def delete_resource_groups(subscription_id: str, rgs_to_be_deleted: list[d
54
56
await asyncio .sleep (1 )
55
57
56
58
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
+
57
88
async def main ():
58
89
"""To test the code"""
59
90
start_time = datetime .utcnow () # Get start time in UTC
@@ -67,7 +98,14 @@ async def main():
67
98
subscription_name = args .subscription_name
68
99
subscription_id = run_azure_rg_query (subscription_name = subscription_name )
69
100
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 )
70
102
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" ))
71
109
end_time = datetime .utcnow () # Get end time in UTC
72
110
print (f"Process completed at (UTC): { end_time } " )
73
111
# Calculate and print elapsed time
0 commit comments