Skip to content

Commit

Permalink
DEVOPS-282 added list of resources got decommsioned in the outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
githubofkrishnadhas committed Sep 25, 2024
1 parent bc5d684 commit 4d797ef
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions terraforminator.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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']}")

Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 4d797ef

Please sign in to comment.