2009-11-27 00:04:49 -05:00
|
|
|
module Fog
|
2010-03-17 23:51:55 -04:00
|
|
|
module Slicehost
|
2009-11-27 00:04:49 -05:00
|
|
|
|
2010-06-01 00:39:17 -04:00
|
|
|
class Error < Fog::Errors::Error; end
|
|
|
|
class NotFound < Fog::Errors::NotFound; end
|
|
|
|
|
2010-03-17 23:51:55 -04:00
|
|
|
def self.new(options={})
|
2010-04-03 03:33:35 -04:00
|
|
|
|
2010-04-13 15:40:46 -04:00
|
|
|
unless @required
|
|
|
|
require 'fog/slicehost/models/flavor'
|
|
|
|
require 'fog/slicehost/models/flavors'
|
|
|
|
require 'fog/slicehost/models/image'
|
|
|
|
require 'fog/slicehost/models/images'
|
|
|
|
require 'fog/slicehost/models/server'
|
|
|
|
require 'fog/slicehost/models/servers'
|
|
|
|
require 'fog/slicehost/requests/create_slice'
|
|
|
|
require 'fog/slicehost/requests/delete_slice'
|
|
|
|
require 'fog/slicehost/requests/get_backups'
|
|
|
|
require 'fog/slicehost/requests/get_flavor'
|
|
|
|
require 'fog/slicehost/requests/get_flavors'
|
|
|
|
require 'fog/slicehost/requests/get_image'
|
|
|
|
require 'fog/slicehost/requests/get_images'
|
|
|
|
require 'fog/slicehost/requests/get_slice'
|
|
|
|
require 'fog/slicehost/requests/get_slices'
|
|
|
|
require 'fog/slicehost/requests/reboot_slice'
|
|
|
|
@required = true
|
|
|
|
end
|
2010-04-03 03:33:35 -04:00
|
|
|
|
2010-03-17 23:51:55 -04:00
|
|
|
unless options[:slicehost_password]
|
|
|
|
raise ArgumentError.new('slicehost_password is required to access slicehost')
|
2009-11-27 00:04:49 -05:00
|
|
|
end
|
2010-03-17 23:51:55 -04:00
|
|
|
if Fog.mocking?
|
|
|
|
Fog::Slicehost::Mock.new(options)
|
|
|
|
else
|
|
|
|
Fog::Slicehost::Real.new(options)
|
2009-11-27 00:04:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-17 23:51:55 -04:00
|
|
|
def self.reset_data(keys=Mock.data.keys)
|
|
|
|
Mock.reset_data(keys)
|
2010-02-12 02:41:40 -05:00
|
|
|
end
|
2009-11-27 00:04:49 -05:00
|
|
|
|
2010-03-17 23:51:55 -04:00
|
|
|
class Mock
|
|
|
|
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
|
|
|
hash[key] = {}
|
|
|
|
end
|
2009-11-27 00:04:49 -05:00
|
|
|
end
|
|
|
|
|
2010-03-17 23:51:55 -04:00
|
|
|
def self.reset_data(keys=data.keys)
|
|
|
|
for key in [*keys]
|
|
|
|
data.delete(key)
|
|
|
|
end
|
2010-01-22 23:50:59 -05:00
|
|
|
end
|
2010-03-17 23:51:55 -04:00
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
@slicehost_password = options[:slicehost_password]
|
|
|
|
@data = self.class.data[@slicehost_password]
|
|
|
|
end
|
|
|
|
|
2009-11-27 00:04:49 -05:00
|
|
|
end
|
|
|
|
|
2010-03-17 23:51:55 -04:00
|
|
|
class Real
|
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
@slicehost_password = options[:slicehost_password]
|
|
|
|
@host = options[:host] || "api.slicehost.com"
|
|
|
|
@port = options[:port] || 443
|
|
|
|
@scheme = options[:scheme] || 'https'
|
2009-11-28 16:16:00 -05:00
|
|
|
end
|
|
|
|
|
2010-03-17 23:51:55 -04:00
|
|
|
def request(params)
|
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}")
|
2010-06-02 11:31:33 -04:00
|
|
|
params[:headers] ||= {}
|
|
|
|
params[:headers].merge!({
|
|
|
|
'Authorization' => "Basic #{Base64.encode64(@slicehost_password).delete("\r\n")}"
|
|
|
|
})
|
2010-03-17 23:51:55 -04:00
|
|
|
case params[:method]
|
|
|
|
when 'DELETE', 'GET', 'HEAD'
|
2010-06-02 11:31:33 -04:00
|
|
|
params[:headers]['Accept'] = 'application/xml'
|
2010-03-17 23:51:55 -04:00
|
|
|
when 'POST', 'PUT'
|
2010-06-02 11:31:33 -04:00
|
|
|
params[:headers]['Content-Type'] = 'application/xml'
|
2010-03-17 23:51:55 -04:00
|
|
|
end
|
2010-02-04 03:27:14 -05:00
|
|
|
|
2010-06-01 00:39:17 -04:00
|
|
|
begin
|
2010-06-05 17:25:30 -04:00
|
|
|
response = @connection.request(params.merge!({:host => @host}))
|
2010-06-01 00:39:17 -04:00
|
|
|
rescue Excon::Errors::Error => error
|
|
|
|
case error
|
|
|
|
when Excon::Errors::NotFound
|
|
|
|
raise Fog::Slicehost::NotFound
|
|
|
|
else
|
|
|
|
raise error
|
|
|
|
end
|
|
|
|
end
|
2009-11-27 00:04:49 -05:00
|
|
|
|
2010-03-17 23:51:55 -04:00
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-11-27 00:04:49 -05:00
|
|
|
end
|
|
|
|
end
|