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/bluebox.rb

88 lines
2.1 KiB
Ruby
Raw Normal View History

module Fog
class Bluebox < Fog::Service
2010-06-12 18:31:17 -04:00
requires :bluebox_api_key, :bluebox_customer_id
model_path 'fog/bluebox/models'
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'
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
class Mock
2010-06-12 18:31:17 -04:00
include Collections
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
def initialize(options={})
@bluebox_api_key = options[:bluebox_api_key]
@bluebox_customer_id = options[:bluebox_customer_id]
@host = options[:bluebox_host] || "boxpanel.blueboxgrp.com"
@port = options[:bluebox_port] || 443
@scheme = options[:bluebox_scheme] || 'https'
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
end
def reload
@connection.reset
end
def request(params)
2010-06-02 11:31:33 -04:00
params[:headers] ||= {}
params[:headers].merge!({
'Authorization' => "Basic #{Base64.encode64([@bluebox_customer_id, @bluebox_api_key].join(':')).delete("\r\n")}"
2010-06-02 11:31:33 -04:00
})
2010-06-02 22:56:50 -04:00
begin
response = @connection.request(params.merge!({:host => @host}))
2010-06-02 22:56:50 -04:00
rescue Excon::Errors::Error => error
raise case error
2010-06-02 22:56:50 -04:00
when Excon::Errors::NotFound
Fog::Bluebox::NotFound.slurp(error)
2010-06-02 22:56:50 -04:00
else
error
2010-06-02 22:56:50 -04:00
end
end
unless response.body.empty?
response.body = JSON.parse(response.body)
end
response
end
end
end
end