2011-08-31 16:52:53 -04:00
|
|
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'ninefold'))
|
|
|
|
require 'fog/storage'
|
2011-06-27 03:55:01 -04:00
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Storage
|
|
|
|
class Ninefold < Fog::Service
|
|
|
|
STORAGE_HOST = "onlinestorage.ninefold.com" #"api.ninefold.com"
|
|
|
|
STORAGE_PATH = "" #"/storage/v1.0"
|
|
|
|
STORAGE_PORT = "80" # "443"
|
|
|
|
STORAGE_SCHEME = "http" # "https"
|
|
|
|
|
|
|
|
requires :ninefold_storage_token, :ninefold_storage_secret
|
|
|
|
|
2011-08-24 15:07:16 -04:00
|
|
|
model_path 'fog/ninefold/models/storage'
|
2011-06-28 02:05:18 -04:00
|
|
|
model :directory
|
|
|
|
collection :directories
|
2011-07-10 09:44:35 -04:00
|
|
|
model :file
|
|
|
|
collection :files
|
2011-06-27 03:55:01 -04:00
|
|
|
|
2011-08-24 15:03:25 -04:00
|
|
|
request_path 'fog/ninefold/requests/storage'
|
2011-06-27 03:55:01 -04:00
|
|
|
# request :delete_container
|
2011-06-28 02:05:18 -04:00
|
|
|
request :get_namespace
|
2011-11-05 20:29:30 -04:00
|
|
|
request :head_namespace
|
2011-06-28 02:05:18 -04:00
|
|
|
request :post_namespace
|
2011-07-10 10:28:45 -04:00
|
|
|
request :put_namespace
|
2011-06-28 02:05:18 -04:00
|
|
|
request :delete_namespace
|
2011-06-27 03:55:01 -04:00
|
|
|
|
|
|
|
module Utils
|
|
|
|
end
|
|
|
|
|
|
|
|
class Mock
|
|
|
|
include Utils
|
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
require 'mime/types'
|
|
|
|
@ninefold_storage_token = options[:ninefold_storage_token]
|
|
|
|
@ninefold_storage_secret = options[:ninefold_storage_secret]
|
|
|
|
end
|
|
|
|
|
|
|
|
def request(options)
|
|
|
|
raise "Ninefold Storage mocks not implemented"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
include Utils
|
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
require 'mime/types'
|
|
|
|
@ninefold_storage_token = options[:ninefold_storage_token]
|
|
|
|
@ninefold_storage_secret = options[:ninefold_storage_secret]
|
|
|
|
@ninefold_storage_secret_decoded = Base64.decode64( @ninefold_storage_secret )
|
|
|
|
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
|
|
|
@hmac = Fog::HMAC.new('sha1', @ninefold_storage_secret_decoded)
|
|
|
|
@persistent = options[:persistent] || true
|
2011-08-31 16:32:12 -04:00
|
|
|
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection = Fog::Connection.new("#{Fog::Storage::Ninefold::STORAGE_SCHEME}://#{Fog::Storage::Ninefold::STORAGE_HOST}:#{Fog::Storage::Ninefold::STORAGE_PORT}", @persistent, @connection_options)
|
2011-06-27 03:55:01 -04:00
|
|
|
end
|
|
|
|
|
2011-07-10 09:44:35 -04:00
|
|
|
def uid
|
|
|
|
@ninefold_storage_token#.split('/')[-1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def sign(string)
|
2011-08-31 16:32:12 -04:00
|
|
|
value = @hmac.sign(string)
|
2011-07-10 09:44:35 -04:00
|
|
|
Base64.encode64( value ).chomp()
|
|
|
|
end
|
|
|
|
|
2011-06-27 03:55:01 -04:00
|
|
|
def reload
|
|
|
|
@connection.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
def request(params, &block)
|
|
|
|
req_path = params[:path]
|
|
|
|
# Force set host and port
|
|
|
|
params.merge!({
|
|
|
|
:host => Fog::Storage::Ninefold::STORAGE_HOST,
|
|
|
|
:path => "#{Fog::Storage::Ninefold::STORAGE_PATH}/rest/#{params[:path]}",
|
|
|
|
})
|
|
|
|
# Set default method and headers
|
|
|
|
params = {:method => 'GET', :headers => {}}.merge params
|
|
|
|
|
|
|
|
params[:headers]["Content-Type"] ||= "application/octet-stream"
|
|
|
|
|
|
|
|
# Add request date
|
|
|
|
params[:headers]["date"] = Time.now().httpdate()
|
|
|
|
params[:headers]["x-emc-uid"] = @ninefold_storage_token
|
|
|
|
|
|
|
|
# Build signature string
|
|
|
|
signstring = ""
|
|
|
|
signstring += params[:method]
|
|
|
|
signstring += "\n"
|
|
|
|
signstring += params[:headers]["Content-Type"]
|
|
|
|
signstring += "\n"
|
|
|
|
if( params[:headers]["range"] )
|
|
|
|
signstring += params[:headers]["range"]
|
|
|
|
end
|
|
|
|
signstring += "\n"
|
|
|
|
signstring += params[:headers]["date"]
|
|
|
|
signstring += "\n"
|
|
|
|
|
|
|
|
signstring += "/rest/" + URI.unescape( req_path ).downcase
|
2011-06-28 02:05:18 -04:00
|
|
|
query_str = params[:query].map{|k,v| "#{k}=#{v}"}.join('&')
|
2011-07-10 09:44:35 -04:00
|
|
|
signstring += '?' + query_str unless query_str.empty?
|
2011-06-27 03:55:01 -04:00
|
|
|
signstring += "\n"
|
|
|
|
|
|
|
|
customheaders = {}
|
|
|
|
params[:headers].each { |key,value|
|
2011-11-01 10:34:24 -04:00
|
|
|
case key
|
|
|
|
when 'x-emc-date', 'x-emc-signature'
|
2011-06-27 03:55:01 -04:00
|
|
|
#skip
|
2011-11-01 10:34:24 -04:00
|
|
|
when /^x-emc-/
|
2011-06-27 03:55:01 -04:00
|
|
|
customheaders[ key.downcase ] = value
|
|
|
|
end
|
|
|
|
}
|
|
|
|
header_arr = customheaders.sort()
|
|
|
|
first = true
|
|
|
|
header_arr.each { |key,value|
|
|
|
|
# Values are lowercase and whitespace-normalized
|
|
|
|
signstring += key + ":" + value.strip.chomp.squeeze( " " ) + "\n"
|
|
|
|
}
|
|
|
|
|
2011-08-31 16:32:12 -04:00
|
|
|
digest = @hmac.sign(signstring.chomp())
|
2011-06-27 03:55:01 -04:00
|
|
|
signature = Base64.encode64( digest ).chomp()
|
|
|
|
params[:headers]["x-emc-signature"] = signature
|
|
|
|
|
|
|
|
begin
|
|
|
|
response = @connection.request(params, &block)
|
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
|
|
|
raise case error
|
|
|
|
when Excon::Errors::NotFound
|
|
|
|
Fog::Storage::Ninefold::NotFound.slurp(error)
|
|
|
|
else
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end
|
2011-06-28 02:05:18 -04:00
|
|
|
unless response.body.empty?
|
|
|
|
if params[:parse]
|
|
|
|
document = Fog::ToHashDocument.new
|
|
|
|
parser = Nokogiri::XML::SAX::PushParser.new(document)
|
|
|
|
parser << response.body
|
|
|
|
parser.finish
|
|
|
|
response.body = document.body
|
|
|
|
end
|
|
|
|
end
|
2011-06-27 03:55:01 -04:00
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|