Skip to content

Commit

Permalink
Added the ebs unit price
Browse files Browse the repository at this point in the history
  • Loading branch information
athiruma committed Apr 18, 2024
1 parent e81554c commit d8eff79
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
19 changes: 19 additions & 0 deletions cloud_governance/common/clouds/aws/price/resources_pricing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions cloud_governance/common/utils/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 7 additions & 5 deletions cloud_governance/policy/aws/cleanup/unattached_volume.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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),
Expand All @@ -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)
Expand All @@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d8eff79

Please sign in to comment.