Created
November 21, 2016 00:52
-
-
Save darkarnium/5be39f9047d7f32bfdd3baafd0bc83f9 to your computer and use it in GitHub Desktop.
Fetch AWS ELB pricing and convert into JSON compatible with Django fixtures.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re | |
import sys | |
import json | |
import requests | |
source = 'http://a0.awsstatic.com/pricing/1/elasticloadbalancer/pricing-elb.min.js' | |
# Request. | |
raw = requests.get(source).text.replace('\n', '') | |
# 'Transform' to JSON. | |
x = re.search(r'^.*callback\((.*)\);', raw) | |
if not x: | |
print 'Unable to parse input data.' | |
sys.exit(-1) | |
# Ensure all keys are quoted, this is janky, but should work for at least the | |
# Amazon pricing data. | |
parsed = x.group(1) | |
parsed = re.sub(r'\{([\$a-zA-Z0-9\-]+)', r'{"\1"', parsed) | |
parsed = re.sub(r',([\$a-zA-Z0-9\-]+)', r',"\1"', parsed) | |
pricing = json.loads(parsed) | |
# Generate fixtures for pricing data. | |
fixtures = [] | |
for region in pricing['config']['regions']: | |
for t in region['types']: | |
unit = { | |
'model': 'MODEL_NAME', | |
'fields': { | |
'REGION': region['region'] | |
} | |
} | |
# Attempt to extract pricing. | |
base_price = 0 | |
for p in t['values']: | |
if p['rate'] == 'perELBHour': | |
base_price = p['prices']['USD'] | |
# Add pricing unit. | |
unit['fields']['TYPE'] = 'ELB' | |
unit['fields']['PER_HOUR_PRICE'] = base_price | |
fixtures.append(unit) | |
print json.dumps(fixtures, sort_keys=True, indent=4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: This doesn't cover ALB pricing.