From 4099ba9310e6a280dc8b7c467fc1ea4b6e480858 Mon Sep 17 00:00:00 2001 From: Wesley Beary Date: Sat, 28 Nov 2009 13:16:00 -0800 Subject: [PATCH] create/delete slices for slicehost --- lib/fog/slicehost.rb | 15 +++++- lib/fog/slicehost/parsers/create_slice.rb | 29 ++++++++++++ lib/fog/slicehost/parsers/get_backups.rb | 2 +- lib/fog/slicehost/parsers/get_slices.rb | 4 +- lib/fog/slicehost/requests/create_slice.rb | 50 ++++++++++++++++++++ lib/fog/slicehost/requests/delete_slice.rb | 48 +++++++++++++++++++ lib/fog/slicehost/requests/get_backups.rb | 2 +- lib/fog/slicehost/requests/get_slices.rb | 8 ++-- spec/slicehost/requests/create_slice_spec.rb | 29 ++++++++++++ spec/slicehost/requests/delete_slice_spec.rb | 18 +++++++ spec/slicehost/requests/get_backups_spec.rb | 2 +- spec/slicehost/requests/get_slices_spec.rb | 10 ++-- 12 files changed, 203 insertions(+), 14 deletions(-) create mode 100644 lib/fog/slicehost/parsers/create_slice.rb create mode 100644 lib/fog/slicehost/requests/create_slice.rb create mode 100644 lib/fog/slicehost/requests/delete_slice.rb create mode 100644 spec/slicehost/requests/create_slice_spec.rb create mode 100644 spec/slicehost/requests/delete_slice_spec.rb diff --git a/lib/fog/slicehost.rb b/lib/fog/slicehost.rb index 1914626ad..a8ade97af 100644 --- a/lib/fog/slicehost.rb +++ b/lib/fog/slicehost.rb @@ -11,11 +11,14 @@ module Fog end def self.reload + 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_slices.rb" + 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_flavors.rb" load "fog/slicehost/requests/get_images.rb" @@ -35,10 +38,20 @@ module Fog end def request(params) + headers = { + 'Authorization' => "Basic #{Base64.encode64(@password).gsub("\n",'')}" + } + case params[:method] + when 'DELETE', 'GET', 'HEAD' + headers['Accept'] = 'application/xml' + when 'POST', 'PUT' + headers['Content-Type'] = 'application/xml' + end + response = @connection.request({ :body => params[:body], :expects => params[:expects], - :headers => { 'Authorization' => "Basic #{Base64.encode64(@password).gsub("\n",'')}"}, + :headers => headers, :host => @host, :method => params[:method], :parser => params[:parser], diff --git a/lib/fog/slicehost/parsers/create_slice.rb b/lib/fog/slicehost/parsers/create_slice.rb new file mode 100644 index 000000000..88df466b3 --- /dev/null +++ b/lib/fog/slicehost/parsers/create_slice.rb @@ -0,0 +1,29 @@ +module Fog + module Parsers + module Slicehost + + class CreateSlice < 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', 'root-password', 'status' + @response[name] = @value + end + end + + end + + end + end +end diff --git a/lib/fog/slicehost/parsers/get_backups.rb b/lib/fog/slicehost/parsers/get_backups.rb index 69329f88b..b8a207788 100644 --- a/lib/fog/slicehost/parsers/get_backups.rb +++ b/lib/fog/slicehost/parsers/get_backups.rb @@ -16,7 +16,7 @@ module Fog @backup = {} when 'date' @backup[name] = Time.parse(@value) - when 'id', 'slice_id' + when 'id', 'slice-id' @backup[name] = @value.to_i when 'name' @backup[name] = @value diff --git a/lib/fog/slicehost/parsers/get_slices.rb b/lib/fog/slicehost/parsers/get_slices.rb index 73839aa1f..e753e38b7 100644 --- a/lib/fog/slicehost/parsers/get_slices.rb +++ b/lib/fog/slicehost/parsers/get_slices.rb @@ -14,8 +14,10 @@ module Fog when 'address' @slice['addresses'] ||= [] @slice['addresses'] << @value - when 'backup_id', 'bw_in', 'bw_out', 'flavor_id', 'id', 'image_id', 'progress' + when 'backup-id', 'flavor-id', 'id', 'image-id', 'progress' @slice[name] = @value.to_i + when 'bw-in', 'bw-out' + @slice[name] = @value.to_f when 'name', 'status' @slice[name] = @value when 'slice' diff --git a/lib/fog/slicehost/requests/create_slice.rb b/lib/fog/slicehost/requests/create_slice.rb new file mode 100644 index 000000000..6dbbb7897 --- /dev/null +++ b/lib/fog/slicehost/requests/create_slice.rb @@ -0,0 +1,50 @@ +unless Fog.mocking? + + module Fog + class Slicehost + + # Get list of slices + # ==== Parameters + # * flavor_id<~Integer> - Id of flavor to create slice with + # * image_id<~Integer> - Id of image to create slice with + # * name<~String> - Name of slice + # + # ==== 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 + # * 'root-password'<~String> - Root password of slice + # * 'status'<~String> - Current status of the slice + def create_slice(flavor_id, image_id, name) + request( + :body => %Q{#{flavor_id}#{image_id}#{name}}, + :expects => 201, + :method => 'POST', + :parser => Fog::Parsers::Slicehost::CreateSlice.new, + :path => 'slices.xml' + ) + end + + end + end + +else + + module Fog + class Slicehost + + def get_slices + end + + end + end + +end diff --git a/lib/fog/slicehost/requests/delete_slice.rb b/lib/fog/slicehost/requests/delete_slice.rb new file mode 100644 index 000000000..1b52b65c6 --- /dev/null +++ b/lib/fog/slicehost/requests/delete_slice.rb @@ -0,0 +1,48 @@ +unless Fog.mocking? + + module Fog + class Slicehost + + # Get list of slices + # ==== Parameters + # * flavor_id<~Integer> - Id of flavor to create slice with + # * image_id<~Integer> - Id of image to create slice with + # * name<~String> - Name of slice + # + # ==== 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 + # * 'root-password'<~String> - Root password of slice + # * 'status'<~String> - Current status of the slice + def delete_slice(slice_id) + request( + :expects => 200, + :method => 'DELETE', + :path => "slices/#{slice_id}.xml" + ) + end + + end + end + +else + + module Fog + class Slicehost + + def get_slices + end + + end + end + +end diff --git a/lib/fog/slicehost/requests/get_backups.rb b/lib/fog/slicehost/requests/get_backups.rb index 6c9e207f2..6522d521c 100644 --- a/lib/fog/slicehost/requests/get_backups.rb +++ b/lib/fog/slicehost/requests/get_backups.rb @@ -11,7 +11,7 @@ unless Fog.mocking? # * '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 + # * 'slice-id'<~Integer> - Id of slice the backup was made from def get_backups request( :expects => 200, diff --git a/lib/fog/slicehost/requests/get_slices.rb b/lib/fog/slicehost/requests/get_slices.rb index af5372641..e67018137 100644 --- a/lib/fog/slicehost/requests/get_slices.rb +++ b/lib/fog/slicehost/requests/get_slices.rb @@ -9,12 +9,12 @@ unless Fog.mocking? # * 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 + # * '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 + # * '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 diff --git a/spec/slicehost/requests/create_slice_spec.rb b/spec/slicehost/requests/create_slice_spec.rb new file mode 100644 index 000000000..d9864e04e --- /dev/null +++ b/spec/slicehost/requests/create_slice_spec.rb @@ -0,0 +1,29 @@ +require File.dirname(__FILE__) + '/../../spec_helper' + +describe 'Slicehost.create_slice' do + describe 'success' do + + after(:each) do + eventually(128) do + slicehost.delete_slice(@slice_id) + end + end + + it "should return proper attributes" do + # flavor_id 1: 256 ram, image_id 3: Gentoo 2008.0 + actual = slicehost.create_slice(1, 3, 'fogslice').body + actual['addresses'].should be_a(Array) + # actual['backup-id'].should be_an(Integer) + actual['bw-in'].should be_an(Float) + actual['bw-out'].should be_an(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['root-password'].should be_a(String) + actual['status'].should be_an(String) + end + + end +end diff --git a/spec/slicehost/requests/delete_slice_spec.rb b/spec/slicehost/requests/delete_slice_spec.rb new file mode 100644 index 000000000..dad02284f --- /dev/null +++ b/spec/slicehost/requests/delete_slice_spec.rb @@ -0,0 +1,18 @@ +require File.dirname(__FILE__) + '/../../spec_helper' + +describe 'Slicehost.delete_slice' do + describe 'success' do + + before(:each) do + # flavor_id 1: 256 ram, image_id 3: Gentoo 2008.0 + @slice_id = slicehost.create_slice(1, 3, 'fog_slice').body['id'] + end + + it "should return proper attributes" do + eventually(128) do + actual = slicehost.delete_slice(@slice_id) + end + end + + end +end diff --git a/spec/slicehost/requests/get_backups_spec.rb b/spec/slicehost/requests/get_backups_spec.rb index 58228c4a2..64d899826 100644 --- a/spec/slicehost/requests/get_backups_spec.rb +++ b/spec/slicehost/requests/get_backups_spec.rb @@ -10,7 +10,7 @@ describe 'Slicehost.get_backups' do # 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) + # backup['slice-id'].should be_an(Integer) end end diff --git a/spec/slicehost/requests/get_slices_spec.rb b/spec/slicehost/requests/get_slices_spec.rb index 9d9c6983d..f9c5ee6c6 100644 --- a/spec/slicehost/requests/get_slices_spec.rb +++ b/spec/slicehost/requests/get_slices_spec.rb @@ -8,12 +8,12 @@ describe 'Slicehost.get_slices' do 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['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['image-id'].should be_an(Integer) # slice['name'].should be_an(String) # slice['progress'].should be_an(Integer) # slice['status'].should be_an(String)