2011-08-31 16:52:53 -04:00
|
|
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'bluebox'))
|
|
|
|
require 'fog/compute'
|
|
|
|
|
2010-09-07 19:26:01 -04:00
|
|
|
module Fog
|
2011-06-16 19:28:54 -04:00
|
|
|
module Compute
|
|
|
|
class Bluebox < Fog::Service
|
2010-09-07 19:26:01 -04:00
|
|
|
|
2010-12-16 18:31:24 -05:00
|
|
|
requires :bluebox_api_key, :bluebox_customer_id
|
|
|
|
recognizes :bluebox_host, :bluebox_port, :bluebox_scheme, :persistent
|
2010-09-07 19:26:01 -04:00
|
|
|
|
2011-08-24 21:32:51 -04:00
|
|
|
model_path 'fog/bluebox/models/compute'
|
2010-09-07 19:26:01 -04:00
|
|
|
model :flavor
|
|
|
|
collection :flavors
|
|
|
|
model :image
|
|
|
|
collection :images
|
|
|
|
model :server
|
|
|
|
collection :servers
|
2012-02-12 03:02:52 -05:00
|
|
|
model :location
|
|
|
|
collection :locations
|
2010-09-07 19:26:01 -04:00
|
|
|
|
2011-08-24 21:32:51 -04:00
|
|
|
request_path 'fog/bluebox/requests/compute'
|
2010-09-07 19:26:01 -04:00
|
|
|
request :create_block
|
|
|
|
request :destroy_block
|
|
|
|
request :get_block
|
|
|
|
request :get_blocks
|
2012-02-12 03:02:52 -05:00
|
|
|
request :get_location
|
|
|
|
request :get_locations
|
2010-09-07 19:26:01 -04:00
|
|
|
request :get_product
|
|
|
|
request :get_products
|
|
|
|
request :get_template
|
|
|
|
request :get_templates
|
2011-09-07 15:43:12 -04:00
|
|
|
request :create_template
|
|
|
|
request :destroy_template
|
2010-09-07 19:26:01 -04:00
|
|
|
request :reboot_block
|
|
|
|
|
|
|
|
class Mock
|
|
|
|
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
|
|
|
hash[key] = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-17 22:40:17 -04:00
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
|
2010-09-07 19:26:01 -04:00
|
|
|
def initialize(options={})
|
|
|
|
@bluebox_api_key = options[:bluebox_api_key]
|
2011-05-19 18:35:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
self.class.data[@bluebox_api_key]
|
2011-03-10 14:16:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
|
|
|
self.class.data.delete(@bluebox_api_key)
|
2010-09-07 19:26:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
|
|
|
|
def initialize(options={})
|
2011-07-15 22:34:16 -04:00
|
|
|
require 'multi_json'
|
2010-09-07 19:26:01 -04:00
|
|
|
@bluebox_api_key = options[:bluebox_api_key]
|
|
|
|
@bluebox_customer_id = options[:bluebox_customer_id]
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
|
|
|
@host = options[:bluebox_host] || "boxpanel.bluebox.net"
|
|
|
|
@persistent = options[:persistent] || false
|
|
|
|
@port = options[:bluebox_port] || 443
|
|
|
|
@scheme = options[:bluebox_scheme] || 'https'
|
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
|
2010-09-07 19:26:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
def request(params)
|
|
|
|
params[:headers] ||= {}
|
|
|
|
params[:headers].merge!({
|
|
|
|
'Authorization' => "Basic #{Base64.encode64([@bluebox_customer_id, @bluebox_api_key].join(':')).delete("\r\n")}"
|
|
|
|
})
|
|
|
|
|
|
|
|
begin
|
|
|
|
response = @connection.request(params.merge!({:host => @host}))
|
2010-11-29 23:07:47 -05:00
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
2010-09-07 19:26:01 -04:00
|
|
|
raise case error
|
|
|
|
when Excon::Errors::NotFound
|
2011-06-16 19:28:54 -04:00
|
|
|
Fog::Compute::Bluebox::NotFound.slurp(error)
|
2010-09-07 19:26:01 -04:00
|
|
|
else
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
unless response.body.empty?
|
2011-07-20 12:08:11 -04:00
|
|
|
response.body = MultiJson.decode(response.body)
|
2010-09-07 19:26:01 -04:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|