Class: AwsPricing::PriceList

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon-pricing/aws-price-list.rb

Overview

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.

Constant Summary collapse

RETRY_LIMIT =
3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePriceList

Returns a new instance of PriceList.


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/amazon-pricing/aws-price-list.rb', line 20

def initialize()
  @_regions = {}

  # Creating regions upfront since different json files all use different naming conventions. No more ad-hoc creation.
  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",
		 "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",
 "ap-northeast-3", "eu-north-1", "ap-east-1"]

  regions.each do |name|
    @_regions[name] = Region.new(name)
  end
end

Instance Attribute Details

#regionsObject

Returns the value of attribute regions.


17
18
19
# File 'lib/amazon-pricing/aws-price-list.rb', line 17

def regions
  @regions
end

Class Method Details

.fetch_url(url) ⇒ Object


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/amazon-pricing/aws-price-list.rb', line 60

def self.fetch_url(url)
  # AWS appears to have started throttling URL accesses, so we now have to retry
  begin
    retry_count ||= 0
    #$stdout.puts "[#{__method__}] url:#{url}"
    uri = URI.parse(url)
    page = Net::HTTP.get_response(uri)
  rescue StandardError => e  #Exception => e
    $stderr.puts "Exception:#{e} retry-#{retry_count} Failed to fetch: #{url}"
    sleep 5 # appears to get past (AWS) throttling
    retry if (retry_count += 1) <= RETRY_LIMIT
  end

  # Now that AWS switched from json to jsonp, remove first/last lines
  body = page.body.gsub("callback(", "").reverse.sub(")", "").reverse
  if body.split("\n").last == ";"
    # Now remove one more line (rds is returning ";", ec2 empty line)
    body = body.reverse.sub(";", "").reverse
  elsif body[-1] == ";"
    body.chop!
  end

  begin
    JSON.parse(body)
  rescue JSON::ParserError
    # Handle "json" with keys that are not quoted
    # When we get {foo: "1"} instead of {"foo": "1"}
    # http://stackoverflow.com/questions/2060356/parsing-json-without-quoted-keys
    JSON.parse(body.gsub(/(\w+)\s*:/, '"\1":'))
  end
rescue Exception => e
  $stderr.puts "Exception:#{e} Failed to parse: #{url} #{e.backtrace}"
  raise e
end

Instance Method Details

#get_instance_type(region_name, api_name) ⇒ Object


54
55
56
57
58
# File 'lib/amazon-pricing/aws-price-list.rb', line 54

def get_instance_type(region_name, api_name)
  region = get_region(region_name)
  raise "Region #{region_name} not found" if region.nil?
  region.get_instance_type(api_name)
end

#get_instance_typesObject


44
45
46
47
48
49
50
51
52
# File 'lib/amazon-pricing/aws-price-list.rb', line 44

def get_instance_types
  instance_types = []
  @_regions.each do |region|
    region.ec2_instance_types.each do |instance_type|
      instance_types << instance_type
    end
  end
  instance_types
end

#get_region(name) ⇒ Object

EBS now reports regions correctly but all else still has the old format - so we need to handle both region mapping and non-mapping


35
36
37
38
# File 'lib/amazon-pricing/aws-price-list.rb', line 35

def get_region(name)
  #@_regions[@@Region_Lookup[name] || name]
  @_regions[convert_region(name)]
end