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

103 lines
2.4 KiB
Ruby
Raw Normal View History

require 'fog/serverlove/core'
2012-04-27 06:26:16 -04:00
module Fog
module Compute
class Serverlove < Fog::Service
2013-06-07 09:18:25 -04:00
2012-04-27 09:35:08 -04:00
API_HOST = "api.z1-man.serverlove.com"
2013-06-07 09:18:25 -04:00
2012-04-27 07:11:33 -04:00
requires :serverlove_uuid, :serverlove_api_key
2013-06-07 09:18:25 -04:00
2012-04-27 07:11:33 -04:00
recognizes :serverlove_api_url
2013-06-07 09:18:25 -04:00
2012-04-27 07:11:33 -04:00
request_path 'fog/serverlove/requests/compute'
2013-06-07 09:18:25 -04:00
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
2013-06-07 09:18:25 -04:00
2012-06-22 06:06:36 -04:00
model_path 'fog/serverlove/models/compute'
2013-06-07 09:18:25 -04:00
model :image
collection :images
2013-06-07 09:18:25 -04:00
model :server
collection :servers
2012-04-27 07:11:33 -04:00
class Mock
2013-06-07 09:18:25 -04:00
2012-04-27 07:11:33 -04:00
def initialize(options)
@serverlove_uuid = options[:serverlove_uuid]
@serverlove_api_key = options[:serverlove_api_key]
2012-04-27 07:11:33 -04:00
end
2013-06-07 09:18:25 -04:00
2012-04-27 07:11:33 -04:00
def request(options)
raise "Not implemented"
end
2013-06-07 09:18:25 -04:00
2012-04-27 07:11:33 -04:00
end
2013-06-07 09:18:25 -04:00
2012-04-27 07:11:33 -04:00
class Real
2013-06-07 09:18:25 -04:00
2012-04-27 07:11:33 -04:00
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
2013-06-07 09:18:25 -04:00
@connection = Fog::XML::Connection.new("https://#{@api_uuid}:#{@api_key}@#{@api_host}")
2012-04-27 07:11:33 -04:00
end
2013-06-07 09:18:25 -04:00
2012-04-27 07:11:33 -04:00
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
)
unless params[:options].nil?
params[:body] = encode_pairs(params[:options])
params.delete(:options)
end
2012-04-27 07:11:33 -04:00
response = @connection.request(params)
2013-06-07 09:18:25 -04:00
2012-04-27 07:11:33 -04:00
raise_if_error!(response)
2013-06-07 09:18:25 -04:00
2012-04-27 10:17:56 -04:00
response.body = Fog::JSON.decode(response.body) if response.body && response.body.length > 0
2013-06-07 09:18:25 -04:00
2012-04-27 07:11:33 -04:00
response
end
2013-06-07 09:18:25 -04:00
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
2013-06-07 09:18:25 -04:00
when 400 then
2012-04-27 07:11:33 -04:00
raise 'omg'
end
end
2013-06-07 09:18:25 -04:00
2012-04-27 07:11:33 -04:00
end
2013-06-07 09:18:25 -04:00
2012-04-27 06:26:16 -04:00
end
end
end