amazon-pricing 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Gemfile
2
+ source "https://rubygems.org"
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'rspec', '~> 2.11.0'
7
+ end
8
+
9
+ # gem "rails"
data/Rakefile CHANGED
@@ -1,6 +1,9 @@
1
1
  require 'bundler'
2
2
  require 'rake/testtask'
3
- require File.expand_path('lib/amazon-pricing/version')
3
+
4
+ $: << File.expand_path(File.dirname(__FILE__), 'lib')
5
+
6
+ require 'amazon-pricing/version'
4
7
 
5
8
  Rake::TestTask.new(:test) do |test|
6
9
  test.libs << 'lib' << 'test'
@@ -73,7 +73,11 @@ module AwsPricing
73
73
  region = find_or_create_region(region_name)
74
74
  reg['instanceTypes'].each do |type|
75
75
  type['sizes'].each do |size|
76
- region.add_or_update_instance_type(:on_demand, InstanceType.new(region, type['type'], size))
76
+ begin
77
+ region.add_or_update_instance_type(:on_demand, InstanceType.new(region, type['type'], size))
78
+ rescue UnknownTypeError
79
+ $stderr.puts "WARNING: encountered #{$!.message}"
80
+ end
77
81
  end
78
82
  end
79
83
  end
@@ -98,7 +102,11 @@ module AwsPricing
98
102
  region = find_or_create_region(region_name)
99
103
  reg['instanceTypes'].each do |type|
100
104
  type['sizes'].each do |size|
101
- region.add_or_update_instance_type(:reserved, ReservedInstanceType.new(region, type['type'], size, reserved_usage_type, platform), reserved_usage_type)
105
+ begin
106
+ region.add_or_update_instance_type(:reserved, ReservedInstanceType.new(region, type['type'], size, reserved_usage_type, platform), reserved_usage_type)
107
+ rescue UnknownTypeError
108
+ $stderr.puts "WARNING: encountered #{$!.message}"
109
+ end
102
110
  end
103
111
  end
104
112
  end
@@ -9,6 +9,9 @@
9
9
  #++
10
10
  module AwsPricing
11
11
 
12
+ class UnknownTypeError < NameError
13
+ end
14
+
12
15
  # InstanceType is a specific type of instance in a region with a defined
13
16
  # price per hour. The price will vary by platform (Linux, Windows).
14
17
  #
@@ -23,7 +26,7 @@ module AwsPricing
23
26
  # type (e.g. stdODI) and the json for the specific instance. The json is
24
27
  # based on the current undocumented AWS pricing API.
25
28
  def initialize(region, instance_type, json)
26
- values = get_values(json)
29
+ values = InstanceType::get_values(json)
27
30
 
28
31
  @size = json['size']
29
32
  @linux_price_per_hour = values['linux'].to_f
@@ -32,8 +35,8 @@ module AwsPricing
32
35
  @windows_price_per_hour = nil if @windows_price_per_hour == 0
33
36
  @instance_type = instance_type
34
37
 
35
- @api_name = get_api_name(@instance_type, @size)
36
- @name = get_name(@instance_type, @size)
38
+ @api_name = self.class.get_api_name(@instance_type, @size)
39
+ @name = self.class.get_name(@instance_type, @size)
37
40
 
38
41
  @memory_in_mb = @@Memory_Lookup[@api_name]
39
42
  @disk_in_mb = @@Disk_Lookup[@api_name]
@@ -63,23 +66,21 @@ module AwsPricing
63
66
 
64
67
  attr_accessor :size, :instance_type
65
68
 
66
- def get_api_name(instance_type, size)
69
+ def self.get_api_name(instance_type, size)
67
70
  # Let's handle new instances more gracefully
68
- unless @@Api_Name_Lookup.has_key?(instance_type)
69
- raise "The instance type #{instance_type} is not found"
70
- end
71
- if @@Api_Name_Lookup[instance_type][size].nil?
72
- raise "The instance type #{instance_type} of size #{size} is not found"
71
+ unless @@Api_Name_Lookup.has_key? instance_type
72
+ raise UnknownTypeError, "Unknown instance type #{instance_type}", caller
73
+ else
74
+ @@Api_Name_Lookup[instance_type][size]
73
75
  end
74
- @@Api_Name_Lookup[instance_type][size]
75
76
  end
76
77
 
77
- def get_name(instance_type, size)
78
+ def self.get_name(instance_type, size)
78
79
  @@Name_Lookup[instance_type][size]
79
80
  end
80
81
 
81
82
  # Turn json into hash table for parsing
82
- def get_values(json)
83
+ def self.get_values(json)
83
84
  values = {}
84
85
  json['valueColumns'].each do |val|
85
86
  values[val['name']] = val['prices']['USD']
@@ -73,18 +73,16 @@ module AwsPricing
73
73
  protected
74
74
  attr_accessor :size, :instance_type
75
75
 
76
- def get_api_name(instance_type, size)
76
+ def self.get_api_name(instance_type, size)
77
77
  # Let's handle new instances more gracefully
