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

first pass at slicehost support (methods for querying existing stuff)

This commit is contained in:
Wesley Beary 2009-11-26 21:04:49 -08:00
parent cc1e573bdb
commit f77cd66ea6
16 changed files with 415 additions and 11 deletions

View file

@ -105,12 +105,11 @@ namespace :fog do
yml = <<YML yml = <<YML
:default: :default:
:aws: :aws_access_key_id: INTENTIONALLY_LEFT_BLANK
:aws_access_key_id: INTENTIONALLY_LEFT_BLANK :aws_secret_access_key: INTENTIONALLY_LEFT_BLANK
:aws_secret_access_key: INTENTIONALLY_LEFT_BLANK :rackspace_api_key: INTENTIONALLY_LEFT_BLANK
:rackspace: :rackspace_username: INTENTIONALLY_LEFT_BLANK
:rackspace_api_key: INTENTIONALLY_LEFT_BLANK :slicehost_password: INTENTIONALLY_LEFT_BLANK
:rackspace_username: INTENTIONALLY_LEFT_BLANK
YML YML
print(yml) print(yml)

View file

@ -35,6 +35,7 @@ module Fog
load "fog/aws.rb" load "fog/aws.rb"
load "fog/rackspace.rb" load "fog/rackspace.rb"
load "fog/slicehost.rb"
end end
def self.credentials(path = File.expand_path('~/.fog')) def self.credentials(path = File.expand_path('~/.fog'))

53
lib/fog/slicehost.rb Normal file
View file

@ -0,0 +1,53 @@
module Fog
class Slicehost
if Fog.mocking?
def self.data
@data
end
def self.reset_data
@data = {}
end
end
def self.reload
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_slices.rb"
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_slices.rb"
if Fog.mocking?
reset_data
end
end
def initialize(options={})
@password = options[:password]
@host = options[:host] || "api.slicehost.com"
@port = options[:port] || 443
@scheme = options[:scheme] || 'https'
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}")
end
def request(params)
response = @connection.request({
:body => params[:body],
:expects => params[:expects],
:headers => { 'Authorization' => "Basic #{Base64.encode64(@password).gsub("\n",'')}"},
:host => @host,
:method => params[:method],
:parser => params[:parser],
:path => params[:path]
})
response
end
end
end
Fog::Slicehost.reload

View file

@ -0,0 +1,30 @@
module Fog
module Parsers
module Slicehost
class GetBackups < Fog::Parsers::Base
def reset
@backup = {}
@response = { 'backups' => [] }
end
def end_element(name)
case name
when 'backup'
@response['backups'] << @backup
@backup = {}
when 'date'
@backup[name] = Time.parse(@value)
when 'id', 'slice_id'
@backup[name] = @value.to_i
when 'name'
@backup[name] = @value
end
end
end
end
end
end

View file

@ -0,0 +1,28 @@
module Fog
module Parsers
module Slicehost
class GetFlavors < Fog::Parsers::Base
def reset
@flavor = {}
@response = { 'flavors' => [] }
end
def end_element(name)
case name
when 'flavor'
@response['flavors'] << @flavor
@flavor = {}
when 'id', 'price', 'ram'
@flavor[name] = @value.to_i
when 'name'
@flavor[name] = @value
end
end
end
end
end
end

View file

@ -0,0 +1,28 @@
module Fog
module Parsers
module Slicehost
class GetImages < Fog::Parsers::Base
def reset
@image = {}
@response = { 'images' => [] }
end
def end_element(name)
case name
when 'id'
@image[name] = @value.to_i
when 'image'
@response['images'] << @image
@image = {}
when 'name'
@image[name] = @value
end
end
end
end
end
end

View file

@ -0,0 +1,31 @@
module Fog
module Parsers
module Slicehost
class GetSlices < Fog::Parsers::Base
def reset
@slice = {}
@response = { 'slices' => [] }
end
def end_element(name)
case name
when 'address'
@slice['addresses'] ||= []
@slice['addresses'] << @value
when 'backup_id', 'bw_in', 'bw_out', 'flavor_id', 'id', 'image_id', 'progress'
@slice[name] = @value.to_i
when 'name', 'status'
@slice[name] = @value
when 'slice'
@response['slices'] << @slice
@slice = {}
end
end
end
end
end
end

View file

