Class: AwsPricing::PriceList

Inherits:
Object
  • Object
show all
Defined in:
lib/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.

Direct Known Subclasses

Ec2PriceList, RdsPriceList

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePriceList

Returns a new instance of PriceList


19
20
21
22
23
24
25
26
27
28
# File 'lib/aws-price-list.rb', line 19

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"]

  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/aws-price-list.rb', line 17

def regions
  @regions
end

Class Method Details

.fetch_url(url) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/aws-price-list.rb', line 57

def self.fetch_url(url)
  uri = URI.parse(url)
  page = Net::HTTP.get_response(uri)
  # 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
end

Instance Method Details

#get_instance_type(region_name, api_name) ⇒ Object


51
52
53
54
55
# File 'lib/aws-price-list.rb', line 51

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


41
42
43
44
45
46
47
48
49
# File 'lib/aws-price-list.rb', line 41

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


32
33
34
35
# File 'lib/aws-price-list.rb', line 32

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