From 533c5c7e548a46df2fb05de1037e84c9ec76f354 Mon Sep 17 00:00:00 2001 From: Thirumalesh Aaraveti Date: Sun, 3 Dec 2023 16:35:42 +0530 Subject: [PATCH] Format the ec2/ volume data --- .../run_zombie_non_cluster_policies.py | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/cloud_governance/policy/policy_operations/aws/zombie_non_cluster/run_zombie_non_cluster_policies.py b/cloud_governance/policy/policy_operations/aws/zombie_non_cluster/run_zombie_non_cluster_policies.py index c1458aec..e56cf1b6 100644 --- a/cloud_governance/policy/policy_operations/aws/zombie_non_cluster/run_zombie_non_cluster_policies.py +++ b/cloud_governance/policy/policy_operations/aws/zombie_non_cluster/run_zombie_non_cluster_policies.py @@ -319,26 +319,37 @@ def _organise_instance_data(self, resources: list): organize_data = [] if 'ec2' in self._policy: for instance in resources: - instance['LaunchTime'] = instance['LaunchTime'].strftime("%Y-%m-%dT%H:%M:%S+00:00") + skip_policy = self._ec2_operations.get_tag_value_from_tags(tags=instance.get('Tags', []), tag_name='Policy') + if not skip_policy: + skip_policy = self._ec2_operations.get_tag_value_from_tags(tags=instance.get('Tags', []), tag_name='Skip') + instance_data = { + 'ResourceId': instance.get('InstanceId'), 'InstanceId': instance.get('InstanceId'), + 'User': self._ec2_operations.get_tag_value_from_tags(tags=instance.get('Tags', []), tag_name='User'), + 'Policy': skip_policy, + 'LaunchTime': instance['LaunchTime'].strftime("%Y-%m-%dT%H:%M:%S+00:00"), + 'InstanceType': instance.get('InstanceType'), + 'InstanceState': instance.get('State', {}).get('Name'), + 'StateTransitionReason': instance.get('StateTransitionReason') + } for index, device_mappings in enumerate(instance['BlockDeviceMappings']): - instance['BlockDeviceMappings'][index]['Ebs']['AttachTime'] = device_mappings['Ebs']['AttachTime'].strftime("%Y-%m-%dT%H:%M:%S+00:00") - for index, network_interface in enumerate(instance['NetworkInterfaces']): - instance['NetworkInterfaces'][index]['Attachment']['AttachTime'] = network_interface['Attachment']['AttachTime'].strftime("%Y-%m-%dT%H:%M:%S+00:00") - if instance.get('UsageOperationUpdateTime'): - instance['UsageOperationUpdateTime'] = instance['UsageOperationUpdateTime'].strftime("%Y-%m-%dT%H:%M:%S+00:00") - if instance.get('metrics'): - for index, metric in enumerate(instance['metrics']): - instance['metrics'][index]['Timestamps'] = [date.strftime("%Y-%m-%dT%H:%M:%S+00:00") for date in metric['Timestamps']] - organize_data.append(instance) + instance_data.setdefault('DeviceMappings', [])\ + .append(device_mappings['Ebs']['AttachTime'].strftime("%Y-%m-%dT%H:%M:%S+00:00")) + organize_data.append(instance_data) else: for volume in resources: + volume_data = {'ResourceId': volume.get('VolumeId'), 'Size': volume.get('Size'), + 'VolumeId': volume.get('VolumeId'), + 'VolumeState': volume.get('State'), 'Iops': volume.get('Iops'), + 'VolumeType': volume.get('VolumeType'), + 'User': self._ec2_operations.get_tag_value_from_tags(tags=volume.get('Tags', []), tag_name='User'), + 'Policy': self._ec2_operations.get_tag_value_from_tags(tags=volume.get('Tags', []), tag_name='Policy')} if volume.get('Attachments'): for attachment in volume.get('Attachments'): - attachment['AttachTime'] = attachment['AttachTime'].strftime("%Y-%m-%dT%H:%M:%S+00:00") - volume['CreateTime'] = volume['CreateTime'].strftime("%Y-%m-%dT%H:%M:%S+00:00") - if 'Tags' in volume: - del volume['Tags'] - organize_data.append(volume) + volume_data.update({ + 'AttachTime': attachment['AttachTime'].strftime("%Y-%m-%dT%H:%M:%S+00:00") + }) + volume_data.update({'CreateTime': volume['CreateTime'].strftime("%Y-%m-%dT%H:%M:%S+00:00")}) + organize_data.append(volume_data) return organize_data def get_ebs_cost(self, resource: any, resource_type: str, resource_hours: int):