@ -0,0 +1,38 @@
unless Fog.mocking?
module Fog
class Slicehost
# Get list of backups
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# * 'date'<~Time> - Timestamp of backup creation
# * 'id'<~Integer> - Id of the backup
# * 'name'<~String> - Name of the backup
# * 'slice_id'<~Integer> - Id of slice the backup was made from
def get_backups
request(
:expects => 200,
:method => 'GET',
:parser => Fog::Parsers::Slicehost::GetBackups.new,
:path => 'backups.xml'
)
end
end
end
else
module Fog
class Slicehost
def get_backups
end
end
end
end

View file

@ -0,0 +1,38 @@
unless Fog.mocking?
module Fog
class Slicehost
# Get list of flavors
#
# ==== 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_flavors
request(
:expects => 200,
:method => 'GET',
:parser => Fog::Parsers::Slicehost::GetFlavors.new,
:path => 'flavors.xml'
)
end
end
end
else
module Fog
class Slicehost
def get_flavors
end
end
end
end

View file

@ -0,0 +1,36 @@
unless Fog.mocking?
module Fog
class Slicehost
# Get list of images
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# * 'id'<~Integer> - Id of the image
# * 'name'<~String> - Name of the image
def get_images
request(
:expects => 200,
:method => 'GET',
:parser => Fog::Parsers::Slicehost::GetImages.new,
:path => 'images.xml'
)
end
end
end
else
module Fog
class Slicehost
def get_images
end
end
end
end

View file

@ -0,0 +1,44 @@
unless Fog.mocking?
module Fog
class Slicehost
# Get list of slices
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# * 'addresses'<~Array> - Ip addresses for the slice
# * 'backup_id'<~Integer> - Id of backup slice was booted from
# * 'bw_in'<~Integer> - Incoming bandwidth total for current billing cycle, in Gigabytes
# * 'bw_out'<~Integer> - 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_slices
request(
:expects => 200,
:method => 'GET',
:parser => Fog::Parsers::Slicehost::GetSlices.new,
:path => 'slices.xml'
)
end
end
end
else
module Fog
class Slicehost
def get_slices
end
end
end
end

View file

@ -0,0 +1,17 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe 'Slicehost.get_backups' do
describe 'success' do
it "should return proper attributes" do
actual = slicehost.get_backups.body
actual['backups'].should be_an(Array)
backup = actual['backups'].first
# backup['date'].should be_a(String)
# backup['id'].should be_an(Integer)
# backup['name'].should be_an(String)
# backup['slice_id'].should be_an(Integer)
end
end
end

View file

@ -0,0 +1,17 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe 'Slicehost.get_flavors' do
describe 'success' do
it "should return proper attributes" do
actual = slicehost.get_flavors.body
actual['flavors'].should be_an(Array)
flavor = actual['flavors'].first
flavor['id'].should be_an(Integer)
flavor['name'].should be_an(String)
flavor['price'].should be_a(Integer)
flavor['ram'].should be_an(Integer)
end
end
end

View file

@ -0,0 +1,15 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe 'Slicehost.get_images' do
describe 'success' do
it "should return proper attributes" do
actual = slicehost.get_images.body
actual['images'].should be_an(Array)
image = actual['images'].first
image['id'].should be_an(Integer)
image['name'].should be_a(String)
end
end
end

View file

@ -0,0 +1,23 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe 'Slicehost.get_slices' do
describe 'success' do
it "should return proper attributes" do
actual = slicehost.get_slices.body
actual['slices'].should be_an(Array)
slice = actual['slices'].first
# 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)
end
end
end

View file

@ -3,7 +3,7 @@ require 'open-uri'
current_directory = File.dirname(__FILE__) current_directory = File.dirname(__FILE__)
require "#{current_directory}/../lib/fog" require "#{current_directory}/../lib/fog"
Fog.mock! # Fog.mock!
def ec2 def ec2
Fog::AWS::EC2.new( Fog::AWS::EC2.new(
@ -34,6 +34,13 @@ def sdb
) )
end end
def s3
Fog::AWS::S3.new(
:aws_access_key_id => Fog.credentials[:aws_access_key_id],
:aws_secret_access_key => Fog.credentials[:aws_secret_access_key]
)
end
def servers def servers
Fog::Rackspace::Servers.new( Fog::Rackspace::Servers.new(
:rackspace_api_key => Fog.credentials[:rackspace_api_key], :rackspace_api_key => Fog.credentials[:rackspace_api_key],
@ -41,10 +48,9 @@ def servers
) )
end end
def s3 def slicehost
Fog::AWS::S3.new( Fog::Slicehost.new(
:aws_access_key_id => Fog.credentials[:aws_access_key_id], :password => Fog.credentials[:slicehost_password]
:aws_secret_access_key => Fog.credentials[:aws_secret_access_key]
) )
end end