Created
November 21, 2016 01:02
-
-
Save darkarnium/2ff024e2980264e0e715750f808f4162 to your computer and use it in GitHub Desktop.
Fetch AWS EIP pricing and convert into JSON compatible with Django fixtures. Raw
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/ec2/pricing-elastic-ips.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_NAME': 'janitor.addressscope', | |
'fields': { | |
'REGION': region['region'] | |
} | |
} | |
# Attempt to extract pricing. | |
base_price = 0 | |
for p in t['values']: | |
if p['rate'] == 'perNonAttachedPerHour': | |
base_price = p['prices']['USD'] | |
# Add pricing unit. | |
unit['fields']['TYPE'] = 'vpc' | |
unit['fields']['PER_NON_ATTACHED_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
standard
scope EIPs (EC2 Classic).