Class: AWSCosts::EC2OnDemand

Inherits:
Object
  • Object
show all
Defined in:
lib/awscosts/ec2_on_demand.rb

Constant Summary collapse

TYPE_TRANSLATION =
{ 'stdODI.sm' => 'm1.small',
'stdODI.med' => 'm1.medium',
'stdODI.lg' => 'm1.large',
'stdODI.xl' => 'm1.xlarge',
'secgenstdODI.xl' => 'm3.xlarge',
'secgenstdODI.xxl' => 'm3.2xlarge',
'uODI.u' => 't1.micro',
'hiMemODI.xl' => 'm2.xlarge',
'hiMemODI.xxl' => 'm2.2xlarge',
'hiMemODI.xxxxl' => 'm2.4xlarge',
'hiCPUODI.med' => 'c1.medium',
'hiCPUODI.xl' => 'c1.xlarge',
'clusterComputeI.xxxxl' => 'cc1.4xlarge',
'clusterComputeI.xxxxxxxxl' => 'cc2.8xlarge',
'clusterGPUI.xxxxl' => 'cg1.4xlarge',
'clusterHiMemODI.xxxxxxxxl' => 'cr1.8xlarge',
'hiStoreODI.xxxxxxxxl' => 'hs1.8xlarge',
'hiIoODI.xxxxl' => 'hi1.4xlarge' }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ EC2OnDemand

Returns a new instance of EC2OnDemand.



24
25
26
# File 'lib/awscosts/ec2_on_demand.rb', line 24

def initialize data
  @data = data
end

Class Method Details

.fetch(type, region) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/awscosts/ec2_on_demand.rb', line 32

def self.fetch type, region
  result = {}
  ['/pricing/1/ec2/%s-od.min.js', '/pricing/1/ec2/previous-generation/%s-od.min.js'].each do |uri|
    AWSCosts::Cache.get_jsonp(uri % type) do |data|
      data['config']['regions'].each do |r|
        result[r['region']] ||= {}
        platforms = result[r['region']]
        r['instanceTypes'].each do |instance_type|
          instance_type['sizes'].each do |instance_size|
            size = instance_size['size']
            platform_cost = Hash.new({})
            instance_size['valueColumns'].each do |value|
              # Don't return 0.0 for "N/A" since that is misleading
              platform_cost[value['name']] = value['prices']['USD'] == 'N/A' ? nil : value['prices']['USD'].to_f
            end

            platform_cost.each_pair do |p,v|
              platforms[p] = {} unless platforms.key?(p)
              platforms[p][size] = v
            end
          end
        end
      end
    end
  end

  raise "No result for region #{region} while fetching EC2 OnDemand Pricing"            if result[region].nil?
  raise "No result for #{type} in region #{region} while fetching EC2 OnDemand Pricing" if result[region][type].nil?

  self.new(result[region][type])
end

Instance Method Details

#price(size = nil) ⇒ Object



28
29
30
# File 'lib/awscosts/ec2_on_demand.rb', line 28

def price size=nil
  size ? @data[size] : @data
end