From a56b03d26d367fa403814e06ab057d0887f45ee3 Mon Sep 17 00:00:00 2001 From: Krishna Harsha Voora <14876995+krishvoor@users.noreply.github.com> Date: Wed, 17 Apr 2024 13:40:38 +0530 Subject: [PATCH] Add TicketId to the Email (#754) * Add TicketId to the Email Signed-off-by: Krishna Harsha Voora * Fixes indentation errors * Minor changes * Move 'TicketId' to second column --------- Signed-off-by: Krishna Harsha Voora --- cloudsensei/lambda_function.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/cloudsensei/lambda_function.py b/cloudsensei/lambda_function.py index de97a85d..6e43ae20 100644 --- a/cloudsensei/lambda_function.py +++ b/cloudsensei/lambda_function.py @@ -71,14 +71,18 @@ 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')}) + 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', 'TicketId', 'Region', 'Name', 'InstanceType', 'InstanceId', 'LaunchDate', 'RunningDays'] 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', 'TicketId', 'Region', 'Name', 'InstanceType', 'InstanceId', 'LaunchDate', 'State', 'RunningDays'] with open('email_template.j2') as template: template = Template(template.read()) body = template.render({'resources_list': resources_list, 'keys_list': keys_list})