From 325eebb24c911716583e281b35c8fd3f8b70705d Mon Sep 17 00:00:00 2001 From: Krishna Harsha Voora Date: Wed, 17 Apr 2024 06:51:37 +0530 Subject: [PATCH] Add TicketId to the Email Signed-off-by: Krishna Harsha Voora --- cloudsensei/lambda_function.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/cloudsensei/lambda_function.py b/cloudsensei/lambda_function.py index de97a85d..e67e687c 100644 --- a/cloudsensei/lambda_function.py +++ b/cloudsensei/lambda_function.py @@ -71,15 +71,19 @@ def get_resources(self): break if tag_key == 'user': user = tag.get('Value') + if tag_key == 'ticketid': + ticket_tags = tag.get('Value') elif tag_key == 'name': name = tag.get('Value') if not skip and user: - long_running_instances_by_user.setdefault(user.lower(), {}).setdefault(region_name, []).append( - {'InstanceId': resource.get('InstanceId'), - 'Name': name, 'LaunchDate': str(launch_time), - 'RunningDays': f"{days} days", 'State': resource.get('State', {}).get('Name'), - 'InstanceType': resource.get('InstanceType')}) - return long_running_instances_by_user + instance_details = {'InstanceId': resource.get('InstanceId'), + 'Name': name, 'LaunchDate': str(launch_time), + 'RunningDays': f"{days} days", 'State': resource.get('State', {}).get('Name'), + 'InstanceType': resource.get('InstanceType')} + if ticket_tags: # Add TicketId tags if available + instance_details['TicketId'] = ticket_tags + long_running_instances_by_user.setdefault(user.lower(), {}).setdefault(region_name, []).append(instance_details) + return long_running_instances_by_user def get_account_alias_name(self): """ @@ -98,7 +102,7 @@ def organize_message_to_send_slack(self, resources_list: dict): """ divider = {"type": "divider"} - keys_list = ['User', 'Region', 'Name', 'InstanceType', 'InstanceId', 'LaunchDate', 'RunningDays'] + keys_list = ['User', 'Region', 'Name', 'InstanceType', 'InstanceId', 'LaunchDate', 'RunningDays', 'TicketId'] rows = [] for user, region_list in resources_list.items(): for region_name, resources_list in region_list.items(): @@ -122,7 +126,7 @@ def organize_message_to_seng_mail(self, resources_list: dict): :param resources_list: :return: """ - keys_list = ['User', 'Region', 'Name', 'InstanceType', 'InstanceId', 'LaunchDate', 'State', 'RunningDays'] + keys_list = ['User', 'Region', 'Name', 'InstanceType', 'InstanceId', 'LaunchDate', 'State', 'RunningDays', 'TicketId'] with open('email_template.j2') as template: template = Template(template.read()) body = template.render({'resources_list': resources_list, 'keys_list': keys_list})