Skip to content

Commit e1abe83

Browse files
authored
[COST-5432] - Add the ability to generate reserved instances (#538)
* Adding RIs option to AWS EC2 generator
1 parent 48cbacc commit e1abe83

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

nise/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "4.7.0"
1+
__version__ = "4.7.1"
22

33

44
VERSION = __version__.split(".")

nise/generators/aws/ec2_generator.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,17 @@ class EC2Generator(AWSGenerator):
2525

2626
INSTANCE_TYPES = (
2727
# NOTE: Each tuple represents
28-
# (instance type, physical_cores, vCPUs, memory, storage, family, cost, rate, savings, description)
28+
# (instance type,
29+
# physical_cores,
30+
# vCPUs,
31+
# memory,
32+
# storage,
33+
# family,
34+
# cost,
35+
# rate,
36+
# Reserved_instances,
37+
# savings,
38+
# description)
2939
(
3040
"m5.large",
3141
"1",
@@ -38,6 +48,7 @@ class EC2Generator(AWSGenerator):
3848
"0.045",
3949
1,
4050
False,
51+
False,
4152
"${cost} per On Demand Linux {inst_type} Instance Hour",
4253
),
4354
(
@@ -52,6 +63,7 @@ class EC2Generator(AWSGenerator):
5263
"0.17",
5364
1,
5465
False,
66+
False,
5567
"${cost} per On Demand Linux {inst_type} Instance Hour",
5668
),
5769
(
@@ -66,6 +78,7 @@ class EC2Generator(AWSGenerator):
6678
"0.099",
6779
1,
6880
False,
81+
False,
6982
"${cost} per On Demand Linux {inst_type} Instance Hour",
7083
),
7184
(
@@ -80,6 +93,7 @@ class EC2Generator(AWSGenerator):
8093
"0.067",
8194
1,
8295
False,
96+
False,
8397
"${cost} per On Demand Linux {inst_type} Instance Hour",
8498
),
8599
)
@@ -124,6 +138,7 @@ def __init__(self, start_date, end_date, currency, payer_account, usage_accounts
124138
instance_type.get("rate"),
125139
instance_type.get("saving"),
126140
instance_type.get("amount", "1"),
141+
instance_type.get("reserved_instance", False),
127142
instance_type.get("negation", False),
128143
"${cost} per On Demand Linux {inst_type} Instance Hour",
129144
)
@@ -141,6 +156,7 @@ def _update_data(self, row, start, end, **kwargs):
141156
rate,
142157
saving,
143158
amount,
159+
reserved_instance,
144160
negation,
145161
description,
146162
) = self._instance_type
@@ -203,6 +219,18 @@ def _update_data(self, row, start, end, **kwargs):
203219
# Overwrite lineItem/LineItemType for items with applied Savings plan
204220
if saving is not None:
205221
row["lineItem/LineItemType"] = "SavingsPlanCoveredUsage"
222+
# Overwrite lineitem/LineItemType for RI's discount usage
223+
if reserved_instance:
224+
row["lineItem/LineItemType"] = "DiscountedUsage"
225+
row["lineItem/UnblendedCost"] = 0
226+
row["lineItem/UnblendedRate"] = 0
227+
row["lineItem/BlendedCost"] = 0
228+
row["lineItem/BlendedRate"] = 0
229+
row["lineItem/LineItemDescription"] = f"{inst_type} reserved instance applied"
230+
row["pricing/publicOnDemandCost"] = "convertible"
231+
row["pricing/publicOnDemandRate"] = "No Upfront"
232+
row["savingsPlan/SavingsPlanEffectiveCost"] = None
233+
row["savingsPlan/SavingsPlanRate"] = None
206234

207235
if negation:
208236
row["lineItem/LineItemType"] = "SavingsPlanNegation"

tests/aws_static_report.yml

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ generators:
2020
cost: 1.000
2121
rate: 0.500
2222
saving: 0.250
23+
- EC2Generator:
24+
start_date: today
25+
end_date: today
26+
processor_arch: 32-bit
27+
resource_id: 55555555
28+
product_sku: VEAJHRNKTJZQ
29+
region: us-east-1a
30+
reserved_instance: True
2331
- S3Generator:
2432
start_date: last_month
2533
end_date: last_month

tests/test_aws_generator.py

+14
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ def test_init_with_attributes(self):
395395
"rate": "1",
396396
"saving": "1",
397397
"amount": 1,
398+
"reserved_instance": False,
398399
"negation": False,
399400
}
400401
self.attributes["instance_type"] = self.instance_type
@@ -419,6 +420,19 @@ def test_update_data_ec2_negation(self):
419420

420421
self.assertEqual(row["lineItem/LineItemType"], "SavingsPlanNegation")
421422

423+
def test_update_data_ec2_reserved_instances(self):
424+
"""Test EC2 specific update data with Reserved Instances."""
425+
self.instance_type = {"reserved_instance": True}
426+
self.attributes["instance_type"] = self.instance_type
427+
generator = EC2Generator(
428+
self.two_hours_ago, self.now, self.currency, self.payer_account, self.usage_accounts, self.attributes
429+
)
430+
start_row = {}
431+
row = generator._update_data(start_row, self.two_hours_ago, self.now)
432+
433+
self.assertEqual(row["lineItem/LineItemType"], "DiscountedUsage")
434+
self.assertEqual(row["lineItem/UnblendedCost"], 0)
435+
422436
def test_update_data(self):
423437
"""Test EBS specific update data method."""
424438
generator = EC2Generator(

0 commit comments

Comments
 (0)