2010-05-24 13:29:01 -04:00
|
|
|
module Fog
|
2010-09-03 04:11:45 -04:00
|
|
|
class Bluebox < Fog::Service
|
2010-06-12 18:31:17 -04:00
|
|
|
|
|
|
|
requires :bluebox_api_key, :bluebox_customer_id
|
|
|
|
|
|
|
|
model_path 'fog/bluebox/models'
|
2010-09-02 19:01:19 -04:00
|
|
|
model :flavor
|
|
|
|
collection :flavors
|
|
|
|
model :image
|
|
|
|
collection :images
|
|
|
|
model :server
|
|
|
|
collection :servers
|
2010-06-12 18:31:17 -04:00
|
|
|
|
|
|
|
request_path 'fog/bluebox/requests'
|
2010-09-02 19:01:19 -04:00
|
|
|
request :create_block
|
|
|
|
request :destroy_block
|
|
|
|
request :get_block
|
|
|
|
request :get_blocks
|
|
|
|
request :get_product
|
|
|
|
request :get_products
|
|
|
|
request :get_template
|
|
|
|
request :get_templates
|
|
|
|
request :reboot_block
|
2010-05-24 13:29:01 -04:00
|
|
|
|
|
|
|
class Mock
|
2010-06-12 18:31:17 -04:00
|
|
|
include Collections
|
2010-05-24 13:29:01 -04:00
|
|
|
|
|
|
|
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={})
|
|
|
|
@bluebox_api_key = options[:bluebox_api_key]
|
|
|
|
@data = self.class.data[@bluebox_api_key]
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
2010-06-12 18:31:17 -04:00
|
|
|
include Collections
|
2010-05-24 13:29:01 -04:00
|
|
|
|
|
|
|
def initialize(options={})
|
2010-06-02 12:06:26 -04:00
|
|
|
@bluebox_api_key = options[:bluebox_api_key]
|
|
|
|
@bluebox_customer_id = options[:bluebox_customer_id]
|
2010-05-24 14:03:52 -04:00
|
|
|
@host = options[:bluebox_host] || "boxpanel.blueboxgrp.com"
|
|
|
|
@port = options[:bluebox_port] || 443
|
|
|
|
@scheme = options[:bluebox_scheme] || 'https'
|
2010-06-19 21:56:38 -04:00
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
2010-05-24 13:29:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def request(params)
|
2010-06-02 11:31:33 -04:00
|
|
|
params[:headers] ||= {}
|
|
|
|
params[:headers].merge!({
|
2010-06-02 12:06:26 -04:00
|
|
|
'Authorization' => "Basic #{Base64.encode64([@bluebox_customer_id, @bluebox_api_key].join(':')).delete("\r\n")}"
|
2010-06-02 11:31:33 -04:00
|
|
|
})
|
2010-05-24 13:29:01 -04:00
|
|
|
|
2010-06-02 22:56:50 -04:00
|
|
|
begin
|
2010-06-05 17:25:30 -04:00
|
|
|
response = @connection.request(params.merge!({:host => @host}))
|
2010-06-02 22:56:50 -04:00
|
|
|
rescue Excon::Errors::Error => error
|
2010-06-09 20:53:54 -04:00
|
|
|
raise case error
|
2010-06-02 22:56:50 -04:00
|
|
|
when Excon::Errors::NotFound
|
2010-06-12 02:11:02 -04:00
|
|
|
Fog::Bluebox::NotFound.slurp(error)
|
2010-06-02 22:56:50 -04:00
|
|
|
else
|
2010-06-09 20:53:54 -04:00
|
|
|
error
|
2010-06-02 22:56:50 -04:00
|
|
|
end
|
|
|
|
end
|
2010-06-02 22:38:50 -04:00
|
|
|
unless response.body.empty?
|
|
|
|
response.body = JSON.parse(response.body)
|
|
|
|
end
|
|
|
|
response
|
2010-05-24 13:29:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|