cleanup and first pass at slicehost server(s) models

This commit is contained in:
geemus (Wesley Beary) 2010-02-04 00:27:14 -08:00
parent 87da79eae2
commit 5187883bae
9 changed files with 307 additions and 10 deletions

View File

@ -11,10 +11,14 @@ module Fog
end
def self.reload
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_flavors.rb"
load "fog/slicehost/parsers/get_images.rb"
load "fog/slicehost/parsers/get_slice.rb"
load "fog/slicehost/parsers/get_slices.rb"
load "fog/slicehost/requests/create_slice.rb"
@ -22,6 +26,7 @@ module Fog
load "fog/slicehost/requests/get_backups.rb"
load "fog/slicehost/requests/get_flavors.rb"
load "fog/slicehost/requests/get_images.rb"
load "fog/slicehost/requests/get_slice.rb"
load "fog/slicehost/requests/get_slices.rb"
if Fog.mocking?
@ -30,7 +35,7 @@ module Fog
end
def initialize(options={})
unless @password = options[:password]
unless @password = options[:slicehost_password]
raise ArgumentError.new('password is required to access slicehost')
end
@host = options[:host] || "api.slicehost.com"
@ -59,6 +64,7 @@ module Fog
:parser => params[:parser],
:path => params[:path]
})
response
end

View File

@ -0,0 +1,55 @@
module Fog
class Slicehost
class Server < Fog::Model
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 :name
attribute :password, 'root-password'
attribute :progress
attribute :status
def destroy
requires :id
connection.delete_slice(@id)
true
end
# def flavor
# requires :flavor_id
# connection.flavors.get(@flavor_id)
# end
# def image
# requires :image_id
# connection.images.get(@image_id)
# end
def ready?
@status == 'active'
end
# def reboot(type = 'SOFT')
# requires :id
# connection.reboot_server(@id, type)
# true
# end
def save
requires :flavor_id, :image_id, :name
data = connection.create_slice(@flavor_id, @image_id, @name)
merge_attributes(data.body)
true
end
end
end
end

View File

@ -0,0 +1,37 @@
module Fog
class Slicehost
def servers
Fog::Slicehost::Servers.new(:connection => self)
end
class Servers < Fog::Collection
model Fog::Slicehost::Server
def all
if @loaded
clear
end
@loaded = true
data = connection.get_slices.body['slices']
for server in data
self << new(server)
end
self
end
def get(server_id)
if server_id && server = connection.get_slice(server_id).body
new(server)
elsif !server_id
nil
end
rescue Excon::Errors::Forbidden
nil
end
end
end
end

View File

@ -0,0 +1,29 @@
module Fog
module Parsers
module Slicehost
class GetSlice < Fog::Parsers::Base
def reset
@response = {}
end
def end_element(name)
case name
when 'address'
@response['addresses'] ||= []
@response['addresses'] << @value
when 'backup-id', 'flavor-id', 'id', 'image-id', 'progress'
@response[name] = @value.to_i
when 'bw-in', 'bw-out'
@response[name] = @value.to_f
when 'name', 'status'
@response[name] = @value
end
end
end
end
end
end

View File

@ -0,0 +1,45 @@
unless Fog.mocking?
module Fog
class Slicehost
# Get details of slice
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'addresses'<~Array> - Ip addresses for the slice
# * 'backup-id'<~Integer> - Id of backup slice was booted from
# * 'bw-in'<~Float> - Incoming bandwidth total for current billing cycle, in Gigabytes
# * 'bw-out'<~Float> - Outgoing bandwidth total for current billing cycle, in Gigabytes
# * 'flavor_id'<~Integer> - Id of flavor slice was booted from
# * 'id'<~Integer> - Id of the slice
# * 'image-id'<~Integer> - Id of image slice was booted from
# * 'name'<~String> - Name of the slice
# * 'progress'<~Integer> - Progress of current action, in percentage
# * 'status'<~String> - Current status of the slice
def get_slice(id)
request(
:expects => 200,
:method => 'GET',
:parser => Fog::Parsers::Slicehost::GetSlice.new,
:path => "/slices/#{id}.xml"
)
end
end
end
else
module Fog
class Slicehost
def get_slice(id)
raise MockNotImplemented.new("Contributions welcome!")
end
end
end
end

View File

