class AwsPricing::PriceList
PriceList
provides the primary interface for retrieving AWS pricing. Upon instantiating a PriceList
object, all the corresponding pricing information will be retrieved from Amazon via currently undocumented json APIs.
Constants
- DH_OD_BASE_URL
- DI_OD_BASE_URL
- EBS_BASE_URL
- EC2_BASE_URL
def find_or_create_region(name)
region = get_region(name) if region.nil? # We must use standard names region = Region.new(name) add_region(region) end region
end
- ELASTICACHE_BASE_URL
- RDS_BASE_URL
- RESERVED_DI_BASE_URL
- RESERVED_DI_PREV_GEN_BASE_URL
- RETRY_LIMIT
Attributes
_regions[RW]
regions[RW]
Public Class Methods
fetch_url(url)
click to toggle source
# File lib/amazon-pricing/aws-price-list.rb 60 def self.fetch_url(url) 61 # AWS appears to have started throttling URL accesses, so we now have to retry 62 begin 63 retry_count ||= 0 64 #$stdout.puts "[#{__method__}] url:#{url}" 65 uri = URI.parse(url) 66 page = Net::HTTP.get_response(uri) 67 rescue StandardError => e #Exception => e 68 $stderr.puts "Exception:#{e} retry-#{retry_count} Failed to fetch: #{url}" 69 sleep 5 # appears to get past (AWS) throttling 70 retry if (retry_count += 1) <= RETRY_LIMIT 71 end 72 73 # Now that AWS switched from json to jsonp, remove first/last lines 74 body = page.body.gsub("callback(", "").reverse.sub(")", "").reverse 75 if body.split("\n").last == ";" 76 # Now remove one more line (rds is returning ";", ec2 empty line) 77 body = body.reverse.sub(";", "").reverse 78 elsif body[-1] == ";" 79 body.chop! 80 end 81 82 begin 83 JSON.parse(body) 84 rescue JSON::ParserError 85 # Handle "json" with keys that are not quoted 86 # When we get {foo: "1"} instead of {"foo": "1"} 87 # http://stackoverflow.com/questions/2060356/parsing-json-without-quoted-keys 88 JSON.parse(body.gsub(/(\w+)\s*:/, '"\1":')) 89 end 90 rescue Exception => e 91 $stderr.puts "Exception:#{e} Failed to parse: #{url} #{e.backtrace}" 92 raise e 93 end
new()
click to toggle source
# File lib/amazon-pricing/aws-price-list.rb 20 def initialize() 21 @_regions = {} 22 23 # Creating regions upfront since different json files all use different naming conventions. No more ad-hoc creation. 24 regions = ["eu-west-1", "sa-east-1", "us-east-1", "ap-northeast-1", "us-west-2", "us-west-1", "ap-southeast-1", "ap-southeast-2", 25 "eu-central-1", "us-gov-west-1", "us-gov-east-1", "ap-northeast-2", "ap-south-1", "us-east-2", "ca-central-1", "eu-west-2", "eu-west-3", 26 "ap-northeast-3", "eu-north-1", "ap-east-1"] 27 28 regions.each do |name| 29 @_regions[name] = Region.new(name) 30 end 31 end
Public Instance Methods
get_instance_type(region_name, api_name)
click to toggle source
# File lib/amazon-pricing/aws-price-list.rb 54 def get_instance_type(region_name, api_name) 55 region = get_region(region_name) 56 raise "Region #{region_name} not found" if region.nil? 57 region.get_instance_type(api_name) 58 end
get_instance_types()
click to toggle source
# File lib/amazon-pricing/aws-price-list.rb 44 def get_instance_types 45 instance_types = [] 46 @_regions.each do |region| 47 region.ec2_instance_types.each do |instance_type| 48 instance_types << instance_type 49 end 50 end 51 instance_types 52 end
get_region(name)
click to toggle source
EBS now reports regions correctly but all else still has the old format - so we need to handle both region mapping and non-mapping
# File lib/amazon-pricing/aws-price-list.rb 35 def get_region(name) 36 #@_regions[@@Region_Lookup[name] || name] 37 @_regions[convert_region(name)] 38 end
Protected Instance Methods
convert_region(name)
click to toggle source
# File lib/amazon-pricing/aws-price-list.rb 123 def convert_region(name) 124 case name 125 when "us-east" 126 "us-east-1" 127 when "us-west" 128 "us-west-1" 129 when "eu-ireland" 130 "eu-west-1" 131 when "apac-sin" 132 "ap-southeast-1" 133 when "apac-syd" 134 "ap-southeast-2" 135 when "apac-tokyo" 136 "ap-northeast-1" 137 when "eu-frankfurt" 138 "eu-central-1" 139 else 140 name 141 end 142 end