From f77cd66ea600b4e4403e039380d2aced7804941f Mon Sep 17 00:00:00 2001 From: Wesley Beary Date: Thu, 26 Nov 2009 21:04:49 -0800 Subject: [PATCH] first pass at slicehost support (methods for querying existing stuff) --- Rakefile | 11 ++--- lib/fog.rb | 1 + lib/fog/slicehost.rb | 53 +++++++++++++++++++++ lib/fog/slicehost/parsers/get_backups.rb | 30 ++++++++++++ lib/fog/slicehost/parsers/get_flavors.rb | 28 +++++++++++ lib/fog/slicehost/parsers/get_images.rb | 28 +++++++++++ lib/fog/slicehost/parsers/get_slices.rb | 31 ++++++++++++ lib/fog/slicehost/requests/get_backups.rb | 38 +++++++++++++++ lib/fog/slicehost/requests/get_flavors.rb | 38 +++++++++++++++ lib/fog/slicehost/requests/get_images.rb | 36 ++++++++++++++ lib/fog/slicehost/requests/get_slices.rb | 44 +++++++++++++++++ spec/slicehost/requests/get_backups_spec.rb | 17 +++++++ spec/slicehost/requests/get_flavors_spec.rb | 17 +++++++ spec/slicehost/requests/get_images_spec.rb | 15 ++++++ spec/slicehost/requests/get_slices_spec.rb | 23 +++++++++ spec/spec_helper.rb | 16 +++++-- 16 files changed, 415 insertions(+), 11 deletions(-) create mode 100644 lib/fog/slicehost.rb create mode 100644 lib/fog/slicehost/parsers/get_backups.rb create mode 100644 lib/fog/slicehost/parsers/get_flavors.rb create mode 100644 lib/fog/slicehost/parsers/get_images.rb create mode 100644 lib/fog/slicehost/parsers/get_slices.rb create mode 100644 lib/fog/slicehost/requests/get_backups.rb create mode 100644 lib/fog/slicehost/requests/get_flavors.rb create mode 100644 lib/fog/slicehost/requests/get_images.rb create mode 100644 lib/fog/slicehost/requests/get_slices.rb create mode 100644 spec/slicehost/requests/get_backups_spec.rb create mode 100644 spec/slicehost/requests/get_flavors_spec.rb create mode 100644 spec/slicehost/requests/get_images_spec.rb create mode 100644 spec/slicehost/requests/get_slices_spec.rb diff --git a/Rakefile b/Rakefile index 805ff8feb..f9fac0c96 100644 --- a/Rakefile +++ b/Rakefile @@ -105,12 +105,11 @@ namespace :fog do yml = < 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 diff --git a/lib/fog/slicehost/parsers/get_backups.rb b/lib/fog/slicehost/parsers/get_backups.rb new file mode 100644 index 000000000..69329f88b --- /dev/null +++ b/lib/fog/slicehost/parsers/get_backups.rb @@ -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 diff --git a/lib/fog/slicehost/parsers/get_flavors.rb b/lib/fog/slicehost/parsers/get_flavors.rb new file mode 100644 index 000000000..d1dcb8747 --- /dev/null +++ b/lib/fog/slicehost/parsers/get_flavors.rb @@ -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 diff --git a/lib/fog/slicehost/parsers/get_images.rb b/lib/fog/slicehost/parsers/get_images.rb new file mode 100644 index 000000000..9f15451a1 --- /dev/null +++ b/lib/fog/slicehost/parsers/get_images.rb @@ -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 diff --git a/lib/fog/slicehost/parsers/get_slices.rb b/lib/fog/slicehost/parsers/get_slices.rb new file mode 100644 index 000000000..73839aa1f --- /dev/null +++ b/lib/fog/slicehost/parsers/get_slices.rb @@ -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 diff --git a/lib/fog/slicehost/requests/get_backups.rb b/lib/fog/slicehost/requests/get_backups.rb new file mode 100644 index 000000000..6c9e207f2 --- /dev/null +++ b/lib/fog/slicehost/requests/get_backups.rb @@ -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 diff --git a/lib/fog/slicehost/requests/get_flavors.rb b/lib/fog/slicehost/requests/get_flavors.rb new file mode 100644 index 000000000..3ef546aaa --- /dev/null +++ b/lib/fog/slicehost/requests/get_flavors.rb @@ -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 diff --git a/lib/fog/slicehost/requests/get_images.rb b/lib/fog/slicehost/requests/get_images.rb new file mode 100644 index 000000000..eff4f77a1 --- /dev/null +++ b/lib/fog/slicehost/requests/get_images.rb @@ -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 diff --git a/lib/fog/slicehost/requests/get_slices.rb b/lib/fog/slicehost/requests/get_slices.rb new file mode 100644 index 000000000..af5372641 --- /dev/null +++ b/lib/fog/slicehost/requests/get_slices.rb @@ -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 diff --git a/spec/slicehost/requests/get_backups_spec.rb b/spec/slicehost/requests/get_backups_spec.rb new file mode 100644 index 000000000..58228c4a2 --- /dev/null +++ b/spec/slicehost/requests/get_backups_spec.rb @@ -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 diff --git a/spec/slicehost/requests/get_flavors_spec.rb b/spec/slicehost/requests/get_flavors_spec.rb new file mode 100644 index 000000000..cb3ce502a --- /dev/null +++ b/spec/slicehost/requests/get_flavors_spec.rb @@ -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 diff --git a/spec/slicehost/requests/get_images_spec.rb b/spec/slicehost/requests/get_images_spec.rb new file mode 100644 index 000000000..0056730f5 --- /dev/null +++ b/spec/slicehost/requests/get_images_spec.rb @@ -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 diff --git a/spec/slicehost/requests/get_slices_spec.rb b/spec/slicehost/requests/get_slices_spec.rb new file mode 100644 index 000000000..9d9c6983d --- /dev/null +++ b/spec/slicehost/requests/get_slices_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 45a6870af..07cbd7212 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,7 +3,7 @@ require 'open-uri' current_directory = File.dirname(__FILE__) require "#{current_directory}/../lib/fog" -Fog.mock! +# Fog.mock! def ec2 Fog::AWS::EC2.new( @@ -34,6 +34,13 @@ def sdb ) 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 Fog::Rackspace::Servers.new( :rackspace_api_key => Fog.credentials[:rackspace_api_key], @@ -41,10 +48,9 @@ def servers ) 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] +def slicehost + Fog::Slicehost.new( + :password => Fog.credentials[:slicehost_password] ) end