@ -0,0 +1,51 @@
require File.dirname(__FILE__) + '/../../spec_helper'
require File.dirname(__FILE__) + '/../../shared_examples/server_examples'
describe 'Fog::Slicehost::Server' do
it_should_behave_like "Server"
# flavor 1 = 256, image 3 = gentoo 2008.0
subject { @server = @servers.new(:flavor_id => 1, :image_id => 3, :name => Time.now.to_i.to_s) }
before(:each) do
@servers = Slicehost[:slices].servers
end
after(:each) do
if @server && !@server.new_record?
@server.wait_for { ready? }
@server.destroy.should be_true
end
end
describe "#initialize" do
it "should remap attributes from parser" do
server = @servers.new({
'addresses' => 'addresses',
'backup-id' => 'backup_id',
'bw-in' => 'bw_in',
'bw-out' => 'bw_out',
'flavor-id' => 'flavor_id',
'image-id' => 'image_id',
'name' => 'name',
'root-password' => 'password',
'progress' => 'progress',
'status' => 'status'
})
server.addresses.should == 'addresses'
server.backup_id.should == 'backup_id'
server.bw_in.should == 'bw_in'
server.bw_out.should == 'bw_out'
server.flavor_id.should == 'flavor_id'
server.image_id.should == 'image_id'
server.name.should == 'name'
server.password.should == 'password'
server.progress.should == 'progress'
server.status.should == 'status'
end
end
end

View File

@ -0,0 +1,22 @@
require File.dirname(__FILE__) + '/../../spec_helper'
require File.dirname(__FILE__) + '/../../shared_examples/servers_examples'
describe 'Fog::Slicehost::Servers' do
it_should_behave_like "Servers"
# flavor 1 = 256, image 3 = gentoo 2008.0
subject { @server = @servers.new(:flavor_id => 1, :image_id => 3, :name => Time.now.to_i.to_s) }
before(:each) do
@servers = Slicehost[:slices].servers
end
after(:each) do
if @server && !@server.new_record?
@server.wait_for { ready? }
@server.destroy.should be_true
end
end
end

View File

@ -0,0 +1,41 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe 'Slicehost.get_slices' do
describe 'success' do
before(:each) do
# flavor_id 1: 256 ram, image_id 3: Gentoo 2008.0
@slice_id = Slicehost[:slices].create_slice(1, 3, 'fog_create_slice').body['id']
end
after(:each) do
eventually(128) do
Slicehost[:slices].delete_slice(@slice_id)
end
end
it "should return proper attributes" do
actual = Slicehost[:slices].get_slice(@slice_id).body
actual['addresses'].should be_a(Array)
# actual['backup-id'].should be_an(Integer)
actual['bw-in'].should be_a(Float)
actual['bw-out'].should be_a(Float)
actual['flavor-id'].should be_an(Integer)
actual['id'].should be_an(Integer)
actual['image-id'].should be_an(Integer)
actual['name'].should be_an(String)
actual['progress'].should be_an(Integer)
actual['status'].should be_an(String)
end
end
describe 'failure' do
it "should raise a Forbidden error if the server does not exist" do
lambda {
Slicehost[:slices].get_slice(0)
}.should raise_error(Excon::Errors::Forbidden)
end
end
end

View File

@ -3,20 +3,31 @@ require File.dirname(__FILE__) + '/../../spec_helper'
describe 'Slicehost.get_slices' do
describe 'success' do
before(:each) do
# flavor_id 1: 256 ram, image_id 3: Gentoo 2008.0
@slice_id = Slicehost[:slices].create_slice(1, 3, 'fog_create_slice').body['id']
end
after(:each) do
eventually(128) do
Slicehost[:slices].delete_slice(@slice_id)
end
end
it "should return proper attributes" do
actual = Slicehost[:slices].get_slices.body
actual['slices'].should be_an(Array)
slice = actual['slices'].first
# slice['addresses'].should be_a(Array)
slice['addresses'].should be_a(Array)
# slice['backup-id'].should be_an(Integer)
# slice['bw-in'].should be_an(Integer)
# slice['bw-out'].should be_an(Integer)
# slice['flavor-id'].should be_an(Integer)
# slice['id'].should be_an(Integer)
# slice['image-id'].should be_an(Integer)
# slice['name'].should be_an(String)
# slice['progress'].should be_an(Integer)
# slice['status'].should be_an(String)
slice['bw-in'].should be_a(Float)
slice['bw-out'].should be_a(Float)
slice['flavor-id'].should be_an(Integer)
slice['id'].should be_an(Integer)
slice['image-id'].should be_an(Integer)
slice['name'].should be_an(String)
slice['progress'].should be_an(Integer)
slice['status'].should be_an(String)
end
end