From ff6fd5c5883223dbd8b002d7a798d89a404261ea Mon Sep 17 00:00:00 2001 From: "geemus (Wesley Beary)" Date: Sat, 6 Feb 2010 15:39:14 -0800 Subject: [PATCH] [slicehost] Flavor(s) and Image(s) models --- lib/fog/slicehost.rb | 8 +++++ lib/fog/slicehost/models/flavor.rb | 41 +++++++++++++++++++++ lib/fog/slicehost/models/flavors.rb | 33 +++++++++++++++++ lib/fog/slicehost/models/image.rb | 13 +++++++ lib/fog/slicehost/models/images.rb | 35 ++++++++++++++++++ lib/fog/slicehost/models/server.rb | 28 +++++++-------- lib/fog/slicehost/parsers/get_flavor.rb | 24 +++++++++++++ lib/fog/slicehost/parsers/get_image.rb | 24 +++++++++++++ lib/fog/slicehost/requests/get_flavor.rb | 42 ++++++++++++++++++++++ lib/fog/slicehost/requests/get_image.rb | 40 +++++++++++++++++++++ lib/fog/slicehost/requests/get_slice.rb | 3 +- spec/slicehost/requests/get_flavor_spec.rb | 24 +++++++++++++ spec/slicehost/requests/get_image_spec.rb | 24 +++++++++++++ 13 files changed, 324 insertions(+), 15 deletions(-) create mode 100644 lib/fog/slicehost/models/flavor.rb create mode 100644 lib/fog/slicehost/models/flavors.rb create mode 100644 lib/fog/slicehost/models/image.rb create mode 100644 lib/fog/slicehost/models/images.rb create mode 100644 lib/fog/slicehost/parsers/get_flavor.rb create mode 100644 lib/fog/slicehost/parsers/get_image.rb create mode 100644 lib/fog/slicehost/requests/get_flavor.rb create mode 100644 lib/fog/slicehost/requests/get_image.rb create mode 100644 spec/slicehost/requests/get_flavor_spec.rb create mode 100644 spec/slicehost/requests/get_image_spec.rb diff --git a/lib/fog/slicehost.rb b/lib/fog/slicehost.rb index 099b814d0..61d5827f9 100644 --- a/lib/fog/slicehost.rb +++ b/lib/fog/slicehost.rb @@ -11,12 +11,18 @@ module Fog end def self.reload + load "fog/slicehost/models/flavor.rb" + load "fog/slicehost/models/flavors.rb" + load "fog/slicehost/models/image.rb" + load "fog/slicehost/models/images.rb" load "fog/slicehost/models/server.rb" load "fog/slicehost/models/servers.rb" load "fog/slicehost/parsers/create_slice.rb" load "fog/slicehost/parsers/get_backups.rb" + load "fog/slicehost/parsers/get_flavor.rb" load "fog/slicehost/parsers/get_flavors.rb" + load "fog/slicehost/parsers/get_image.rb" load "fog/slicehost/parsers/get_images.rb" load "fog/slicehost/parsers/get_slice.rb" load "fog/slicehost/parsers/get_slices.rb" @@ -24,7 +30,9 @@ module Fog load "fog/slicehost/requests/create_slice.rb" load "fog/slicehost/requests/delete_slice.rb" load "fog/slicehost/requests/get_backups.rb" + load "fog/slicehost/requests/get_flavor.rb" load "fog/slicehost/requests/get_flavors.rb" + load "fog/slicehost/requests/get_image.rb" load "fog/slicehost/requests/get_images.rb" load "fog/slicehost/requests/get_slice.rb" load "fog/slicehost/requests/get_slices.rb" diff --git a/lib/fog/slicehost/models/flavor.rb b/lib/fog/slicehost/models/flavor.rb new file mode 100644 index 000000000..811cd5d77 --- /dev/null +++ b/lib/fog/slicehost/models/flavor.rb @@ -0,0 +1,41 @@ +module Fog + class Slicehost + + class Flavor < Fog::Model + + identity :id + + attribute :name + attribute :price + attribute :ram + + def bits + # 64 + raise StandardError.new("Figure me out!?!") + end + + def cores + # # 2 quad-cores >= 2Ghz = 8 cores + # 8 * case ram + # when 256 + # 1/64.0 + # when 512 + # 1/32.0 + # when 1024 + # 1/16.0 + # when 2048 + # 1/8.0 + # when 4096 + # 1/4.0 + # when 8192 + # 1/2.0 + # when 15872 + # 1 + # end + raise StandardError.new("Figure me out!?!") + end + + end + + end +end diff --git a/lib/fog/slicehost/models/flavors.rb b/lib/fog/slicehost/models/flavors.rb new file mode 100644 index 000000000..b519cedd7 --- /dev/null +++ b/lib/fog/slicehost/models/flavors.rb @@ -0,0 +1,33 @@ +module Fog + class Slicehost + + def flavors + Fog::Slicehost::Flavors.new(:connection => self) + end + + class Flavors < Fog::Collection + + model Fog::Slicehost::Flavor + + def all + if @loaded + clear + end + @loaded = true + data = connection.get_flavors.body + for flavor in data['flavors'] + self << new(flavor) + end + self + end + + def get(flavor_id) + connection.get_flavor(flavor_id) + rescue Excon::Errors::Forbidden + nil + end + + end + + end +end diff --git a/lib/fog/slicehost/models/image.rb b/lib/fog/slicehost/models/image.rb new file mode 100644 index 000000000..57c0582dd --- /dev/null +++ b/lib/fog/slicehost/models/image.rb @@ -0,0 +1,13 @@ +module Fog + class Slicehost + + class Image < Fog::Model + + identity :id + + attribute :name + + end + + end +end diff --git a/lib/fog/slicehost/models/images.rb b/lib/fog/slicehost/models/images.rb new file mode 100644 index 000000000..024c50bb9 --- /dev/null +++ b/lib/fog/slicehost/models/images.rb @@ -0,0 +1,35 @@ +module Fog + class Slicehost + + def images(attributes = {}) + Fog::Slicehost::Images.new({ + :connection => self + }.merge!(attributes)) + end + + class Images < Fog::Collection + + model Fog::Slicehost::Image + + def all + if @loaded + clear + end + @loaded = true + data = connection.get_images.body + for image in data['images'] + self << new(image) + end + self + end + + def get(image_id) + connection.get_image(image_id) + rescue Excon::Errors::Forbidden + nil + end + + end + + end +end diff --git a/lib/fog/slicehost/models/server.rb b/lib/fog/slicehost/models/server.rb index 7086f9f0c..97808136f 100644 --- a/lib/fog/slicehost/models/server.rb +++ b/lib/fog/slicehost/models/server.rb @@ -6,13 +6,13 @@ module Fog identity :id attribute :addresses - attribute :backup_id, 'backup-id' - attribute :bw_in, 'bw-in' - attribute :bw_out, 'bw-out' - attribute :flavor_id, 'flavor-id' - attribute :image_id, 'image-id' + attribute :backup_id, 'backup-id' + attribute :bandwidth_in, 'bw-in' + attribute :bandwidth_out, 'bw-out' + attribute :flavor_id, 'flavor-id' + attribute :image_id, 'image-id' attribute :name - attribute :password, 'root-password' + attribute :password, 'root-password' attribute :progress attribute :status @@ -22,15 +22,15 @@ module Fog true end - # def flavor - # requires :flavor_id - # connection.flavors.get(@flavor_id) - # end + def flavor + requires :flavor_id + connection.flavors.get(@flavor_id) + end - # def image - # requires :image_id - # connection.images.get(@image_id) - # end + def image + requires :image_id + connection.images.get(@image_id) + end def ready? @status == 'active' diff --git a/lib/fog/slicehost/parsers/get_flavor.rb b/lib/fog/slicehost/parsers/get_flavor.rb new file mode 100644 index 000000000..7b9c6db7d --- /dev/null +++ b/lib/fog/slicehost/parsers/get_flavor.rb @@ -0,0 +1,24 @@ +module Fog + module Parsers + module Slicehost + + class GetFlavor < Fog::Parsers::Base + + def reset + @response = {} + end + + def end_element(name) + case name + when 'id', 'price', 'ram' + @response[name] = @value.to_i + when 'name' + @response[name] = @value + end + end + + end + + end + end +end diff --git a/lib/fog/slicehost/parsers/get_image.rb b/lib/fog/slicehost/parsers/get_image.rb new file mode 100644 index 000000000..0bb8b263e --- /dev/null +++ b/lib/fog/slicehost/parsers/get_image.rb @@ -0,0 +1,24 @@ +module Fog + module Parsers + module Slicehost + + class GetImage < Fog::Parsers::Base + + def reset + @response = {} + end + + def end_element(name) + case name + when 'id' + @response[name] = @value.to_i + when 'name' + @response[name] = @value + end + end + + end + + end + end +end diff --git a/lib/fog/slicehost/requests/get_flavor.rb b/lib/fog/slicehost/requests/get_flavor.rb new file mode 100644 index 000000000..f3abb1bff --- /dev/null +++ b/lib/fog/slicehost/requests/get_flavor.rb @@ -0,0 +1,42 @@ +unless Fog.mocking? + + module Fog + class Slicehost + + # Get details of a flavor + # + # ==== Parameters + # * flavor_id<~Integer> - Id of flavor to lookup + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Array>: + # * 'id'<~Integer> - Id of the flavor + # * 'name'<~String> - Name of the flavor + # * 'price'<~Integer> - Price in cents + # * 'ram'<~Integer> - Amount of ram for the flavor + def get_flavor(flavor_id) + request( + :expects => 200, + :method => 'GET', + :parser => Fog::Parsers::Slicehost::GetFlavor.new, + :path => "flavors/#{flavor_id}.xml" + ) + end + + end + end + +else + + module Fog + class Slicehost + + def get_flavor(flavor_id) + raise MockNotImplemented.new("Contributions welcome!") + end + + end + end + +end diff --git a/lib/fog/slicehost/requests/get_image.rb b/lib/fog/slicehost/requests/get_image.rb new file mode 100644 index 000000000..3d2b69c8c --- /dev/null +++ b/lib/fog/slicehost/requests/get_image.rb @@ -0,0 +1,40 @@ +unless Fog.mocking? + + module Fog + class Slicehost + + # Get details of an image + # + # ==== Parameters + # * image_id<~Integer> - Id of image to lookup + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Array>: + # * 'id'<~Integer> - Id of the image + # * 'name'<~String> - Name of the image + def get_image(image_id) + request( + :expects => 200, + :method => 'GET', + :parser => Fog::Parsers::Slicehost::GetImage.new, + :path => "images/#{image_id}.xml" + ) + end + + end + end + +else + + module Fog + class Slicehost + + def get_image(image_id) + raise MockNotImplemented.new("Contributions welcome!") + end + + end + end + +end diff --git a/lib/fog/slicehost/requests/get_slice.rb b/lib/fog/slicehost/requests/get_slice.rb index 7a297a31c..bd4e025a6 100644 --- a/lib/fog/slicehost/requests/get_slice.rb +++ b/lib/fog/slicehost/requests/get_slice.rb @@ -3,7 +3,8 @@ unless Fog.mocking? module Fog class Slicehost - # Get details of slice + # Get details of a slice + # # ==== Parameters # * slice_id<~Integer> - Id of slice to lookup # diff --git a/spec/slicehost/requests/get_flavor_spec.rb b/spec/slicehost/requests/get_flavor_spec.rb new file mode 100644 index 000000000..8f63039b2 --- /dev/null +++ b/spec/slicehost/requests/get_flavor_spec.rb @@ -0,0 +1,24 @@ +require File.dirname(__FILE__) + '/../../spec_helper' + +describe 'Slicehost.get_flavor' do + describe 'success' do + + it "should return proper attributes" do + actual = Slicehost[:slices].get_flavor(1).body + actual['id'].should be_an(Integer) + actual['name'].should be_an(String) + actual['price'].should be_a(Integer) + actual['ram'].should be_an(Integer) + end + + end + describe 'failure' do + + it "should raise a Forbidden error if the flavor does not exist" do + lambda { + Slicehost[:slices].get_flavor(0) + }.should raise_error(Excon::Errors::Forbidden) + end + + end +end diff --git a/spec/slicehost/requests/get_image_spec.rb b/spec/slicehost/requests/get_image_spec.rb new file mode 100644 index 000000000..88f233f65 --- /dev/null +++ b/spec/slicehost/requests/get_image_spec.rb @@ -0,0 +1,24 @@ +require File.dirname(__FILE__) + '/../../spec_helper' + +describe 'Slicehost.get_image' do + describe 'success' do + + it "should return proper attributes" do + actual = Slicehost[:slices].get_image.body + actual['id'].should be_an(Integer) + actual['name'].should be_a(String) + end + + end + + describe 'failure' do + + it "should raise a Forbidden error if the flavor does not exist" do + lambda { + Slicehost[:slices].get_image(0) + }.should raise_error(Excon::Errors::Forbidden) + end + + end + +end