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

93 lines
2.1 KiB
Ruby
Raw Normal View History

module Fog
module Slicehost
2010-06-12 18:31:17 -04:00
extend Fog::Service
requires :slicehost_password
model_path 'fog/slicehost/models'
model 'flavor'
model 'flavors'
model 'image'
model 'images'
model 'server'
model 'servers'
request_path 'fog/slicehost/requests'
request 'create_slice'
request 'delete_slice'
request 'get_backups'
request 'get_flavor'
request 'get_flavors'
request 'get_image'
request 'get_images'
request 'get_slice'
request 'get_slices'
request 'reboot_slice'
class Mock
2010-06-12 18:31:17 -04:00
include Collections
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {}
end
end
def self.reset_data(keys=data.keys)
for key in [*keys]
data.delete(key)
end
end
def initialize(options={})
@slicehost_password = options[:slicehost_password]
@data = self.class.data[@slicehost_password]
end
end
class Real
2010-06-12 18:31:17 -04:00
include Collections
def initialize(options={})
@slicehost_password = options[:slicehost_password]
@host = options[:host] || "api.slicehost.com"
@port = options[:port] || 443
@scheme = options[:scheme] || 'https'
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
end
def reload
@connection.reset
2009-11-28 16:16:00 -05:00
end
def request(params)
2010-06-02 11:31:33 -04:00
params[:headers] ||= {}
params[:headers].merge!({
'Authorization' => "Basic #{Base64.encode64(@slicehost_password).delete("\r\n")}"
})
case params[:method]
when 'DELETE', 'GET', 'HEAD'
2010-06-02 11:31:33 -04:00
params[:headers]['Accept'] = 'application/xml'
when 'POST', 'PUT'
2010-06-02 11:31:33 -04:00
params[:headers]['Content-Type'] = 'application/xml'
end
2010-06-01 00:39:17 -04:00
begin
response = @connection.request(params.merge!({:host => @host}))
2010-06-01 00:39:17 -04:00
rescue Excon::Errors::Error => error
raise case error
2010-06-01 00:39:17 -04:00
when Excon::Errors::NotFound
Fog::Slicehost::NotFound.slurp(error)
2010-06-01 00:39:17 -04:00
else
error
2010-06-01 00:39:17 -04:00
end
end
response
end
end
end
end