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/serverlove/compute.rb

98 lines
2.6 KiB
Ruby
Raw Normal View History

2012-04-27 06:26:16 -04:00
module Fog
module Compute
class Serverlove < Fog::Service
2012-04-27 09:35:08 -04:00
API_HOST = "api.z1-man.serverlove.com"
2012-04-27 07:11:33 -04:00
requires :serverlove_uuid, :serverlove_api_key
recognizes :serverlove_api_url
request_path 'fog/serverlove/requests/compute'
2012-06-22 06:06:36 -04:00
# Image
2012-04-29 16:38:29 -04:00
request :get_image
request :get_images
request :destroy_image
request :create_image
request :update_image
request :load_standard_image
2012-06-22 06:06:36 -04:00
# Server
request :get_servers
request :get_server
request :destroy_server
request :create_server
request :update_server
request :start_server
request :stop_server
request :shutdown_server
request :reset_server
2012-06-22 06:06:36 -04:00
model_path 'fog/serverlove/models/compute'
2012-04-27 07:11:33 -04:00
model :image
collection :images
2012-04-27 09:43:52 -04:00
model :server
collection :servers
2012-04-27 07:11:33 -04:00
class Mock
def initialize(options)
@serverlove_uuid = options[:serverlove_uuid] || Fog.credentials[:serverlove_uuid]
@serverlove_api_key = options[:serverlove_api_key] || Fog.credentials[:serverlove_api_key]
end
def request(options)
raise "Not implemented"
end
end
class Real
def initialize(options)
@api_uuid = options[:serverlove_uuid] || Fog.credentials[:serverlove_uuid]
@api_key = options[:serverlove_api_key] || Fog.credentials[:serverlove_api_key]
2012-04-27 09:35:08 -04:00
@api_host = options[:serverlove_api_url] || Fog.credentials[:serverlove_api_url] || API_HOST
2012-04-27 07:11:33 -04:00
2012-04-27 09:35:08 -04:00
@connection = Fog::Connection.new("https://#{@api_uuid}:#{@api_key}@#{@api_host}")
2012-04-27 07:11:33 -04:00
end
def request(params)
2012-04-27 09:08:24 -04:00
params = params.merge!(
:headers => {
2012-04-27 09:35:08 -04:00
"Accept" => "application/json"
2012-04-27 09:31:12 -04:00
}
2012-04-27 09:08:24 -04:00
)
params[:body] = encode_pairs(params[:options]) unless params[:options].nil?
2012-04-27 07:11:33 -04:00
response = @connection.request(params)
raise_if_error!(response)
2012-04-27 10:17:56 -04:00
response.body = Fog::JSON.decode(response.body) if response.body && response.body.length > 0
2012-04-27 07:11:33 -04:00
response
end
def encode_pairs(params)
params.keys.collect do |key|
"#{key} #{params[key]}"
end.join("\n")
end
# TODO
2012-04-27 07:11:33 -04:00
def raise_if_error!(response)
case response.status
when 400 then
raise 'omg'
end
end
end
2012-04-27 06:26:16 -04:00
end
end
end