mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
First iteration of BBG Block's support.
This commit is contained in:
parent
7f34e1300a
commit
b9ad8ea418
11 changed files with 394 additions and 0 deletions
|
@ -43,6 +43,7 @@ require 'fog/rackspace'
|
|||
require 'fog/slicehost'
|
||||
require 'fog/terremark'
|
||||
require 'fog/vcloud'
|
||||
require 'fog/bluebox'
|
||||
|
||||
module Fog
|
||||
|
||||
|
|
85
lib/fog/bluebox.rb
Normal file
85
lib/fog/bluebox.rb
Normal file
|
@ -0,0 +1,85 @@
|
|||
module Fog
|
||||
module Bluebox
|
||||
|
||||
def self.new(options={})
|
||||
|
||||
unless @required
|
||||
require 'fog/bluebox/models/templates'
|
||||
require 'fog/bluebox/requests/get_templates'
|
||||
require 'fog/bluebox/models/products'
|
||||
require 'fog/bluebox/requests/get_products'
|
||||
@required = true
|
||||
end
|
||||
|
||||
unless options[:bluebox_api_key]
|
||||
raise ArgumentError.new('bluebox_api_key is required to access Blue Box')
|
||||
end
|
||||
if Fog.mocking?
|
||||
Fog::Bluebox::Mock.new(options)
|
||||
else
|
||||
Fog::Bluebox::Real.new(options)
|
||||
end
|
||||
end
|
||||
|
||||
def self.reset_data(keys=Mock.data.keys)
|
||||
Mock.reset_data(keys)
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
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
|
||||
|
||||
def initialize(options={})
|
||||
@bluebox_api_key = options[:bluebox_api_key]
|
||||
@host = options[:host] || "boxpanel.blueboxgrp.com"
|
||||
@port = options[:port] || 443
|
||||
@scheme = options[:scheme] || 'https'
|
||||
end
|
||||
|
||||
def request(params)
|
||||
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}")
|
||||
headers = {
|
||||
'Authorization' => "Basic #{Base64.encode64(@bluebox_api_key).delete("\r\n")}"
|
||||
}
|
||||
case params[:method]
|
||||
when 'DELETE', 'GET', 'HEAD'
|
||||
headers['Accept'] = 'application/xml'
|
||||
when 'POST', 'PUT'
|
||||
headers['Content-Type'] = 'application/xml'
|
||||
end
|
||||
|
||||
response = @connection.request({
|
||||
:body => params[:body],
|
||||
:expects => params[:expects],
|
||||
:headers => headers.merge!(params[:headers] || {}),
|
||||
:host => @host,
|
||||
:method => params[:method],
|
||||
:parser => params[:parser],
|
||||
:path => params[:path]
|
||||
})
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
42
lib/fog/bluebox/bin.rb
Normal file
42
lib/fog/bluebox/bin.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
module Bluebox
|
||||
class << self
|
||||
if Fog.credentials[:bluebox_api_key]
|
||||
|
||||
def initialized?
|
||||
true
|
||||
end
|
||||
|
||||
def [](service)
|
||||
@@connections ||= Hash.new do |hash, key|
|
||||
credentials = Fog.credentials.reject do |k,v|
|
||||
![:bluebox_api_key].include?(k)
|
||||
end
|
||||
hash[key] = case key
|
||||
when :slices
|
||||
Fog::Bluebox.new(credentials)
|
||||
end
|
||||
end
|
||||
@@connections[service]
|
||||
end
|
||||
|
||||
def flavors
|
||||
self[:slices].flavors
|
||||
end
|
||||
|
||||
def images
|
||||
self[:slices].images
|
||||
end
|
||||
|
||||
def servers
|
||||
self[:slices].servers
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
def initialized?
|
||||
false
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
42
lib/fog/bluebox/models/product.rb
Normal file
42
lib/fog/bluebox/models/product.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
require 'fog/model'
|
||||
|
||||
module Fog
|
||||
module Bluebox
|
||||
|
||||
class Product < Fog::Model
|
||||
|
||||
identity :id
|
||||
|
||||
attribute :name
|
||||
attribute :cost
|
||||
|
||||
def bits
|
||||
# 64
|
||||
raise StandardError.new("Figure me out!?!")
|
||||
end
|
||||
|
||||
def cores
|
||||
# # 2 quad-cores >= 2Ghz = 8 cores
|
||||
# 8 * case ram
|
||||
# when 256
|
||||
# 1/64.0
|
||||
# when 512
|
||||
# 1/32.0
|
||||
# when 1024
|
||||
# 1/16.0
|
||||
# when 2048
|
||||
# 1/8.0
|
||||
# when 4096
|
||||
# 1/4.0
|
||||
# when 8192
|
||||
# 1/2.0
|
||||
# when 15872
|
||||
# 1
|
||||
# end
|
||||
raise StandardError.new("Figure me out!?!")
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
37
lib/fog/bluebox/models/products.rb
Normal file
37
lib/fog/bluebox/models/products.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
require 'fog/collection'
|
||||
require 'fog/bluebox/models/product'
|
||||
|
||||
module Fog
|
||||
module Bluebox
|
||||
|
||||
class Mock
|
||||
def products
|
||||
Fog::Bluebox::Products.new(:connection => self)
|
||||
end
|
||||
end
|
||||
|
||||
class Real
|
||||
def products
|
||||
Fog::Bluebox::Products.new(:connection => self)
|
||||
end
|
||||
end
|
||||
|
||||
class Products < Fog::Collection
|
||||
|
||||
model Fog::Bluebox::Product
|
||||
|
||||
def all
|
||||
data = connection.get_products.body['products']
|
||||
load(data)
|
||||
end
|
||||
|
||||
def get(product_id)
|
||||
connection.get_product(product_id)
|
||||
rescue Excon::Errors::Forbidden
|
||||
nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
17
lib/fog/bluebox/models/template.rb
Normal file
17
lib/fog/bluebox/models/template.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
require 'fog/model'
|
||||
|
||||
module Fog
|
||||
module Bluebox
|
||||
|
||||
class Template < Fog::Model
|
||||
|
||||
identity :id
|
||||
|
||||
attribute :description
|
||||
attribute :public
|
||||
attribute :created_at, 'created'
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
41
lib/fog/bluebox/models/templates.rb
Normal file
41
lib/fog/bluebox/models/templates.rb
Normal file
|
@ -0,0 +1,41 @@
|
|||
require 'fog/collection'
|
||||
require 'fog/bluebox/models/template'
|
||||
|
||||
module Fog
|
||||
module Bluebox
|
||||
|
||||
class Mock
|
||||
def images(attributes = {})
|
||||
Fog::Bluebox::Templates.new({
|
||||
:connection => self
|
||||
}.merge!(attributes))
|
||||
end
|
||||
end
|
||||
|
||||
class Real
|
||||
def images(attributes = {})
|
||||
Fog::Bluebox::Templates.new({
|
||||
:connection => self
|
||||
}.merge!(attributes))
|
||||
end
|
||||
end
|
||||
|
||||
class Templates < Fog::Collection
|
||||
|
||||
model Fog::Bluebox::Template
|
||||
|
||||
def all
|
||||
data = connection.get_templates.body['templates']
|
||||
load(data)
|
||||
end
|
||||
|
||||
def get(template_id)
|
||||
connection.get_image(template_id)
|
||||
rescue Excon::Errors::Forbidden
|
||||
nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
30
lib/fog/bluebox/parsers/get_products.rb
Normal file
30
lib/fog/bluebox/parsers/get_products.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
module Fog
|
||||
module Parsers
|
||||
module Bluebox
|
||||
|
||||
class GetProducts < Fog::Parsers::Base
|
||||
|
||||
def reset
|
||||
@product = {}
|
||||
@response = { 'products' => [] }
|
||||
end
|
||||
|
||||
def end_element(name)
|
||||
case name
|
||||
when 'cost'
|
||||
@product[name] = @value
|
||||
when 'id'
|
||||
@product[name] = @value
|
||||
when 'description'
|
||||
@product[name] = @value
|
||||
when 'record'
|
||||
@response['products'] << @product
|
||||
@product = {}
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
30
lib/fog/bluebox/parsers/get_templates.rb
Normal file
30
lib/fog/bluebox/parsers/get_templates.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
module Fog
|
||||
module Parsers
|
||||
module Bluebox
|
||||
|
||||
class GetTemplates < Fog::Parsers::Base
|
||||
|
||||
def reset
|
||||
@template = {}
|
||||
@response = { 'templates' => [] }
|
||||
end
|
||||
|
||||
def end_element(name)
|
||||
case name
|
||||
when 'public'
|
||||
@template[name] = @value
|
||||
when 'id'
|
||||
@template[name] = @value
|
||||
when 'description'
|
||||
@template[name] = @value
|
||||
when 'record'
|
||||
@response['templates'] << @template
|
||||
@template = {}
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
34
lib/fog/bluebox/requests/get_products.rb
Normal file
34
lib/fog/bluebox/requests/get_products.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
module Fog
|
||||
module Bluebox
|
||||
class Real
|
||||
|
||||
require 'fog/bluebox/parsers/get_products'
|
||||
|
||||
# Get list of products
|
||||
#
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
# * body<~Array>:
|
||||
# * 'id'<~String> - UUID of the product
|
||||
# * 'description'<~String> - Description of the product
|
||||
# * 'cost'<~Decimal> - Hourly cost of the product
|
||||
def get_products
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:parser => Fog::Parsers::Bluebox::GetProducts.new,
|
||||
:path => 'api/products.xml'
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def get_images
|
||||
raise MockNotImplemented.new("Contributions welcome!")
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
35
lib/fog/bluebox/requests/get_templates.rb
Normal file
35
lib/fog/bluebox/requests/get_templates.rb
Normal file
|
@ -0,0 +1,35 @@
|
|||
module Fog
|
||||
module Bluebox
|
||||
class Real
|
||||
|
||||
require 'fog/bluebox/parsers/get_templates'
|
||||
|
||||
# Get list of OS templates
|
||||
#
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
# * body<~Array>:
|
||||
# * 'id'<~String> - UUID of the image
|
||||
# * 'description'<~String> - Description of the image
|
||||
# * 'public'<~Boolean> - Public / Private image
|
||||
# * 'created'<~Datetime> - Timestamp of when the image was created
|
||||
def get_templates
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:parser => Fog::Parsers::Bluebox::GetTemplates.new,
|
||||
:path => 'api/block_templates.xml'
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def get_images
|
||||
raise MockNotImplemented.new("Contributions welcome!")
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue