Skip to content

Commit

Permalink
Include bare metal instance offers for EC2 (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Valenzuela authored and garrettheel committed Jul 10, 2018
1 parent 55aca70 commit 559479b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
12 changes: 7 additions & 5 deletions awspricing/offers.py
Expand Up @@ -104,7 +104,7 @@ def _generate_reverse_sku_mapping(self,
result; products which collide on hash will be discarded.
"""

product_family = kwargs.get('product_family')
product_families = kwargs.get('product_families')

result = {} # type: Dict[str, str]

Expand All @@ -116,8 +116,7 @@ def _generate_reverse_sku_mapping(self,
for sku, product in six.iteritems(self._offer_data['products']):
# Introduced for Data transfer SKU's that are not like regular EC2 offers
try:
if (product_family is not None and
product['productFamily'] != product_family):
if product_families and product['productFamily'] not in product_families:
continue
except KeyError:
continue
Expand Down Expand Up @@ -154,7 +153,10 @@ def __init__(self, *args, **kwargs):
self._reverse_sku = self._generate_reverse_sku_mapping(
'instanceType', 'operatingSystem', 'tenancy', 'licenseModel',
'preInstalledSw', 'location',
product_family='Compute Instance'
# Both families are queried assuming that instance names will never clash between
# them. This should be true given metal instance naming conventions thus far (instance
# size is 'metal').
product_families=['Compute Instance', 'Compute Instance (bare metal)']
)

# Lazily-loaded cache to hold offerTermCodes within a SKU
Expand Down Expand Up @@ -379,7 +381,7 @@ def __init__(self, *args, **kwargs):
'licenseModel',
'location',
'databaseEdition',
product_family='Database Instance'
product_families=['Database Instance']
)

# Lazily-loaded cache to hold offerTermCodes within a SKU
Expand Down
41 changes: 40 additions & 1 deletion tests/data/ec2_offer.py
Expand Up @@ -380,4 +380,43 @@
},
}
}
}
}


BARE_METAL_EC2_SKU = 'SBVNSX4BKU246KVM'


BARE_METAL_EC2_OFFER = {
'offerCode': 'AmazonEC2',
'version': '20161213014831',
'products': {
"SBVNSX4BKU246KVM": {
"productFamily": "Compute Instance (bare metal)",
"sku": "SBVNSX4BKU246KVM",
"attributes": {
"servicename": "Amazon Elastic Compute Cloud",
"preInstalledSw": "SQL Ent",
"normalizationSizeFactor": "128",
"ecu": "208",
"capacitystatus": "Used",
"operation": "RunInstances:0102",
"physicalProcessor": "Intel Xeon E5-2686 v4 (Broadwell)",
"vcpu": "72",
"instanceFamily": "Storage optimized",
"currentGeneration": "Yes",
"instanceType": "i3.metal",
"locationType": "AWS Region",
"location": "EU (Ireland)",
"servicecode": "AmazonEC2",
"memory": "512 GiB",
"storage": "8 x 1900 NVMe SSD",
"networkPerformance": "25 Gigabit",
"processorArchitecture": "64-bit",
"tenancy": "Shared",
"operatingSystem": "Windows",
"licenseModel": "No License required",
"usagetype": "EU-BoxUsage:i3.metal"
},
}
}
}
18 changes: 17 additions & 1 deletion tests/unit/test_offers.py
Expand Up @@ -5,7 +5,13 @@
from awspricing.offers import AWSOffer, EC2Offer
from awspricing.constants import EC2_PURCHASE_OPTION, EC2_LEASE_CONTRACT_LENGTH

from tests.data.ec2_offer import BASIC_EC2_OFFER_DATA, BASIC_EC2_OFFER_SKU, BASIC_EC2_OFFER_MODIFIED_FORMAT
from tests.data.ec2_offer import (
BARE_METAL_EC2_OFFER,
BARE_METAL_EC2_SKU,
BASIC_EC2_OFFER_DATA,
BASIC_EC2_OFFER_SKU,
BASIC_EC2_OFFER_MODIFIED_FORMAT
)


class TestAWSOffer(object):
Expand All @@ -14,6 +20,10 @@ class TestAWSOffer(object):
def basic_offer(self):
return AWSOffer(BASIC_EC2_OFFER_DATA)

@pytest.fixture
def bare_metal_offer(self):
return AWSOffer(BARE_METAL_EC2_OFFER)

def test_raw(self, offer):
assert 'version' in offer.raw
assert 'products' in offer.raw
Expand Down Expand Up @@ -47,6 +57,12 @@ def test_generate_reverse_sku_mapping(self, offer):
'instanceType', 'operatingSystem', 'tenancy'
) == {'c4.large|Linux|Shared': BASIC_EC2_OFFER_SKU}

def test_bare_metal_included(self, bare_metal_offer):
assert bare_metal_offer._generate_reverse_sku_mapping(
'instanceType', 'operatingSystem', 'tenancy',
product_families=['Compute Instance (bare metal)']
) == {'i3.metal|Windows|Shared': BARE_METAL_EC2_SKU}

def test_generate_reverse_sku_mapping_collision(self, offer):
collision_sku = 'collision_sku'

Expand Down

0 comments on commit 559479b

Please sign in to comment.