in scripts/populate_aws_shapes.py [0:0]
def _populate_prices(paginator, customer, os, region):
from mongoengine import NotUniqueError
from models.shape_price import ShapePrice
from models.base_model import CloudEnum
pages = paginator.paginate(ServiceCode='AmazonEC2', Filters=[
{'Type': 'TERM_MATCH', 'Field': 'operatingSystem',
'Value': os},
{'Type': 'TERM_MATCH', 'Field': 'preInstalledSw', 'Value': 'NA'},
{'Type': 'TERM_MATCH', 'Field': 'tenancy', 'Value': 'Shared'},
{'Type': 'TERM_MATCH', 'Field': 'regionCode',
'Value': region},
{'Type': 'TERM_MATCH', 'Field': 'capacityStatus', 'Value': 'Used'},
{'Type': 'TERM_MATCH', 'Field': 'serviceCode', 'Value': 'AmazonEC2'},
{'Type': 'TERM_MATCH', 'Field': 'licenseModel',
'Value': 'No License required'},
])
entries = []
for page in pages:
entries.extend(page['PriceList'])
result = []
for entry in entries:
obj = json.loads(entry)
attributes = obj.get("product", {}).get('attributes', {})
if not attributes:
continue
instance_type = attributes.get('instanceType')
instance_region = attributes.get('regionCode')
instance_os = attributes.get('operatingSystem')
on_demand = obj.get('terms', {}).get('OnDemand')
if not on_demand:
continue
# extract value by unknown dict key
first_key = list(on_demand)[0]
price_dimensions = on_demand[first_key]['priceDimensions']
# extract value by unknown dict key
first_key = list(price_dimensions)[0]
price_per_unit = price_dimensions[first_key][
'pricePerUnit']['USD']
price_per_unit = float(price_per_unit)
if price_per_unit == 0:
continue
price_item = ShapePrice(
customer=customer,
cloud=CloudEnum.CLOUD_AWS.value,
name=instance_type,
region=instance_region,
os=instance_os.upper(),
on_demand=price_per_unit
)
try:
price_item.save()
except NotUniqueError:
print(f"Price {price_item.name} already exist, rewriting")
old_price = ShapePrice.objects.get(name=price_item.name,
customer=price_item.customer,
region=price_item.region,
os=price_item.os)
old_price.delete()
price_item.save()
result.append(price_item)
return result