From 4d797efd1b940cb03f64b1d20aaee61ff056e07e Mon Sep 17 00:00:00 2001 From: githubofkrishnadhas Date: Thu, 26 Sep 2024 01:20:44 +0530 Subject: [PATCH] DEVOPS-282 added list of resources got decommsioned in the outputs --- terraforminator.py | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/terraforminator.py b/terraforminator.py index a78fe52..e197e0f 100644 --- a/terraforminator.py +++ b/terraforminator.py @@ -1,7 +1,8 @@ import os -from datetime import datetime +from datetime import datetime, date import argparse import asyncio +from tabulate import tabulate from dotenv import load_dotenv from azure.identity import DefaultAzureCredential 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): } rgs_to_deleted.append(rg_dict) # final dictionary of rgs to be deleted with Temporary tag value as TRUE - print(rgs_to_deleted) - + # print(rgs_to_deleted) return rgs_to_deleted + + async def delete_resource_groups(subscription_id: str, rgs_to_be_deleted: list[dict]): """ 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 for rg in rgs_to_be_deleted: try: - print(f"Deleting {rg['name']} from {subscription_id}") + print(f"Deleting {rg['name']} from {subscription_id} subscription") resource_management_client.resource_groups.begin_delete(resource_group_name=rg['name']).result() print(f"Successfully deleted {rg['name']}") @@ -54,6 +56,35 @@ async def delete_resource_groups(subscription_id: str, rgs_to_be_deleted: list[d await asyncio.sleep(1) +def list_resources_in_rg(subscription_id:str, rgs_to_be_deleted: list[dict]): + """ + get the list of resources inside an RG + :param rgs_to_be_deleted: + :return: + """ + credential = DefaultAzureCredential() + resource_management_client = ResourceManagementClient(subscription_id=subscription_id, credential=credential) + + details_to_display = [] + for rg in rgs_to_be_deleted: + try: + resource_list = resource_management_client.resources.list_by_resource_group(resource_group_name=rg['name']) + for resources in resource_list: + resource = { + 'name' : resources.name, + 'resource_id' : resources.id, + 'resource_type' : resources.type, + 'resource_group' : rg['name'] + } + details_to_display.append(resource) + + except Exception as e: + + print(f"Failed to reteive resources from resource group '{rg['name']}': {e}") + + return details_to_display + + async def main(): """To test the code""" start_time = datetime.utcnow() # Get start time in UTC @@ -67,7 +98,14 @@ async def main(): subscription_name = args.subscription_name subscription_id = run_azure_rg_query(subscription_name=subscription_name) rgs_to_deleted = await list_resource_groups_with_temporary_tag(subscription_id=subscription_id) + details_to_dispaly = list_resources_in_rg(subscription_id=subscription_id, rgs_to_be_deleted=rgs_to_deleted) await delete_resource_groups(subscription_id=subscription_id, rgs_to_be_deleted=rgs_to_deleted) + print(f"The below resources are decommisioned on {date.today()}") + # Extracting headers and rows + headers = ["Name", "Type", "ID", "Resource Group Name"] + rows = [[item["name"], item["resource_type"], item["resource_id"], item["resource_group"]] for item in details_to_dispaly] + # Printing in tabular format + print(tabulate(rows, headers=headers, tablefmt="grid")) end_time = datetime.utcnow() # Get end time in UTC print(f"Process completed at (UTC): {end_time}") # Calculate and print elapsed time