1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[slicehost] Flavor(s) and Image(s) models

This commit is contained in:
geemus (Wesley Beary) 2010-02-06 15:39:14 -08:00
parent 9351fb2bdd
commit ff6fd5c588
13 changed files with 324 additions and 15 deletions

View file

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

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,13 @@
module Fog
class Slicehost
class Image < Fog::Model
identity :id
attribute :name
end
end
end

View file

@ -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

View file

@ -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'

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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
#

View file

@ -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

View file

@ -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