diff --git a/cloud_governance/common/clouds/aws/price/resources_pricing.py b/cloud_governance/common/clouds/aws/price/resources_pricing.py index b13da57d..74518f6e 100644 --- a/cloud_governance/common/clouds/aws/price/resources_pricing.py +++ b/cloud_governance/common/clouds/aws/price/resources_pricing.py @@ -71,3 +71,22 @@ def get_nat_gateway_unit_price(self, region_name: str): ] unit_price = self._aws_pricing.get_service_pricing(service_code, filter_dict) return round(unit_price, DEFAULT_ROUND_DIGITS) + + def get_ebs_unit_price(self, region_name: str, ebs_type: str): + """ + This method returns the ebs_type unit price + :param region_name: + :type region_name: + :param ebs_type: + :type ebs_type: + :return: + :rtype: + """ + service_code = 'AmazonEC2' + filter_dict = [ + {"Field": "productFamily", "Value": "Storage", "Type": "TERM_MATCH"}, + {"Field": "regionCode", "Value": region_name, "Type": "TERM_MATCH"}, + {"Field": "volumeApiName", "Value": ebs_type, "Type": "TERM_MATCH"}, + ] + unit_price = self._aws_pricing.get_service_pricing(service_code, filter_dict) + return round(unit_price, DEFAULT_ROUND_DIGITS) diff --git a/cloud_governance/common/utils/configs.py b/cloud_governance/common/utils/configs.py index 4d76da58..0a9b7e70 100644 --- a/cloud_governance/common/utils/configs.py +++ b/cloud_governance/common/utils/configs.py @@ -6,6 +6,7 @@ PUBLIC_ACCOUNTS_COST_REPORTS = 'Accounts' HOURS_IN_SECONDS = 3600 HOURS_IN_DAY = 24 +HOURS_IN_MONTH = 720 DATE_FORMAT = "%Y-%m-%d" diff --git a/cloud_governance/policy/aws/cleanup/unattached_volume.py b/cloud_governance/policy/aws/cleanup/unattached_volume.py index 6a0b13f1..d3660a52 100644 --- a/cloud_governance/policy/aws/cleanup/unattached_volume.py +++ b/cloud_governance/policy/aws/cleanup/unattached_volume.py @@ -1,11 +1,9 @@ - - +from cloud_governance.common.utils.configs import HOURS_IN_MONTH from cloud_governance.policy.helpers.aws.aws_policy_operations import AWSPolicyOperations from cloud_governance.common.utils.utils import Utils class UnattachedVolume(AWSPolicyOperations): - RESOURCE_ACTION = "Delete" def __init__(self): @@ -29,6 +27,9 @@ def run_policy_operations(self): cleanup_days = self.get_clean_up_days_count(tags=tags) cleanup_result = self.verify_and_delete_resource(resource_id=resource_id, tags=tags, clean_up_days=cleanup_days) + monthly_price = self._resource_pricing.get_ebs_unit_price(region_name=self._region, + ebs_type=volume.get('VolumeType', '')) + unit_price = (monthly_price / HOURS_IN_MONTH) * float(volume.get('Size')) resource_data = self._get_es_schema(resource_id=resource_id, user=self.get_tag_name_from_tags(tags=tags, tag_name='User'), skip_policy=self.get_skip_policy_value(tags=tags), @@ -39,7 +40,9 @@ def run_policy_operations(self): resource_action=self.RESOURCE_ACTION, cloud_name=self._cloud_name, resource_type=volume.get('VolumeType', ''), - resource_state=volume.get('State') if not cleanup_result else "Deleted", + unit_price=unit_price, + resource_state=volume.get( + 'State') if not cleanup_result else "Deleted", volume_size=f"{volume.get('Size')} GB" ) unattached_volumes.append(resource_data) @@ -49,4 +52,3 @@ def run_policy_operations(self): self.update_resource_day_count_tag(resource_id=resource_id, cleanup_days=cleanup_days, tags=tags) return unattached_volumes - diff --git a/cloud_governance/policy/helpers/abstract_policy_operations.py b/cloud_governance/policy/helpers/abstract_policy_operations.py index cb92bcca..e69da11e 100644 --- a/cloud_governance/policy/helpers/abstract_policy_operations.py +++ b/cloud_governance/policy/helpers/abstract_policy_operations.py @@ -182,7 +182,7 @@ def __current_savings_year(self, unit_price: float): """ year_end_date = datetime.utcnow().date().replace(month=12, day=31) total_days = (year_end_date - datetime.utcnow().date()).days + 1 - return total_days * unit_price + return total_days * 24 * unit_price # ES Schema format