78
- unless @@Api_Name_Lookup_Reserved.has_key?(instance_type)
79
- raise "The instance type #{instance_type} is not found"
78
+ unless @@Api_Name_Lookup_Reserved.has_key? instance_type
79
+ raise UnknownTypeError, "Unknown instance type #{instance_type}", caller
80
+ else
81
+ @@Api_Name_Lookup_Reserved[instance_type][size]
80
82
  end
81
- if @@Api_Name_Lookup_Reserved[instance_type][size].nil?
82
- raise "The instance type #{instance_type} of size #{size} is not found"
83
- end
84
- @@Api_Name_Lookup_Reserved[instance_type][size]
85
83
  end
86
84
 
87
- def get_name(instance_type, size)
85
+ def self.get_name(instance_type, size)
88
86
  @@Name_Lookup_Reserved[instance_type][size]
89
87
  end
90
88
 
@@ -8,5 +8,5 @@
8
8
  # Home:: http://github.com/sonian/amazon-pricing
9
9
  #++
10
10
  module AwsPricing
11
- VERSION = '0.1.1'
11
+ VERSION = '0.1.2'
12
12
  end
@@ -0,0 +1,11 @@
1
+ require 'amazon-pricing/instance-type'
2
+
3
+ describe AwsPricing::InstanceType do
4
+ describe '::get_api_name' do
5
+ it "raises an UnknownTypeError on an unexpected instance type" do
6
+ expect {
7
+ AwsPricing::InstanceType::get_api_name 'QuantumODI', 'huge'
8
+ }.to raise_error(AwsPricing::UnknownTypeError)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,39 @@
1
+ require 'amazon-pricing'
2
+
3
+ describe AwsPricing::PriceList do
4
+ describe "#get_ec2_on_demand_instance_pricing" do
5
+ let(:price_list) {
6
+ {
7
+ 'config' => {
8
+ 'regions' => [{
9
+ 'region' => 'us-east',
10
+ 'instanceTypes' => [{
11
+ 'type' => 'stdODI',
12
+ 'sizes' => [{
13
+ 'size' => 'sm',
14
+ 'valueColumns' => [
15
+ {'name' => 'linux', 'prices' => { 'USD' => '0.065' }},
16
+ {'name' => 'mswin', 'prices' => { 'USD' => '0.265' }}
17
+ ]}]}]}]
18
+ }
19
+ }
20
+ }
21
+
22
+ before do
23
+ AwsPricing::PriceList.any_instance.tap do |it|
24
+ it.stub :get_ec2_reserved_instance_pricing
25
+ it.stub :fetch_ec2_ebs_pricing
26
+ it.stub :fetch_url => price_list
27
+ end
28
+ end
29
+
30
+ it "doesn't break when amazon adds instance types" do
31
+ newType = {'type' => 'quantumODI', 'sizes' => [{'size' => 'sm', 'valueColumns' => []}]}
32
+ price_list['config']['regions'][0]['instanceTypes'] << newType
33
+
34
+ expect {
35
+ AwsPricing::PriceList.new
36
+ }.to_not raise_error
37
+ end
38
+ end
39
+ end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # IBM SmartCloud Ruby library
2
+ # Amazon Web Services Pricing Ruby library
3
3
  #
4
4
  # Ruby Gem Name:: amazon-pricing
5
5
  # Author:: Joe Kinsella (mailto:joe.kinsella@gmail.com)
@@ -10,8 +10,8 @@
10
10
  # Home:: http://github.com/sonian/amazon-pricing
11
11
  #++
12
12
 
13
- puts Dir.pwd
14
- require File.join(Dir.pwd, 'test', 'helper')
13
+ $: << File.expand_path(File.dirname(__FILE__))
14
+ require 'helper'
15
15
  require 'test/unit'
16
16
 
17
17
  class TestEc2InstanceTypes < Test::Unit::TestCase
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amazon-pricing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-02 00:00:00.000000000 Z
12
+ date: 2012-11-03 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A Ruby library for retrieving pricing for Amazon Web Services
15
15
  email:
@@ -21,6 +21,7 @@ extra_rdoc_files:
21
21
  - README
22
22
  files:
23
23
  - CHANGELOG
24
+ - Gemfile
24
25
  - LICENSE
25
26
  - README
26
27
  - Rakefile
@@ -31,6 +32,8 @@ files:
31
32
  - lib/amazon-pricing/region.rb
32
33
  - lib/amazon-pricing/reserved-instance-type.rb
33
34
  - lib/amazon-pricing/version.rb
35
+ - spec/instance_type_spec.rb
36
+ - spec/price_list_spec.rb
34
37
  - test/helper.rb
35
38
  - test/test-ec2-instance-types.rb
36
39
  homepage: http://github.com/sonian/amazon-pricing
@@ -63,5 +66,7 @@ signing_key:
63
66
  specification_version: 3
64
67
  summary: Amazon Web Services Pricing Ruby gem
65
68
  test_files:
69
+ - spec/instance_type_spec.rb
70
+ - spec/price_list_spec.rb
66
71
  - test/helper.rb
67
72
  - test/test-ec2-instance-types.rb