class Fog::Storage::Ninefold::Real

Public Class Methods

new(options={}) click to toggle source
# File lib/fog/ninefold/storage.rb, line 49
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 )

  @connection_options = options[:connection_options] || {}
  @hmac               = Fog::HMAC.new('sha1', @ninefold_storage_secret_decoded)
  @persistent         = options[:persistent] || true

  @connection = Fog::Connection.new("#{Fog::Storage::Ninefold::STORAGE_SCHEME}://#{Fog::Storage::Ninefold::STORAGE_HOST}:#{Fog::Storage::Ninefold::STORAGE_PORT}", @persistent, @connection_options)
end

Public Instance Methods

delete_namespace(namespace = '', options = {}) click to toggle source
# File lib/fog/ninefold/requests/storage/delete_namespace.rb, line 6
def delete_namespace(namespace = '', options = {})
  options = options.reject {|key, value| value.nil?}
  request({
            :expects  => 204,
            :method   => 'DELETE',
            :path     => "namespace/" + namespace,
            :query    => options
          }.merge(options))
end
get_namespace(namespace = '', options = {}) click to toggle source
# File lib/fog/ninefold/requests/storage/get_namespace.rb, line 6
def get_namespace(namespace = '', options = {})
  options = options.reject {|key, value| value.nil?}
  request({
            :expects  => 200,
            :method   => 'GET',
            :path     => "namespace/" + namespace,
            :query    => {},
            :parse => true
          }.merge(options))
end
head_namespace(namespace = '', options = {}) click to toggle source
# File lib/fog/ninefold/requests/storage/head_namespace.rb, line 6
def head_namespace(namespace = '', options = {})
  options = options.reject {|key, value| value.nil?}
  request({
            :expects  => 200,
            :method   => 'HEAD',
            :path     => "namespace/" + namespace,
            :query    => {},
            :parse => true
          }.merge(options))
end
post_namespace(namespace = '', options = {}) click to toggle source
# File lib/fog/ninefold/requests/storage/post_namespace.rb, line 6
def post_namespace(namespace = '', options = {})
  options = options.reject {|key, value| value.nil?}
  request({
            :expects  => 201,
            :method   => 'POST',
            :path     => "namespace/" + namespace,
            :query    => {},
            :parse => true
          }.merge(options))
end
put_namespace(namespace = '', options = {}) click to toggle source
# File lib/fog/ninefold/requests/storage/put_namespace.rb, line 6
def put_namespace(namespace = '', options = {})
  options = options.reject {|key, value| value.nil?}
  request({
            :expects  => 200,
            :method   => 'PUT',
            :path     => "namespace/" + namespace,
            :query    => {},
            :parse => true
          }.merge(options))
end
reload() click to toggle source
# File lib/fog/ninefold/storage.rb, line 71
def reload
  @connection.reset
end
request(params, &block) click to toggle source
# File lib/fog/ninefold/storage.rb, line 75
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
  query_str = params[:query].map{|k,v| "#{k}=#{v}"}.join('&')
  signstring += '?' + query_str unless query_str.empty?
  signstring += "\n"

  customheaders = {}
  params[:headers].each { |key,value|
    case key
    when 'x-emc-date', 'x-emc-signature'
      #skip
    when %r^x-emc-/
      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"
  }

  digest = @hmac.sign(signstring.chomp())
  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
  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
  response
end
sign(string) click to toggle source
# File lib/fog/ninefold/storage.rb, line 66
def sign(string)
  value = @hmac.sign(string)
  Base64.encode64( value ).chomp()
end
uid() click to toggle source
# File lib/fog/ninefold/storage.rb, line 62
def uid
  @ninefold_storage_token#.split('/')[-1]
end