Skip to content

Instantly share code, notes, and snippets.

@shinsaka
Forked from tatsuru/parse.rb
Last active April 18, 2017 10:35
Show Gist options
  • Save shinsaka/c27fce36ec772eda76d3 to your computer and use it in GitHub Desktop.
Save shinsaka/c27fce36ec772eda76d3 to your computer and use it in GitHub Desktop.
ec2 RI parser
#!/usr/bin/env ruby
require 'json'
def parse(uri)
JSON.parse `curl -s #{uri}`.gsub(/\/\*.*\*\/\s*callback\(\{/m,'{').gsub("\);", '').gsub(/([a-zA-Z]+):/, '"\1":')
end
puts %w(
platform
region
type
term
purchase_option
savingsOverOD
upfront
monthlyStar
effectiveHourly
onDemandHourlyPrice
).join("\t")
sources = {
"linux-unix.1" => "https://a0.awsstatic.com/pricing/1/ec2/ri-v2/linux-unix-shared.min.js",
"linux-unix.2" => "https://a0.awsstatic.com/pricing/1/ec2/previous-generation/ri-v2/linux-unix-shared.min.js",
"rhel.1" => "https://a0.awsstatic.com/pricing/1/ec2/ri-v2/red-hat-enterprise-linux-shared.min.js",
"rhel.2" => "https://a0.awsstatic.com/pricing/1/ec2/previous-generation/ri-v2/red-hat-enterprise-linux-shared.min.js",
"suse-linux.1" => "https://a0.awsstatic.com/pricing/1/ec2/ri-v2/suse-linux-shared.min.js",
"suse-linux.2" => "https://a0.awsstatic.com/pricing/1/ec2/previous-generation/ri-v2/suse-linux-shared.min.js",
"windows.1" => "https://a0.awsstatic.com/pricing/1/ec2/ri-v2/windows-shared.min.js",
"windows.2" => "https://a0.awsstatic.com/pricing/1/ec2/previous-generation/ri-v2/windows-shared.min.js",
"win-sql-std.1" => "https://a0.awsstatic.com/pricing/1/ec2/ri-v2/windows-with-sql-server-standard-shared.min.js",
"win-sql-std.2" => "https://a0.awsstatic.com/pricing/1/ec2/previous-generation/ri-v2/windows-with-sql-server-standard-shared.min.js",
"win-sql-web.1" => "https://a0.awsstatic.com/pricing/1/ec2/ri-v2/windows-with-sql-server-web-shared.min.js",
"win-sql-web.2" => "https://a0.awsstatic.com/pricing/1/ec2/previous-generation/ri-v2/windows-with-sql-server-web-shared.min.js",
"win-sql-ent.1" => "https://a0.awsstatic.com/pricing/1/ec2/ri-v2/windows-with-sql-server-enterprise-shared.min.js",
"win-sql-ent.2" => "https://a0.awsstatic.com/pricing/1/ec2/previous-generation/ri-v2/windows-with-sql-server-enterprise-shared.min.js",
}
sources.each do |k, 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|
on_demand_price = ""
type_table["onDemandHourly"].each do |on_demand|
on_demand_price = on_demand["prices"]["USD"]
end
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 [k.gsub(/\.\d+$/, ""), current_region, current_type, current_term, purchase_option, savingsOverOD, upfront, monthlyStar, effectiveHourly, on_demand_price].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