2009-11-27 05:04:49 +00:00
|
|
|
module Fog
|
2010-03-18 03:51:55 +00:00
|
|
|
module Slicehost
|
2010-06-12 22:31:17 +00:00
|
|
|
extend Fog::Service
|
|
|
|
|
|
|
|
requires :slicehost_password
|
|
|
|
|
|
|
|
model_path 'fog/slicehost/models'
|
2010-09-02 23:01:19 +00:00
|
|
|
model :flavor
|
|
|
|
collection :flavors
|
|
|
|
model :image
|
|
|
|
collection :images
|
|
|
|
model :server
|
|
|
|
collection :servers
|
2010-06-12 22:31:17 +00:00
|
|
|
|
|
|
|
request_path 'fog/slicehost/requests'
|
2010-09-02 23:01:19 +00:00
|
|
|
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
|
2009-11-27 05:04:49 +00:00
|
|
|
|
2010-03-18 03:51:55 +00:00
|
|
|
class Mock
|
2010-06-12 22:31:17 +00:00
|
|
|
include Collections
|
2010-03-18 03:51:55 +00:00
|
|
|
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
|
|
|
hash[key] = {}
|
|
|
|
end
|
2009-11-27 05:04:49 +00:00
|
|
|
end
|
|
|
|
|
2010-03-18 03:51:55 +00:00
|
|
|
def self.reset_data(keys=data.keys)
|
|
|
|
for key in [*keys]
|
|
|
|
data.delete(key)
|
|
|
|
end
|
2010-01-23 04:50:59 +00:00
|
|
|
end
|
2010-03-18 03:51:55 +00:00
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
@slicehost_password = options[:slicehost_password]
|
|
|
|
@data = self.class.data[@slicehost_password]
|
|
|
|
end
|
|
|
|
|
2009-11-27 05:04:49 +00:00
|
|
|
end
|
|
|
|
|
2010-03-18 03:51:55 +00:00
|
|
|
class Real
|
2010-06-12 22:31:17 +00:00
|
|
|
include Collections
|
2010-03-18 03:51:55 +00:00
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
@slicehost_password = options[:slicehost_password]
|
|
|
|
@host = options[:host] || "api.slicehost.com"
|
|
|
|
@port = options[:port] || 443
|
|
|
|
@scheme = options[:scheme] || 'https'
|
2010-06-20 01:56:38 +00:00
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
2009-11-28 21:16:00 +00:00
|
|
|
end
|
|
|
|
|
2010-03-18 03:51:55 +00:00
|
|
|
def request(params)
|
2010-06-02 15:31:33 +00:00
|
|
|
params[:headers] ||= {}
|
|
|
|
params[:headers].merge!({
|
|
|
|
'Authorization' => "Basic #{Base64.encode64(@slicehost_password).delete("\r\n")}"
|
|
|
|
})
|
2010-03-18 03:51:55 +00:00
|
|
|
case params[:method]
|
|
|
|
when 'DELETE', 'GET', 'HEAD'
|
2010-06-02 15:31:33 +00:00
|
|
|
params[:headers]['Accept'] = 'application/xml'
|
2010-03-18 03:51:55 +00:00
|
|
|
when 'POST', 'PUT'
|
2010-06-02 15:31:33 +00:00
|
|
|
params[:headers]['Content-Type'] = 'application/xml'
|
2010-03-18 03:51:55 +00:00
|
|
|
end
|
2010-02-04 08:27:14 +00:00
|
|
|
|
2010-06-01 04:39:17 +00:00
|
|
|
begin
|
2010-06-05 21:25:30 +00:00
|
|
|
response = @connection.request(params.merge!({:host => @host}))
|
2010-06-01 04:39:17 +00:00
|
|
|
rescue Excon::Errors::Error => error
|
2010-06-10 00:53:54 +00:00
|
|
|
raise case error
|
2010-06-01 04:39:17 +00:00
|
|
|
when Excon::Errors::NotFound
|
2010-06-12 06:11:02 +00:00
|
|
|
Fog::Slicehost::NotFound.slurp(error)
|
2010-06-01 04:39:17 +00:00
|
|
|
else
|
2010-06-10 00:53:54 +00:00
|
|
|
error
|
2010-06-01 04:39:17 +00:00
|
|
|
end
|
|
|
|
end
|
2009-11-27 05:04:49 +00:00
|
|
|
|
2010-03-18 03:51:55 +00:00
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-11-27 05:04:49 +00:00
|
|
|
end
|
|
|
|
end
|