ec2 RI parser
#!/usr/bin/env ruby | |
require 'json' | |
def parse(uri) | |
JSON.parse `curl -s #{uri}`.gsub("/*\n * This file is intended for use only on aws.amazon.com. We do not guarantee its availability or accuracy.\n *\n * Copyright 2014 Amazon.com, Inc. or its affiliates. All rights reserved.\n */\ncallback({",'{').gsub("\);", '').gsub(/([a-zA-Z]+):/, '"\1":') | |
end | |
puts %w( | |
region | |
type | |
term | |
purchase_option | |
savingsOverOD | |
upfront | |
monthlyStar | |
effectiveHourly | |
).join("\t") | |
sources = %w( | |
https://a0.awsstatic.com/pricing/1/ec2/ri-v2/linux-unix-shared.min.js | |
http://a0.awsstatic.com/pricing/1/ec2/previous-generation/ri-v2/linux-unix-shared.min.js | |
) | |
sources.each do |s| | |
table = parse(s) | |
table['config']['regions'].each do |region_table| | |
current_region = region_table["region"] | |
region_table["instanceTypes"].each do |type_group| | |
current_type = type_group['type'] | |
type_group["terms"].each do |type_table| | |
current_term = type_table["term"] | |
type_table['purchaseOptions'].each do |options| | |
purchase_option = options['purchaseOption'] | |
savingsOverOD = options['savingsOverOD'] | |
upfront = options['valueColumns'].find{|x| x["name"] == "upfront"}["prices"]["USD"].to_f | |
monthlyStar = options['valueColumns'].find{|x| x["name"] == "monthlyStar"}["prices"]["USD"].to_f | |
effectiveHourly = options['valueColumns'].find{|x| x["name"] == "effectiveHourly"}["prices"]["USD"].to_f | |
puts [current_region, current_type, current_term, purchase_option, savingsOverOD, upfront, monthlyStar, effectiveHourly].join("\t") | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment